From Antonopoulos' book I try to use the example 3-3 on page 49 which uses a python skript called rpc_example.py
to show the usage of the JSON-RPC API:
from bitcoin.rpc import RawProxy
p = RawProxy()
info = p.getinfo()
print(info['blocks'])
which on $ python rpc_example.py
should give 394875
.
Of course getinfo
has been depricated and I would like to use another function, for example getblock
.
I am not sure if I use it correctly or if I have an error in my syntax or if the problem below is due to something related to python.
I already had some troubles installing the library with pip install python-bitcoinlib
and also did pip3 install python-bitcoinlib
because I was not sure if the problem I now have is related to encoding/decoding in python version 2.
My code now is
from bitcoin.rpc import RawProxy
p = RawProxy()
infoblock = p.getblock()
print(infoblock['difficulty'])
which should print the difficulty of the block, at least in my understanding (I am new to python and to bitcoin).
I get the following error with python 2:
$ python BuchSkripte/49_rpc_example.py
Traceback (most recent call last):
File "BuchSkripte/49_rpc_example.py", line 2, in <module>
p = RawProxy()
File "/usr/local/lib/python2.7/dist-packages/bitcoin/rpc.py", line 295, in __init__
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/bitcoin/rpc.py", line 159,
in __init__
if '#' in line:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)
With python3 I get:
$ python3 BuchSkripte/49_rpc_example.py
Traceback (most recent call last):
File "BuchSkripte/49_rpc_example.py", line 3, in <module>
infoblock = p.getblock()
File "/usr/local/lib/python3.5/dist-packages/bitcoin/rpc.py", line 306, in <lambda>
f = lambda *args: self._call(name, *args)
File "/usr/local/lib/python3.5/dist-packages/bitcoin/rpc.py", line 238, in _call
raise JSONRPCError(response['error'])
bitcoin.rpc.JSONRPCError: {'code': -1, 'message': 'getblock "blockhash" ( verbosity )\n\nIf verbosity is 0, returns a string that is serialized, hex-encoded data for block \'hash\'.\nIf verbosity is 1, returns an Object with information about block <hash>.\nIf verbosity is 2, returns an Object with information about block <hash> and information about each transaction. \n\nArguments:\n1. blockhash (string, required) The block hash\n2. verbosity (numeric, optional, default=1) 0 for hex-encoded data, 1 for a json object, and 2 for json object with transaction data\n\nResult (for verbosity = 0):\n"data" (string) A string that is serialized, hex-encoded data for block \'hash\'.\n\nResult (for verbosity = 1):\n{\n "hash" : "hash", (string) the block hash (same as provided)\n "confirmations" : n, (numeric) The number of confirmations, or -1 if the block is not on the main chain\n "size" : n, (numeric) The block size\n "strippedsize" : n, (numeric) The block size excluding witness data\n "weight" : n (numeric) The block weight as defined in BIP 141\n "height" : n, (numeric) The block height or index\n "version" : n, (numeric) The block version\n "versionHex" : "00000000", (string) The block version formatted in hexadecimal\n "merkleroot" : "xxxx", (string) The merkle root\n "tx" : [ (array of string) The transaction ids\n "transactionid" (string) The transaction id\n ,...\n ],\n "time" : ttt, (numeric) The block time in seconds since epoch (Jan 1 1970 GMT)\n "mediantime" : ttt, (numeric) The median block time in seconds since epoch (Jan 1 1970 GMT)\n "nonce" : n, (numeric) The nonce\n "bits" : "1d00ffff", (string) The bits\n "difficulty" : x.xxx, (numeric) The difficulty\n "chainwork" : "xxxx", (string) Expected number of hashes required to produce the chain up to this block (in hex)\n "nTx" : n, (numeric) The number of transactions in the block.\n "previousblockhash" : "hash", (string) The hash of the previous block\n "nextblockhash" : "hash" (string) The hash of the next block\n}\n\nResult (for verbosity = 2):\n{\n ..., Same output as verbosity = 1.\n "tx" : [ (array of Objects) The transactions in the format of the getrawtransaction RPC. Different from verbosity = 1 "tx" result.\n ,...\n ],\n ,... Same output as verbosity = 1.\n}\n\nExamples:\n> bitcoin-cli getblock "00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"\n> curl --user myusername --data-binary \'{"jsonrpc": "1.0", "id":"curltest", "method": "getblock", "params": ["00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09"] }\' -H \'content-type: text/plain;\' http://127.0.0.1:8332/\n'}