简体   繁体   中英

Cannot read certain block transactions

I was playing with the blochain using the python librairy 'bitcoinrpc' and bitcoin-core, however I can never read the transaction in block 9.

I even downloaded the blockchain a 2nd time thinking mine had a problem with a 2nd installation of bitcoin-core pointing to a different data directory and I still can't read block 9.

Can you reproduce this problem? Any idea why?

Script for reading and displaying block 7, 8, 9, 10 :

from bitcoinrpc.authproxy import AuthServiceProxy
import traceback

RPC_ADDRESS="127.0.0.1:8332"
RPC_USER="u"
RPC_PASSWORD="p"

def display_block_info(rpc, block_number):
    print "-------- block {} start -------".format(block_number)
    block_hash = rpc.getblockhash(block_number)
    print "Block hash : " + block_hash
    block = rpc.getblock(block_hash)
    for tx in block[u'tx']:
        print "processing transaction " + tx
        print rpc.getrawtransaction(tx, True)
    print "-------- block {} end -------".format(block_number)
    print ""

if __name__ == "__main__":
    rpc = AuthServiceProxy("http://%s:%s@%s"%(RPC_USER, RPC_PASSWORD, RPC_ADDRESS))
    block_numbers = [7, 8, 9, 10]
    for block_number in block_numbers:
        try:
            display_block_info(rpc, block_number)
        except Exception as e:
            traceback.print_exc()
            print "-------- block {} end -------".format(block_number)
            print ""

Result:

-------- block 7 start -------
Block hash : 0000000071966c2b1d065fd446b1e485b2c9d9594acd2007ccbd5441cfc89444
processing transaction 8aa673bc752f2851fd645d6a0a92917e967083007d9c1684f9423b100540673f
{u'hash': u'8aa673bc752f2851fd645d6a0a92917e967083007d9c1684f9423b100540673f', u'blockhash': u'0000000071966c2b1d065fd446b1e485b2c9d9594acd2007ccbd5441cfc89444', u'vout': [{u'scriptPubKey': {u'reqSigs': 1, u'hex': u'4104a59e64c774923d003fae7491b2a7f75d6b7aa3f35606a8ff1cf06cd3317d16a41aa16928b1df1f631f31f28c7da35d4edad3603adb2338c4d4dd268f31530555ac', u'addresses': [u'16LoW7y83wtawMg5XmT4M3Q7EdjjUmenjM'], u'asm': u'04a59e64c774923d003fae7491b2a7f75d6b7aa3f35606a8ff1cf06cd3317d16a41aa16928b1df1f631f31f28c7da35d4edad3603adb2338c4d4dd268f31530555 OP_CHECKSIG', u'type': u'pubkey'}, u'value': Decimal('50.00000000'), u'n': 0}], u'hex': u'01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d012bffffffff0100f2052a01000000434104a59e64c774923d003fae7491b2a7f75d6b7aa3f35606a8ff1cf06cd3317d16a41aa16928b1df1f631f31f28c7da35d4edad3603adb2338c4d4dd268f31530555ac00000000', u'vin': [{u'coinbase': u'04ffff001d012b', u'sequence': 4294967295L}], u'txid': u'8aa673bc752f2851fd645d6a0a92917e967083007d9c1684f9423b100540673f', u'blocktime': 1231472369, u'version': 1, u'confirmations': 3677, u'time': 1231472369, u'locktime': 0, u'vsize': 134, u'size': 134}
-------- block 7 end -------

-------- block 8 start -------
Block hash : 00000000408c48f847aa786c2268fc3e6ec2af68e8468a34a28c61b7f1de0dc6
processing transaction a6f7f1c0dad0f2eb6b13c4f33de664b1b0e9f22efad5994a6d5b6086d85e85e3
{u'hash': u'a6f7f1c0dad0f2eb6b13c4f33de664b1b0e9f22efad5994a6d5b6086d85e85e3', u'blockhash': u'00000000408c48f847aa786c2268fc3e6ec2af68e8468a34a28c61b7f1de0dc6', u'vout': [{u'scriptPubKey': {u'reqSigs': 1, u'hex': u'4104cc8d85f5e7933cb18f13b97d165e1189c1fb3e9c98b0dd5446b2a1989883ff9e740a8a75da99cc59a21016caf7a7afd3e4e9e7952983e18d1ff70529d62e0ba1ac', u'addresses': [u'1J6PYEzr4CUoGbnXrELyHszoTSz3wCsCaj'], u'asm': u'04cc8d85f5e7933cb18f13b97d165e1189c1fb3e9c98b0dd5446b2a1989883ff9e740a8a75da99cc59a21016caf7a7afd3e4e9e7952983e18d1ff70529d62e0ba1 OP_CHECKSIG', u'type': u'pubkey'}, u'value': Decimal('50.00000000'), u'n': 0}], u'hex': u'01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d012cffffffff0100f2052a01000000434104cc8d85f5e7933cb18f13b97d165e1189c1fb3e9c98b0dd5446b2a1989883ff9e740a8a75da99cc59a21016caf7a7afd3e4e9e7952983e18d1ff70529d62e0ba1ac00000000', u'vin': [{u'coinbase': u'04ffff001d012c', u'sequence': 4294967295L}], u'txid': u'a6f7f1c0dad0f2eb6b13c4f33de664b1b0e9f22efad5994a6d5b6086d85e85e3', u'blocktime': 1231472743, u'version': 1, u'confirmations': 3681, u'time': 1231472743, u'locktime': 0, u'vsize': 134, u'size': 134}
-------- block 8 end -------

-------- block 9 start -------
Block hash : 000000008d9dc510f23c2657fc4f67bea30078cc05a90eb89e84cc475c080805
processing transaction 0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9
Traceback (most recent call last):
  File "blocReader.py", line 24, in <module>
    display_block_info(rpc, block_number)
  File "blocReader.py", line 15, in display_block_info
    print rpc.getrawtransaction(tx, True)
  File "c:\Python27\lib\site-packages\bitcoinrpc\authproxy.py", line 116, in __call__
    raise JSONRPCException(response['error'])
JSONRPCException
-------- block 9 end -------

-------- block 10 start -------
Block hash : 000000002c05cc2e78923c34df87fd108b22221ac6076c18f3ade378a4d915e9
processing transaction d3ad39fa52a89997ac7381c95eeffeaf40b66af7a57e9eba144be0a175a12b11
{u'hash': u'd3ad39fa52a89997ac7381c95eeffeaf40b66af7a57e9eba144be0a175a12b11', u'blockhash': u'000000002c05cc2e78923c34df87fd108b22221ac6076c18f3ade378a4d915e9', u'vout': [{u'scriptPubKey': {u'reqSigs': 1, u'hex': u'4104fcc2888ca91cf0103d8c5797c256bf976e81f280205d002d85b9b622ed1a6f820866c7b5fe12285cfa78c035355d752fc94a398b67597dc4fbb5b386816425ddac', u'addresses': [u'15yN7NPEpu82sHhB6TzCW5z5aXoamiKeGy'], u'asm': u'04fcc2888ca91cf0103d8c5797c256bf976e81f280205d002d85b9b622ed1a6f820866c7b5fe12285cfa78c035355d752fc94a398b67597dc4fbb5b386816425dd OP_CHECKSIG', u'type': u'pubkey'}, u'value': Decimal('50.00000000'), u'n': 0}], u'hex': u'01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0136ffffffff0100f2052a01000000434104fcc2888ca91cf0103d8c5797c256bf976e81f280205d002d85b9b622ed1a6f820866c7b5fe12285cfa78c035355d752fc94a398b67597dc4fbb5b386816425ddac00000000', u'vin': [{u'coinbase': u'04ffff001d0136', u'sequence': 4294967295L}], u'txid': u'd3ad39fa52a89997ac7381c95eeffeaf40b66af7a57e9eba144be0a175a12b11', u'blocktime': 1231473952, u'version': 1, u'confirmations': 3687, u'time': 1231473952, u'locktime': 0, u'vsize': 134, u'size': 134}
-------- block 10 end -------

As you can see the transaction '0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9' cannot be read using rpc.getrawtransaction(tx, True)

The issue is most likely due to the fact that you are running bitcoind without the -txindex command line flag. Without it bitcoind will only track unspent transaction outputs and transactions that it received. The reason why the first few blocks work, is because they only contain unspent genesis outputs, ie, newly generated coins. The transaction you are failing to parse is the first ever transaction from Satoshi to Hal Finney, which was spent, hence no longer in the index.

Try either running with -txindex or better use getblock(block_hash, 0) to get the hex serialized block and then parse it locally (eg, with bitcoinlib ), instead of using bitcoind to retrieve every single transaction. You'll be a lot faster this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM