public async Task AddNodeAsync(NodeInfo nodeInfo)
 {
     if (await _nodeManager.AddNodeAsync(nodeInfo))
     {
         _discoveredNodeCacheProvider.Add(nodeInfo.Endpoint);
     }
 }
        private async Task ProcessNodeAsync(NodeInfo node)
        {
            try
            {
                if (!await ValidateNodeAsync(node))
                {
                    return;
                }

                if (await _nodeManager.AddNodeAsync(node))
                {
                    _discoveredNodeCacheProvider.Add(node.Endpoint);
                    Logger.LogDebug($"Discover and add node: {node.Endpoint} successfully.");
                }
                else
                {
                    var endpointLocal = await TakeEndpointFromDiscoveredNodeCacheAsync();

                    if (endpointLocal.IsNullOrWhiteSpace())
                    {
                        return;
                    }

                    if (await _networkServer.CheckEndpointAvailableAsync(endpointLocal))
                    {
                        _discoveredNodeCacheProvider.Add(endpointLocal);
                        Logger.LogDebug($"Only refresh node: {endpointLocal}.");
                    }
                    else
                    {
                        await _nodeManager.RemoveNodeAsync(endpointLocal);

                        if (await _nodeManager.AddNodeAsync(node))
                        {
                            _discoveredNodeCacheProvider.Add(node.Endpoint);
                        }

                        Logger.LogDebug(
                            $"Remove unavailable node: {endpointLocal}, and add node: {node.Endpoint} successfully.");
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogWarning(e, "Process node failed.");
            }
        }
Beispiel #3
0
        public async Task AddNode_Test()
        {
            var node   = GenerateTestNodes(1).First();
            var result = await _nodeManager.AddNodeAsync(node);

            result.ShouldBeTrue();

            var data = node.ToDiagnosticString();

            data.ShouldContain("endpoint");
            data.ShouldContain(node.Pubkey.ToHex().Substring(0, 45));

            //add duplicate one
            var result1 = await _nodeManager.AddNodeAsync(node);

            result1.ShouldBeFalse();
        }
Beispiel #4
0
        public async Task SendDiscoveryJob_NodeExist_Test()
        {
            var oldNode = new NodeInfo
            {
                Endpoint = "192.168.100.100:8003",
                Pubkey   = ByteString.CopyFromUtf8("OldPubkey")
            };
            await _nodeManager.AddNodeAsync(oldNode);

            _discoveredNodeCacheProvider.Add(oldNode.Endpoint);

            var peer = _peerPool.FindPeerByPublicKey("PeerWithNormalNode");

            await SendDiscoveryJobAsync(peer);

            var nodes = await _nodeManager.GetRandomNodesAsync(10);

            nodes.Nodes.Count.ShouldBe(1);
            nodes.Nodes[0].Endpoint.ShouldBe("192.168.100.100:8003");
            nodes.Nodes[0].Pubkey.ShouldBe(ByteString.CopyFromUtf8("192.168.100.100:8003"));
        }
Beispiel #5
0
        public async Task AddNode_Test()
        {
            var node      = GenerateTestNode(100);
            var addResult = await _nodeManager.AddNodeAsync(node);

            addResult.ShouldBeTrue();

            addResult = await _nodeManager.AddNodeAsync(node);

            addResult.ShouldBeFalse();

            for (int i = 0; i < _networkOptions.PeerDiscoveryMaxNodesToKeep - 1; i++)
            {
                node      = GenerateTestNode(i + 1);
                addResult = await _nodeManager.AddNodeAsync(node);

                addResult.ShouldBeTrue();
            }

            node      = GenerateTestNode(101);
            addResult = await _nodeManager.AddNodeAsync(node);

            addResult.ShouldBeFalse();
        }
Beispiel #6
0
        public async Task GetNodes_Test()
        {
            var context = BuildServerCallContext();
            var result  = await _service.GetNodes(null, context);

            result.ShouldBe(new NodeList());

            var node = new NodeInfo
            {
                Endpoint = "127.0.0.1:2001",
                Pubkey   = ByteString.CopyFromUtf8("pubkey1")
            };
            await _nodeManager.AddNodeAsync(node);

            var request = new NodesRequest
            {
                MaxCount = 1
            };

            result = await _service.GetNodes(request, context);

            result.Nodes.Count.ShouldBe(1);
            result.Nodes[0].ShouldBe(node);
        }
 public async Task AddNodeAsync(NodeInfo nodeInfo)
 {
     await _nodeManager.AddNodeAsync(nodeInfo);
 }