def get_dummy_block_header( nonce, timestamp=None, block_number=None, prev_block_hash: Optional[Sha256Hash] = None) -> BlockHeader: if timestamp is None: timestamp = 5 * nonce if block_number is None: block_number = 2 * nonce if prev_block_hash is None: prev_block_hash = helpers.generate_bytes( eth_common_constants.BLOCK_HASH_LEN) else: prev_block_hash = prev_block_hash.binary # create BlockHeader object with dummy values multiplied by nonce to be able generate txs with different value return BlockHeader( prev_block_hash, helpers.generate_bytes(eth_common_constants.BLOCK_HASH_LEN), helpers.generate_bytes(eth_common_constants.ADDRESS_LEN), helpers.generate_bytes(eth_common_constants.MERKLE_ROOT_LEN), helpers.generate_bytes(eth_common_constants.MERKLE_ROOT_LEN), helpers.generate_bytes(eth_common_constants.MERKLE_ROOT_LEN), 100 * nonce, nonce, block_number, 3 * nonce, 4 * nonce, timestamp, helpers.generate_bytes(100 * nonce), helpers.generate_bytes(eth_common_constants.BLOCK_HASH_LEN), helpers.generate_bytes(nonce))
def test_new_block_parts_to_new_block_message(self): txs = [] txs_bytes = [] txs_hashes = [] tx_count = 10 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(1) txs.append(tx) tx_bytes = rlp.encode(tx, Transaction) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) block_header = mock_eth_messages.get_dummy_block_header(1) uncles = [ mock_eth_messages.get_dummy_block_header(2), mock_eth_messages.get_dummy_block_header(3), ] block = Block(block_header, txs, uncles) dummy_chain_difficulty = 10 new_block_msg = NewBlockEthProtocolMessage(None, block, dummy_chain_difficulty) self.assertTrue(new_block_msg.rawbytes()) block_body = TransientBlockBody(txs, uncles) block_bytes = rlp.encode(Block.serialize(block)) block_header_bytes = memoryview( rlp.encode(BlockHeader.serialize(block_header))) block_body_bytes = memoryview( rlp.encode(TransientBlockBody.serialize(block_body))) self.assertEqual(len(block_bytes), len(block_header_bytes) + len(block_body_bytes)) internal_new_block_msg = InternalEthBlockInfo.from_new_block_msg( new_block_msg) new_block_parts = internal_new_block_msg.to_new_block_parts() new_block_msg_from_block_parts = NewBlockEthProtocolMessage.from_new_block_parts( new_block_parts, dummy_chain_difficulty) self.assertEqual(len(new_block_msg.rawbytes()), len(new_block_msg_from_block_parts.rawbytes())) self.assertEqual(new_block_msg.rawbytes(), new_block_msg_from_block_parts.rawbytes()) self.assertEqual(new_block_msg_from_block_parts.get_chain_difficulty(), dummy_chain_difficulty)
def test_new_block_internal_eth_message_to_from_new_block_parts(self): txs = [] txs_bytes = [] txs_hashes = [] tx_count = 10 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(1) txs.append(tx) tx_bytes = rlp.encode(tx, Transaction) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) block_header = mock_eth_messages.get_dummy_block_header(1) uncles = [ mock_eth_messages.get_dummy_block_header(2), mock_eth_messages.get_dummy_block_header(3), ] block_number = 100000 block_body = TransientBlockBody(txs, uncles) block_header_bytes = memoryview( rlp.encode(BlockHeader.serialize(block_header))) block_body_bytes = memoryview( rlp.encode(TransientBlockBody.serialize(block_body))) new_block_parts = NewBlockParts(block_header_bytes, block_body_bytes, block_number) new_block_internal_eth_msg = InternalEthBlockInfo.from_new_block_parts( new_block_parts) self.assertIsNotNone(new_block_internal_eth_msg) self.assertTrue(new_block_internal_eth_msg.rawbytes()) parsed_new_block_parts = new_block_internal_eth_msg.to_new_block_parts( ) self.assertIsInstance(parsed_new_block_parts, NewBlockParts) self.assertEqual(block_header_bytes, parsed_new_block_parts.block_header_bytes) self.assertEqual(block_body_bytes, parsed_new_block_parts.block_body_bytes) self.assertEqual(block_number, parsed_new_block_parts.block_number)
def _create_block_header(self, block_number: int, difficulty: int) -> BlockHeader: block_header = BlockHeader( helpers.generate_bytes(eth_common_constants.BLOCK_HASH_LEN), helpers.generate_bytes(eth_common_constants.BLOCK_HASH_LEN), helpers.generate_bytes(eth_common_constants.ADDRESS_LEN), helpers.generate_bytes(eth_common_constants.MERKLE_ROOT_LEN), helpers.generate_bytes(eth_common_constants.MERKLE_ROOT_LEN), helpers.generate_bytes(eth_common_constants.MERKLE_ROOT_LEN), 100, difficulty, block_number, 3, 4, 1601410624, helpers.generate_bytes(100), helpers.generate_bytes(eth_common_constants.BLOCK_HASH_LEN), helpers.generate_bytes(12345)) return block_header
def _create_block_header_bytes(self, block_number: int, difficulty: int) -> memoryview: block_header = BlockHeader( helpers.generate_bytes(eth_common_constants.BLOCK_HASH_LEN), helpers.generate_bytes(eth_common_constants.BLOCK_HASH_LEN), helpers.generate_bytes(eth_common_constants.ADDRESS_LEN), helpers.generate_bytes(eth_common_constants.MERKLE_ROOT_LEN), helpers.generate_bytes(eth_common_constants.MERKLE_ROOT_LEN), helpers.generate_bytes(eth_common_constants.MERKLE_ROOT_LEN), 100, difficulty, block_number, 3, 4, 1601410624, helpers.generate_bytes(100), helpers.generate_bytes(eth_common_constants.BLOCK_HASH_LEN), helpers.generate_bytes(12345)) block_header_bytes = rlp.encode(block_header, BlockHeader) return memoryview(block_header_bytes)
def test_block_headers_msg_from_header_bytes(self): block_header = mock_eth_messages.get_dummy_block_header(1) block_header_bytes = memoryview( rlp.encode(BlockHeader.serialize(block_header))) block_headers_msg = BlockHeadersEthProtocolMessage.from_header_bytes( block_header_bytes) raw_headers = block_headers_msg.get_block_headers() headers_list = list(raw_headers) self.assertEqual(len(headers_list), 1) self.assertTrue(headers_list) self.assertEqual(1, len(block_headers_msg.get_block_headers())) self.assertEqual(1, len(block_headers_msg.get_block_headers_bytes())) self.assertEqual(block_header, block_headers_msg.get_block_headers()[0]) self.assertEqual( block_header_bytes.tobytes(), block_headers_msg.get_block_headers_bytes()[0].tobytes())
def test_new_block_parts(self): txs = [] txs_bytes = [] txs_hashes = [] tx_count = 10 for i in range(1, tx_count): tx = mock_eth_messages.get_dummy_transaction(1) txs.append(tx) tx_bytes = rlp.encode(tx, Transaction) txs_bytes.append(tx_bytes) tx_hash = tx.hash() txs_hashes.append(tx_hash) block_header = mock_eth_messages.get_dummy_block_header(1) uncles = [ mock_eth_messages.get_dummy_block_header(2), mock_eth_messages.get_dummy_block_header(3), ] block_number = 100000 block_body = TransientBlockBody(txs, uncles) block_header_bytes = memoryview( rlp.encode(BlockHeader.serialize(block_header))) block_body_bytes = memoryview( rlp.encode(TransientBlockBody.serialize(block_body))) new_block_parts = NewBlockParts(block_header_bytes, block_body_bytes, block_number) self.assertIsInstance(new_block_parts.get_block_hash(), Sha256Hash) self.assertIsInstance(new_block_parts.get_previous_block_hash(), Sha256Hash) self.assertEqual(1, new_block_parts.get_block_difficulty())