def __init__(self, tx_versioner: 'TransactionVersioner'): BaseBlockBuilder.__init__(self, tx_versioner) # Attributes that must be assigned self.reps: List[ExternalAddress] = None self.next_reps: List[ExternalAddress] = None self.next_reps_hash: Hash32 = None self.next_reps_change_reason: NextRepsChangeReason = NextRepsChangeReason.NoChange self.leader_votes: List[LeaderVote] = [] self.prev_votes: List[BlockVote] = None # Attributes to be assigned(optional) self.fixed_timestamp: int = None self.state_hash: 'Hash32' = None self.next_leader: 'ExternalAddress' = None self.is_max_made_block_count: bool = None # Attributes to be generated self.transactions_hash: 'Hash32' = None self.receipts_hash: 'Hash32' = None self.reps_hash: 'Hash32' = None self.leader_votes_hash: 'Hash32' = None self.prev_votes_hash: 'Hash32' = None self.logs_bloom: 'BloomFilter' = None self._timestamp: int = None self._receipts: list = None
def __get_test_block(): block_builder = BlockBuilder.new("0.1a", TransactionVersioner()) block_builder.height = 0 block_builder.prev_hash = None block = block_builder.build( ) # It does not have commit state. It will be rebuilt. return block
def makeup_block(self, complain_votes: 'LeaderVotes', prev_votes, new_term: bool = False, skip_add_tx: bool = False): last_block = self.__blockchain.last_unconfirmed_block or self.__blockchain.last_block block_height = last_block.header.height + 1 block_version = self.__blockchain.block_versioner.get_version( block_height) block_builder = BlockBuilder.new(block_version, self.__blockchain.tx_versioner) block_builder.fixed_timestamp = int(time.time() * 1_000_000) block_builder.prev_votes = prev_votes if complain_votes and complain_votes.get_result(): block_builder.leader_votes = complain_votes.votes if new_term: block_builder.next_leader = None block_builder.reps = None elif skip_add_tx: utils.logger.debug(f"skip_add_tx for block height({self.height})") else: self.__add_tx_to_block(block_builder) return block_builder
def block_maker(timestamp: int, height: int = 0, prev_hash=None): """Make dummy block""" tx_versioner = TransactionVersioner() dummy_receipts = {} block_builder = BlockBuilder.new("0.1a", tx_versioner) for i in range(1000): tx_builder = TransactionBuilder.new("0x3", None, tx_versioner) tx_builder.signer = test_signer tx_builder.to_address = ExternalAddress.new() tx_builder.step_limit = random.randint(0, 10000) tx_builder.value = random.randint(0, 10000) tx_builder.nid = 2 tx = tx_builder.build() tx_serializer = TransactionSerializer.new( tx.version, tx.type(), tx_versioner) block_builder.transactions[tx.hash] = tx dummy_receipts[tx.hash.hex()] = { "dummy_receipt": "dummy", "tx_dumped": tx_serializer.to_full_data(tx) } block_builder.signer = test_signer block_builder.prev_hash = prev_hash block_builder.height = height block_builder.state_hash = Hash32(bytes(Hash32.size)) block_builder.receipts = dummy_receipts block_builder.reps = [ ExternalAddress.fromhex_address(test_signer.address) ] block_builder.peer_id = ExternalAddress.fromhex( test_signer.address) block_builder.next_leader = ExternalAddress.fromhex( test_signer.address) block_builder.fixed_timestamp = timestamp b = block_builder.build() assert b.header.timestamp == timestamp return b
def _verify_common(self, block: 'Block', prev_block: 'Block', **kwargs): generator: 'ExternalAddress' = kwargs.get("generator") header: BlockHeader = block.header body: BlockBody = block.body builder = BlockBuilder.new(self.version, self._tx_versioner) builder.height = header.height builder.prev_hash = header.prev_hash builder.fixed_timestamp = header.timestamp for tx in body.transactions.values(): builder.transactions[tx.hash] = tx invoke_result = None if self.invoke_func: self.verify_invoke(builder, block, prev_block) builder.build_merkle_tree_root_hash() if header.merkle_tree_root_hash != builder.merkle_tree_root_hash: exception = RuntimeError( f"Block({header.height}, {header.hash.hex()}, " f"MerkleTreeRootHash({header.merkle_tree_root_hash.hex()}), " f"Expected({builder.merkle_tree_root_hash.hex()}).") self._handle_exception(exception) builder.build_hash() if header.hash != builder.hash: exception = RuntimeError( f"Block({header.height}, {header.hash.hex()}, " f"Hash({header.hash.hex()}, " f"Expected({builder.hash.hex()}).") self._handle_exception(exception) if generator: self.verify_generator(block, generator) return invoke_result
def test_block_v0_4(self): block_version = "0.4" test_signer = Signer.from_prikey(os.urandom(32)) tx_versioner = TransactionVersioner() dummy_receipts = {} block_builder = BlockBuilder.new(block_version, tx_versioner) for i in range(5): tx_builder = TransactionBuilder.new("0x3", None, tx_versioner) tx_builder.signer = test_signer tx_builder.to_address = ExternalAddress.new() tx_builder.step_limit = random.randint(0, 10000) tx_builder.value = random.randint(0, 10000) tx_builder.nid = 2 tx = tx_builder.build() tx_serializer = TransactionSerializer.new(tx.version, tx.type(), tx_versioner) block_builder.transactions[tx.hash] = tx dummy_receipts[tx.hash.hex()] = { "dummy_receipt": "dummy", "tx_dumped": tx_serializer.to_full_data(tx) } next_leader = ExternalAddress.fromhex( "hx00112233445566778899aabbccddeeff00112233") block_builder.signer = test_signer block_builder.height = 0 block_builder.prev_hash = Hash32(bytes(Hash32.size)) block_builder.state_hash = Hash32(bytes(Hash32.size)) block_builder.receipts = dummy_receipts block_builder.reps = [ ExternalAddress.fromhex_address(test_signer.address) ] block_builder.next_leader = next_leader block_builder.next_reps = [] vote = BlockVote.new(test_signer, utils.get_time_stamp(), block_builder.height - 1, 0, block_builder.prev_hash) votes = BlockVotes(block_builder.reps, conf.VOTING_RATIO, block_builder.height - 1, 0, block_builder.prev_hash) votes.add_vote(vote) block_builder.prev_votes = votes.votes block = block_builder.build() block_verifier = BlockVerifier.new(block_version, tx_versioner) block_verifier.invoke_func = lambda b, prev_b: (block, dummy_receipts) reps_getter = lambda _: block_builder.reps generator = ExternalAddress.fromhex_address(test_signer.address) block_verifier.verify(block, None, None, generator=generator, reps_getter=reps_getter) block_serializer = BlockSerializer.new(block_version, tx_versioner) block_serialized = block_serializer.serialize(block) logging.info(json.dumps(block_serialized, indent=4)) block_deserialized = block_serializer.deserialize(block_serialized) logging.info( json.dumps(block_serializer.serialize(block_deserialized), indent=4)) assert block.header == block_deserialized.header assert block.body == block_deserialized.body tx_hashes = list(block.body.transactions) tx_index = random.randrange(0, len(tx_hashes)) block_prover = BlockProver.new(block.header.version, tx_hashes, BlockProverType.Transaction) tx_proof = block_prover.get_proof(tx_index) assert block_prover.prove(tx_hashes[tx_index], block.header.transactions_hash, tx_proof) block_prover = BlockProver.new(block.header.version, block_builder.receipts, BlockProverType.Receipt) receipt_proof = block_prover.get_proof(tx_index) receipts_hash = block_prover.to_hash32( block_builder.receipts[tx_index]) assert block_prover.prove(receipts_hash, block.header.receipts_hash, receipt_proof)