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 Deserialize(self, reader): 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): self.__log.debug("Rebuilding merlke root!") if self.Transactions is not None and len(self.Transactions) > 0: self.MerkleRoot = MerkleTree.ComputeRoot([tx.Hash for tx in self.Transactions])