def rebuild_template(self): """Rebuild a version 2 blockheader and serialize it. oldest satoshi client coinbase script contains: nBits, bnExtraNonce (nBits = GetNextWorkRequired(pindexPrev);) later timestamp was used instead of "bits" to ensure coinbase txn is unique even if address is the same (github:83f4cd156e9d52bd7c4351336dfa4806a43ee4e4=. now in version 2 blocks the height is included instead of the timestamp. """ coinbase_script = Script( [push_bignum_instruction(self.block_height)] + [push_bignum_instruction(self.extra_nonce)] + [push_data_instruction(flag) for flag in self.coinbase_flags]) coinbase_txin = TxIn(Outpoint.null(), coinbase_script, sequence=TxIn.NSEQUENCE_FINAL) coinbase_tx = Tx(version=1, in_list=[coinbase_txin], out_list=self.coinbase_txout_list, locktime=0) self.block_transactions = [coinbase_tx] + self.transactions self.blockheader = BlockHeader(version=2, hash_prev=self.hash_prev, hash_merkle=compute_merkle_root( self.block_transactions), time=self.time, bits=self.bits, nonce=self.nonce) self.serialized = self.serializer.serialize(self.blockheader)
def rebuild_template(self): """Rebuild a version 2 blockheader and serialize it. oldest satoshi client coinbase script contains: nBits, bnExtraNonce (nBits = GetNextWorkRequired(pindexPrev);) later timestamp was used instead of "bits" to ensure coinbase txn is unique even if address is the same (github:83f4cd156e9d52bd7c4351336dfa4806a43ee4e4=. now in version 2 blocks the height is included instead of the timestamp. """ coinbase_script = Script([push_bignum_instruction(self.block_height)] + [push_bignum_instruction(self.extra_nonce)] + [push_data_instruction(flag) for flag in self.coinbase_flags]) coinbase_txin = TxIn(Outpoint.null(), coinbase_script, sequence=TxIn.NSEQUENCE_FINAL) coinbase_tx = Tx(version=1, in_list=[coinbase_txin], out_list=self.coinbase_txout_list, locktime=0) self.block_transactions = [coinbase_tx] + self.transactions self.blockheader = BlockHeader(version=2, hash_prev=self.hash_prev, hash_merkle=compute_merkle_root(self.block_transactions), time=self.time, bits=self.bits, nonce=self.nonce) self.serialized = self.serializer.serialize(self.blockheader)
def check_merkle_root(self, hash, block): merkle = compute_merkle_root(block.transactions) if merkle != block.blockheader.hash_merkle: raise Exception("merkel root incorrect for block %s: %s != %s" % (str(hash), str(merkle), str(block.blockheader.hash_merkle)) )
Script([push_data_instruction(decodehexstr("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f")),Instruction(OP_CHECKSIG)]))], 0) #locktime ]) GENESIS = {MAIN : GENESIS_MAIN, TESTNET : GENESIS_TESTNET, TESTNET3: GENESIS_TESTNET3, UNITNET : GENESIS_UNITNET } if __name__ == '__main__': from coinpy.lib.blocks.hash_block import hash_block from coinpy.tools.hex import hexstr from coinpy.lib.serialization.structures.s11n_blockheader import BlockheaderSerializer from coinpy.tools.bitcoin.sha256 import doublesha256 from coinpy.lib.transactions.hash_tx import hash_tx from coinpy.lib.serialization.structures.s11n_tx import TxSerializer print GENESIS_MAIN print hash_block(GENESIS_MAIN) assert GENESIS_MAIN.blockheader.hash_merkle == compute_merkle_root(GENESIS_MAIN.transactions) print GENESIS_TESTNET print hash_block(GENESIS_TESTNET) assert GENESIS_TESTNET.blockheader.hash_merkle == compute_merkle_root(GENESIS_TESTNET.transactions) print GENESIS_TESTNET3 print hash_block(GENESIS_TESTNET3) assert GENESIS_TESTNET3.blockheader.hash_merkle == compute_merkle_root(GENESIS_TESTNET3.transactions) print GENESIS_UNITNET print hash_block(GENESIS_UNITNET) print GENESIS_UNITNET.blockheader.hash_merkle assert GENESIS_UNITNET.blockheader.hash_merkle == compute_merkle_root(GENESIS_UNITNET.transactions)
def check_merkle_root(self, hash, block): merkle = compute_merkle_root(block.transactions) if merkle != block.blockheader.hash_merkle: raise Exception( "merkel root incorrect for block %s: %s != %s" % (str(hash), str(merkle), str(block.blockheader.hash_merkle)))