Пример #1
0
        // TODO - Cancellation token?
        // TODO - not a fan of this function name - CatchUpCHT, AddMissingCHTBlocks, ...?
        public Task BuildCHT()
        {
            return(Task.Run(() =>
            {
                lock (_chtLock)
                {
                    if (_cht == null)
                    {
                        throw new InvalidAsynchronousStateException("CHT reference is null when building CHT.");
                    }

                    // Note: The spec says this should be 2048, but I don't think we'd ever want it to be higher than the max reorg depth we allow.
                    long maxSection = CanonicalHashTrie.GetSectionFromBlockNo(_blockTree.FindLatestHeader().Number - Sync.MaxReorgLength);
                    long maxKnownSection = _cht.GetMaxSectionIndex();

                    for (long section = (maxKnownSection + 1); section <= maxSection; section++)
                    {
                        long sectionStart = section * CanonicalHashTrie.SectionSize;
                        for (int blockOffset = 0; blockOffset < CanonicalHashTrie.SectionSize; blockOffset++)
                        {
                            _cht.Set(_blockTree.FindHeader(sectionStart + blockOffset));
                        }

                        _cht.Commit(section);
                    }
                }
            }));
        }
Пример #2
0
        public SyncServer(
            ISnapshotableDb stateDb,
            ISnapshotableDb codeDb,
            IBlockTree blockTree,
            IReceiptFinder receiptFinder,
            IBlockValidator blockValidator,
            ISealValidator sealValidator,
            ISyncPeerPool pool,
            ISyncModeSelector syncModeSelector,
            ISyncConfig syncConfig,
            ILogManager logManager,
            CanonicalHashTrie cht = null)
        {
            _syncConfig       = syncConfig ?? throw new ArgumentNullException(nameof(syncConfig));
            _pool             = pool ?? throw new ArgumentNullException(nameof(pool));
            _syncModeSelector = syncModeSelector ?? throw new ArgumentNullException(nameof(syncModeSelector));
            _sealValidator    = sealValidator ?? throw new ArgumentNullException(nameof(sealValidator));
            _stateDb          = stateDb ?? throw new ArgumentNullException(nameof(stateDb));
            _codeDb           = codeDb ?? throw new ArgumentNullException(nameof(codeDb));
            _blockTree        = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
            _receiptFinder    = receiptFinder ?? throw new ArgumentNullException(nameof(receiptFinder));
            _blockValidator   = blockValidator ?? throw new ArgumentNullException(nameof(blockValidator));
            _logger           = logManager.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
            _cht         = cht;
            _pivotNumber = _syncConfig.PivotNumberParsed;

            _blockTree.NewHeadBlock += OnNewHeadBlock;
            _pivotHash = new Keccak(_syncConfig.PivotHash ?? Keccak.Zero.ToString());
        }
Пример #3
0
        private async Task Initialize(CancellationToken cancellationToken)
        {
            if (_ctx.DbProvider == null)
            {
                throw new StepDependencyException(nameof(_ctx.DbProvider));
            }
            if (_ctx.BlockTree == null)
            {
                throw new StepDependencyException(nameof(_ctx.BlockTree));
            }
            if (_ctx.ReceiptStorage == null)
            {
                throw new StepDependencyException(nameof(_ctx.ReceiptStorage));
            }
            if (_ctx.BlockValidator == null)
            {
                throw new StepDependencyException(nameof(_ctx.BlockValidator));
            }
            if (_ctx.SealValidator == null)
            {
                throw new StepDependencyException(nameof(_ctx.SealValidator));
            }
            if (_ctx.Enode == null)
            {
                throw new StepDependencyException(nameof(_ctx.Enode));
            }

            if (_networkConfig.DiagTracerEnabled)
            {
                NetworkDiagTracer.IsEnabled = true;
                NetworkDiagTracer.Start();
            }

            ThisNodeInfo.AddInfo("Mem est netty:", $"{NettyMemoryEstimator.Estimate((uint)Environment.ProcessorCount, _networkConfig.NettyArenaOrder) / 1000 / 1000}MB".PadLeft(8));
            ThisNodeInfo.AddInfo("Mem est peers:", $"{_networkConfig.ActivePeersMaxCount}MB".PadLeft(8));
            Environment.SetEnvironmentVariable("io.netty.allocator.maxOrder", _networkConfig.NettyArenaOrder.ToString());

            var cht = new CanonicalHashTrie(_ctx.DbProvider.ChtDb);

            int maxPeersCount = _networkConfig.ActivePeersMaxCount;

            _ctx.SyncPeerPool = new SyncPeerPool(_ctx.BlockTree, _ctx.NodeStatsManager, maxPeersCount, _ctx.LogManager);
            _ctx.DisposeStack.Push(_ctx.SyncPeerPool);

            SyncProgressResolver  syncProgressResolver = new SyncProgressResolver(_ctx.BlockTree, _ctx.ReceiptStorage, _ctx.DbProvider.StateDb, _ctx.DbProvider.BeamStateDb, _syncConfig, _ctx.LogManager);
            MultiSyncModeSelector syncModeSelector     = new MultiSyncModeSelector(syncProgressResolver, _ctx.SyncPeerPool, _syncConfig, _ctx.LogManager);

            if (_ctx.SyncModeSelector != null)
            {
                // this is really bad and is a result of lack of proper dependency management
                PendingSyncModeSelector pendingOne = (PendingSyncModeSelector)_ctx.SyncModeSelector;
                pendingOne.SetActual(syncModeSelector);
            }

            _ctx.SyncModeSelector = syncModeSelector;
            _ctx.DisposeStack.Push(syncModeSelector);

            _ctx.Synchronizer = new Synchronizer(
                _ctx.DbProvider,
                _ctx.SpecProvider,
                _ctx.BlockTree,
                _ctx.ReceiptStorage,
                _ctx.BlockValidator,
                _ctx.SealValidator,
                _ctx.SyncPeerPool,
                _ctx.NodeStatsManager,
                _ctx.SyncModeSelector,
                _syncConfig,
                _ctx.LogManager);
            _ctx.DisposeStack.Push(_ctx.Synchronizer);

            _ctx.SyncServer = new SyncServer(
                _ctx.DbProvider.StateDb,
                _ctx.DbProvider.CodeDb,
                _ctx.BlockTree,
                _ctx.ReceiptStorage,
                _ctx.BlockValidator,
                _ctx.SealValidator,
                _ctx.SyncPeerPool,
                _ctx.SyncModeSelector,
                _ctx.Config <ISyncConfig>(),
                _ctx.LogManager,
                cht);

            _ = _ctx.SyncServer.BuildCHT();

            _ctx.DisposeStack.Push(_ctx.SyncServer);

            InitDiscovery();
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            await InitPeer().ContinueWith(initPeerTask =>
            {
                if (initPeerTask.IsFaulted)
                {
                    _logger.Error("Unable to init the peer manager.", initPeerTask.Exception);
                }
            });

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            await StartSync().ContinueWith(initNetTask =>
            {
                if (initNetTask.IsFaulted)
                {
                    _logger.Error("Unable to start the synchronizer.", initNetTask.Exception);
                }
            });

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            await StartDiscovery().ContinueWith(initDiscoveryTask =>
            {
                if (initDiscoveryTask.IsFaulted)
                {
                    _logger.Error("Unable to start the discovery protocol.", initDiscoveryTask.Exception);
                }
            });

            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                StartPeer();
            }
            catch (Exception e)
            {
                _logger.Error("Unable to start the peer manager.", e);
            }

            ThisNodeInfo.AddInfo("Ethereum     :", $"tcp://{_ctx.Enode.HostIp}:{_ctx.Enode.Port}");
            ThisNodeInfo.AddInfo("Version      :", $"{ClientVersion.Description.Replace("Nethermind/v", string.Empty)}");
            ThisNodeInfo.AddInfo("This node    :", $"{_ctx.Enode.Info}");
            ThisNodeInfo.AddInfo("Node address :", $"{_ctx.Enode.Address} (do not use as an account)");
        }