当前位置 : 主页 > 大数据 > 区块链 >

(转)《精通比特币》原码分析: rpc_block

来源:互联网 收集:自由互联 发布时间:2021-06-22
https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/code/rpc_block.py 目前报错:说未发现bitcoin.rpc。 from bitcoin.rpc import RawProxyp = RawProxy()# The block height where Alice 's transaction was recordedblockheight =

https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/code/rpc_block.py

目前报错:说未发现bitcoin.rpc。

from bitcoin.rpc import RawProxy

p = RawProxy()

# The block height where Alice's transaction was recorded
blockheight = 277316

# Get the block hash of block with height 277316
blockhash = p.getblockhash(blockheight)

# Retrieve the block by its hash
block = p.getblock(blockhash)

# Element tx contains the list of all transaction IDs in the block
transactions = block['tx']

block_value = 0

# Iterate through each transaction ID in the block
for txid in transactions:
    tx_value = 0
    # Retrieve the raw transaction by ID
    raw_tx = p.getrawtransaction(txid)
    # Decode the transaction
    decoded_tx = p.decoderawtransaction(raw_tx)
    # Iterate through each output in the transaction
    for output in decoded_tx['vout']:
        # Add up the value of each output
        tx_value = tx_value + output['value']

    # Add the value of this transaction to the total 
    block_value = block_value + tx_value

print("Total value in block: ", block_value)
网友评论