def test_data_received(self, mock): node = NeoNode() node.endpoint = Endpoint('hello.com', 1234) node.host = node.endpoint.host node.port = node.endpoint.port payload = VersionPayload(10234, 1234, 'version') message = Message('version', payload=payload) stream = StreamManager.GetStream() writer = BinaryWriter(stream) message.Serialize(writer) out = stream.getvalue() node.dataReceived(out) mock.assert_called_once() self.assertEqual(node.Version.Nonce, payload.Nonce)
def test_handle_message(self, mock): node = NeoNode() node.endpoint = Endpoint('hello.com', 1234) node.host = node.endpoint.host node.port = node.endpoint.port payload = VersionPayload(10234, 1234, 'version') message = Message('version', payload=payload) stream = StreamManager.GetStream() writer = BinaryWriter(stream) message.Serialize(writer) out = stream.getvalue() print("OUT %s " % out) out1 = out[0:10] out2 = out[10:20] out3 = out[20:] node.dataReceived(out1) node.dataReceived(out2) self.assertEqual(node.buffer_in, out1 + out2) # import pdb # pdb.set_trace() self.assertEqual(node.bytes_in, 20) mock.assert_not_called() node.dataReceived(out3) self.assertEqual(node.bytes_in, len(out)) # mock.assert_called_with(message) mock.assert_called_once()
def mock_connect_tcp(host, port, factory, timeout=120): node = NeoNode() node.endpoint = Endpoint(host, port) leader.AddConnectedPeer(node) return node
def test_peer_adding(self): leader = NodeLeader.Instance() Blockchain.Default()._block_cache = {'hello': 1} def mock_call_later(delay, method, *args): method(*args) def mock_connect_tcp(host, port, factory, timeout=120): node = NeoNode() node.endpoint = Endpoint(host, port) leader.AddConnectedPeer(node) return node def mock_disconnect(peer): return True def mock_send_msg(node, message): return True settings.set_max_peers(len(settings.SEED_LIST)) with patch('twisted.internet.reactor.connectTCP', mock_connect_tcp): with patch('twisted.internet.reactor.callLater', mock_call_later): with patch('neo.Network.NeoNode.NeoNode.Disconnect', mock_disconnect): with patch( 'neo.Network.NeoNode.NeoNode.SendSerializedMessage', mock_send_msg): leader.Start() self.assertEqual(len(leader.Peers), len(settings.SEED_LIST)) # now test adding another leader.RemoteNodePeerReceived('hello.com', 1234, 6) # it shouldnt add anything so it doesnt go over max connected peers self.assertEqual(len(leader.Peers), len(settings.SEED_LIST)) # test adding peer peer = NeoNode() peer.endpoint = Endpoint('hellloo.com', 12344) leader.ADDRS.append('hellloo.com:12344') leader.AddConnectedPeer(peer) self.assertEqual(len(leader.Peers), len(settings.SEED_LIST)) # now get a peer peer = leader.Peers[0] leader.RemoveConnectedPeer(peer) self.assertEqual(len(leader.Peers), len(settings.SEED_LIST) - 1) self.assertEqual(len(leader.ADDRS), len(settings.SEED_LIST)) # now test adding another leader.RemoteNodePeerReceived('hello.com', 1234, 6) self.assertEqual(len(leader.Peers), len(settings.SEED_LIST)) # now if we remove all peers, it should restart peers = leader.Peers[:] for peer in peers: leader.RemoveConnectedPeer(peer) # test reset leader.ResetBlockRequestsAndCache() self.assertEqual(Blockchain.Default()._block_cache, {})
def mock_connect_tcp(host, port, factory): node = NeoNode() node.endpoint = Endpoint(host, port) leader.AddConnectedPeer(node) return node
def test_peer_adding(self): leader = NodeLeader.Instance() Blockchain.Default()._block_cache = {'hello': 1} def mock_call_later(delay, method, *args): method(*args) def mock_connect_tcp(host, port, factory): node = NeoNode() node.endpoint = Endpoint(host, port) leader.AddConnectedPeer(node) return node def mock_disconnect(peer): return True def mock_send_msg(node, message): return True settings.set_max_peers(len(settings.SEED_LIST)) with patch('twisted.internet.reactor.connectTCP', mock_connect_tcp): with patch('twisted.internet.reactor.callLater', mock_call_later): with patch('neo.Network.NeoNode.NeoNode.Disconnect', mock_disconnect): with patch('neo.Network.NeoNode.NeoNode.SendSerializedMessage', mock_send_msg): leader.Start() self.assertEqual(len(leader.Peers), len(settings.SEED_LIST)) # now test adding another leader.RemoteNodePeerReceived('hello.com', 1234, 6) # it shouldnt add anything so it doesnt go over max connected peers self.assertEqual(len(leader.Peers), len(settings.SEED_LIST)) # test adding peer peer = NeoNode() peer.endpoint = Endpoint('hellloo.com', 12344) leader.ADDRS.append('hellloo.com:12344') leader.AddConnectedPeer(peer) self.assertEqual(len(leader.Peers), len(settings.SEED_LIST)) # now get a peer peer = leader.Peers[0] leader.RemoveConnectedPeer(peer) self.assertEqual(len(leader.Peers), len(settings.SEED_LIST) - 1) self.assertEqual(len(leader.ADDRS), len(settings.SEED_LIST) - 1) # now test adding another leader.RemoteNodePeerReceived('hello.com', 1234, 6) self.assertEqual(len(leader.Peers), len(settings.SEED_LIST)) # now on updated max peers test leader.OnUpdatedMaxPeers(settings.CONNECTED_PEER_MAX, settings.CONNECTED_PEER_MAX - 1) leader.OnUpdatedMaxPeers(settings.CONNECTED_PEER_MAX - 1, 10) # now if we remove all peers, it should restart peers = leader.Peers[:] for peer in peers: leader.RemoveConnectedPeer(peer) # and peers should be equal to the seed list self.assertEqual(len(leader.Peers), len(settings.SEED_LIST)) # test reset leader.ResetBlockRequestsAndCache() self.assertEqual(Blockchain.Default()._block_cache, {})