public Context() { BlockTree = Substitute.For <IBlockTree>(); Stats = Substitute.For <INodeStatsManager>(); PeerStrategy = new TotalDifficultyBetterPeerStrategy(LimboLogs.Instance); Pool = new SyncPeerPool(BlockTree, Stats, PeerStrategy, 25, 50, LimboLogs.Instance); }
public SyncingContext AfterPeerIsAdded(ISyncPeer syncPeer) { ((SyncPeerMock)syncPeer).Disconnected += (s, e) => SyncPeerPool.RemovePeer(syncPeer); _logger.Info($"PEER ADDED {syncPeer.ClientId}"); _peers.TryAdd(syncPeer.ClientId, syncPeer); SyncPeerPool.AddPeer(syncPeer); return(this); }
public void Stop() { Synchronizer.SyncEvent -= SynchronizerOnSyncEvent; Task task = new Task(async() => { await Synchronizer.StopAsync(); await SyncPeerPool.StopAsync(); }); task.RunSynchronously(); }
public SyncingContext Stop() { Synchronizer.SyncEvent -= SynchronizerOnSyncEvent; Task task = new Task(async() => { await Synchronizer.StopAsync(); await SyncPeerPool.StopAsync(); }); task.RunSynchronously(); return(this); }
public async Task Setup() { _genesisBlock = Build.A.Block.WithNumber(0).TestObject; _blockTree = Build.A.BlockTree(_genesisBlock).OfChainLength(1).TestObject; IDbProvider dbProvider = await TestMemDbProvider.InitAsync(); _stateDb = dbProvider.StateDb; _codeDb = dbProvider.CodeDb; _receiptStorage = Substitute.For <IReceiptStorage>(); SyncConfig quickConfig = new SyncConfig(); quickConfig.FastSync = false; ITimerFactory timerFactory = Substitute.For <ITimerFactory>(); var stats = new NodeStatsManager(timerFactory, LimboLogs.Instance); _pool = new SyncPeerPool(_blockTree, stats, 25, LimboLogs.Instance); SyncConfig syncConfig = new SyncConfig(); SyncProgressResolver resolver = new SyncProgressResolver( _blockTree, _receiptStorage, _stateDb, dbProvider.BeamTempDb, new TrieStore(_stateDb, LimboLogs.Instance), syncConfig, LimboLogs.Instance); MultiSyncModeSelector syncModeSelector = new MultiSyncModeSelector(resolver, _pool, syncConfig, LimboLogs.Instance); _synchronizer = new Synchronizer(dbProvider, MainnetSpecProvider.Instance, _blockTree, _receiptStorage, Always.Valid, Always.Valid, _pool, stats, syncModeSelector, syncConfig, LimboLogs.Instance); _syncServer = new SyncServer( _stateDb, _codeDb, _blockTree, _receiptStorage, Always.Valid, Always.Valid, _pool, syncModeSelector, quickConfig, new WitnessCollector(new MemDb(), LimboLogs.Instance), LimboLogs.Instance); }
public void Setup() { _genesisBlock = Build.A.Block.WithNumber(0).TestObject; _blockTree = Build.A.BlockTree(_genesisBlock).OfChainLength(1).TestObject; MemDbProvider dbProvider = new MemDbProvider(); _stateDb = dbProvider.StateDb; _codeDb = dbProvider.CodeDb; _receiptsDb = dbProvider.ReceiptsDb; _receiptStorage = Substitute.For <IReceiptStorage>(); SyncConfig quickConfig = new SyncConfig(); quickConfig.FastSync = false; var stats = new NodeStatsManager(new StatsConfig(), LimboLogs.Instance); _pool = new SyncPeerPool(_blockTree, stats, 25, LimboLogs.Instance); SyncConfig syncConfig = new SyncConfig(); SyncProgressResolver resolver = new SyncProgressResolver(_blockTree, _receiptStorage, _stateDb, dbProvider.BeamStateDb, syncConfig, LimboLogs.Instance); MultiSyncModeSelector syncModeSelector = new MultiSyncModeSelector(resolver, _pool, syncConfig, LimboLogs.Instance); _synchronizer = new Synchronizer(dbProvider, MainnetSpecProvider.Instance, _blockTree, _receiptStorage, Always.Valid, Always.Valid, _pool, stats, syncModeSelector, syncConfig, LimboLogs.Instance); _syncServer = new SyncServer(_stateDb, _codeDb, _blockTree, _receiptStorage, Always.Valid, Always.Valid, _pool, syncModeSelector, quickConfig, LimboLogs.Instance); }
public void SetUp() { _blockTree = Substitute.For <IBlockTree>(); _stats = Substitute.For <INodeStatsManager>(); _pool = new SyncPeerPool(_blockTree, _stats, 25, 50, LimboLogs.Instance); }
public SyncingContext AfterPeerIsRemoved(ISyncPeer syncPeer) { _peers.Remove(syncPeer.ClientId); SyncPeerPool.RemovePeer(syncPeer); return(this); }
private SyncTestContext CreateSyncManager(int index) { var logManager = NoErrorLimboLogs.Instance; ConsoleAsyncLogger logger = new ConsoleAsyncLogger(LogLevel.Debug, "PEER " + index + " "); // var logManager = new OneLoggerLogManager(logger); var specProvider = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, MainnetSpecProvider.Instance.ChainId); var dbProvider = TestMemDbProvider.Init(); IDb blockDb = dbProvider.BlocksDb; IDb headerDb = dbProvider.HeadersDb; IDb blockInfoDb = dbProvider.BlockInfosDb; ISnapshotableDb codeDb = dbProvider.CodeDb; ISnapshotableDb stateDb = dbProvider.StateDb; var stateReader = new StateReader(stateDb, codeDb, logManager); var stateProvider = new StateProvider(stateDb, codeDb, logManager); stateProvider.CreateAccount(TestItem.AddressA, 10000.Ether()); stateProvider.Commit(specProvider.GenesisSpec); stateProvider.CommitTree(); stateProvider.RecalculateStateRoot(); stateDb.Commit(); var storageProvider = new StorageProvider(stateDb, stateProvider, logManager); var receiptStorage = new InMemoryReceiptStorage(); var ecdsa = new EthereumEcdsa(specProvider.ChainId, logManager); var txPool = new TxPool.TxPool(new InMemoryTxStorage(), ecdsa, specProvider, new TxPoolConfig(), stateProvider, logManager); var tree = new BlockTree(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, NullBloomStorage.Instance, logManager); var blockhashProvider = new BlockhashProvider(tree, LimboLogs.Instance); var virtualMachine = new VirtualMachine(stateProvider, storageProvider, blockhashProvider, specProvider, logManager); var sealValidator = Always.Valid; var headerValidator = new HeaderValidator(tree, sealValidator, specProvider, logManager); var txValidator = Always.Valid; var ommersValidator = new OmmersValidator(tree, headerValidator, logManager); var blockValidator = new BlockValidator(txValidator, headerValidator, ommersValidator, specProvider, logManager); ISyncConfig syncConfig = _synchronizerType == SynchronizerType.Fast ? SyncConfig.WithFastSync : SyncConfig.WithFullSyncOnly; var rewardCalculator = new RewardCalculator(specProvider); var txProcessor = new TransactionProcessor(specProvider, stateProvider, storageProvider, virtualMachine, logManager); var blockProcessor = new BlockProcessor(specProvider, blockValidator, rewardCalculator, txProcessor, stateDb, codeDb, stateProvider, storageProvider, txPool, receiptStorage, logManager); var step = new RecoverSignatures(ecdsa, txPool, specProvider, logManager); var processor = new BlockchainProcessor(tree, blockProcessor, step, logManager, BlockchainProcessor.Options.Default); var nodeStatsManager = new NodeStatsManager(logManager); var syncPeerPool = new SyncPeerPool(tree, nodeStatsManager, 25, logManager); StateProvider devState = new StateProvider(stateDb, codeDb, logManager); StorageProvider devStorage = new StorageProvider(stateDb, devState, logManager); var devEvm = new VirtualMachine(devState, devStorage, blockhashProvider, specProvider, logManager); var devTxProcessor = new TransactionProcessor(specProvider, devState, devStorage, devEvm, logManager); var devBlockProcessor = new BlockProcessor(specProvider, blockValidator, rewardCalculator, devTxProcessor, stateDb, codeDb, devState, devStorage, txPool, receiptStorage, logManager); var devChainProcessor = new BlockchainProcessor(tree, devBlockProcessor, step, logManager, BlockchainProcessor.Options.NoReceipts); var transactionSelector = new TxPoolTxSource(txPool, stateReader, logManager); var producer = new DevBlockProducer( transactionSelector, devChainProcessor, stateProvider, tree, processor, txPool, Timestamper.Default, logManager); SyncProgressResolver resolver = new SyncProgressResolver(tree, receiptStorage, stateDb, new MemDb(), syncConfig, logManager); MultiSyncModeSelector selector = new MultiSyncModeSelector(resolver, syncPeerPool, syncConfig, logManager); Synchronizer synchronizer = new Synchronizer( dbProvider, MainnetSpecProvider.Instance, tree, NullReceiptStorage.Instance, blockValidator, sealValidator, syncPeerPool, nodeStatsManager, StaticSelector.Full, syncConfig, logManager); var syncServer = new SyncServer(stateDb, codeDb, tree, receiptStorage, Always.Valid, Always.Valid, syncPeerPool, selector, syncConfig, logManager); ManualResetEventSlim waitEvent = new ManualResetEventSlim(); tree.NewHeadBlock += (s, e) => waitEvent.Set(); if (index == 0) { _genesis = Build.A.Block.Genesis.WithStateRoot(stateProvider.StateRoot).TestObject; producer.Start(); } syncPeerPool.Start(); synchronizer.Start(); processor.Start(); tree.SuggestBlock(_genesis); if (!waitEvent.Wait(1000)) { throw new Exception("No genesis"); } SyncTestContext context = new SyncTestContext(); context.Ecdsa = ecdsa; context.BlockchainProcessor = processor; context.PeerPool = syncPeerPool; context.StateProvider = stateProvider; context.Synchronizer = synchronizer; context.SyncServer = syncServer; context.Tree = tree; context.BlockProducer = producer; context.TxPool = txPool; context.Logger = logger; return(context); }
public Context() { BlockTree = Substitute.For <IBlockTree>(); Stats = Substitute.For <INodeStatsManager>(); Pool = new SyncPeerPool(BlockTree, Stats, 25, 50, LimboLogs.Instance); }
public async Task <long> DownloadHeaders(PeerInfo bestPeer, BlocksRequest blocksRequest, CancellationToken cancellation) { if (bestPeer == null) { string message = $"Not expecting best peer to be null inside the {nameof(BlockDownloader)}"; _logger.Error(message); throw new ArgumentNullException(message); } int headersSynced = 0; int ancestorLookupLevel = 0; long currentNumber = Math.Max(0, Math.Min(_blockTree.BestKnownNumber, bestPeer.HeadNumber - 1)); while (bestPeer.TotalDifficulty > (_blockTree.BestSuggestedHeader?.TotalDifficulty ?? 0) && currentNumber <= bestPeer.HeadNumber) { int headersSyncedInPreviousRequests = headersSynced; if (_logger.IsTrace) { _logger.Trace($"Continue headers sync with {bestPeer} (our best {_blockTree.BestKnownNumber})"); } long blocksLeft = bestPeer.HeadNumber - currentNumber - (blocksRequest.NumberOfLatestBlocksToBeIgnored ?? 0); int headersToRequest = (int)Math.Min(blocksLeft + 1, _syncBatchSize.Current); if (headersToRequest <= 1) { break; } if (_logger.IsDebug) { _logger.Debug($"Headers request {currentNumber}+{headersToRequest} to peer {bestPeer} with {bestPeer.HeadNumber} blocks. Got {currentNumber} and asking for {headersToRequest} more."); } BlockHeader?[] headers = await RequestHeaders(bestPeer, cancellation, currentNumber, headersToRequest); BlockHeader?startingPoint = headers[0] == null ? null : _blockTree.FindHeader(headers[0].Hash, BlockTreeLookupOptions.TotalDifficultyNotNeeded); if (startingPoint == null) { ancestorLookupLevel++; if (ancestorLookupLevel >= _ancestorJumps.Length) { if (_logger.IsWarn) { _logger.Warn($"Could not find common ancestor with {bestPeer}"); } throw new EthSyncException("Peer with inconsistent chain in sync"); } int ancestorJump = _ancestorJumps[ancestorLookupLevel] - _ancestorJumps[ancestorLookupLevel - 1]; currentNumber = currentNumber >= ancestorJump ? (currentNumber - ancestorJump) : 0L; continue; } ancestorLookupLevel = 0; _sinceLastTimeout++; if (_sinceLastTimeout >= 2) { // if peers are not timing out then we can try to be slightly more eager _syncBatchSize.Expand(); } for (int i = 1; i < headers.Length; i++) { if (cancellation.IsCancellationRequested) { break; } BlockHeader?currentHeader = headers[i]; if (currentHeader == null) { if (headersSynced - headersSyncedInPreviousRequests > 0) { break; } SyncPeerPool.ReportNoSyncProgress(bestPeer, AllocationContexts.Blocks); return(0); } if (_logger.IsTrace) { _logger.Trace($"Received {currentHeader} from {bestPeer:s}"); } bool isValid = i > 1 ? _blockValidator.ValidateHeader(currentHeader, headers[i - 1], false) : _blockValidator.ValidateHeader(currentHeader, false); if (!isValid) { throw new EthSyncException($"{bestPeer} sent a block {currentHeader.ToString(BlockHeader.Format.Short)} with an invalid header"); } // i == 0 is always false but leave it this was as it will be possible that we will change the // loop iterator to start with o if (HandleAddResult(bestPeer, currentHeader, i == 0, _blockTree.Insert(currentHeader))) { headersSynced++; } currentNumber = currentNumber + 1; } if (headersSynced > 0) { _syncReport.FullSyncBlocksDownloaded.Update(_blockTree.BestSuggestedHeader?.Number ?? 0); _syncReport.FullSyncBlocksKnown = bestPeer.HeadNumber; } else { break; } } return(headersSynced); }
public SyncingContext AfterPeerIsRemoved(ISyncPeer syncPeer) { _peers.Remove(syncPeer.ClientId); SyncPeerPool.RemovePeer(syncPeer, EthSyncPeerPool.PeerRemoveReason.SessionDisconnected); return(this); }
public async Task Setup() { _genesisBlock = Build.A.Block.WithNumber(0).TestObject; _blockTree = Build.A.BlockTree(_genesisBlock).OfChainLength(1).TestObject; IDbProvider dbProvider = await TestMemDbProvider.InitAsync(); _stateDb = dbProvider.StateDb; _codeDb = dbProvider.CodeDb; _receiptStorage = Substitute.For <IReceiptStorage>(); SyncConfig quickConfig = new() { FastSync = false }; ITimerFactory timerFactory = Substitute.For <ITimerFactory>(); NodeStatsManager stats = new(timerFactory, LimboLogs.Instance); _pool = new SyncPeerPool(_blockTree, stats, new TotalDifficultyBetterPeerStrategy(LimboLogs.Instance), 25, LimboLogs.Instance); SyncConfig syncConfig = new(); ProgressTracker progressTracker = new(_blockTree, dbProvider.StateDb, LimboLogs.Instance); SnapProvider snapProvider = new(progressTracker, dbProvider, LimboLogs.Instance); TrieStore trieStore = new(_stateDb, LimboLogs.Instance); SyncProgressResolver resolver = new( _blockTree, _receiptStorage, _stateDb, trieStore, progressTracker, syncConfig, LimboLogs.Instance); TotalDifficultyBetterPeerStrategy bestPeerStrategy = new(LimboLogs.Instance); MultiSyncModeSelector syncModeSelector = new(resolver, _pool, syncConfig, No.BeaconSync, bestPeerStrategy, LimboLogs.Instance); Pivot pivot = new (syncConfig); SyncReport syncReport = new(_pool, stats, syncModeSelector, syncConfig, pivot, LimboLogs.Instance); BlockDownloaderFactory blockDownloaderFactory = new( MainnetSpecProvider.Instance, _blockTree, _receiptStorage, Always.Valid, Always.Valid, _pool, new TotalDifficultyBetterPeerStrategy(LimboLogs.Instance), syncReport, LimboLogs.Instance); _synchronizer = new Synchronizer( dbProvider, MainnetSpecProvider.Instance, _blockTree, _receiptStorage, _pool, stats, syncModeSelector, syncConfig, snapProvider, blockDownloaderFactory, pivot, syncReport, LimboLogs.Instance); _syncServer = new SyncServer( trieStore, _codeDb, _blockTree, _receiptStorage, Always.Valid, Always.Valid, _pool, syncModeSelector, quickConfig, new WitnessCollector(new MemDb(), LimboLogs.Instance), Policy.FullGossip, MainnetSpecProvider.Instance, LimboLogs.Instance); }