Пример #1
0
        public async Task <NodeList> DiscoverNodesAsync()
        {
            Random rnd = new Random();

            var peers = _peerPool.GetPeers()
                        .OrderBy(x => rnd.Next())
                        .Take(NetworkConstants.DefaultDiscoveryPeersToRequestCount)
                        .ToList();

            var discoveredNodes = new NodeList();

            foreach (var peer in peers)
            {
                try
                {
                    var nodes = await peer.GetNodesAsync();

                    if (nodes != null && nodes.Nodes.Count > 0)
                    {
                        Logger.LogDebug($"Discovery: {peer} responded with the following nodes: {nodes}.");

                        var added = await _nodeManager.AddOrUpdateNodesAsync(nodes);

                        if (added != null)
                        {
                            discoveredNodes.Nodes.AddRange(added.Nodes);
                        }
                    }
                    else
                    {
                        Logger.LogDebug($"Discovery: {peer} responded with no nodes.");
                    }
                }
                catch (NetworkException ex)
                {
                    Logger.LogError(ex, $"Error during discover - {peer}.");
                }
            }

            if (discoveredNodes.Nodes.Count <= 0)
            {
                return(discoveredNodes);
            }

            // Check that a peer did not send us this node
            var localPubKey = await _accountService.GetPublicKeyAsync();

            string hexPubkey = localPubKey.ToHex();

            discoveredNodes.Nodes.RemoveAll(n => n.Pubkey.ToHex().Equals(hexPubkey));

            return(discoveredNodes);
        }
Пример #2
0
        private async Task AddNodes_Test()
        {
            var nodes = new NodeList
            {
                Nodes = { GenerateTestNodes(3).ToArray() }
            };
            var result = await _nodeManager.AddOrUpdateNodesAsync(nodes);

            result.Nodes.Count.ShouldBe(3);

            var data = nodes.ToDiagnosticString();

            foreach (var node in nodes.Nodes)
            {
                data.ShouldContain(node.Endpoint);
            }
        }