예제 #1
0
 def buildProtocol(self, addr):
     print(f"building new protocol for addr: {addr}")
     self.leader.AddKnownAddress(
         Address(f"{addr.host}:{addr.port}"))
     p = NeoNode(incoming_client=True)
     p.factory = self
     return p
예제 #2
0
    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)
예제 #3
0
 def SetupConnection(self, addr, endpoint=None):
     if len(self.Peers
            ) + self.peers_connecting < settings.CONNECTED_PEER_MAX:
         try:
             host, port = addr.split(':')
             if endpoint:
                 point = endpoint
             else:
                 point = TCP4ClientEndpoint(self.reactor,
                                            host,
                                            int(port),
                                            timeout=5)
             self.peers_connecting += 1
             d = connectProtocol(point, NeoNode())  # type: Deferred
             d.addErrback(self.clientConnectionFailed, addr)
             return d
         except Exception as e:
             logger.error(f"Setup connection with with {e}")
예제 #4
0
    def test_getpeers(self):
        # Given this is an isolated environment and there is no peers
        # lets simulate that at least some addresses are known
        node = NodeLeader.Instance()
        node.ADDRS = ["127.0.0.1:20333", "127.0.0.2:20334"]
        test_node = NeoNode()
        test_node.host = "127.0.0.1"
        test_node.port = 20333
        node.Peers.append(test_node)

        req = self._gen_rpc_req("getpeers", params=[])
        mock_req = mock_request(json.dumps(req).encode("utf-8"))
        res = json.loads(self.app.home(mock_req))

        self.assertEqual(len(node.Peers), len(res['result']['connected']))
        self.assertEqual(len(res['result']['unconnected']),
                         len(node.ADDRS) - len(node.Peers))
        # To avoid messing up the next tests
        node.Peers = []
        node.ADDRS = []
예제 #5
0
    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()
예제 #6
0
 def mock_connect_tcp(host, port, factory, timeout=120):
     node = NeoNode()
     node.endpoint = Endpoint(host, port)
     leader.AddConnectedPeer(node)
     return node
예제 #7
0
    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, {})
예제 #8
0
 def buildProtocol(self, addr):
     return NeoNode(self.incoming)
예제 #9
0
 def SetupConnection(self, host, port):
     self.__log.debug("Setting up connection! %s %s " % (host, port))
     point = TCP4ClientEndpoint(reactor, host, int(port))
     d = connectProtocol(point, NeoNode(NeoFactory))
     d.addCallbacks(self.onProtocolConnected, errback=self.onProtocolError)
     reactor.callLater(5, d.cancel)
예제 #10
0
 def buildProtocol(self, addr):
     return NeoNode(self)