def test_create_factory(self): factory = P2PFactory(chain_manager=ChainManager(None), sync_state=None, qrl_node=None) self.assertEqual( P2PProtocol, factory.protocol, "Factory has not been assigned the expected protocol")
def start_listening(self): self._p2pfactory = P2PFactory( chain_manager=self._chain_manager, sync_state=self.sync_state, qrl_node=self) # FIXME: Try to avoid cyclic references self._p2pfactory.start_listening()
def setUp(self): port = '9000' self.m_qrlnode = Mock(autospec=QRLNode, name='Fake QRLNode') self.channel_1 = Mock(autospec=P2PProtocol, name='mock Channel 1', peer_ip='1.1.1.1', peer_port=port) self.channel_2 = Mock(autospec=P2PProtocol, name='mock Channel 2', peer_ip='2.2.2.2', peer_port=port) self.channel_3 = Mock(autospec=P2PProtocol, name='mock Channel 3', peer_ip='3.3.3.3', peer_port=port) self.factory = P2PFactory(chain_manager=Mock(autospec=ChainManager), sync_state=None, qrl_node=self.m_qrlnode) self.factory.pow = Mock(autospec=POW) self.m_qrlnode.is_banned.return_value = False self.factory.add_connection(self.channel_1) self.factory.add_connection(self.channel_2) self.factory.add_connection(self.channel_3) # The peer has two blocks. It has sent a list of their hashes, and block_number indicates that the first hash # in the list is for block_number 1. self.factory._target_node_header_hash = qrl_pb2.NodeHeaderHash( block_number=1, headerhashes=[bhstr2bin('123456'), bhstr2bin('deadbeef')] ) # This mocking ensures that the Block gets added to the Chain, and that the next block is requested. self.factory._target_peer = self.channel_1 self.factory._last_requested_block_number = 1 self.factory._chain_manager.add_block.return_value = True self.factory.peer_fetch_block = Mock(autospec=P2PFactory.peer_fetch_block)
def setUp(self): self.m_qrlnode = Mock(autospec=QRLNode, name='Fake QRLNode') self.m_qrlnode.peer_manager = Mock(autospec=P2PPeerManager, name='Fake PeerManager') self.m_qrlnode.peer_manager.is_banned.return_value = False self.m_qrlnode.peer_manager.load_known_peers.return_value = [ '8.8.8.8', '9.9.9.9' ] self.channel_1 = Mock(autospec=P2PProtocol, name='mock Channel 1', peer=IPMetadata('1.1.1.1', config.user.p2p_public_port)) self.channel_2 = Mock(autospec=P2PProtocol, name='mock Channel 2', peer=IPMetadata('2.2.2.2', config.user.p2p_public_port)) self.channel_3 = Mock(autospec=P2PProtocol, name='mock Channel 3', peer=IPMetadata('3.3.3.3', config.user.p2p_public_port)) self.factory = P2PFactory( chain_manager=ChainManager(state=Mock(autospec=State)), sync_state=None, qrl_node=self.m_qrlnode) self.factory.pow = Mock(autospec=POW) self.factory.add_connection(self.channel_1) self.factory.add_connection(self.channel_2) self.factory.add_connection(self.channel_3)
def setUp(self): def replacement_get_block_by_number(i): return self.blocks[i - 1] port = '9000' self.m_qrlnode = Mock(autospec=QRLNode, name='Fake QRLNode') self.factory = P2PFactory( chain_manager=ChainManager(state=Mock(autospec=State)), sync_state=None, qrl_node=self.m_qrlnode) self.factory.peer_fetch_block = Mock( autospec=P2PFactory.peer_fetch_block) self.factory._chain_manager.get_block_by_number = replacement_get_block_by_number self.channel_1 = Mock(autospec=P2PProtocol, name='mock Channel 1', peer_ip='1.1.1.1', peer_port=port) self.factory.add_connection(self.channel_1) self.blocks = [ Mock(autospec=Block, name='0th Block', block_number=1, headerhash=bhstr2bin('123456')), Mock(autospec=Block, name='1st Block', block_number=2, headerhash=bhstr2bin('7890ab')), Mock(autospec=Block, name='2nd Block', block_number=3, headerhash=bhstr2bin('deadbeef')) ] self.node_header_hash = qrl_pb2.NodeHeaderHash( block_number=1, headerhashes=[ bhstr2bin('123456'), bhstr2bin('7890ab'), bhstr2bin('deadbeef') ])
def setUp(self): port = '9000' self.m_qrlnode = Mock(autospec=QRLNode, name='Fake QRLNode') self.channel_1 = Mock(autospec=P2PProtocol, name='mock Channel 1', peer_ip='1.1.1.1', peer_port=port) self.channel_2 = Mock(autospec=P2PProtocol, name='mock Channel 2', peer_ip='2.2.2.2', peer_port=port) self.channel_3 = Mock(autospec=P2PProtocol, name='mock Channel 3', peer_ip='3.3.3.3', peer_port=port) self.factory = P2PFactory( chain_manager=ChainManager(state=Mock(autospec=State)), sync_state=None, qrl_node=self.m_qrlnode) self.factory.pow = Mock(autospec=POW) self.m_qrlnode.is_banned.return_value = False self.factory.add_connection(self.channel_1) self.factory.add_connection(self.channel_2) self.factory.add_connection(self.channel_3) self.factory._target_channel = self.channel_1 self.factory.is_syncing_finished = Mock( return_value=False, autospec=P2PFactory.is_syncing_finished) self.factory._target_node_header_hash = qrl_pb2.NodeHeaderHash( block_number=1, headerhashes=[ bhstr2bin('123456'), bhstr2bin('deadbeef'), bhstr2bin('abcdef') ]) self.factory._last_requested_block_number = 1