无法读取某些大宗交易
Cannot read certain block transactions
我正在使用 python 库 'bitcoinrpc' 和比特币核心来玩 blochain,但是我永远无法读取区块 9 中的交易。
我什至第二次下载了区块链,认为我的第二次安装 bitcoin-core 指向不同的数据目录时出现问题,但我仍然无法读取区块 9。
你能重现这个问题吗?知道为什么吗?
用于读取和显示块 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 ""
结果:
-------- 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 -------
如您所见,无法使用 rpc.getrawtransaction(tx, True)
读取交易“0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9”
问题很可能是由于您 运行 bitcoind
没有 -txindex
命令行标志。没有它 bitcoind
将只跟踪未花费的交易输出和它收到的交易。前几个区块之所以有效,是因为它们只包含未花费的创世输出,即新生成的硬币。您未能解析的交易是从 Satoshi 到 Hal Finney 的第一笔交易,该交易已被花费,因此不再在索引中。
尝试 运行 -txindex
或更好地使用 getblock(block_hash, 0)
获取十六进制序列化块,然后在本地解析它(例如,使用 bitcoinlib),而不是使用bitcoind
检索每笔交易。这样你会快很多。
我正在使用 python 库 'bitcoinrpc' 和比特币核心来玩 blochain,但是我永远无法读取区块 9 中的交易。
我什至第二次下载了区块链,认为我的第二次安装 bitcoin-core 指向不同的数据目录时出现问题,但我仍然无法读取区块 9。
你能重现这个问题吗?知道为什么吗?
用于读取和显示块 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 ""
结果:
-------- 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 -------
如您所见,无法使用 rpc.getrawtransaction(tx, True)
问题很可能是由于您 运行 bitcoind
没有 -txindex
命令行标志。没有它 bitcoind
将只跟踪未花费的交易输出和它收到的交易。前几个区块之所以有效,是因为它们只包含未花费的创世输出,即新生成的硬币。您未能解析的交易是从 Satoshi 到 Hal Finney 的第一笔交易,该交易已被花费,因此不再在索引中。
尝试 运行 -txindex
或更好地使用 getblock(block_hash, 0)
获取十六进制序列化块,然后在本地解析它(例如,使用 bitcoinlib),而不是使用bitcoind
检索每笔交易。这样你会快很多。