def test_block_cache(self): block_store = {} cache = BlockCache(block_store=block_store, keep_time=1, purge_frequency=1) header1 = BlockHeader(previous_block_id="000") block1 = BlockWrapper(Block(header=header1.SerializeToString(), header_signature="ABC")) header2 = BlockHeader(previous_block_id="ABC") block2 = BlockWrapper(Block(header=header2.SerializeToString(), header_signature="DEF")) header3 = BlockHeader(previous_block_id="BCA") block3 = BlockWrapper(Block(header=header3.SerializeToString(), header_signature="FED")) cache[block1.header_signature] = block1 cache[block2.header_signature] = block2 # Check that blocks are in the BlockCache self.assertIn("ABC", cache) self.assertIn("DEF", cache) # Wait for purge time to expire time.sleep(1) # Add "FED" cache[block3.header_signature] = block3 # Check that "ABC" is still in the cache even though the keep time has # expired because it has a referecne count of 1 but "DEF" has been # removed self.assertIn("ABC", cache) self.assertNotIn("DEF", cache) self.assertIn("FED", cache)
def _create_blocks(self, block_count, batch_count, valid_block=True, valid_batch=True): block_list = [] for i in range(block_count): batch_list = self._create_batches(batch_count, 2, valid_batch=valid_batch) batch_ids = [batch.header_signature for batch in batch_list] block_header = BlockHeader(signer_pubkey=self.public_key, batch_ids=batch_ids) header_bytes = block_header.SerializeToString() if valid_block: signature = signing.sign(header_bytes, self.private_key) else: signature = "bad_signature" block = Block(header=header_bytes, batches=batch_list, header_signature=signature) block_list.append(block) return block_list
def add_block(self, base_id, root=b'merkle_root'.hex()): block_id = 'b' * (128 - len(base_id)) + base_id head = self.chain_head if head: previous_id = head.header_signature num = head.header.block_num + 1 else: previous_id = 'zzzzz' num = 0 signer_public_key = b'public_key' + bytes(base_id, 'utf-8') header = BlockHeader( block_num=num, previous_block_id=previous_id, signer_public_key=signer_public_key.hex(), batch_ids=[block_id], consensus=b'consensus', state_root_hash=root) block = Block( header=header.SerializeToString(), header_signature=block_id, batches=[make_mock_batch(base_id)]) self.put_blocks([block])
def create_block(self, previous_block_id="0000000000000000"): block_header = BlockHeader(block_num=85, state_root_hash="0987654321fedcba", previous_block_id=previous_block_id) block = BlockWrapper( Block(header_signature="abcdef1234567890", header=block_header.SerializeToString())) return block
def do_block_cache(): block_store = {} cache = BlockCache(block_store=block_store, keep_time=1, purge_frequency=1) header1 = BlockHeader(previous_block_id="000") block1 = BlockWrapper(Block(header=header1.SerializeToString(), header_signature="ABC")) header2 = BlockHeader(previous_block_id="ABC") block2 = BlockWrapper(Block(header=header2.SerializeToString(), header_signature="DEF")) header3 = BlockHeader(previous_block_id="BCA") block3 = BlockWrapper(Block(header=header3.SerializeToString(), header_signature="FED")) cache[block1.header_signature] = block1 cache[block2.header_signature] = block2 return cache
def test_block_missing_predecessor(self): """ The block is completed but the predecessor is missing. """ block = self._create_blocks(1, 1, missing_predecessor=True)[0] self.completer.add_block(block) self.assertEquals(len(self.blocks),0 ) self.assertIn("Missing", self.gossip.requested_blocks) header = BlockHeader(previous_block_id=NULL_BLOCK_IDENTIFIER) missing_block = Block(header_signature="Missing", header=header.SerializeToString()) self.completer.add_block(missing_block) self.assertIn(block.header_signature, self.blocks) self.assertEquals( block, self.completer.get_block(block.header_signature).get_block())
def _make_block(self, txns_family, signer_pubkey, same_pubkey=True): transactions = [] for family in txns_family: txn_header = TransactionHeader( family_name=family, signer_pubkey=signer_pubkey) txn = Transaction(header=txn_header.SerializeToString()) transactions.append(txn) batch = Batch(transactions=transactions) if same_pubkey: block_header = BlockHeader(signer_pubkey=signer_pubkey) else: block_header = BlockHeader(signer_pubkey="other") block = Block(header=block_header.SerializeToString(), batches=[batch]) return BlockWrapper(block)
def test_consensus_chain_head_get_handler(self): header = BlockHeader(previous_block_id='abcd') self.mock_proxy.chain_head_get.return_value = Mock( identifier='abcd', previous_block_id='abcd', header_signature='abcd', header=header.SerializeToString(), signer_public_key='abcd', block_num=1, consensus=b'consensus') handler = handlers.ConsensusChainHeadGetHandler(self.mock_proxy) request_class = handler.request_class request = request_class() result = handler.handle(None, request.SerializeToString()) response = result.message_out self.assertEqual(response.status, handler.response_class.OK) self.mock_proxy.chain_head_get.assert_called_with()
def test_consensus_blocks_get_handler(self): header = BlockHeader(previous_block_id='abcd') self.mock_proxy.blocks_get.return_value = [ Mock(identifier='abcd', previous_block_id='abcd', header_signature='abcd', header=header.SerializeToString(), signer_public_key='abcd', block_num=1, consensus=b'consensus') ] handler = handlers.ConsensusBlocksGetHandler(self.mock_proxy) request_class = handler.request_class request = request_class() request.block_ids.extend([b"test"]) result = handler.handle('mock-id', request.SerializeToString()) response = result.message_out self.assertEqual(response.status, handler.response_class.OK) self.mock_proxy.blocks_get.assert_called_with(request.block_ids)
def _create_blocks(self, block_count, batch_count, missing_predecessor=False, missing_batch=False, find_batch=True): block_list = [] pred = 0 for i in range(0, block_count): batch_list = self._create_batches(batch_count, 2) batch_ids = [batch.header_signature for batch in batch_list] if missing_predecessor: predecessor = "Missing" else: predecessor = (block_list[i-1].header_signature if i > 0 else NULL_BLOCK_IDENTIFIER) block_header = BlockHeader( signer_pubkey=self.public_key, batch_ids=batch_ids, block_num=i, previous_block_id= predecessor ) header_bytes = block_header.SerializeToString() signature = signing.sign( header_bytes, self.private_key) if missing_batch: if find_batch: self.completer.add_batch(batch_list[-1]) batch_list = batch_list[:-1] block = Block(header=header_bytes, batches=batch_list, header_signature=signature) block_list.append(block) return block_list
def add_block(self, block_id, root='merkle_root'): head = self.chain_head if head: previous_id = head.header_signature num = head.header.block_num + 1 else: previous_id = 'zzzzz' num = 0 header = BlockHeader(block_num=num, previous_block_id=previous_id, signer_pubkey='pubkey', batch_ids=[block_id], consensus=b'consensus', state_root_hash=root) block = Block(header=header.SerializeToString(), header_signature=block_id, batches=[_make_mock_batch(block_id)]) self.update_chain([BlockWrapper(block)], [])
def test_consensus_register_handler(self): header = BlockHeader(previous_block_id="beef", signer_public_key="abcd", block_num=12, consensus=b'deadbeef') mock_chain_head = Mock(header_signature="dead", header=header.SerializeToString()) mock_startup_info = StartupInfo(chain_head=mock_chain_head, peers=['dead', 'beef'], local_peer_info=b'abc') self.mock_proxy.register.return_value = mock_startup_info handler = handlers.ConsensusRegisterHandler(self.mock_proxy) request_class = handler.request_class request = request_class() result = handler.handle(None, request.SerializeToString()) response = result.message_out self.assertEqual(response.status, handler.response_class.OK) self.assertEqual(response.chain_head.block_id, bytes.fromhex("dead")) self.assertEqual(response.peers[0].peer_id, bytes.fromhex("dead")) self.assertEqual(response.local_peer_info.peer_id, b'abc') self.mock_proxy.register.assert_called_with('', '', None)
def do_block_event_extractor(): block_header = BlockHeader(block_num=85, state_root_hash="0987654321fedcba", previous_block_id="0000000000000000") block = BlockWrapper( Block(header_signature="abcdef1234567890", header=block_header.SerializeToString())) extractor = BlockEventExtractor(block) events = extractor.extract( [EventSubscription(event_type="sawtooth/block-commit")]) assert events == [ Event(event_type="sawtooth/block-commit", attributes=[ Event.Attribute(key="block_id", value="abcdef1234567890"), Event.Attribute(key="block_num", value="85"), Event.Attribute(key="state_root_hash", value="0987654321fedcba"), Event.Attribute(key="previous_block_id", value="0000000000000000") ]) ]
def test_block_event_extractor(self): """Test that a sawtooth/block-commit event is generated correctly.""" block_header = BlockHeader(block_num=85, state_root_hash="0987654321fedcba", previous_block_id="0000000000000000") block = BlockWrapper( Block(header_signature="abcdef1234567890", header=block_header.SerializeToString())) extractor = BlockEventExtractor(block) events = extractor.extract( [EventSubscription(event_type="sawtooth/block-commit")]) self.assertEqual(events, [ Event(event_type="sawtooth/block-commit", attributes=[ Event.Attribute(key="block_id", value="abcdef1234567890"), Event.Attribute(key="block_num", value="85"), Event.Attribute(key="state_root_hash", value="0987654321fedcba"), Event.Attribute(key="previous_block_id", value="0000000000000000") ]) ])