示例#1
0
        private void InitEthProtocol(ISession session, Eth62ProtocolHandler handler)
        {
            handler.ProtocolInitialized += (sender, args) =>
            {
                if (!RunBasicChecks(session, handler.ProtocolCode, handler.ProtocolVersion))
                {
                    return;
                }
                var typedArgs = (EthProtocolInitializedEventArgs)args;
                _stats.ReportEthInitializeEvent(session.Node, new EthNodeDetails
                {
                    ChainId         = typedArgs.ChainId,
                    BestHash        = typedArgs.BestHash,
                    GenesisHash     = typedArgs.GenesisHash,
                    ProtocolVersion = typedArgs.ProtocolVersion,
                    TotalDifficulty = typedArgs.TotalDifficulty
                });

                bool isValid = _protocolValidator.DisconnectOnInvalid(Protocol.Eth, session, args);
                if (isValid)
                {
                    handler.ClientId = session.Node.ClientId;

                    if (_syncPeers.TryAdd(session.SessionId, handler))
                    {
                        _syncPool.AddPeer(handler);
                        _txPool.AddPeer(handler);
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"{handler.ClientId} sync peer {session} created.");
                        }
                    }
                    else
                    {
                        if (_logger.IsTrace)
                        {
                            _logger.Trace($"Not able to add a sync peer on {session} for {session.Node:s}");
                        }
                        session.InitiateDisconnect(DisconnectReason.AlreadyConnected, "sync peer");
                    }

                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Finalized ETH protocol initialization on {session} - adding sync peer {session.Node:s}");
                    }

                    //Add/Update peer to the storage and to sync manager
                    _peerStorage.UpdateNodes(new[] { new NetworkNode(session.Node.Id, session.Node.Host, session.Node.Port, _stats.GetOrAdd(session.Node).NewPersistedNodeReputation) });
                }
                else
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"|NetworkTrace| {handler.ProtocolCode}{handler.ProtocolVersion} is invalid on {session}");
                    }
                }
            };
        }
        private NodeDataDownloader PrepareDownloader(ISyncPeer syncPeer)
        {
            DbContext dbContext = new DbContext(_logger);
            BlockTree blockTree = Build.A.BlockTree().OfChainLength((int)BlockTree.BestSuggestedHeader.Number).TestObject;

            _pool = new EthSyncPeerPool(blockTree, new NodeStatsManager(new StatsConfig(), LimboLogs.Instance), 25, LimboLogs.Instance);
            _pool.Start();
            _pool.AddPeer(syncPeer);

            NodeDataFeed       feed       = new NodeDataFeed(dbContext.LocalCodeDb, dbContext.LocalStateDb, _logManager);
            NodeDataDownloader downloader = new NodeDataDownloader(_pool, feed, NullDataConsumer.Instance, _logManager);

            return(downloader);
        }
 public void Cannot_add_when_not_started()
 {
     for (int i = 0; i < 3; i++)
     {
         Assert.AreEqual(0, _pool.PeerCount);
         _pool.AddPeer(new SimpleSyncPeerMock(TestItem.PublicKeys[i]));
     }
 }
        public void Retrieves_missing_blocks_in_batches()
        {
            _remoteBlockTree = Build.A.BlockTree(_genesisBlock).OfChainLength(SyncBatchSize.Max * 2).TestObject;
            ISyncPeer peer = new SyncPeerMock(_remoteBlockTree);

            ManualResetEvent resetEvent = new ManualResetEvent(false);

            _synchronizer.SyncEvent += (sender, args) =>
            {
                if (args.SyncEvent == SyncEvent.Completed || args.SyncEvent == SyncEvent.Failed)
                {
                    resetEvent.Set();
                }
            };
            _pool.Start();
            _synchronizer.Start();
            _pool.AddPeer(peer);

            resetEvent.WaitOne(_standardTimeoutUnit);
            Assert.AreEqual(SyncBatchSize.Max * 2 - 1, (int)_blockTree.BestSuggested.Number);
        }