pythonblockchainbitcoinbitcoindbitcoin-testnet

Extract all confirmed transactions from a bitcoin block using python


I want to extract all confirmed transactions from the bitcoin blockchain. I know there are repos out there (e.g. https://github.com/znort987/blockparser) but I want to write something myself for my better understanding.

I am have tried the following code after having downloaded far more than 42 blocks and while running bitcoind (minimal example):

from bitcoin.rpc import RawProxy

proxy = RawProxy()
blockheight=42
block = proxy.getblock(proxy.getblockhash(blockheight))
tx_list = block['tx']

for tx_id in tx_list:
    raw_tx = proxy.getrawtransaction(tx_id)

This yields the following error:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/donkeykong/.local/lib/python3.7/site-packages/bitcoin/rpc.py", line 315, in <lambda>
    f = lambda *args: self._call(name, *args)
  File "/home/donkeykong/.local/lib/python3.7/site-packages/bitcoin/rpc.py", line 239, in _call
    'message': err.get('message', 'error message not specified')})
bitcoin.rpc.InvalidAddressOrKeyError: {'code': -5, 'message': 'No such mempool transaction. Use -txindex or provide a block hash to enable blockchain transaction queries. Use gettransaction for wallet transactions.'}

Could anyone elucidate what I am misunderstanding?

For reproduction:


Solution

  • Running the client as bitcoind -txindex solves the problem as it maintains the full transaction index. I should have spent more attention to the error message...

    Excerpt from bitcoind --help:

    -txindex
           Maintain a full transaction index, used by the getrawtransaction rpc
           call (default: 0)