Esempio n. 1
0
        private void InitializeUdpChannel()
        {
            if (_logger.IsDebug)
            {
                _logger.Debug($"Discovery    : udp://{_networkConfig.LocalIp}:{_networkConfig.DiscoveryPort}");
            }
            ThisNodeInfo.AddInfo("Discovery    :", $"udp://{_networkConfig.LocalIp}:{_networkConfig.DiscoveryPort}");
            _group = new MultithreadEventLoopGroup(1);
            Bootstrap bootstrap = new Bootstrap();

            bootstrap
            .Group(_group);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                bootstrap
                .ChannelFactory(() => new SocketDatagramChannel(AddressFamily.InterNetwork))
                .Handler(new ActionChannelInitializer <IDatagramChannel>(InitializeChannel));
            }
            else
            {
                bootstrap
                .Channel <SocketDatagramChannel>()
                .Handler(new ActionChannelInitializer <IDatagramChannel>(InitializeChannel));
            }

            _bindingTask = bootstrap.BindAsync(IPAddress.Parse(_networkConfig.LocalIp), _networkConfig.DiscoveryPort)
                           .ContinueWith(t => _channel = t.Result);
        }
Esempio n. 2
0
        private ChainSpec LoadChainSpec(IJsonSerializer ethereumJsonSerializer)
        {
            bool hiveEnabled         = Environment.GetEnvironmentVariable("NETHERMIND_HIVE_ENABLED")?.ToLowerInvariant() == "true";
            bool hiveChainSpecExists = File.Exists(_initConfig.HiveChainSpecPath);

            string chainSpecFile;

            if (hiveEnabled && hiveChainSpecExists)
            {
                chainSpecFile = _initConfig.HiveChainSpecPath;
            }
            else
            {
                chainSpecFile = _initConfig.ChainSpecPath;
            }

            if (_logger.IsDebug)
            {
                _logger.Debug($"Loading chain spec from {chainSpecFile}");
            }

            ThisNodeInfo.AddInfo("Chainspec    :", $"{chainSpecFile}");

            IChainSpecLoader loader = new ChainSpecLoader(ethereumJsonSerializer);

            return(loader.LoadFromFile(chainSpecFile));
        }
Esempio n. 3
0
        private async Task <RocksDbProvider> GetRocksDbProvider(IDbConfig dbConfig, string basePath, bool useReceiptsDb)
        {
            RocksDbProvider debugRecorder = new RocksDbProvider(_context.LogManager, _context.Config <INdmConfig>().Enabled);

            ThisNodeInfo.AddInfo("DB location  :", $"{basePath}");
            await debugRecorder.Init(basePath, dbConfig, useReceiptsDb);

            return(debugRecorder);
        }
Esempio n. 4
0
        public BlockTree(
            IDb blockDb,
            IDb headerDb,
            IDb blockInfoDb,
            IChainLevelInfoRepository chainLevelInfoRepository,
            ISpecProvider specProvider,
            ITxPool txPool,
            IBloomStorage bloomStorage,
            ISyncConfig syncConfig,
            ILogManager logManager)
        {
            _logger                   = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
            _blockDb                  = blockDb ?? throw new ArgumentNullException(nameof(blockDb));
            _headerDb                 = headerDb ?? throw new ArgumentNullException(nameof(headerDb));
            _blockInfoDb              = blockInfoDb ?? throw new ArgumentNullException(nameof(blockInfoDb));
            _specProvider             = specProvider;
            _txPool                   = txPool ?? throw new ArgumentNullException(nameof(txPool));
            _bloomStorage             = bloomStorage ?? throw new ArgumentNullException(nameof(txPool));
            _syncConfig               = syncConfig ?? throw new ArgumentNullException(nameof(syncConfig));
            _chainLevelInfoRepository = chainLevelInfoRepository ?? throw new ArgumentNullException(nameof(chainLevelInfoRepository));

            var deletePointer = _blockInfoDb.Get(DeletePointerAddressInDb);

            if (deletePointer != null)
            {
                DeleteBlocks(new Keccak(deletePointer));
            }

            ChainLevelInfo genesisLevel = LoadLevel(0, true);

            if (genesisLevel != null)
            {
                if (genesisLevel.BlockInfos.Length != 1)
                {
                    // just for corrupted test bases
                    genesisLevel.BlockInfos = new[] { genesisLevel.BlockInfos[0] };
                    _chainLevelInfoRepository.PersistLevel(0, genesisLevel);
                    //throw new InvalidOperationException($"Genesis level in DB has {genesisLevel.BlockInfos.Length} blocks");
                }

                if (genesisLevel.BlockInfos[0].WasProcessed)
                {
                    BlockHeader genesisHeader = FindHeader(genesisLevel.BlockInfos[0].BlockHash, BlockTreeLookupOptions.None);
                    Genesis = genesisHeader;
                    LoadHeadBlockAtStart();
                }

                RecalculateTreeLevels();
            }

            if (_logger.IsInfo)
            {
                _logger.Info($"Block tree initialized, last processed is {Head?.Header?.ToString(BlockHeader.Format.Short) ?? "0"}, best queued is {BestSuggestedHeader?.Number.ToString() ?? "0"}, best known is {BestKnownNumber}, lowest inserted header {LowestInsertedHeader?.Number}, body {LowestInsertedBody?.Number}");
            }
            ThisNodeInfo.AddInfo("Chain ID     :", $"{Nethermind.Core.ChainId.GetChainName(ChainId)}");
            ThisNodeInfo.AddInfo("Chain head   :", $"{Head?.Header?.ToString(BlockHeader.Format.Short) ?? "0"}");
        }
Esempio n. 5
0
        private async Task <RocksDbProvider> GetRocksDbProvider(IDbConfig dbConfig, string basePath, bool useReceiptsDb)
        {
            // bool addNdmDbs = _api.Config<INdmConfig>().Enabled;
            IInitConfig     initConfig    = _api.Config <IInitConfig>();
            RocksDbProvider debugRecorder = new RocksDbProvider(_api.LogManager, false, false);

            ThisNodeInfo.AddInfo("DB location  :", $"{basePath}");
            await debugRecorder.Init(basePath, dbConfig, useReceiptsDb);

            return(debugRecorder);
        }
Esempio n. 6
0
        public Task Start(CancellationToken cancellationToken)
        {
            if (_logger.IsDebug)
            {
                _logger.Debug("Initializing JSON RPC");
            }
            string[] urls    = _jsonRpcUrlCollection.Urls;
            var      webHost = WebHost.CreateDefaultBuilder()
                               .ConfigureServices(s =>
            {
                s.AddSingleton(_configurationProvider);
                s.AddSingleton(_jsonRpcProcessor);
                s.AddSingleton(_jsonRpcUrlCollection);
                s.AddSingleton(_webSocketsManager);
                s.AddSingleton(_rpcAuthentication);
                foreach (var plugin in _api.Plugins.OfType <INethermindServicesPlugin>())
                {
                    plugin.AddServices(s);
                }
                ;
            })
                               .UseStartup <Startup>()
                               .UseUrls(urls)
                               .ConfigureLogging(logging =>
            {
                logging.SetMinimumLevel(LogLevel.Information);
                logging.ClearProviders();
                logging.AddProvider(new CustomMicrosoftLoggerProvider(_logManager));
            })
                               .Build();

            string urlsString = string.Join(" ; ", urls);

            // TODO: replace http with ws where relevant

            ThisNodeInfo.AddInfo("JSON RPC     :", $"{urlsString}");

            _webHost = webHost;

            if (!cancellationToken.IsCancellationRequested)
            {
                _webHost.Start();
                if (_logger.IsDebug)
                {
                    _logger.Debug($"JSON RPC     : {urlsString}");
                }
            }

            return(Task.CompletedTask);
        }
Esempio n. 7
0
        public async Task Start(CancellationToken cancellationToken)
        {
            if (_logger.IsDebug)
            {
                _logger.Debug("Initializing Ethereum");
            }

            EthereumStepsLoader  stepsLoader  = new EthereumStepsLoader(GetType().Assembly);
            EthereumStepsManager stepsManager = new EthereumStepsManager(stepsLoader, _api, _api.LogManager);
            await stepsManager.InitializeAll(cancellationToken);

            string infoScreen = ThisNodeInfo.BuildNodeInfoScreen();

            if (_logger.IsInfo)
            {
                _logger.Info(infoScreen);
            }
        }
Esempio n. 8
0
        private IPAddress InitializeExternalIp()
        {
            string externalIpSetInEnv = Environment.GetEnvironmentVariable("NETHERMIND_ENODE_IPADDRESS");

            if (externalIpSetInEnv != null)
            {
                return(IPAddress.Parse(externalIpSetInEnv));
            }

            if (_networkConfig.ExternalIp != null)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn($"Using the external IP override: {nameof(NetworkConfig)}.{nameof(NetworkConfig.ExternalIp)} = {_networkConfig.ExternalIp}");
                }
                return(IPAddress.Parse(_networkConfig.ExternalIp));
            }

            try
            {
                const string url = "http://checkip.amazonaws.com";
                if (_logger.IsInfo)
                {
                    _logger.Info($"Using {url} to get external ip");
                }
                string ip = new WebClient().DownloadString(url).Trim();
                if (_logger.IsDebug)
                {
                    _logger.Debug($"External ip: {ip}");
                }
                ThisNodeInfo.AddInfo("External IP  :", $"{ip}");

                return(IPAddress.Parse(ip));
            }
            catch (Exception e)
            {
                if (_logger.IsError)
                {
                    _logger.Error("Error while getting external ip", e);
                }
                return(IPAddress.Loopback);
            }
        }
Esempio n. 9
0
        public async Task Start(CancellationToken cancellationToken)
        {
            if (_logger.IsDebug)
            {
                _logger.Debug("Initializing Ethereum");
            }

            EthereumStepsManager stepsManager = new EthereumStepsManager(_context);
            await stepsManager.DiscoverAll(cancellationToken);

            await stepsManager.InitializeAll(cancellationToken);

            string infoScreen = ThisNodeInfo.BuildNodeInfoScreen();

            if (_logger.IsInfo)
            {
                _logger.Info(infoScreen);
            }
        }
Esempio n. 10
0
        private async Task <IPAddress> InitializeExternalIp()
        {
            IEnumerable <IIPSource> GetIPSources()
            {
                yield return(new EnvironmentVariableIPSource());

                yield return(new NetworkConfigExternalIPSource(_networkConfig, _logManager));

                yield return(new WebIPSource("http://ipv4.icanhazip.com", _logManager));

                yield return(new WebIPSource("http://ipv4bot.whatismyipaddress.com", _logManager));

                yield return(new WebIPSource("http://checkip.amazonaws.com", _logManager));

                yield return(new WebIPSource("http://ipinfo.io/ip", _logManager));

                yield return(new WebIPSource("http://api.ipify.org", _logManager));
            }

            try
            {
                foreach (IIPSource s in GetIPSources())
                {
                    (bool success, IPAddress ip) = await s.TryGetIP();

                    if (success)
                    {
                        ThisNodeInfo.AddInfo("External IP  :", $"{ip}");
                        return(ip);
                    }
                }
            }
            catch (Exception e)
            {
                if (_logger.IsError)
                {
                    _logger.Error("Error while getting external ip", e);
                }
            }

            return(IPAddress.Loopback);
        }
Esempio n. 11
0
        public async Task Start()
        {
            if (_logger.IsDebug)
            {
                _logger.Debug("Initializing Ethereum");
            }
            _context.RunnerCancellation = new CancellationTokenSource();
            _context.DisposeStack.Push(_context.RunnerCancellation);

            EthereumStepsManager stepsManager = new EthereumStepsManager(_context);

            stepsManager.DiscoverAll();
            await stepsManager.InitializeAll();

            string infoScreen = ThisNodeInfo.BuildNodeInfoScreen();

            if (_logger.IsInfo)
            {
                _logger.Info(infoScreen);
            }
        }
        public EthereumRunnerContextFactory(IConfigProvider configProvider, IJsonSerializer ethereumJsonSerializer, ILogManager logManager)
        {
            _configProvider = configProvider;
            _logManager     = logManager;

            IInitConfig initConfig = configProvider.GetConfig <IInitConfig>();
            ILogger     logger     = _logManager.GetClassLogger();

            bool hiveEnabled         = Environment.GetEnvironmentVariable("NETHERMIND_HIVE_ENABLED")?.ToLowerInvariant() == "true";
            bool hiveChainSpecExists = File.Exists(initConfig.HiveChainSpecPath);

            string chainSpecFile;

            if (hiveEnabled && hiveChainSpecExists)
            {
                chainSpecFile = initConfig.HiveChainSpecPath;
            }
            else
            {
                chainSpecFile = initConfig.ChainSpecPath;
            }

            if (logger.IsDebug)
            {
                logger.Debug($"Loading chain spec from {chainSpecFile}");
            }

            ThisNodeInfo.AddInfo("Chainspec    :", $"{chainSpecFile}");
            IChainSpecLoader loader = new ChainSpecLoader(ethereumJsonSerializer);

            ChainSpec chainSpec = loader.LoadFromFile(chainSpecFile);

            logManager.SetGlobalVariable("chain", chainSpec.Name);
            logManager.SetGlobalVariable("chainId", chainSpec.ChainId);
            logManager.SetGlobalVariable("engine", chainSpec.SealEngineType);

            Context              = Create(chainSpec.SealEngineType);
            Context.ChainSpec    = chainSpec;
            Context.SpecProvider = new ChainSpecBasedSpecProvider(Context.ChainSpec);
        }
Esempio n. 13
0
        /// <summary>
        /// If <paramref name="expectedGenesisHash"/> is <value>null</value> then it means that we do not care about the genesis hash (e.g. in some quick testing of private chains)/>
        /// </summary>
        /// <param name="expectedGenesisHash"></param>
        private void ValidateGenesisHash(Keccak?expectedGenesisHash)
        {
            if (_api.StateProvider == null)
            {
                throw new StepDependencyException(nameof(_api.StateProvider));
            }
            if (_api.BlockTree == null)
            {
                throw new StepDependencyException(nameof(_api.BlockTree));
            }

            BlockHeader genesis = _api.BlockTree.Genesis !;

            if (expectedGenesisHash != null && genesis.Hash != expectedGenesisHash)
            {
                if (_logger.IsWarn)
                {
                    _logger.Warn(_api.StateProvider.DumpState());
                }
                if (_logger.IsWarn)
                {
                    _logger.Warn(genesis.ToString(BlockHeader.Format.Full));
                }
                if (_logger.IsError)
                {
                    _logger.Error($"Unexpected genesis hash, expected {expectedGenesisHash}, but was {genesis.Hash}");
                }
            }
            else
            {
                if (_logger.IsDebug)
                {
                    _logger.Info($"Genesis hash :  {genesis.Hash}");
                }
            }

            ThisNodeInfo.AddInfo("Genesis hash :", $"{genesis.Hash}");
        }
Esempio n. 14
0
        public EthereumRunnerContextFactory(IConfigProvider configProvider, IJsonSerializer ethereumJsonSerializer, ILogManager logManager)
        {
            _configProvider = configProvider;
            _logManager     = logManager;

            IInitConfig initConfig = configProvider.GetConfig <IInitConfig>();
            ILogger     logger     = _logManager.GetClassLogger();

            if (logger.IsDebug)
            {
                logger.Debug($"Loading chain spec from {initConfig.ChainSpecPath}");
            }
            ThisNodeInfo.AddInfo("Chainspec    :", $"{initConfig.ChainSpecPath}");
            IChainSpecLoader loader    = new ChainSpecLoader(ethereumJsonSerializer);
            ChainSpec        chainSpec = loader.LoadFromFile(initConfig.ChainSpecPath);

            logManager.SetGlobalVariable("chain", chainSpec.Name);
            logManager.SetGlobalVariable("chainId", chainSpec.ChainId);
            logManager.SetGlobalVariable("engine", chainSpec.SealEngineType);

            Context              = Create(chainSpec.SealEngineType);
            Context.ChainSpec    = chainSpec;
            Context.SpecProvider = new ChainSpecBasedSpecProvider(Context.ChainSpec);
        }
Esempio n. 15
0
        protected virtual DbOptions BuildOptions(IDbConfig dbConfig)
        {
            _maxThisDbSize = 0;
            BlockBasedTableOptions tableOptions = new BlockBasedTableOptions();

            tableOptions.SetBlockSize(16 * 1024);
            tableOptions.SetPinL0FilterAndIndexBlocksInCache(true);
            tableOptions.SetCacheIndexAndFilterBlocks(ReadConfig <bool>(dbConfig, nameof(dbConfig.CacheIndexAndFilterBlocks)));

            tableOptions.SetFilterPolicy(BloomFilterPolicy.Create(10, true));
            tableOptions.SetFormatVersion(2);

            ulong blockCacheSize = ReadConfig <ulong>(dbConfig, nameof(dbConfig.BlockCacheSize));

            _maxThisDbSize += (long)blockCacheSize;

            IntPtr cache = Native.Instance.rocksdb_cache_create_lru(new UIntPtr(blockCacheSize));

            tableOptions.SetBlockCache(cache);

            DbOptions options = new DbOptions();

            options.SetCreateIfMissing(true);
            options.SetAdviseRandomOnOpen(true);
            options.OptimizeForPointLookup(blockCacheSize); // I guess this should be the one option controlled by the DB size property - bind it to LRU cache size
            //options.SetCompression(CompressionTypeEnum.rocksdb_snappy_compression);
            //options.SetLevelCompactionDynamicLevelBytes(true);

            /*
             * Multi-Threaded Compactions
             * Compactions are needed to remove multiple copies of the same key that may occur if an application overwrites an existing key. Compactions also process deletions of keys. Compactions may occur in multiple threads if configured appropriately.
             * The entire database is stored in a set of sstfiles. When a memtable is full, its content is written out to a file in Level-0 (L0). RocksDB removes duplicate and overwritten keys in the memtable when it is flushed to a file in L0. Some files are periodically read in and merged to form larger files - this is called compaction.
             * The overall write throughput of an LSM database directly depends on the speed at which compactions can occur, especially when the data is stored in fast storage like SSD or RAM. RocksDB may be configured to issue concurrent compaction requests from multiple threads. It is observed that sustained write rates may increase by as much as a factor of 10 with multi-threaded compaction when the database is on SSDs, as compared to single-threaded compactions.
             * TKS: Observed 500MB/s compared to ~100MB/s between multithreaded and single thread compactions on my machine (processor count is returning 12 for 6 cores with hyperthreading)
             * TKS: CPU goes to insane 30% usage on idle - compacting only app
             */
            options.SetMaxBackgroundCompactions(Environment.ProcessorCount);

            //options.SetMaxOpenFiles(32);
            ulong writeBufferSize = ReadConfig <ulong>(dbConfig, nameof(dbConfig.WriteBufferSize));

            options.SetWriteBufferSize(writeBufferSize);
            int writeBufferNumber = (int)ReadConfig <uint>(dbConfig, nameof(dbConfig.WriteBufferNumber));

            options.SetMaxWriteBufferNumber(writeBufferNumber);
            options.SetMinWriteBufferNumberToMerge(2);

            lock (DbsByPath)
            {
                _maxThisDbSize += (long)writeBufferSize * writeBufferNumber;
                Interlocked.Add(ref _maxRocksSize, _maxThisDbSize);
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Expected max memory footprint of {Name} DB is {_maxThisDbSize / 1024 / 1024}MB ({writeBufferNumber} * {writeBufferSize / 1024 / 1024}MB + {blockCacheSize / 1024 / 1024}MB)");
                }
                if (_logger.IsDebug)
                {
                    _logger.Debug($"Total max DB footprint so far is {_maxRocksSize / 1024 / 1024}MB");
                }
                ThisNodeInfo.AddInfo("DB mem est   :", $"{_maxRocksSize / 1024 / 1024}MB");
            }

            options.SetBlockBasedTableFactory(tableOptions);

            options.SetMaxBackgroundFlushes(Environment.ProcessorCount);
            options.IncreaseParallelism(Environment.ProcessorCount);
            options.SetRecycleLogFileNum(dbConfig.RecycleLogFileNum); // potential optimization for reusing allocated log files

//            options.SetLevelCompactionDynamicLevelBytes(true); // only switch on on empty DBs
            WriteOptions = new WriteOptions();
            WriteOptions.SetSync(dbConfig.WriteAheadLogSync); // potential fix for corruption on hard process termination, may cause performance degradation

            return(options);
        }
Esempio n. 16
0
        private async Task Initialize()
        {
            if (_context.DbProvider == null)
            {
                throw new StepDependencyException(nameof(_context.DbProvider));
            }
            if (_context.BlockTree == null)
            {
                throw new StepDependencyException(nameof(_context.BlockTree));
            }
            if (_context.ReceiptStorage == null)
            {
                throw new StepDependencyException(nameof(_context.ReceiptStorage));
            }
            if (_context.BlockValidator == null)
            {
                throw new StepDependencyException(nameof(_context.BlockValidator));
            }
            if (_context.SealValidator == null)
            {
                throw new StepDependencyException(nameof(_context.SealValidator));
            }
            if (_context.Enode == null)
            {
                throw new StepDependencyException(nameof(_context.Enode));
            }

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

            // Environment.SetEnvironmentVariable("io.netty.allocator.pageSize", "8192");
            Environment.SetEnvironmentVariable("io.netty.allocator.maxOrder", _networkConfig.NettyArenaOrder.ToString());

            int maxPeersCount = _networkConfig.ActivePeersMaxCount;

            _context.SyncPeerPool = new EthSyncPeerPool(_context.BlockTree, _context.NodeStatsManager, maxPeersCount, _context.LogManager);
            _context.DisposeStack.Push(_context.SyncPeerPool);
            NodeDataFeed       feed = new NodeDataFeed(_context.DbProvider.CodeDb, _context.DbProvider.StateDb, _context.LogManager);
            NodeDataDownloader nodeDataDownloader = new NodeDataDownloader(_context.SyncPeerPool, feed, _context.NodeDataConsumer, _context.LogManager);

            _context.Synchronizer = new Synchronizer(_context.SpecProvider, _context.BlockTree, _context.ReceiptStorage, _context.BlockValidator, _context.SealValidator, _context.SyncPeerPool, _context.Config <ISyncConfig>(), nodeDataDownloader, _context.NodeStatsManager, _context.LogManager);
            _context.DisposeStack.Push(_context.Synchronizer);

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

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

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

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

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

            ThisNodeInfo.AddInfo("Ethereum     :", $"tcp://{_context.Enode.HostIp}:{_context.Enode.Port}");
            ThisNodeInfo.AddInfo("Version      :", $"{ClientVersion.Description.Replace("Nethermind/v", string.Empty)}");
            ThisNodeInfo.AddInfo("This node    :", $"{_context.Enode.Info}");
            ThisNodeInfo.AddInfo("Node address :", $"{_context.Enode.Address} (do not use as an account)");
        }
Esempio n. 17
0
        private async Task Initialize(CancellationToken cancellationToken)
        {
            if (_networkConfig.DiagTracerEnabled)
            {
                NetworkDiagTracer.IsEnabled = true;
                NetworkDiagTracer.Start();
            }

            Environment.SetEnvironmentVariable("io.netty.allocator.maxOrder", _networkConfig.NettyArenaOrder.ToString());

            var cht = new CanonicalHashTrie(_api.DbProvider !.ChtDb);

            int maxPeersCount = _networkConfig.ActivePeersMaxCount;

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

            SyncProgressResolver  syncProgressResolver = new SyncProgressResolver(_api.BlockTree !, _api.ReceiptStorage !, _api.DbProvider.StateDb, _api.DbProvider.BeamStateDb, _syncConfig, _api.LogManager);
            MultiSyncModeSelector syncModeSelector     = CreateMultiSyncModeSelector(syncProgressResolver);

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

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

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

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

            _ = _api.SyncServer.BuildCHT();

            _api.DisposeStack.Push(_api.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);
            }

            if (_api.Enode == null)
            {
                throw new InvalidOperationException("Cannot initialize network without knowing own enode");
            }

            ThisNodeInfo.AddInfo("Ethereum     :", $"tcp://{_api.Enode.HostIp}:{_api.Enode.Port}");
            ThisNodeInfo.AddInfo("Version      :", $"{ClientVersion.Description.Replace("Nethermind/v", string.Empty)}");
            ThisNodeInfo.AddInfo("This node    :", $"{_api.Enode.Info}");
            ThisNodeInfo.AddInfo("Node address :", $"{_api.Enode.Address} (do not use as an account)");
        }
Esempio n. 18
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());

            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);
            _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)");
        }
Esempio n. 19
0
        private async Task Initialize(CancellationToken cancellationToken)
        {
            if (_api.DbProvider == null)
            {
                throw new StepDependencyException(nameof(_api.DbProvider));
            }

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

            if (NetworkDiagTracer.IsEnabled)
            {
                NetworkDiagTracer.Start(_api.LogManager);
            }

            Environment.SetEnvironmentVariable("io.netty.allocator.maxOrder", _networkConfig.NettyArenaOrder.ToString());
            CanonicalHashTrie cht = new CanonicalHashTrie(_api.DbProvider !.ChtDb);

            ProgressTracker progressTracker = new(_api.BlockTree !, _api.DbProvider.StateDb, _api.LogManager);

            _api.SnapProvider = new SnapProvider(progressTracker, _api.DbProvider, _api.LogManager);

            SyncProgressResolver syncProgressResolver = new(
                _api.BlockTree !,
                _api.ReceiptStorage !,
                _api.DbProvider.StateDb,
                _api.ReadOnlyTrieStore !,
                progressTracker,
                _syncConfig,
                _api.LogManager);

            _api.SyncProgressResolver = syncProgressResolver;
            _api.BetterPeerStrategy   = new TotalDifficultyBetterPeerStrategy(_api.LogManager);

            int          maxPeersCount         = _networkConfig.ActivePeersMaxCount;
            int          maxPriorityPeersCount = _networkConfig.PriorityPeersMaxCount;
            SyncPeerPool apiSyncPeerPool       = new(_api.BlockTree !, _api.NodeStatsManager !, _api.BetterPeerStrategy, maxPeersCount, maxPriorityPeersCount, SyncPeerPool.DefaultUpgradeIntervalInMs, _api.LogManager);

            _api.SyncPeerPool = apiSyncPeerPool;
            _api.PeerDifficultyRefreshPool = apiSyncPeerPool;
            _api.DisposeStack.Push(_api.SyncPeerPool);

            IEnumerable <ISynchronizationPlugin> synchronizationPlugins = _api.GetSynchronizationPlugins();

            foreach (ISynchronizationPlugin plugin in synchronizationPlugins)
            {
                await plugin.InitSynchronization();
            }

            _api.SyncModeSelector ??= CreateMultiSyncModeSelector(syncProgressResolver);
            _api.DisposeStack.Push(_api.SyncModeSelector);

            _api.Pivot ??= new Pivot(_syncConfig);

            if (_api.BlockDownloaderFactory is null || _api.Synchronizer is null)
            {
                SyncReport syncReport = new(_api.SyncPeerPool !, _api.NodeStatsManager !, _api.SyncModeSelector, _syncConfig, _api.Pivot, _api.LogManager);

                _api.BlockDownloaderFactory ??= new BlockDownloaderFactory(_api.SpecProvider !,
                                                                           _api.BlockTree !,
                                                                           _api.ReceiptStorage !,
                                                                           _api.BlockValidator !,
                                                                           _api.SealValidator !,
                                                                           _api.SyncPeerPool !,
                                                                           _api.BetterPeerStrategy !,
                                                                           syncReport,
                                                                           _api.LogManager);
                _api.Synchronizer ??= new Synchronizer(
                    _api.DbProvider,
                    _api.SpecProvider !,
                    _api.BlockTree !,
                    _api.ReceiptStorage !,
                    _api.SyncPeerPool,
                    _api.NodeStatsManager !,
                    _api.SyncModeSelector,
                    _syncConfig,
                    _api.SnapProvider,
                    _api.BlockDownloaderFactory,
                    _api.Pivot,
                    syncReport,
                    _api.LogManager);
            }

            _api.DisposeStack.Push(_api.Synchronizer);

            _api.SyncServer = new SyncServer(
                _api.TrieStore !,
                _api.DbProvider.CodeDb,
                _api.BlockTree !,
                _api.ReceiptStorage !,
                _api.BlockValidator !,
                _api.SealValidator !,
                _api.SyncPeerPool,
                _api.SyncModeSelector,
                _api.Config <ISyncConfig>(),
                _api.WitnessRepository,
                _api.GossipPolicy,
                _api.SpecProvider !,
                _api.LogManager,
                cht);

            _ = _api.SyncServer.BuildCHT();
            _api.DisposeStack.Push(_api.SyncServer);

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

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

            if (_syncConfig.SnapSync)
            {
                SnapCapabilitySwitcher snapCapabilitySwitcher = new(_api.ProtocolsManager, progressTracker);
                snapCapabilitySwitcher.EnableSnapCapabilityUntilSynced();
            }

            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);
            }

            if (_api.Enode == null)
            {
                throw new InvalidOperationException("Cannot initialize network without knowing own enode");
            }

            ThisNodeInfo.AddInfo("Ethereum     :", $"tcp://{_api.Enode.HostIp}:{_api.Enode.Port}");
            ThisNodeInfo.AddInfo("Version      :", $"{ClientVersion.Description.Replace("Nethermind/v", string.Empty)}");
            ThisNodeInfo.AddInfo("This node    :", $"{_api.Enode.Info}");
            ThisNodeInfo.AddInfo("Node address :", $"{_api.Enode.Address} (do not use as an account)");
        }
Esempio n. 20
0
        public Task Start(CancellationToken cancellationToken)
        {
            IEnumerable <string> GetUrls()
            {
                const string nethermindUrlVariable = "NETHERMIND_URL";
                string       host        = _jsonRpcConfig.Host;
                string       scheme      = "http";
                var          defaultUrl  = $"{scheme}://{host}:{_jsonRpcConfig.Port}";
                var          urlVariable = Environment.GetEnvironmentVariable(nethermindUrlVariable);
                string       url         = defaultUrl;

                if (!string.IsNullOrWhiteSpace(urlVariable))
                {
                    if (Uri.TryCreate(urlVariable, UriKind.Absolute, out var uri))
                    {
                        url    = urlVariable;
                        host   = uri.Host;
                        scheme = uri.Scheme;
                    }
                    else
                    {
                        if (_logger.IsWarn)
                        {
                            _logger.Warn($"Environment variable '{nethermindUrlVariable}' value '{urlVariable}' is not valid JSON RPC URL, using default url : '{defaultUrl}'");
                        }
                    }
                }

                yield return(url);

                if (_initConfig.WebSocketsEnabled && _jsonRpcConfig.WebSocketsPort != _jsonRpcConfig.Port)
                {
                    yield return($"{scheme}://{host}:{_jsonRpcConfig.WebSocketsPort}");
                }
            }

            if (_logger.IsDebug)
            {
                _logger.Debug("Initializing JSON RPC");
            }
            var urls    = GetUrls().ToArray();
            var webHost = WebHost.CreateDefaultBuilder()
                          .ConfigureServices(s =>
            {
                s.AddSingleton(_configurationProvider);
                s.AddSingleton(_jsonRpcProcessor);
                s.AddSingleton(_webSocketsManager);
            })
                          .UseStartup <Startup>()
                          .UseUrls(urls)
                          .ConfigureLogging(logging =>
            {
                logging.SetMinimumLevel(LogLevel.Information);
                logging.ClearProviders();
                logging.AddProvider(new CustomMicrosoftLoggerProvider(_logManager));
            })
                          .Build();

            string urlsString = string.Join(" ; ", urls);

            ThisNodeInfo.AddInfo("JSON RPC     :", $"{urlsString}");
            ThisNodeInfo.AddInfo("RPC modules  :", $"{string.Join(", ", _moduleProvider.Enabled.OrderBy(x => x))}");

            _webHost = webHost;

            if (!cancellationToken.IsCancellationRequested)
            {
                _webHost.Start();
                if (_logger.IsDebug)
                {
                    _logger.Debug($"JSON RPC     : {urlsString}");
                }
                if (_logger.IsDebug)
                {
                    _logger.Debug($"RPC modules  : {string.Join(", ", _moduleProvider.Enabled.OrderBy(x => x))}");
                }
            }

            return(Task.CompletedTask);
        }
Esempio n. 21
0
        public virtual async Task Execute(CancellationToken cancellationToken)
        {
            if (_api.BlockTree == null)
            {
                throw new StepDependencyException(nameof(_api.BlockTree));
            }
            if (_api.ReceiptFinder == null)
            {
                throw new StepDependencyException(nameof(_api.ReceiptFinder));
            }
            if (_api.BloomStorage == null)
            {
                throw new StepDependencyException(nameof(_api.BloomStorage));
            }
            if (_api.LogManager == null)
            {
                throw new StepDependencyException(nameof(_api.LogManager));
            }

            IJsonRpcConfig jsonRpcConfig = _api.Config <IJsonRpcConfig>();

            if (!jsonRpcConfig.Enabled)
            {
                return;
            }

            if (_api.RpcModuleProvider == null)
            {
                throw new StepDependencyException(nameof(_api.RpcModuleProvider));
            }
            if (_api.FileSystem == null)
            {
                throw new StepDependencyException(nameof(_api.FileSystem));
            }
            if (_api.TxPool == null)
            {
                throw new StepDependencyException(nameof(_api.TxPool));
            }
            if (_api.Wallet == null)
            {
                throw new StepDependencyException(nameof(_api.Wallet));
            }
            if (_api.SpecProvider == null)
            {
                throw new StepDependencyException(nameof(_api.SpecProvider));
            }
            if (_api.TxSender == null)
            {
                throw new StepDependencyException(nameof(_api.TxSender));
            }
            if (_api.StateReader == null)
            {
                throw new StepDependencyException(nameof(_api.StateReader));
            }
            if (_api.PeerManager == null)
            {
                throw new StepDependencyException(nameof(_api.PeerManager));
            }

            if (jsonRpcConfig.Enabled)
            {
                _api.RpcModuleProvider = new RpcModuleProvider(_api.FileSystem, jsonRpcConfig, _api.LogManager);
            }
            else
            {
                _api.RpcModuleProvider ??= NullModuleProvider.Instance;
            }

            // the following line needs to be called in order to make sure that the CLI library is referenced from runner and built alongside
            ILogger logger = _api.LogManager.GetClassLogger();

            IInitConfig    initConfig    = _api.Config <IInitConfig>();
            IJsonRpcConfig rpcConfig     = _api.Config <IJsonRpcConfig>();
            INetworkConfig networkConfig = _api.Config <INetworkConfig>();

            // lets add threads to support parallel eth_getLogs
            ThreadPool.GetMinThreads(out int workerThreads, out int completionPortThreads);
            ThreadPool.SetMinThreads(workerThreads + Environment.ProcessorCount, completionPortThreads + Environment.ProcessorCount);

            EthModuleFactory ethModuleFactory = new(
                _api.TxPool,
                _api.TxSender,
                _api.Wallet,
                _api.BlockTree,
                _api.Config <IJsonRpcConfig>(),
                _api.LogManager,
                _api.StateReader,
                _api);

            _api.RpcModuleProvider.RegisterBounded(ethModuleFactory, rpcConfig.EthModuleConcurrentInstances ?? Environment.ProcessorCount, rpcConfig.Timeout);

            if (_api.DbProvider == null)
            {
                throw new StepDependencyException(nameof(_api.DbProvider));
            }
            if (_api.BlockPreprocessor == null)
            {
                throw new StepDependencyException(nameof(_api.BlockPreprocessor));
            }
            if (_api.BlockValidator == null)
            {
                throw new StepDependencyException(nameof(_api.BlockValidator));
            }
            if (_api.RewardCalculatorSource == null)
            {
                throw new StepDependencyException(nameof(_api.RewardCalculatorSource));
            }

            ProofModuleFactory proofModuleFactory = new(_api.DbProvider, _api.BlockTree, _api.ReadOnlyTrieStore, _api.BlockPreprocessor, _api.ReceiptFinder, _api.SpecProvider, _api.LogManager);

            _api.RpcModuleProvider.RegisterBounded(proofModuleFactory, 2, rpcConfig.Timeout);

            DebugModuleFactory debugModuleFactory = new(
                _api.DbProvider,
                _api.BlockTree,
                rpcConfig,
                _api.BlockValidator,
                _api.BlockPreprocessor,
                _api.RewardCalculatorSource,
                _api.ReceiptStorage,
                new ReceiptMigration(_api),
                _api.ReadOnlyTrieStore,
                _api.ConfigProvider,
                _api.SpecProvider,
                _api.LogManager);

            _api.RpcModuleProvider.RegisterBoundedByCpuCount(debugModuleFactory, rpcConfig.Timeout);

            TraceModuleFactory traceModuleFactory = new(
                _api.DbProvider,
                _api.BlockTree,
                _api.ReadOnlyTrieStore,
                rpcConfig,
                _api.BlockPreprocessor,
                _api.RewardCalculatorSource,
                _api.ReceiptStorage,
                _api.SpecProvider,
                _api.LogManager);

            _api.RpcModuleProvider.RegisterBoundedByCpuCount(traceModuleFactory, rpcConfig.Timeout);

            if (_api.EthereumEcdsa == null)
            {
                throw new StepDependencyException(nameof(_api.EthereumEcdsa));
            }
            if (_api.Wallet == null)
            {
                throw new StepDependencyException(nameof(_api.Wallet));
            }

            PersonalRpcModule personalRpcModule = new(
                _api.EthereumEcdsa,
                _api.Wallet,
                _api.KeyStore);

            _api.RpcModuleProvider.RegisterSingle <IPersonalRpcModule>(personalRpcModule);

            if (_api.PeerManager == null)
            {
                throw new StepDependencyException(nameof(_api.PeerManager));
            }
            if (_api.StaticNodesManager == null)
            {
                throw new StepDependencyException(nameof(_api.StaticNodesManager));
            }
            if (_api.Enode == null)
            {
                throw new StepDependencyException(nameof(_api.Enode));
            }

            AdminRpcModule adminRpcModule = new(
                _api.BlockTree,
                networkConfig,
                _api.PeerManager,
                _api.StaticNodesManager,
                _api.Enode,
                initConfig.BaseDbPath);

            _api.RpcModuleProvider.RegisterSingle <IAdminRpcModule>(adminRpcModule);

            if (_api.TxPoolInfoProvider == null)
            {
                throw new StepDependencyException(nameof(_api.TxPoolInfoProvider));
            }

            TxPoolRpcModule txPoolRpcModule = new(_api.BlockTree, _api.TxPoolInfoProvider, _api.LogManager);

            _api.RpcModuleProvider.RegisterSingle <ITxPoolRpcModule>(txPoolRpcModule);

            if (_api.SyncServer == null)
            {
                throw new StepDependencyException(nameof(_api.SyncServer));
            }
            if (_api.EngineSignerStore == null)
            {
                throw new StepDependencyException(nameof(_api.EngineSignerStore));
            }

            NetRpcModule netRpcModule = new(_api.LogManager, new NetBridge(_api.Enode, _api.SyncServer));

            _api.RpcModuleProvider.RegisterSingle <INetRpcModule>(netRpcModule);

            ParityRpcModule parityRpcModule = new(
                _api.EthereumEcdsa,
                _api.TxPool,
                _api.BlockTree,
                _api.ReceiptFinder,
                _api.Enode,
                _api.EngineSignerStore,
                _api.KeyStore,
                _api.LogManager,
                _api.PeerManager);

            _api.RpcModuleProvider.RegisterSingle <IParityRpcModule>(parityRpcModule);

            SubscriptionFactory subscriptionFactory = new(
                _api.LogManager,
                _api.BlockTree,
                _api.TxPool,
                _api.ReceiptStorage,
                _api.FilterStore);

            SubscriptionManager subscriptionManager = new(subscriptionFactory, _api.LogManager);

            SubscribeRpcModule subscribeRpcModule = new(subscriptionManager);

            _api.RpcModuleProvider.RegisterSingle <ISubscribeRpcModule>(subscribeRpcModule);

            Web3RpcModule web3RpcModule = new(_api.LogManager);

            _api.RpcModuleProvider.RegisterSingle <IWeb3RpcModule>(web3RpcModule);

            EvmRpcModule evmRpcModule = new(_api.ManualBlockProductionTrigger);

            _api.RpcModuleProvider.RegisterSingle <IEvmRpcModule>(evmRpcModule);

            foreach (INethermindPlugin plugin in _api.Plugins)
            {
                await plugin.InitRpcModules();
            }

            if (logger.IsDebug)
            {
                logger.Debug($"RPC modules  : {string.Join(", ", _api.RpcModuleProvider.Enabled.OrderBy(x => x))}");
            }
            ThisNodeInfo.AddInfo("RPC modules  :", $"{string.Join(", ", _api.RpcModuleProvider.Enabled.OrderBy(x => x))}");
        }
Esempio n. 22
0
        private Task InitBlockchain()
        {
            if (_context.ChainSpec == null)
            {
                throw new StepDependencyException(nameof(_context.ChainSpec));
            }
            if (_context.DbProvider == null)
            {
                throw new StepDependencyException(nameof(_context.DbProvider));
            }
            if (_context.SpecProvider == null)
            {
                throw new StepDependencyException(nameof(_context.SpecProvider));
            }

            ILogger     logger     = _context.LogManager.GetClassLogger();
            IInitConfig initConfig = _context.Config <IInitConfig>();
            ISyncConfig syncConfig = _context.Config <ISyncConfig>();

            if (syncConfig.DownloadReceiptsInFastSync && !syncConfig.DownloadBodiesInFastSync)
            {
                logger.Warn($"{nameof(syncConfig.DownloadReceiptsInFastSync)} is selected but {nameof(syncConfig.DownloadBodiesInFastSync)} - enabling bodies to support receipts download.");
                syncConfig.DownloadBodiesInFastSync = true;
            }

            Account.AccountStartNonce = _context.ChainSpec.Parameters.AccountStartNonce;

            _context.Signer = new Signer(_context.SpecProvider.ChainId, _context.OriginalSignerKey);

            _context.StateProvider = new StateProvider(
                _context.DbProvider.StateDb,
                _context.DbProvider.CodeDb,
                _context.LogManager);

            _context.EthereumEcdsa = new EthereumEcdsa(_context.SpecProvider.ChainId, _context.LogManager);
            _context.TxPool        = new TxPool.TxPool(
                new PersistentTxStorage(_context.DbProvider.PendingTxsDb),
                Timestamper.Default,
                _context.EthereumEcdsa,
                _context.SpecProvider,
                _context.Config <ITxPoolConfig>(),
                _context.StateProvider,
                _context.LogManager);

            var bloomConfig = _context.Config <IBloomConfig>();

            var fileStoreFactory = initConfig.DiagnosticMode == DiagnosticMode.MemDb
                ? (IFileStoreFactory) new InMemoryDictionaryFileStoreFactory()
                : new FixedSizeFileStoreFactory(Path.Combine(initConfig.BaseDbPath, DbNames.Bloom), DbNames.Bloom, Bloom.ByteLength);

            _context.BloomStorage = bloomConfig.Index
                ? new BloomStorage(bloomConfig, _context.DbProvider.BloomDb, fileStoreFactory)
                : (IBloomStorage)NullBloomStorage.Instance;

            _context.DisposeStack.Push(_context.BloomStorage);

            _context.ChainLevelInfoRepository = new ChainLevelInfoRepository(_context.DbProvider.BlockInfosDb);

            _context.BlockTree = new BlockTree(
                _context.DbProvider.BlocksDb,
                _context.DbProvider.HeadersDb,
                _context.DbProvider.BlockInfosDb,
                _context.ChainLevelInfoRepository,
                _context.SpecProvider,
                _context.TxPool,
                _context.BloomStorage,
                _context.Config <ISyncConfig>(),
                _context.LogManager);

            // Init state if we need system calls before actual processing starts
            if (_context.BlockTree.Head != null)
            {
                _context.StateProvider.StateRoot = _context.BlockTree.Head.StateRoot;
            }

            _context.ReceiptStorage = initConfig.StoreReceipts ? (IReceiptStorage?)new PersistentReceiptStorage(_context.DbProvider.ReceiptsDb, _context.SpecProvider, new ReceiptsRecovery()) : NullReceiptStorage.Instance;
            _context.ReceiptFinder  = new FullInfoReceiptFinder(_context.ReceiptStorage, new ReceiptsRecovery(), _context.BlockTree);

            _context.RecoveryStep = new TxSignaturesRecoveryStep(_context.EthereumEcdsa, _context.TxPool, _context.LogManager);

            _context.StorageProvider = new StorageProvider(
                _context.DbProvider.StateDb,
                _context.StateProvider,
                _context.LogManager);

            // blockchain processing
            BlockhashProvider blockhashProvider = new BlockhashProvider(
                _context.BlockTree, _context.LogManager);

            VirtualMachine virtualMachine = new VirtualMachine(
                _context.StateProvider,
                _context.StorageProvider,
                blockhashProvider,
                _context.SpecProvider,
                _context.LogManager);

            _context.TransactionProcessor = new TransactionProcessor(
                _context.SpecProvider,
                _context.StateProvider,
                _context.StorageProvider,
                virtualMachine,
                _context.LogManager);

            InitSealEngine();
            if (_context.SealValidator == null)
            {
                throw new StepDependencyException(nameof(_context.SealValidator));
            }

            /* validation */
            _context.HeaderValidator = CreateHeaderValidator();

            OmmersValidator ommersValidator = new OmmersValidator(
                _context.BlockTree,
                _context.HeaderValidator,
                _context.LogManager);

            TxValidator txValidator = new TxValidator(_context.SpecProvider.ChainId);

            _context.BlockValidator = new BlockValidator(
                txValidator,
                _context.HeaderValidator,
                ommersValidator,
                _context.SpecProvider,
                _context.LogManager);

            ReadOnlyDbProvider readOnly    = new ReadOnlyDbProvider(_context.DbProvider, false);
            StateReader        stateReader = new StateReader(readOnly.StateDb, readOnly.CodeDb, _context.LogManager);

            _context.TxPoolInfoProvider = new TxPoolInfoProvider(stateReader, _context.TxPool);

            _context.MainBlockProcessor = CreateBlockProcessor();

            BlockchainProcessor blockchainProcessor = new BlockchainProcessor(
                _context.BlockTree,
                _context.MainBlockProcessor,
                _context.RecoveryStep,
                _context.LogManager,
                new BlockchainProcessor.Options
            {
                AutoProcess            = !syncConfig.BeamSync,
                StoreReceiptsByDefault = initConfig.StoreReceipts,
                RunGethTracer          = initConfig.DiagnosticMode == DiagnosticMode.GethTrace,
                RunParityTracer        = initConfig.DiagnosticMode == DiagnosticMode.ParityTrace,
            });

            _context.BlockProcessingQueue = blockchainProcessor;
            _context.BlockchainProcessor  = blockchainProcessor;

            if (syncConfig.BeamSync)
            {
                BeamBlockchainProcessor beamBlockchainProcessor = new BeamBlockchainProcessor(
                    new ReadOnlyDbProvider(_context.DbProvider, false),
                    _context.BlockTree,
                    _context.SpecProvider,
                    _context.LogManager,
                    _context.BlockValidator,
                    _context.RecoveryStep,
                    _context.RewardCalculatorSource,
                    _context.BlockProcessingQueue,
                    _context.BlockchainProcessor,
                    _context.SyncModeSelector);

                _context.DisposeStack.Push(beamBlockchainProcessor);
            }

            ThisNodeInfo.AddInfo("Mem est trie :", $"{LruCache<Keccak, byte[]>.CalculateMemorySize(52 + 320, Trie.MemoryAllowance.TrieNodeCacheSize) / 1024 / 1024}MB".PadLeft(8));

            return(Task.CompletedTask);
        }