def test_block_deserialize(self): block = Helper.AsSerializableWithType(self.rawblock_hex, 'neo.Core.Block.Block') self.assertEqual(self.rb_prev, block.PrevHash.ToBytes()) self.assertEqual(self.rb_merlke, block.MerkleRoot.ToBytes()) self.assertEqual(self.rb_ts, block.Timestamp) self.assertEqual(self.rb_h, block.Index) self.assertEqual(self.rb_nonce, block.ConsensusData) self.assertEqual(self.rconsenusdata, block.ConsensusData) # self.assertEqual(self.rb_hash, block.HashToString()) tx = block.Transactions[0] self.assertEqual(tx.Nonce, self.rblock_tx_nonce) self.assertEqual(len(tx.inputs), 0) self.assertEqual(len(tx.outputs), 0) self.assertEqual(len(tx.Attributes), 0) self.assertEqual(type(tx.scripts), list) rawdata = block.RawData() compair_data = self.rawblock[:len(rawdata)] self.assertEqual(rawdata, compair_data) out = block.Hash.ToBytes() self.assertEqual(out, self.rb_hash) root = MerkleTree.ComputeRoot([tx.Hash for tx in block.Transactions]) self.assertEqual(root, block.MerkleRoot)
def test_compute_root_multiple_hashes(self): expected_hash = Helper.bin_dbl_sha256( binascii.unhexlify(b'aa' * 32 + b'bb' * 32)) hash1 = UInt256(data=binascii.unhexlify(b'aa' * 32)) hash2 = UInt256(data=binascii.unhexlify(b'bb' * 32)) hashes = [hash1, hash2] root = MerkleTree.ComputeRoot(hashes) self.assertEqual(expected_hash, root.ToArray())
def Deserialize(self, reader): """ Deserialize full object. Args: reader (neo.IO.BinaryReader): """ super(Block, self).Deserialize(reader) self.Transactions = [] byt = reader.ReadVarInt() transaction_length = byt if transaction_length < 1: raise Exception('Invalid format') for i in range(0, transaction_length): tx = Transaction.DeserializeFrom(reader) self.Transactions.append(tx) if MerkleTree.ComputeRoot([tx.Hash for tx in self.Transactions]) != self.MerkleRoot: raise Exception("Merkle Root Mismatch")
def test_block_two(self): hexdata = binascii.unhexlify(self.b2raw) block = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block') self.assertEqual(block.Index, self.b2height) self.assertEqual(block.ConsensusData, self.b2nonce) self.assertEqual(block.Timestamp, self.b2timestamp) self.assertEqual(block.PrevHash.ToBytes(), self.b2prev_hash) self.assertEqual(block.Hash.ToString(), self.b2hash) next_consensus_address = Crypto.ToAddress(block.NextConsensus) self.assertEqual(next_consensus_address, self.b2nextconsensus) witness = block.Script ins = binascii.hexlify(witness.InvocationScript) vns = binascii.hexlify(witness.VerificationScript) self.assertEqual(ins, self.b2invocation) self.assertEqual(vns, self.b2verification) self.assertEqual(len(block.Transactions), self.b2tx_len) tx = block.Transactions[0] self.assertEqual(tx.inputs, self.b2tx_vin) self.assertEqual(tx.outputs, self.b2tx_vout) self.assertEqual(tx.Nonce, self.b2tx_nonce) txhash = tx.Hash.ToBytes() self.assertEqual(txhash, self.b2tx_id) root = MerkleTree.ComputeRoot([tx.Hash for tx in block.Transactions]) self.assertEqual(root, block.MerkleRoot)
def RebuildMerkleRoot(self): """Rebuild the merkle root of the block""" logger.debug("Rebuilding merkle root!") if self.Transactions is not None and len(self.Transactions) > 0: self.MerkleRoot = MerkleTree.ComputeRoot( [tx.Hash for tx in self.Transactions])
def test_computer_root_no_input(self): with self.assertRaises(Exception) as context: MerkleTree.ComputeRoot([]) self.assertTrue("Hashes must have length" in str(context.exception))
def test_compute_root_single_hash(self): data = binascii.unhexlify(b'aa' * 32) hash1 = UInt256(data=data) root = MerkleTree.ComputeRoot([hash1]) self.assertEqual(data, root.ToArray())