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 test_block_deserialize(self): blk, _ = BlockSerializer().deserialize(decodehexstr("01000000a3a71e679426f6cd46a27441ac5021a5bf75b2168b6e14811508ec0f000000006a921ac1ae5e23610d07d2b2a9377fbc3c3bb233e900aa1a5bc40f5562a19d0dafaa8d4d7ffa0f1cef18524c0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07047ffa0f1c014effffffff018076242a010000004341049cc3cae30927c40598032044b9e9e25f4739b0d7ade62803f5e9cf075debc817e6d29f42c70d0a1beb1c904eaaa50ef885b011f9fbaa16ef288a7ad193e11567ac0000000001000000010e0907321e9fcb500c20eb658667b40ac721870ddc2ce165ab53a47b68cbc517000000008b483045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c014104b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3effffffff02802385d1050000001976a9144d8b17fbce571614be89df4bd872de892a47984488ac00f2052a010000001976a914fadad27c40adbe230f5e3c04d44a29297508483188ac00000000")) blockheader = BlockHeader(version=1, hash_prev=Uint256.from_hexstr("000000000fec081581146e8b16b275bfa52150ac4174a246cdf62694671ea7a3"), hash_merkle=Uint256.from_hexstr("0d9da162550fc45b1aaa00e933b23b3cbc7f37a9b2d2070d61235eaec11a926a"), time=1301129903, bits=470809215, nonce=1280448751) tx1 = Tx(version=1, in_list=[TxIn(previous_output=Outpoint.null(), script=Script([Instruction(4, decodehexstr("7ffa0f1c")), Instruction(1, decodehexstr("4e"))]), sequence=TxIn.NSEQUENCE_FINAL )], out_list=[TxOut(value=5002000000, script=Script([Instruction(65, decodehexstr("049cc3cae30927c40598032044b9e9e25f4739b0d7ade62803f5e9cf075debc817e6d29f42c70d0a1beb1c904eaaa50ef885b011f9fbaa16ef288a7ad193e11567")), Instruction(OP_CHECKSIG)]))], locktime=0) tx2 = Tx(version=1, in_list=[TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr("17c5cb687ba453ab65e12cdc0d8721c70ab4678665eb200c50cb9f1e3207090e"), index=0), script=Script([Instruction(72, decodehexstr("3045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c01")), Instruction(65, decodehexstr("04b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3e"))]), sequence=TxIn.NSEQUENCE_FINAL )], out_list=[TxOut(value=24990000000, script=Script([Instruction(OP_DUP), Instruction(OP_HASH160), Instruction(20, decodehexstr("4d8b17fbce571614be89df4bd872de892a479844")), Instruction(OP_EQUALVERIFY), Instruction(OP_CHECKSIG)])), TxOut(value=5000000000, script=Script([Instruction(OP_DUP), Instruction(OP_HASH160), Instruction(20, decodehexstr("fadad27c40adbe230f5e3c04d44a292975084831")), Instruction(OP_EQUALVERIFY), Instruction(OP_CHECKSIG)]))], locktime=0) self.assertEquals(blk, Block(blockheader, [tx1, tx2]))
def test_block_serialize(self): blockheader = BlockHeader( version=1, hash_prev=Uint256.from_hexstr( "000000000fec081581146e8b16b275bfa52150ac4174a246cdf62694671ea7a3" ), hash_merkle=Uint256.from_hexstr( "0d9da162550fc45b1aaa00e933b23b3cbc7f37a9b2d2070d61235eaec11a926a" ), time=1301129903, bits=470809215, nonce=1280448751) tx1 = Tx( version=1, in_list=[ TxIn(previous_output=Outpoint.null(), script=Script([ Instruction(4, decodehexstr("7ffa0f1c")), Instruction(1, decodehexstr("4e")) ]), sequence=TxIn.NSEQUENCE_FINAL) ], out_list=[ TxOut( value=5002000000, script=Script([ Instruction( 65, decodehexstr( "049cc3cae30927c40598032044b9e9e25f4739b0d7ade62803f5e9cf075debc817e6d29f42c70d0a1beb1c904eaaa50ef885b011f9fbaa16ef288a7ad193e11567" )), Instruction(OP_CHECKSIG) ])) ], locktime=0) tx2 = Tx( version=1, in_list=[ TxIn(previous_output=Outpoint(hash=Uint256.from_hexstr( "17c5cb687ba453ab65e12cdc0d8721c70ab4678665eb200c50cb9f1e3207090e" ), index=0), script=Script([ Instruction( 72, decodehexstr( "3045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c01" )), Instruction( 65, decodehexstr( "04b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3e" )) ]), sequence=TxIn.NSEQUENCE_FINAL) ], out_list=[ TxOut(value=24990000000, script=Script([ Instruction(OP_DUP), Instruction(OP_HASH160), Instruction( 20, decodehexstr( "4d8b17fbce571614be89df4bd872de892a479844")), Instruction(OP_EQUALVERIFY), Instruction(OP_CHECKSIG) ])), TxOut(value=5000000000, script=Script([ Instruction(OP_DUP), Instruction(OP_HASH160), Instruction( 20, decodehexstr( "fadad27c40adbe230f5e3c04d44a292975084831")), Instruction(OP_EQUALVERIFY), Instruction(OP_CHECKSIG) ])) ], locktime=0) blk = Block(blockheader, [tx1, tx2]) data = BlockSerializer().serialize(blk) self.assertEquals( data, decodehexstr( "01000000a3a71e679426f6cd46a27441ac5021a5bf75b2168b6e14811508ec0f000000006a921ac1ae5e23610d07d2b2a9377fbc3c3bb233e900aa1a5bc40f5562a19d0dafaa8d4d7ffa0f1cef18524c0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07047ffa0f1c014effffffff018076242a010000004341049cc3cae30927c40598032044b9e9e25f4739b0d7ade62803f5e9cf075debc817e6d29f42c70d0a1beb1c904eaaa50ef885b011f9fbaa16ef288a7ad193e11567ac0000000001000000010e0907321e9fcb500c20eb658667b40ac721870ddc2ce165ab53a47b68cbc517000000008b483045022100ab2dc8932ca1d26f4cdac1feae09020a60ccc4d17b14e5fc5b21f3ab8c3a9cee022040a7208c172d19a19902280d66201c7fe2c3d8b2df7e23cc4e5b70fd52ecba2c014104b77dd1f3a21cb3d067a7e76982a609d7310f8692f5d61346f3225323c425604a0c12862755335c49e392673106adfc5dfdee1e4d367f10353e8911fac687db3effffffff02802385d1050000001976a9144d8b17fbce571614be89df4bd872de892a47984488ac00f2052a010000001976a914fadad27c40adbe230f5e3c04d44a29297508483188ac00000000" ))