def setUp(self): self._mock_block_manager = MockBlockManager() self._mock_block_publisher = Mock() self._mock_chain_controller = Mock() self._mock_gossip = Mock() self._mock_identity_signer = Mock() self._mock_settings_view_factory = Mock() self._mock_state_view_factory = Mock() self._proxy = ConsensusProxy( block_manager=self._mock_block_manager, chain_controller=self._mock_chain_controller, block_publisher=self._mock_block_publisher, gossip=self._mock_gossip, identity_signer=self._mock_identity_signer, settings_view_factory=self._mock_settings_view_factory, state_view_factory=self._mock_state_view_factory)
def setUp(self): self._mock_block_cache = {} self._mock_block_publisher = Mock() self._mock_chain_controller = Mock() self._mock_gossip = MockGossip() self._mock_identity_signer = MockIdentitySigner() self._mock_settings_view_factory = Mock() self._mock_state_view_factory = Mock() self._consensus_registry = MockConsensusRegistry() self._proxy = ConsensusProxy( block_cache=self._mock_block_cache, chain_controller=self._mock_chain_controller, block_publisher=self._mock_block_publisher, gossip=self._mock_gossip, identity_signer=self._mock_identity_signer, settings_view_factory=self._mock_settings_view_factory, state_view_factory=self._mock_state_view_factory, consensus_registry=self._consensus_registry)
def setUp(self): self._mock_block_manager = MockBlockManager() self._mock_journal = Mock() self._mock_gossip = MockGossip() self._mock_identity_signer = MockIdentitySigner() self._mock_settings_view_factory = Mock() self._mock_state_view_factory = Mock() self._consensus_registry = MockConsensusRegistry() self._consensus_notifier = Mock() self._proxy = ConsensusProxy( block_manager=self._mock_block_manager, journal=self._mock_journal, gossip=self._mock_gossip, identity_signer=self._mock_identity_signer, settings_view_factory=self._mock_settings_view_factory, state_view_factory=self._mock_state_view_factory, consensus_registry=self._consensus_registry, consensus_notifier=self._consensus_notifier)
def setUp(self): self._mock_block_cache = {} self._mock_block_publisher = Mock() self._mock_chain_controller = Mock() self._mock_gossip = Mock() self._mock_identity_signer = Mock() self._mock_settings_view_factory = Mock() self._mock_state_view_factory = Mock() self._proxy = ConsensusProxy( block_cache=self._mock_block_cache, chain_controller=self._mock_chain_controller, block_publisher=self._mock_block_publisher, gossip=self._mock_gossip, identity_signer=self._mock_identity_signer, settings_view_factory=self._mock_settings_view_factory, state_view_factory=self._mock_state_view_factory)
def setUp(self): self._mock_block_manager = MockBlockManager() self._mock_block_publisher = Mock() self._mock_chain_controller = Mock() self._mock_gossip = MockGossip() self._mock_identity_signer = MockIdentitySigner() self._mock_settings_view_factory = Mock() self._mock_state_view_factory = Mock() self._consensus_registry = MockConsensusRegistry() self._consensus_notifier = Mock() self._proxy = ConsensusProxy( block_manager=self._mock_block_manager, chain_controller=self._mock_chain_controller, block_publisher=self._mock_block_publisher, gossip=self._mock_gossip, identity_signer=self._mock_identity_signer, settings_view_factory=self._mock_settings_view_factory, state_view_factory=self._mock_state_view_factory, consensus_registry=self._consensus_registry, consensus_notifier=self._consensus_notifier)
def __init__(self, bind_network, bind_component, bind_consensus, endpoint, peering, seeds_list, peer_list, data_dir, config_dir, identity_signer, scheduler_type, permissions, minimum_peer_connectivity, maximum_peer_connectivity, state_pruning_block_depth, network_public_key=None, network_private_key=None, roles=None): """Constructs a validator instance. Args: bind_network (str): the network endpoint bind_component (str): the component endpoint endpoint (str): the zmq-style URI of this validator's publically reachable endpoint peering (str): The type of peering approach. Either 'static' or 'dynamic'. In 'static' mode, no attempted topology buildout occurs -- the validator only attempts to initiate peering connections with endpoints specified in the peer_list. In 'dynamic' mode, the validator will first attempt to initiate peering connections with endpoints specified in the peer_list and then attempt to do a topology buildout starting with peer lists obtained from endpoints in the seeds_list. In either mode, the validator will accept incoming peer requests up to max_peers. seeds_list (list of str): a list of addresses to connect to in order to perform the initial topology buildout peer_list (list of str): a list of peer addresses data_dir (str): path to the data directory config_dir (str): path to the config directory identity_signer (str): cryptographic signer the validator uses for signing """ # -- Setup Global State Database and Factory -- # global_state_db_filename = os.path.join( data_dir, 'merkle-{}.lmdb'.format(bind_network[-2:])) LOGGER.debug('global state database file is %s', global_state_db_filename) global_state_db = NativeLmdbDatabase( global_state_db_filename, indexes=MerkleDatabase.create_index_configuration()) state_view_factory = StateViewFactory(global_state_db) # -- Setup Receipt Store -- # receipt_db_filename = os.path.join( data_dir, 'txn_receipts-{}.lmdb'.format(bind_network[-2:])) LOGGER.debug('txn receipt store file is %s', receipt_db_filename) receipt_db = LMDBNoLockDatabase(receipt_db_filename, 'c') receipt_store = TransactionReceiptStore(receipt_db) # -- Setup Block Store -- # block_db_filename = os.path.join( data_dir, 'block-{}.lmdb'.format(bind_network[-2:])) LOGGER.debug('block store file is %s', block_db_filename) block_db = IndexedDatabase( block_db_filename, BlockStore.serialize_block, BlockStore.deserialize_block, flag='c', indexes=BlockStore.create_index_configuration()) block_store = BlockStore(block_db) # The cache keep time for the journal's block cache must be greater # than the cache keep time used by the completer. base_keep_time = 1200 block_cache = BlockCache(block_store, keep_time=int(base_keep_time * 9 / 8), purge_frequency=30) # -- Setup Thread Pools -- # component_thread_pool = InstrumentedThreadPoolExecutor( max_workers=10, name='Component') network_thread_pool = InstrumentedThreadPoolExecutor(max_workers=10, name='Network') client_thread_pool = InstrumentedThreadPoolExecutor(max_workers=5, name='Client') sig_pool = InstrumentedThreadPoolExecutor(max_workers=3, name='Signature') # -- Setup Dispatchers -- # component_dispatcher = Dispatcher() network_dispatcher = Dispatcher() # -- Setup Services -- # component_service = Interconnect(bind_component, component_dispatcher, secured=False, heartbeat=False, max_incoming_connections=20, monitor=True, max_future_callback_workers=10) zmq_identity = hashlib.sha512( time.time().hex().encode()).hexdigest()[:23] secure = False if network_public_key is not None and network_private_key is not None: secure = True network_service = Interconnect(bind_network, dispatcher=network_dispatcher, zmq_identity=zmq_identity, secured=secure, server_public_key=network_public_key, server_private_key=network_private_key, heartbeat=True, public_endpoint=endpoint, connection_timeout=120, max_incoming_connections=100, max_future_callback_workers=10, authorize=True, signer=identity_signer, roles=roles) # -- Setup Transaction Execution Platform -- # context_manager = ContextManager(global_state_db) batch_tracker = BatchTracker(block_store) settings_cache = SettingsCache( SettingsViewFactory(state_view_factory), ) transaction_executor = TransactionExecutor( service=component_service, context_manager=context_manager, settings_view_factory=SettingsViewFactory(state_view_factory), scheduler_type=scheduler_type, invalid_observers=[batch_tracker]) component_service.set_check_connections( transaction_executor.check_connections) event_broadcaster = EventBroadcaster(component_service, block_store, receipt_store) # -- Setup P2P Networking -- # gossip = Gossip(network_service, settings_cache, lambda: block_store.chain_head, block_store.chain_head_state_root, endpoint=endpoint, peering_mode=peering, initial_seed_endpoints=seeds_list, initial_peer_endpoints=peer_list, minimum_peer_connectivity=minimum_peer_connectivity, maximum_peer_connectivity=maximum_peer_connectivity, topology_check_frequency=1) completer = Completer(block_store, gossip, cache_keep_time=base_keep_time, cache_purge_frequency=30, requested_keep_time=300) block_sender = BroadcastBlockSender(completer, gossip) batch_sender = BroadcastBatchSender(completer, gossip) chain_id_manager = ChainIdManager(data_dir) identity_view_factory = IdentityViewFactory( StateViewFactory(global_state_db)) id_cache = IdentityCache(identity_view_factory) # -- Setup Permissioning -- # permission_verifier = PermissionVerifier( permissions, block_store.chain_head_state_root, id_cache) identity_observer = IdentityObserver(to_update=id_cache.invalidate, forked=id_cache.forked) settings_observer = SettingsObserver( to_update=settings_cache.invalidate, forked=settings_cache.forked) # -- Consensus Engine -- # consensus_thread_pool = InstrumentedThreadPoolExecutor( max_workers=3, name='Consensus') consensus_dispatcher = Dispatcher() consensus_service = Interconnect(bind_consensus, consensus_dispatcher, secured=False, heartbeat=False, max_incoming_connections=20, monitor=True, max_future_callback_workers=10) consensus_notifier = ConsensusNotifier(consensus_service) # -- Setup Journal -- # batch_injector_factory = DefaultBatchInjectorFactory( block_cache=block_cache, state_view_factory=state_view_factory, signer=identity_signer) block_publisher = BlockPublisher( transaction_executor=transaction_executor, block_cache=block_cache, state_view_factory=state_view_factory, settings_cache=settings_cache, block_sender=block_sender, batch_sender=batch_sender, chain_head=block_store.chain_head, identity_signer=identity_signer, data_dir=data_dir, config_dir=config_dir, permission_verifier=permission_verifier, check_publish_block_frequency=0.1, batch_observers=[batch_tracker], batch_injector_factory=batch_injector_factory) block_publisher_batch_sender = block_publisher.batch_sender() block_validator = BlockValidator( block_cache=block_cache, state_view_factory=state_view_factory, transaction_executor=transaction_executor, identity_signer=identity_signer, data_dir=data_dir, config_dir=config_dir, permission_verifier=permission_verifier) chain_controller = ChainController( block_store=block_store, block_cache=block_cache, block_validator=block_validator, state_database=global_state_db, chain_head_lock=block_publisher.chain_head_lock, state_pruning_block_depth=state_pruning_block_depth, data_dir=data_dir, observers=[ event_broadcaster, receipt_store, batch_tracker, identity_observer, settings_observer ]) genesis_controller = GenesisController( context_manager=context_manager, transaction_executor=transaction_executor, completer=completer, block_store=block_store, state_view_factory=state_view_factory, identity_signer=identity_signer, data_dir=data_dir, config_dir=config_dir, chain_id_manager=chain_id_manager, batch_sender=batch_sender) responder = Responder(completer) completer.set_on_batch_received(block_publisher_batch_sender.send) completer.set_on_block_received(chain_controller.queue_block) completer.set_chain_has_block(chain_controller.has_block) # -- Register Message Handler -- # network_handlers.add(network_dispatcher, network_service, gossip, completer, responder, network_thread_pool, sig_pool, chain_controller.has_block, block_publisher.has_batch, permission_verifier, block_publisher, consensus_notifier) component_handlers.add(component_dispatcher, gossip, context_manager, transaction_executor, completer, block_store, batch_tracker, global_state_db, self.get_chain_head_state_root_hash, receipt_store, event_broadcaster, permission_verifier, component_thread_pool, client_thread_pool, sig_pool, block_publisher) # -- Store Object References -- # self._component_dispatcher = component_dispatcher self._component_service = component_service self._component_thread_pool = component_thread_pool self._network_dispatcher = network_dispatcher self._network_service = network_service self._network_thread_pool = network_thread_pool consensus_proxy = ConsensusProxy( block_cache=block_cache, chain_controller=chain_controller, block_publisher=block_publisher, gossip=gossip, identity_signer=identity_signer, settings_view_factory=SettingsViewFactory(state_view_factory), state_view_factory=state_view_factory) consensus_handlers.add(consensus_dispatcher, consensus_thread_pool, consensus_proxy) self._consensus_dispatcher = consensus_dispatcher self._consensus_service = consensus_service self._consensus_thread_pool = consensus_thread_pool self._client_thread_pool = client_thread_pool self._sig_pool = sig_pool self._context_manager = context_manager self._transaction_executor = transaction_executor self._genesis_controller = genesis_controller self._gossip = gossip self._block_publisher = block_publisher self._chain_controller = chain_controller self._block_validator = block_validator
class TestProxy(unittest.TestCase): def setUp(self): self._mock_block_manager = MockBlockManager() self._mock_block_publisher = Mock() self._mock_chain_controller = Mock() self._mock_gossip = Mock() self._mock_identity_signer = Mock() self._mock_settings_view_factory = Mock() self._mock_state_view_factory = Mock() self._proxy = ConsensusProxy( block_manager=self._mock_block_manager, chain_controller=self._mock_chain_controller, block_publisher=self._mock_block_publisher, gossip=self._mock_gossip, identity_signer=self._mock_identity_signer, settings_view_factory=self._mock_settings_view_factory, state_view_factory=self._mock_state_view_factory) def test_send_to(self): self._proxy.send_to(peer_id=b'peer_id', message=b'message') def test_broadcast(self): self._proxy.broadcast(message=b'message') # Using block publisher def test_initialize_block(self): self._proxy.initialize_block(None) self._mock_block_publisher.initialize_block.assert_called_with( self._mock_chain_controller.chain_head) self._mock_block_manager["34"] = "a block" self._proxy.initialize_block(previous_id=bytes([0x34])) self._mock_block_publisher\ .initialize_block.assert_called_with("a block") def test_summarize_block(self): self._mock_block_publisher.summarize_block.return_value =\ b"summary" summary = self._proxy.summarize_block() self._mock_block_publisher.summarize_block.assert_called_with() self.assertEqual(summary, b"summary") def test_finalize_block(self): self._mock_block_publisher.finalize_block.return_value = "00" data = bytes([0x56]) self._proxy.finalize_block(data) self._mock_block_publisher.finalize_block.assert_called_with( consensus=data) def test_cancel_block(self): self._proxy.cancel_block() self._mock_block_publisher.cancel_block.assert_called_with() # Using chain controller def test_check_blocks(self): block_ids = [bytes([0x56]), bytes([0x78])] self._mock_block_manager["56"] = "block0" self._mock_block_manager["78"] = "block1" self._proxy.check_blocks(block_ids) with self.assertRaises(UnknownBlock): self._proxy.check_blocks([bytes([0x00])]) def test_commit_block(self): self._mock_block_manager["34"] = "a block" self._proxy.commit_block(block_id=bytes([0x34])) self._mock_chain_controller\ .commit_block\ .assert_called_with("a block") def test_ignore_block(self): self._mock_block_manager["34"] = "a block" self._proxy.ignore_block(block_id=bytes([0x34])) self._mock_chain_controller\ .ignore_block\ .assert_called_with("a block") def test_fail_block(self): self._mock_block_manager["34"] = "a block" self._proxy.fail_block(block_id=bytes([0x34])) self._mock_chain_controller\ .fail_block\ .assert_called_with("a block") # Using blockstore and state database def test_blocks_get(self): block_1 = Mock(identifier=b'id-1', previous_block_id=b'prev-1', header_signature=b'sign-1', block_num=1, consensus=b'consensus') self._mock_block_manager[b'block1'.hex()] = block_1 block_2 = Mock(identifier=b'id-2', previous_block_id=b'prev-2', header_signature=b'sign-2', block_num=2, consensus=b'consensus') self._mock_block_manager[b'block2'.hex()] = block_2 proxy_block_1, proxy_block_2 = self._proxy.blocks_get( [b'block1', b'block2']) self.assertEqual(block_1, proxy_block_1) self.assertEqual(block_2, proxy_block_2) def test_chain_head_get(self): chain_head = Mock(identifier=b'id-2', previous_block_id=b'prev-2', header_signature=b'sign-2', block_num=2, consensus=b'consensus') self._mock_chain_controller.chain_head = chain_head self.assertEqual(self._proxy.chain_head_get(), chain_head) @unittest.skip("Test will have to be rethought due to removal of " "blockwrapper from proxy") def test_settings_get(self): self._mock_block_manager[b'block'.hex()] = MockBlock() self.assertEqual(self._proxy.settings_get(b'block', ['key1', 'key2']), [ ('key1', 'mock-key1'), ('key2', 'mock-key2'), ]) @unittest.skip("Test will have to be rethought due to removal of " "blockwrapper from proxy") def test_state_get(self): self._mock_block_manager[b'block'.hex()] = MockBlock() address_1 = '1' * 70 address_2 = '2' * 70 self.assertEqual( self._proxy.state_get(b'block', [address_1, address_2]), [ (address_1, 'mock-{}'.format(address_1).encode()), (address_2, 'mock-{}'.format(address_2).encode()), ])
class TestProxy(unittest.TestCase): def setUp(self): self._mock_block_cache = {} self._mock_block_publisher = Mock() self._mock_chain_controller = Mock() self._mock_gossip = Mock() self._mock_identity_signer = Mock() self._mock_settings_view_factory = Mock() self._mock_state_view_factory = Mock() self._proxy = ConsensusProxy( block_cache=self._mock_block_cache, chain_controller=self._mock_chain_controller, block_publisher=self._mock_block_publisher, gossip=self._mock_gossip, identity_signer=self._mock_identity_signer, settings_view_factory=self._mock_settings_view_factory, state_view_factory=self._mock_state_view_factory) def test_send_to(self): self._proxy.send_to(peer_id=b'peer_id', message=b'message') def test_broadcast(self): self._proxy.broadcast(message=b'message') # Using block publisher def test_initialize_block(self): self._proxy.initialize_block(None) self._mock_block_publisher.initialize_block.assert_called_with( self._mock_chain_controller.chain_head) self._mock_block_cache["34"] = "a block" self._proxy.initialize_block(previous_id=bytes([0x34])) self._mock_block_publisher\ .initialize_block.assert_called_with("a block") def test_summarize_block(self): self._mock_block_publisher.summarize_block.return_value =\ b"summary" summary = self._proxy.summarize_block() self._mock_block_publisher.summarize_block.assert_called_with() self.assertEqual(summary, b"summary") def test_finalize_block(self): self._mock_block_publisher.finalize_block.return_value =\ FinalizeBlockResult( block=None, remaining_batches=None, last_batch=None, injected_batches=None) self._mock_block_publisher.publish_block.return_value = "00" data = bytes([0x56]) self._proxy.finalize_block(data) self._mock_block_publisher.finalize_block.assert_called_with( consensus=data) self._mock_block_publisher.publish_block.assert_called_with(None, None) def test_cancel_block(self): self._proxy.cancel_block() self._mock_block_publisher.cancel_block.assert_called_with() # Using chain controller def test_check_blocks(self): block_ids = [bytes([0x56]), bytes([0x78])] self._mock_block_cache["56"] = "block0" self._mock_block_cache["78"] = "block1" self._proxy.check_blocks(block_ids) self._mock_chain_controller\ .submit_blocks_for_verification\ .assert_called_with(["block0", "block1"]) def test_commit_block(self): self._mock_block_cache["34"] = "a block" self._proxy.commit_block(block_id=bytes([0x34])) self._mock_chain_controller\ .commit_block\ .assert_called_with("a block") def test_ignore_block(self): self._mock_block_cache["34"] = "a block" self._proxy.ignore_block(block_id=bytes([0x34])) self._mock_chain_controller\ .ignore_block\ .assert_called_with("a block") def test_fail_block(self): self._mock_block_cache["34"] = "a block" self._proxy.fail_block(block_id=bytes([0x34])) self._mock_chain_controller\ .fail_block\ .assert_called_with("a block") # Using blockstore and state database def test_blocks_get(self): block_1 = Mock( identifier=b'id-1', previous_block_id=b'prev-1', header_signature=b'sign-1', block_num=1, consensus=b'consensus') self._mock_block_cache[b'block1'.hex()] = block_1 block_2 = Mock( identifier=b'id-2', previous_block_id=b'prev-2', header_signature=b'sign-2', block_num=2, consensus=b'consensus') self._mock_block_cache[b'block2'.hex()] = block_2 proxy_block_1, proxy_block_2 = self._proxy.blocks_get([ b'block1', b'block2']) self.assertEqual( block_1, proxy_block_1) self.assertEqual( block_2, proxy_block_2) def test_chain_head_get(self): chain_head = Mock( identifier=b'id-2', previous_block_id=b'prev-2', header_signature=b'sign-2', block_num=2, consensus=b'consensus') self._mock_chain_controller.chain_head = chain_head self.assertEqual( self._proxy.chain_head_get(), chain_head) def test_settings_get(self): self._mock_block_cache[b'block'.hex()] = MockBlock() self.assertEqual( self._proxy.settings_get(b'block', ['key1', 'key2']), [ ('key1', 'mock-key1'), ('key2', 'mock-key2'), ]) def test_state_get(self): self._mock_block_cache[b'block'.hex()] = MockBlock() self.assertEqual( self._proxy.state_get(b'block', ['address-1', 'address-2']), [ ('address-1', b'mock-address-1'), ('address-2', b'mock-address-2'), ])
class TestProxy(unittest.TestCase): def setUp(self): self._mock_block_manager = MockBlockManager() self._mock_block_publisher = Mock() self._mock_chain_controller = Mock() self._mock_gossip = MockGossip() self._mock_identity_signer = MockIdentitySigner() self._mock_settings_view_factory = Mock() self._mock_state_view_factory = Mock() self._consensus_registry = MockConsensusRegistry() self._consensus_notifier = Mock() self._proxy = ConsensusProxy( block_manager=self._mock_block_manager, chain_controller=self._mock_chain_controller, block_publisher=self._mock_block_publisher, gossip=self._mock_gossip, identity_signer=self._mock_identity_signer, settings_view_factory=self._mock_settings_view_factory, state_view_factory=self._mock_state_view_factory, consensus_registry=self._consensus_registry, consensus_notifier=self._consensus_notifier) def test_send_to(self): self._proxy.send_to( peer_id=b'peer_id', content=b'message', message_type='message_type', connection_id=b'') def test_broadcast(self): self._proxy.broadcast( content=b'message', message_type='message_type', connection_id=b'') # Using block publisher def test_initialize_block(self): self._proxy.initialize_block(None) self._mock_block_publisher.initialize_block.assert_called_with( self._mock_chain_controller.chain_head) self._mock_block_manager["34"] = "a block" self._proxy.initialize_block(previous_id=bytes([0x34])) self._mock_block_publisher\ .initialize_block.assert_called_with("a block") def test_summarize_block(self): self._mock_block_publisher.summarize_block.return_value =\ b"summary" summary = self._proxy.summarize_block() self._mock_block_publisher.summarize_block.assert_called_with() self.assertEqual(summary, b"summary") def test_finalize_block(self): self._mock_block_publisher.finalize_block.return_value = "00" data = bytes([0x56]) self._proxy.finalize_block(data) self._mock_block_publisher.finalize_block.assert_called_with( consensus=data) def test_cancel_block(self): self._proxy.cancel_block() self._mock_block_publisher.cancel_block.assert_called_with() # Using chain controller def test_check_blocks(self): block_ids = [bytes([0x56]), bytes([0x78])] self._mock_block_manager["56"] = "block0" self._mock_block_manager["78"] = "block1" self._proxy.check_blocks(block_ids) with self.assertRaises(UnknownBlock): self._proxy.check_blocks([bytes([0x00])]) def test_commit_block(self): self._mock_block_manager["34"] = "a block" self._proxy.commit_block(block_id=bytes([0x34])) self._mock_chain_controller\ .commit_block\ .assert_called_with("a block") def test_ignore_block(self): self._mock_block_manager["34"] = "a block" self._proxy.ignore_block(block_id=bytes([0x34])) self._mock_chain_controller\ .ignore_block\ .assert_called_with("a block") def test_fail_block(self): self._mock_block_manager["34"] = "a block" self._proxy.fail_block(block_id=bytes([0x34])) self._mock_chain_controller\ .fail_block\ .assert_called_with("a block") # Using blockstore and state database def test_blocks_get(self): block_1 = Mock( identifier=b'id-1', previous_block_id=b'prev-1', header_signature=b'sign-1', block_num=1, consensus=b'consensus') self._mock_block_manager[b'block1'.hex()] = block_1 block_2 = Mock( identifier=b'id-2', previous_block_id=b'prev-2', header_signature=b'sign-2', block_num=2, consensus=b'consensus') self._mock_block_manager[b'block2'.hex()] = block_2 proxy_block_1, proxy_block_2 = self._proxy.blocks_get([ b'block1', b'block2']) self.assertEqual( block_1, proxy_block_1) self.assertEqual( block_2, proxy_block_2) def test_chain_head_get(self): chain_head = Mock( identifier=b'id-2', previous_block_id=b'prev-2', header_signature=b'sign-2', block_num=2, consensus=b'consensus') self._mock_chain_controller.chain_head = chain_head self.assertEqual( self._proxy.chain_head_get(), chain_head) @unittest.skip("Test will have to be rethought due to removal of " "blockwrapper from proxy") def test_settings_get(self): self._mock_block_manager[b'block'.hex()] = MockBlock() self.assertEqual( self._proxy.settings_get(b'block', ['key1', 'key2']), [ ('key1', 'mock-key1'), ('key2', 'mock-key2'), ]) @unittest.skip("Test will have to be rethought due to removal of " "blockwrapper from proxy") def test_state_get(self): self._mock_block_manager[b'block'.hex()] = MockBlock() address_1 = '1' * 70 address_2 = '2' * 70 self.assertEqual( self._proxy.state_get(b'block', [address_1, address_2]), [ (address_1, 'mock-{}'.format(address_1).encode()), (address_2, 'mock-{}'.format(address_2).encode()), ])
class TestProxy(unittest.TestCase): def setUp(self): self._mock_block_cache = {} self._mock_block_publisher = Mock() self._mock_chain_controller = Mock() self._mock_gossip = Mock() self._mock_identity_signer = Mock() self._mock_settings_view_factory = Mock() self._mock_state_view_factory = Mock() self._proxy = ConsensusProxy( block_cache=self._mock_block_cache, chain_controller=self._mock_chain_controller, block_publisher=self._mock_block_publisher, gossip=self._mock_gossip, identity_signer=self._mock_identity_signer, settings_view_factory=self._mock_settings_view_factory, state_view_factory=self._mock_state_view_factory) def test_send_to(self): self._proxy.send_to(peer_id=b'peer_id', message=b'message') def test_broadcast(self): self._proxy.broadcast(message=b'message') # Using block publisher def test_initialize_block(self): self._proxy.initialize_block(None) self._mock_block_publisher.initialize_block.assert_called_with( self._mock_chain_controller.chain_head) self._mock_block_cache["34"] = "a block" self._proxy.initialize_block(previous_id=bytes([0x34])) self._mock_block_publisher\ .initialize_block.assert_called_with("a block") def test_summarize_block(self): self._mock_block_publisher.summarize_block.return_value =\ b"summary" summary = self._proxy.summarize_block() self._mock_block_publisher.summarize_block.assert_called_with() self.assertEqual(summary, b"summary") def test_finalize_block(self): self._mock_block_publisher.finalize_block.return_value =\ FinalizeBlockResult( block=None, remaining_batches=None, last_batch=None, injected_batches=None) self._mock_block_publisher.publish_block.return_value = "00" data = bytes([0x56]) self._proxy.finalize_block(data) self._mock_block_publisher.finalize_block.assert_called_with( consensus=data) self._mock_block_publisher.publish_block.assert_called_with(None, None) def test_cancel_block(self): self._proxy.cancel_block() self._mock_block_publisher.cancel_block.assert_called_with() # Using chain controller def test_check_blocks(self): block_ids = [bytes([0x56]), bytes([0x78])] self._mock_block_cache["56"] = "block0" self._mock_block_cache["78"] = "block1" self._proxy.check_blocks(block_ids) self._mock_chain_controller\ .submit_blocks_for_verification\ .assert_called_with(["block0", "block1"]) def test_commit_block(self): self._mock_block_cache["34"] = "a block" self._proxy.commit_block(block_id=bytes([0x34])) self._mock_chain_controller\ .commit_block\ .assert_called_with("a block") def test_ignore_block(self): self._mock_block_cache["34"] = "a block" self._proxy.ignore_block(block_id=bytes([0x34])) self._mock_chain_controller\ .ignore_block\ .assert_called_with("a block") def test_fail_block(self): self._mock_block_cache["34"] = "a block" self._proxy.fail_block(block_id=bytes([0x34])) self._mock_chain_controller\ .fail_block\ .assert_called_with("a block") # Using blockstore and state database def test_blocks_get(self): block_1 = Mock(identifier=b'id-1', previous_block_id=b'prev-1', header_signature=b'sign-1', block_num=1, consensus=b'consensus') self._mock_block_cache[b'block1'.hex()] = block_1 block_2 = Mock(identifier=b'id-2', previous_block_id=b'prev-2', header_signature=b'sign-2', block_num=2, consensus=b'consensus') self._mock_block_cache[b'block2'.hex()] = block_2 proxy_block_1, proxy_block_2 = self._proxy.blocks_get( [b'block1', b'block2']) self.assertEqual(block_1, proxy_block_1) self.assertEqual(block_2, proxy_block_2) def test_chain_head_get(self): chain_head = Mock(identifier=b'id-2', previous_block_id=b'prev-2', header_signature=b'sign-2', block_num=2, consensus=b'consensus') self._mock_chain_controller.chain_head = chain_head self.assertEqual(self._proxy.chain_head_get(), chain_head) def test_settings_get(self): self._mock_block_cache[b'block'.hex()] = MockBlock() self.assertEqual(self._proxy.settings_get(b'block', ['key1', 'key2']), [ ('key1', 'mock-key1'), ('key2', 'mock-key2'), ]) def test_state_get(self): self._mock_block_cache[b'block'.hex()] = MockBlock() self.assertEqual( self._proxy.state_get(b'block', ['address-1', 'address-2']), [ ('address-1', b'mock-address-1'), ('address-2', b'mock-address-2'), ])