1
0
mirror of https://github.com/bitcoinbook/bitcoinbook synced 2024-11-25 17:38:26 +00:00
bitcoinbook/code/rpc_block.py

35 lines
972 B
Python
Raw Normal View History

2016-02-05 02:52:15 +00:00
from bitcoin.rpc import RawProxy
p = RawProxy()
# The block height where Alice's transaction was recorded
blockheight = 775072
2016-02-05 02:52:15 +00:00
# Get the block hash of the block at the given height
2016-02-05 02:52:15 +00:00
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']
2018-02-03 00:33:46 +00:00
# Add the value of this transaction to the total
2016-02-05 02:52:15 +00:00
block_value = block_value + tx_value
print("Total value in block: ", block_value)