class BlockIndexSerializer(): BLOCKINDEX = Structure([Field("<I", "version"), Uint256Serializer("hash_next"), Field("<I", "file"), Field("<I", "blockpos"), Field("<I", "height"), BlockheaderSerializer()], "txindex") def __init__(self): pass def serialize(self, blockindex_obj): return (self.BLOCKINDEX.serialize([blockindex_obj.version, blockindex_obj.hash_next, blockindex_obj.file, blockindex_obj.blockpos, blockindex_obj.height, blockindex_obj.blockheader])) def deserialize(self, data, cursor=0): result, cursor = self.BLOCKINDEX.deserialize(data, cursor) (version, hash_next, file, blockpos, height, blockheader) = result return (DbBlockIndex(version, hash_next, file, blockpos, height, blockheader), cursor)
def test_blockheader_serialize(self): h = BlockHeader( version=1, hash_prev=Uint256.from_hexstr( "1e4baab89f3a3251818c31bc87f6a33b4a5a88ef76673e2cc77ab2127b7afded" ), #hash_prev hash_merkle=Uint256.from_hexstr( "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" ), #merkle time=1237006505, bits=486605799, nonce=2083236893) self.assertEquals( hexstr(BlockheaderSerializer().serialize(h)), "01000000edfd7a7b12b27ac72c3e6776ef885a4a3ba3f687bc318c8151323a9fb8aa4b1e3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4aa938bb49e703011d1dac2b7c" )
def _index_transactions(self, blockhash, block=None): block_handle = self.get_block_handle(blockhash) #Add all transactions to the indexdb if not block: block = block_handle.get_block() size_blockheader = BlockheaderSerializer().get_size(block.blockheader) size_tx_size = VarintSerializer().get_size(len(block.transactions)) tx_serializer = TxSerializer() blockpos = block_handle.blockindex.blockpos txpos = block_handle.blockindex.blockpos + size_blockheader + size_tx_size for i, tx in enumerate(block.transactions): txindex = DbTxIndex(1, DiskTxPos(1, blockpos, txpos), [DiskTxPos() for _ in range(tx.output_count())]) self.indexdb.set_transactionindex(hash_tx(tx), txindex) #TODO: speed this up... if tx.rawdata: txpos += len(tx.rawdata) else: txpos += tx_serializer.get_size(tx)
def __init__( self, hash_prev, block_height, # version 2 blocks see BIP-34 coinbase_txout_list, transactions, time, bits, nonce=0, extra_nonce=0, coinbase_flags=["/P2SH/"]): self.nonce = nonce self.extra_nonce = extra_nonce self.hash_prev = hash_prev self.block_height = block_height self.coinbase_txout_list = coinbase_txout_list self.transactions = transactions self.time = time self.bits = bits self.coinbase_flags = coinbase_flags self.serializer = BlockheaderSerializer() self.rebuild_template()
from coinpy.lib.serialization.structures.s11n_blockheader import BlockheaderSerializer from coinpy.model.protocol.structures.uint256 import Uint256 from coinpy.tools.bitcoin.sha256 import doublesha256 BLOCK_SERIALIZE = BlockheaderSerializer() def hash_blockheader(blockheader): if blockheader.hash: return blockheader.hash if not blockheader.rawdata: blockheader.rawdata = BLOCK_SERIALIZE.serialize(blockheader) blockheader.hash = Uint256.from_bytestr(doublesha256(blockheader.rawdata)) return blockheader.hash def hash_block(block): return (hash_blockheader(block.blockheader))
def __init__(self, flags=0): self.BLOCK = Structure([ BlockheaderSerializer(flags), VarsizelistSerializer(VarintSerializer("txn_count"), TxSerializer()) ], "block")