private static void AddGuidIdAttribute(BlockProcessor blockProcessor, Block block) { var attrs = block.GetAttributes(); attrs.Id = attrs.Id ?? Guid.NewGuid().ToString().Substring(0, 8); }
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 = new MemDbProvider(); 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, logManager); var txPool = new TxPool.TxPool(new InMemoryTxStorage(), Timestamper.Default, ecdsa, specProvider, new TxPoolConfig(), stateProvider, logManager); var tree = new BlockTree(blockDb, headerDb, blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, txPool, 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 TxSignaturesRecoveryStep(ecdsa, txPool, logManager); var processor = new BlockchainProcessor(tree, blockProcessor, step, logManager, true); var nodeStatsManager = new NodeStatsManager(new StatsConfig(), 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, false); 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); }
protected async Task <EthereumTestResult> RunTest(BlockchainTest test, Stopwatch stopwatch = null) { TestContext.Write($"Running {test.Name} at {DateTime.UtcNow:HH:mm:ss.ffffff}"); Assert.IsNull(test.LoadFailure, "test data loading failure"); ISnapshotableDb stateDb = new StateDb(); ISnapshotableDb codeDb = new StateDb(); ISpecProvider specProvider; if (test.NetworkAfterTransition != null) { specProvider = new CustomSpecProvider( (0, Frontier.Instance), (1, test.Network), (test.TransitionBlockNumber, test.NetworkAfterTransition)); } else { specProvider = new CustomSpecProvider( (0, Frontier.Instance), // TODO: this thing took a lot of time to find after it was removed!, genesis block is always initialized with Frontier (1, test.Network)); } if (specProvider.GenesisSpec != Frontier.Instance) { Assert.Fail("Expected genesis spec to be Frontier for blockchain tests"); } DifficultyCalculator.Wrapped = new DifficultyCalculator(specProvider); IRewardCalculator rewardCalculator = new RewardCalculator(specProvider); IEthereumEcdsa ecdsa = new EthereumEcdsa(specProvider.ChainId, _logManager); IStateProvider stateProvider = new StateProvider(stateDb, codeDb, _logManager); ITxPool transactionPool = new TxPool(NullTxStorage.Instance, new Timestamper(), ecdsa, specProvider, new TxPoolConfig(), stateProvider, _logManager); IReceiptStorage receiptStorage = NullReceiptStorage.Instance; var blockInfoDb = new MemDb(); IBlockTree blockTree = new BlockTree(new MemDb(), new MemDb(), blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, transactionPool, NullBloomStorage.Instance, _logManager); IBlockhashProvider blockhashProvider = new BlockhashProvider(blockTree, _logManager); ITxValidator txValidator = new TxValidator(ChainId.Mainnet); IHeaderValidator headerValidator = new HeaderValidator(blockTree, Sealer, specProvider, _logManager); IOmmersValidator ommersValidator = new OmmersValidator(blockTree, headerValidator, _logManager); IBlockValidator blockValidator = new BlockValidator(txValidator, headerValidator, ommersValidator, specProvider, _logManager); IStorageProvider storageProvider = new StorageProvider(stateDb, stateProvider, _logManager); IVirtualMachine virtualMachine = new VirtualMachine( stateProvider, storageProvider, blockhashProvider, specProvider, _logManager); IBlockProcessor blockProcessor = new BlockProcessor( specProvider, blockValidator, rewardCalculator, new TransactionProcessor( specProvider, stateProvider, storageProvider, virtualMachine, _logManager), stateDb, codeDb, stateProvider, storageProvider, transactionPool, receiptStorage, _logManager); IBlockchainProcessor blockchainProcessor = new BlockchainProcessor( blockTree, blockProcessor, new TxSignaturesRecoveryStep(ecdsa, NullTxPool.Instance, _logManager), _logManager, BlockchainProcessor.Options.NoReceipts); InitializeTestState(test, stateProvider, storageProvider, specProvider); List <(Block Block, string ExpectedException)> correctRlp = new List <(Block, string)>(); for (int i = 0; i < test.Blocks.Length; i++) { try { TestBlockJson testBlockJson = test.Blocks[i]; var rlpContext = Bytes.FromHexString(testBlockJson.Rlp).AsRlpStream(); Block suggestedBlock = Rlp.Decode <Block>(rlpContext); suggestedBlock.Header.SealEngineType = test.SealEngineUsed ? SealEngineType.Ethash : SealEngineType.None; Assert.AreEqual(new Keccak(testBlockJson.BlockHeader.Hash), suggestedBlock.Header.Hash, "hash of the block"); for (int ommerIndex = 0; ommerIndex < suggestedBlock.Ommers.Length; ommerIndex++) { Assert.AreEqual(new Keccak(testBlockJson.UncleHeaders[ommerIndex].Hash), suggestedBlock.Ommers[ommerIndex].Hash, "hash of the ommer"); } correctRlp.Add((suggestedBlock, testBlockJson.ExpectedException)); } catch (Exception) { _logger?.Info($"Invalid RLP ({i})"); } } if (correctRlp.Count == 0) { var result = new EthereumTestResult(test.Name, null); try { Assert.AreEqual(new Keccak(test.GenesisBlockHeader.Hash), test.LastBlockHash); } catch (AssertionException) { result.Pass = false; return(result); } result.Pass = true; return(result); } if (test.GenesisRlp == null) { test.GenesisRlp = Rlp.Encode(new Block(JsonToEthereumTest.Convert(test.GenesisBlockHeader))); } Block genesisBlock = Rlp.Decode <Block>(test.GenesisRlp.Bytes); Assert.AreEqual(new Keccak(test.GenesisBlockHeader.Hash), genesisBlock.Header.Hash, "genesis header hash"); ManualResetEvent genesisProcessed = new ManualResetEvent(false); blockTree.NewHeadBlock += (sender, args) => { if (args.Block.Number == 0) { Assert.AreEqual(genesisBlock.Header.StateRoot, stateProvider.StateRoot, "genesis state root"); genesisProcessed.Set(); } }; blockchainProcessor.Start(); blockTree.SuggestBlock(genesisBlock); genesisProcessed.WaitOne(); for (int i = 0; i < correctRlp.Count; i++) { stopwatch?.Start(); try { if (correctRlp[i].ExpectedException != null) { _logger.Info($"Expecting block exception: {correctRlp[i].ExpectedException}"); } if (correctRlp[i].Block.Hash == null) { throw new Exception($"null hash in {test.Name} block {i}"); } // TODO: mimic the actual behaviour where block goes through validating sync manager? if (!test.SealEngineUsed || blockValidator.ValidateSuggestedBlock(correctRlp[i].Block)) { blockTree.SuggestBlock(correctRlp[i].Block); } else { Console.WriteLine("Invalid block"); } } catch (InvalidBlockException) { } catch (Exception ex) { _logger?.Info(ex.ToString()); } } await blockchainProcessor.StopAsync(true); stopwatch?.Stop(); List <string> differences = RunAssertions(test, blockTree.RetrieveHeadBlock(), storageProvider, stateProvider); // if (differences.Any()) // { // BlockTrace blockTrace = blockchainProcessor.TraceBlock(blockTree.BestSuggested.Hash); // _logger.Info(new UnforgivingJsonSerializer().Serialize(blockTrace, true)); // } Assert.Zero(differences.Count, "differences"); return(new EthereumTestResult { Pass = differences.Count == 0, Name = test.Name }); }
public override BlockState TryOpen(BlockProcessor processor) { var slice = processor.Line; var column = processor.Column; var sourcePosition = processor.Start; if (processor.IsCodeIndent || !ExtensionsHelper.MatchStart(ref slice, ":::")) { return(BlockState.None); } ExtensionsHelper.SkipSpaces(ref slice); var extensionName = "triple-colon"; ITripleColonExtensionInfo extension; IDictionary <string, string> attributes; HtmlAttributes htmlAttributes; IDictionary <string, string> renderProperties; Action <string> logError = (string message) => _context.LogError( $"invalid-{extensionName}", $"Invalid {extensionName} on line {processor.LineIndex}. \"{slice.Text}\" is invalid. {message}", null, line: processor.LineIndex); if (!TryMatchIdentifier(ref slice, out extensionName) || !_extensions.TryGetValue(extensionName, out extension) || !extension.TryValidateAncestry(processor.CurrentContainer, logError) || !TryMatchAttributes(ref slice, out attributes, extensionName, extension.SelfClosing, logError) || !extension.TryProcessAttributes(attributes, out htmlAttributes, out renderProperties, logError, processor)) { return(BlockState.None); } var block = new TripleColonBlock(this) { Closed = false, Column = column, Line = processor.LineIndex, Span = new SourceSpan(sourcePosition, slice.End), Extension = extension, RenderProperties = renderProperties }; if (htmlAttributes != null) { block.SetData(typeof(HtmlAttributes), htmlAttributes); } processor.NewBlocks.Push(block); if (extension.GetType() == typeof(ImageExtension) && htmlAttributes != null && ImageExtension.RequiresClosingTripleColon(attributes)) { ((TripleColonBlock)block).EndingTripleColons = true; return(BlockState.ContinueDiscard); } if (extension.SelfClosing) { return(BlockState.BreakDiscard); } return(BlockState.ContinueDiscard); }
private async Task Initialize(bool auRa = false) { IDbProvider dbProvider = await TestMemDbProvider.InitAsync(); ISpecProvider specProvider = MainnetSpecProvider.Instance; _jsonRpcConfig = new JsonRpcConfig(); IEthereumEcdsa ethereumEcdsa = new EthereumEcdsa(specProvider.ChainId, LimboLogs.Instance); ITxStorage txStorage = new InMemoryTxStorage(); _stateDb = new MemDb(); ITrieStore trieStore = new TrieStore(_stateDb, LimboLogs.Instance); _stateProvider = new StateProvider(trieStore, new MemDb(), LimboLogs.Instance); _stateProvider.CreateAccount(TestItem.AddressA, 1000.Ether()); _stateProvider.CreateAccount(TestItem.AddressB, 1000.Ether()); _stateProvider.CreateAccount(TestItem.AddressC, 1000.Ether()); byte[] code = Bytes.FromHexString("0xabcd"); Keccak codeHash = Keccak.Compute(code); _stateProvider.UpdateCode(code); _stateProvider.UpdateCodeHash(TestItem.AddressA, codeHash, specProvider.GenesisSpec); IStorageProvider storageProvider = new StorageProvider(trieStore, _stateProvider, LimboLogs.Instance); storageProvider.Set(new StorageCell(TestItem.AddressA, UInt256.One), Bytes.FromHexString("0xabcdef")); storageProvider.Commit(); _stateProvider.Commit(specProvider.GenesisSpec); _stateProvider.CommitTree(0); IChainLevelInfoRepository chainLevels = new ChainLevelInfoRepository(dbProvider); IBlockTree blockTree = new BlockTree(dbProvider, chainLevels, specProvider, NullBloomStorage.Instance, LimboLogs.Instance); ITxPool txPool = new TxPool.TxPool(txStorage, ethereumEcdsa, new ChainHeadSpecProvider(specProvider, blockTree), new TxPoolConfig(), _stateProvider, new TxValidator(specProvider.ChainId), LimboLogs.Instance); IReceiptStorage receiptStorage = new InMemoryReceiptStorage(); VirtualMachine virtualMachine = new VirtualMachine(_stateProvider, storageProvider, new BlockhashProvider(blockTree, LimboLogs.Instance), specProvider, LimboLogs.Instance); TransactionProcessor txProcessor = new TransactionProcessor(specProvider, _stateProvider, storageProvider, virtualMachine, LimboLogs.Instance); IBlockProcessor blockProcessor = new BlockProcessor( specProvider, Always.Valid, new RewardCalculator(specProvider), txProcessor, _stateProvider, storageProvider, txPool, receiptStorage, NullWitnessCollector.Instance, LimboLogs.Instance); RecoverSignatures signatureRecovery = new RecoverSignatures(ethereumEcdsa, txPool, specProvider, LimboLogs.Instance); BlockchainProcessor blockchainProcessor = new BlockchainProcessor(blockTree, blockProcessor, signatureRecovery, LimboLogs.Instance, BlockchainProcessor.Options.Default); blockchainProcessor.Start(); ManualResetEventSlim resetEvent = new ManualResetEventSlim(false); blockTree.NewHeadBlock += (s, e) => { Console.WriteLine(e.Block.Header.Hash); if (e.Block.Number == 9) { resetEvent.Set(); } }; var genesisBlockBuilder = Build.A.Block.Genesis.WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f")); if (auRa) { genesisBlockBuilder.WithAura(0, new byte[65]); } Block genesis = genesisBlockBuilder.TestObject; blockTree.SuggestBlock(genesis); Block previousBlock = genesis; for (int i = 1; i < 10; i++) { List <Transaction> transactions = new List <Transaction>(); for (int j = 0; j < i; j++) { transactions.Add(Build.A.Transaction.WithNonce((UInt256)j).SignedAndResolved().TestObject); } BlockBuilder builder = Build.A.Block.WithNumber(i).WithParent(previousBlock).WithTransactions(MuirGlacier.Instance, transactions.ToArray()).WithStateRoot(new Keccak("0x1ef7300d8961797263939a3d29bbba4ccf1702fabf02d8ad7a20b454edb6fd2f")); if (auRa) { builder.WithAura(i, i.ToByteArray()); } Block block = builder.TestObject; blockTree.SuggestBlock(block); previousBlock = block; } ReceiptsRecovery receiptsRecovery = new ReceiptsRecovery(new EthereumEcdsa(specProvider.ChainId, LimboLogs.Instance), specProvider); IReceiptFinder receiptFinder = new FullInfoReceiptFinder(receiptStorage, receiptsRecovery, blockTree); resetEvent.Wait(2000); _traceModule = new TraceModule(receiptFinder, new Tracer(_stateProvider, blockchainProcessor), blockTree, _jsonRpcConfig); }
public BlockState ExposedTryOpenBlock(BlockProcessor blockProcessor) { return(TryOpenBlock(blockProcessor)); }
/// <summary> /// Closes a <typeparamref name="TProxy"/>, optionally returning a <typeparamref name="TMain"/> to replace it with. /// </summary> /// <param name="blockProcessor"> /// <para>The <see cref="BlockProcessor"/> processing the <typeparamref name="TProxy"/> to close. Never <c>null</c>.</para> /// </param> /// <param name="proxyBlock"> /// <para>The <typeparamref name="TProxy"/> to close. Never <c>null</c>.</para> /// </param> /// <returns> /// Returns <c>null</c> if the <typeparamref name="TProxy"/> should be discarded with no <typeparamref name="TMain"/> replacement. /// Returns a replacement <typeparamref name="TMain"/> if the <typeparamref name="TProxy"/> is to be replaced. /// </returns> protected virtual TMain CloseProxy(BlockProcessor blockProcessor, TProxy proxyBlock) { // Discard the proxy block with no replacement by default return(null); }
private bool TryParseFromLine(BlockProcessor processor, QuoteSectionNoteBlock block) { int originalColumn = processor.Column; block.QuoteType = QuoteSectionNoteType.MarkdownQuote; if (processor.CurrentChar != '[') { return(false); } var stringBuilder = StringBuilderCache.Local(); var c = processor.CurrentChar; var hasExcape = false; while (c != '\0' && (c != ']' || hasExcape)) { if (c == '\\' && !hasExcape) { hasExcape = true; } else { stringBuilder.Append(c); hasExcape = false; } c = processor.NextChar(); } stringBuilder.Append(c); var infoString = stringBuilder.ToString().Trim(); if (c == '\0') { processor.GoToColumn(originalColumn); return(false); } if (c == ']') { // "> [!NOTE] content" is invalid, go to end to see it. processor.NextChar(); while (processor.CurrentChar.IsSpaceOrTab()) { processor.NextChar(); } var isNoteVideoDiv = (infoString.StartsWith("[!div", StringComparison.OrdinalIgnoreCase)) || (infoString.StartsWith("[!Video", StringComparison.OrdinalIgnoreCase)) || IsNoteType(infoString); if (processor.CurrentChar != '\0' && isNoteVideoDiv) { Logger.LogWarning("Text in the first line of Note/Section/Video is not valid. Will be rendererd to <blockquote>"); processor.GoToColumn(originalColumn); return(false); } } if (IsNoteType(infoString)) { block.QuoteType = QuoteSectionNoteType.DFMNote; block.NoteTypeString = infoString.Substring(2, infoString.Length - 3); return(true); } if (infoString.StartsWith("[!div", StringComparison.OrdinalIgnoreCase)) { block.QuoteType = QuoteSectionNoteType.DFMSection; string attribute = infoString.Substring(5, infoString.Length - 6).Trim(); if (attribute.Length >= 2 && attribute.First() == '`' && attribute.Last() == '`') { block.SectionAttributeString = attribute.Substring(1, attribute.Length - 2).Trim(); } if (attribute.Length >= 1 && attribute.First() != '`' && attribute.Last() != '`') { block.SectionAttributeString = attribute; } return(true); } if (infoString.StartsWith("[!Video", StringComparison.OrdinalIgnoreCase)) { string link = infoString.Substring(7, infoString.Length - 8); if (link.StartsWith(" http://") || link.StartsWith(" https://")) { block.QuoteType = QuoteSectionNoteType.DFMVideo; block.VideoLink = link.Trim(); return(true); } } processor.GoToColumn(originalColumn); return(false); }
protected override CustomContainer CreateFencedBlock(BlockProcessor processor) { return(new CustomContainer(this)); }
public On CreateNode(PrivateKey privateKey, bool withGenesisAlreadyProcessed = false) { if (_logger.IsInfo) { _logger.Info($"CREATING NODE {privateKey.Address}"); } _logManagers[privateKey] = LimboLogs.Instance; // _logManagers[privateKey] = new OneLoggerLogManager(new ConsoleAsyncLogger(LogLevel.Debug, $"{privateKey.Address} ")); var nodeLogManager = _logManagers[privateKey]; AutoResetEvent newHeadBlockEvent = new AutoResetEvent(false); _blockEvents.Add(privateKey, newHeadBlockEvent); MemDb blocksDb = new MemDb(); MemDb headersDb = new MemDb(); MemDb blockInfoDb = new MemDb(); ISnapshotableDb stateDb = new StateDb(); ISnapshotableDb codeDb = new StateDb(); IDb traceDb = new MemDb(); ISpecProvider specProvider = RinkebySpecProvider.Instance; StateProvider stateProvider = new StateProvider(stateDb, codeDb, nodeLogManager); stateProvider.CreateAccount(TestItem.PrivateKeyD.Address, 100.Ether()); stateProvider.Commit(GoerliSpecProvider.Instance.GenesisSpec); TxPool txPool = new TxPool(new InMemoryTxStorage(), _timestamper, _ethereumEcdsa, GoerliSpecProvider.Instance, new TxPoolConfig(), stateProvider, _logManager); _pools[privateKey] = txPool; BlockTree blockTree = new BlockTree(blocksDb, headersDb, blockInfoDb, GoerliSpecProvider.Instance, txPool, nodeLogManager); blockTree.NewHeadBlock += (sender, args) => { _blockEvents[privateKey].Set(); }; BlockhashProvider blockhashProvider = new BlockhashProvider(blockTree, LimboLogs.Instance); _blockTrees.Add(privateKey, blockTree); IBasicWallet wallet = new BasicWallet(privateKey); SnapshotManager snapshotManager = new SnapshotManager(_cliqueConfig, blocksDb, blockTree, _ethereumEcdsa, nodeLogManager); _snapshotManager[privateKey] = snapshotManager; CliqueSealer cliqueSealer = new CliqueSealer(wallet, _cliqueConfig, snapshotManager, privateKey.Address, nodeLogManager); _genesis.StateRoot = _genesis3Validators.StateRoot = stateProvider.StateRoot; _genesis.Hash = BlockHeader.CalculateHash(_genesis.Header); _genesis3Validators.Hash = BlockHeader.CalculateHash(_genesis3Validators.Header); StorageProvider storageProvider = new StorageProvider(stateDb, stateProvider, nodeLogManager); TransactionProcessor transactionProcessor = new TransactionProcessor(GoerliSpecProvider.Instance, stateProvider, storageProvider, new VirtualMachine(stateProvider, storageProvider, blockhashProvider, specProvider, nodeLogManager), nodeLogManager); BlockProcessor blockProcessor = new BlockProcessor(GoerliSpecProvider.Instance, AlwaysValidBlockValidator.Instance, NoBlockRewards.Instance, transactionProcessor, stateDb, codeDb, traceDb, stateProvider, storageProvider, txPool, NullReceiptStorage.Instance, nodeLogManager); BlockchainProcessor processor = new BlockchainProcessor(blockTree, blockProcessor, new AuthorRecoveryStep(snapshotManager), nodeLogManager, false, false); processor.Start(); StateProvider minerStateProvider = new StateProvider(stateDb, codeDb, nodeLogManager); StorageProvider minerStorageProvider = new StorageProvider(stateDb, minerStateProvider, nodeLogManager); VirtualMachine minerVirtualMachine = new VirtualMachine(minerStateProvider, minerStorageProvider, blockhashProvider, specProvider, nodeLogManager); TransactionProcessor minerTransactionProcessor = new TransactionProcessor(GoerliSpecProvider.Instance, minerStateProvider, minerStorageProvider, minerVirtualMachine, nodeLogManager); BlockProcessor minerBlockProcessor = new BlockProcessor(GoerliSpecProvider.Instance, AlwaysValidBlockValidator.Instance, NoBlockRewards.Instance, minerTransactionProcessor, stateDb, codeDb, traceDb, minerStateProvider, minerStorageProvider, txPool, NullReceiptStorage.Instance, nodeLogManager); BlockchainProcessor minerProcessor = new BlockchainProcessor(blockTree, minerBlockProcessor, new AuthorRecoveryStep(snapshotManager), nodeLogManager, false, false); if (withGenesisAlreadyProcessed) { ProcessGenesis(privateKey); } CliqueBlockProducer blockProducer = new CliqueBlockProducer(txPool, minerProcessor, blockTree, _timestamper, new CryptoRandom(), minerStateProvider, snapshotManager, cliqueSealer, privateKey.Address, _cliqueConfig, nodeLogManager); blockProducer.Start(); _producers.Add(privateKey, blockProducer); return(this); }
public override bool TryParse(BlockProcessor state, char pendingBulletType, out ListInfo result) { result = new ListInfo(state.CurrentChar); state.NextChar(); return(true); }
private BlockState ParseRowSeparator(BlockProcessor state, GridTableState tableState, Table gridTable) { // A grid table must start with a line like this: // + ------------- + ------------ + ---------------------------------------- + // Spaces are optional var line = state.Line; var c = line.CurrentChar; bool isFirst = true; var delimiterChar = '\0'; while (true) { if (c == '+') { line.NextChar(); if (line.IsEmptyOrWhitespace()) { if (isFirst) { return(BlockState.None); } break; } TableColumnAlign align; if (TableHelper.ParseColumnHeaderDetect(ref line, ref delimiterChar, out align)) { isFirst = false; c = line.CurrentChar; continue; } } // If we have any other characters, this is an invalid line return(BlockState.None); } // If we have an header row var isHeader = delimiterChar == '='; // Terminate the current row TerminateLastRow(state, tableState, gridTable, false); // If we had a header row separator, we can mark all rows since last row separator // to be header rows if (isHeader) { for (int i = tableState.StartRowGroup; i < gridTable.Count; i++) { var row = (TableRow)gridTable[i]; row.IsHeader = true; } } // Makr the next start row group continue on the next row tableState.StartRowGroup = gridTable.Count; // We don't keep the line return(BlockState.ContinueDiscard); }
public override BlockState TryOpen(BlockProcessor processor) { // A grid table cannot start more than an indent if (processor.IsCodeIndent) { return(BlockState.None); } var line = processor.Line; // A grid table must start with a line like this: // + ------------- + ------------ + ---------------------------------------- + // Spaces are optional GridTableState tableState = null; var c = line.CurrentChar; var startPosition = processor.Start; while (true) { if (c == '+') { var startCharacter = line.Start; line.NextChar(); if (line.IsEmptyOrWhitespace()) { if (tableState == null) { return(BlockState.None); } break; } TableColumnAlign align; if (TableHelper.ParseColumnHeader(ref line, '-', out align)) { if (tableState == null) { tableState = new GridTableState() { Start = processor.Start, ExpectRow = true, }; } tableState.AddColumn(startCharacter - startPosition, line.Start - 1 - startPosition, align); c = line.CurrentChar; continue; } } // If we have any other characters, this is an invalid line return(BlockState.None); } // Store the line (if we need later to build a ParagraphBlock because the GridTable was in fact invalid) tableState.AddLine(ref processor.Line); // Create the grid table var table = new Table(this); table.SetData(typeof(GridTableState), tableState); // Calculate the total width of all columns int totalWidth = 0; foreach (var columnSlice in tableState.ColumnSlices) { totalWidth += columnSlice.End - columnSlice.Start; } // Store the column width and alignment foreach (var columnSlice in tableState.ColumnSlices) { var columnDefinition = new TableColumnDefinition { // Column width proportional to the total width Width = (float)(columnSlice.End - columnSlice.Start) * 100.0f / totalWidth, Alignment = columnSlice.Align, }; table.ColumnDefinitions.Add(columnDefinition); } processor.NewBlocks.Push(table); return(BlockState.ContinueDiscard); }
public override BlockState TryContinue(BlockProcessor processor, Block block) { var gridTable = (Table)block; var tableState = (GridTableState)block.GetData(typeof(GridTableState)); // We expect to start at the same //if (processor.Start == tableState.Start) { var columns = tableState.ColumnSlices; foreach (var columnSlice in columns) { columnSlice.PreviousColumnSpan = columnSlice.CurrentColumnSpan; columnSlice.CurrentColumnSpan = 0; } if (processor.CurrentChar == '+') { var result = ParseRowSeparator(processor, tableState, gridTable); if (result != BlockState.None) { return(result); } } else if (processor.CurrentChar == '|') { var line = processor.Line; // | ------------- | ------------ | ---------------------------------------- | // Calculate the colspan for the new row int columnIndex = -1; foreach (var columnSlice in columns) { if (line.PeekCharExtra(columnSlice.Start) == '|') { columnIndex++; } if (columnIndex >= 0) { columns[columnIndex].CurrentColumnSpan++; } } // Check if the colspan of the current row is the same than the previous row bool continueRow = true; foreach (var columnSlice in columns) { if (columnSlice.PreviousColumnSpan != columnSlice.CurrentColumnSpan) { continueRow = false; break; } } // If the current row doesn't continue the previous row (col span are different) // Close the previous row if (!continueRow) { TerminateLastRow(processor, tableState, gridTable, false); } for (int i = 0; i < columns.Count;) { var column = columns[i]; var nextColumnIndex = i + column.CurrentColumnSpan; // If the span is 0, we exit if (nextColumnIndex == i) { break; } var nextColumn = nextColumnIndex < columns.Count ? columns[nextColumnIndex] : null; var sliceForCell = line; sliceForCell.Start = line.Start + column.Start + 1; if (nextColumn != null) { sliceForCell.End = line.Start + nextColumn.Start - 1; } else { var columnEnd = columns[columns.Count - 1].End; // If there is a `|` exactly at the expected end of the table row, we cut the line // otherwise we allow to have the last cell of a row to be open for longer cell content if (line.PeekCharExtra(columnEnd + 1) == '|') { sliceForCell.End = line.Start + columnEnd; } } sliceForCell.TrimEnd(); // Process the content of the cell column.BlockProcessor.LineIndex = processor.LineIndex; column.BlockProcessor.ProcessLine(sliceForCell); // Go to next column i = nextColumnIndex; } return(BlockState.ContinueDiscard); } } TerminateLastRow(processor, tableState, gridTable, true); // If we don't have a row, it means that only the header was valid // So we need to remove the grid table, and create a ParagraphBlock // with the 2 slices if (gridTable.Count == 0) { var parser = processor.Parsers.FindExact <ParagraphBlockParser>(); // Discard the grid table var parent = gridTable.Parent; processor.Discard(gridTable); var paragraphBlock = new ParagraphBlock(parser) { Lines = tableState.Lines, }; parent.Add(paragraphBlock); processor.Open(paragraphBlock); } return(BlockState.Break); }
/// <summary> /// Opens a <see cref="ProxyLeafBlock"/> if a non-blank line begins with 4 or more spaces. /// </summary> /// <param name="blockProcessor">The <see cref="BlockProcessor"/> for the document that contains a non-blank line that begins with 4 or more spaces.</param> /// <returns> /// <see cref="BlockState.None"/> if the current line is a blank line. /// <see cref="BlockState.None"/> if the current line does not have code indent. /// <see cref="BlockState.Continue"/> if a <see cref="ProxyLeafBlock"/> is opened. /// </returns> protected override BlockState TryOpenBlock(BlockProcessor blockProcessor) { return(ParseLine(blockProcessor, null)); }
public void CreateWithNoBlockChain() { BlockProcessor processor = new BlockProcessor(); Assert.IsNull(processor.BlockChain); }
/// <summary> /// Continues a <see cref="ProxyLeafBlock"/> if the current line has code indent or is blank. /// </summary> /// <param name="blockProcessor">The <see cref="BlockProcessor"/> for the <see cref="ProxyLeafBlock"/> to try continuing.</param> /// <param name="block">The <see cref="ProxyLeafBlock"/> to try continuing.</param> /// <returns> /// <see cref="BlockState.None"/> if the current line does not have code indent and is not blank. This closes the <see cref="ProxyLeafBlock"/>. /// <see cref="BlockState.Continue"/> if the <see cref="ProxyLeafBlock"/> remains open. /// </returns> protected override BlockState TryContinueBlock(BlockProcessor blockProcessor, ProxyLeafBlock block) { return(ParseLine(blockProcessor, block)); }
public override BlockState TryContinue(BlockProcessor processor, Block block) { var definitionItem = (DefinitionItem)block; if (processor.IsCodeIndent) { processor.GoToCodeIndent(); return(BlockState.Continue); } var list = (DefinitionList)definitionItem.Parent; var lastBlankLine = definitionItem.LastChild as BlankLineBlock; // Check if we have another definition list if (Array.IndexOf(OpeningCharacters, processor.CurrentChar) >= 0) { var startPosition = processor.Start; var column = processor.ColumnBeforeIndent; processor.NextChar(); processor.ParseIndent(); var delta = processor.Column - column; // We expect to have a least if (delta < 4) { // Remove the blankline before breaking this definition item if (lastBlankLine != null) { definitionItem.RemoveAt(definitionItem.Count - 1); } list.Span.End = list.LastChild.Span.End; return(BlockState.None); } if (delta > 4) { processor.GoToColumn(column + 4); } processor.Close(definitionItem); var nextDefinitionItem = new DefinitionItem(this) { Span = new SourceSpan(startPosition, processor.Line.End), Line = processor.LineIndex, Column = processor.Column, OpeningCharacter = processor.CurrentChar, }; list.Add(nextDefinitionItem); processor.Open(nextDefinitionItem); return(BlockState.Continue); } var isBreakable = definitionItem.LastChild?.IsBreakable ?? true; if (processor.IsBlankLine) { if (lastBlankLine == null && isBreakable) { definitionItem.Add(new BlankLineBlock()); } return(isBreakable ? BlockState.ContinueDiscard : BlockState.Continue); } var paragraphBlock = definitionItem.LastChild as ParagraphBlock; if (lastBlankLine == null && paragraphBlock != null) { return(BlockState.Continue); } // Remove the blankline before breaking this definition item if (lastBlankLine != null) { definitionItem.RemoveAt(definitionItem.Count - 1); } list.Span.End = list.LastChild.Span.End; return(BlockState.Break); }
public BlockState ExposedTryContinueBlock(BlockProcessor blockProcessor, ProxyTableBlock proxyTableBlock) { return(TryContinueBlock(blockProcessor, proxyTableBlock)); }
public override BlockState TryOpen(BlockProcessor processor) { var paragraphBlock = processor.LastBlock as ParagraphBlock; if (processor.IsCodeIndent || paragraphBlock == null || paragraphBlock.LastLine - processor.LineIndex > 1) { return(BlockState.None); } var startPosition = processor.Start; var column = processor.ColumnBeforeIndent; processor.NextChar(); processor.ParseIndent(); var delta = processor.Column - column; // We expect to have a least if (delta < 4) { // Return back to original position processor.GoToColumn(column); return(BlockState.None); } if (delta > 4) { processor.GoToColumn(column + 4); } var previousParent = paragraphBlock.Parent; var indexOfParagraph = previousParent.IndexOf(paragraphBlock); var currentDefinitionList = indexOfParagraph - 1 >= 0 ? previousParent[indexOfParagraph - 1] as DefinitionList : null; processor.Discard(paragraphBlock); // If the paragraph block was not part of the opened blocks, we need to remove it manually from its parent container if (paragraphBlock.Parent != null) { paragraphBlock.Parent.Remove(paragraphBlock); } if (currentDefinitionList == null) { currentDefinitionList = new DefinitionList(this) { Span = new SourceSpan(paragraphBlock.Span.Start, processor.Line.End), Column = paragraphBlock.Column, Line = paragraphBlock.Line, }; previousParent.Add(currentDefinitionList); } var definitionItem = new DefinitionItem(this) { Line = processor.LineIndex, Column = column, Span = new SourceSpan(startPosition, processor.Line.End), OpeningCharacter = processor.CurrentChar, }; for (int i = 0; i < paragraphBlock.Lines.Count; i++) { var line = paragraphBlock.Lines.Lines[i]; var term = new DefinitionTerm(this) { Column = paragraphBlock.Column, Line = line.Line, Span = new SourceSpan(paragraphBlock.Span.Start, paragraphBlock.Span.End), IsOpen = false }; term.AppendLine(ref line.Slice, line.Column, line.Line, line.Position); definitionItem.Add(term); } currentDefinitionList.Add(definitionItem); processor.Open(definitionItem); // Update the end position currentDefinitionList.UpdateSpanEnd(processor.Line.End); return(BlockState.Continue); }
public bool TryProcessAttributes(IDictionary <string, string> attributes, out HtmlAttributes htmlAttributes, out IDictionary <string, string> renderProperties, Action <string> logError, BlockProcessor processor) { htmlAttributes = null; renderProperties = new Dictionary <string, string>(); var source = string.Empty; var range = string.Empty; var id = string.Empty; var highlight = string.Empty; var language = string.Empty; var interactive = string.Empty; foreach (var attribute in attributes) { var name = attribute.Key; var value = attribute.Value; switch (name) { case "source": source = value; break; case "range": range = value; break; case "id": id = value; break; case "highlight": highlight = value; break; case "language": language = value; break; case "interactive": interactive = value; break; default: logError($"Unexpected attribute \"{name}\"."); return(false); } } if (string.IsNullOrEmpty(source)) { logError($"source is a required attribute. Please ensure you have specified a source attribute"); return(false); } if (string.IsNullOrEmpty(language)) { language = InferLanguageFromFile(source, logError); } htmlAttributes = new HtmlAttributes(); htmlAttributes.AddProperty("class", $"lang-{language}"); if (!string.IsNullOrEmpty(interactive)) { htmlAttributes.AddProperty("data-interactive", language); htmlAttributes.AddProperty("data-interactive-mode", interactive); } if (!string.IsNullOrEmpty(highlight)) { htmlAttributes.AddProperty("highlight-lines", highlight); } RenderDelegate = (renderer, obj) => { var currentId = string.Empty; var currentRange = string.Empty; var currentSource = string.Empty; obj.Attributes.TryGetValue("id", out currentId); //it's okay if this is null obj.Attributes.TryGetValue("range", out currentRange); //it's okay if this is null obj.Attributes.TryGetValue("source", out currentSource); //source has already been checked above var(code, codePath) = _context.ReadFile(currentSource, InclusionContext.File, obj); if (string.IsNullOrEmpty(code)) { logError($"The code snippet \"{source}\" could not be found."); return(false); } var updatedCode = GetCodeSnippet(currentRange, currentId, code, logError).TrimEnd(); if (updatedCode == string.Empty) { return(false); } renderer.WriteLine("<pre>"); renderer.Write("<code").WriteAttributes(obj).Write(">"); renderer.WriteLine(updatedCode); renderer.WriteLine("</code></pre>"); return(true); }; return(true); }
/// <inheritdoc /> public FlexiSectionHeadingBlock Create(BlockProcessor blockProcessor, IFlexiSectionBlockOptions flexiSectionBlockOptions, BlockParser blockParser) { // Create int lineIndex = blockProcessor.LineIndex; int column = blockProcessor.Column; ref StringSlice line = ref blockProcessor.Line;
// Start the node public void start(bool verboseConsoleOutput) { char node_type = 'W'; // Check if we're in worker-only mode if (Config.workerOnly) { CoreConfig.simultaneousConnectedNeighbors = 4; } // Generate presence list PresenceList.init(IxianHandler.publicIP, Config.serverPort, node_type); ActivityStorage.prepareStorage(); // Initialize the block chain blockChain = new BlockChain(); //runDiffTests(); //return; // Create the block processor and sync blockProcessor = new BlockProcessor(); blockSync = new BlockSync(); if (Config.devInsertFromJson) { Console.WriteLine("Inserting from JSON"); devInsertFromJson(); Program.noStart = true; return; } if (Config.apiBinds.Count == 0) { Config.apiBinds.Add("http://localhost:" + Config.apiPort + "/"); } // Start the HTTP JSON API server apiServer = new APIServer(Config.apiBinds, Config.apiUsers, Config.apiAllowedIps); if (IXICore.Platform.onMono() == false && !Config.disableWebStart) { System.Diagnostics.Process.Start(Config.apiBinds[0]); } miner = new Miner(); // Start the network queue NetworkQueue.start(); // prepare stats screen ConsoleHelpers.verboseConsoleOutput = verboseConsoleOutput; Logging.consoleOutput = verboseConsoleOutput; Logging.flush(); if (ConsoleHelpers.verboseConsoleOutput == false) { statsConsoleScreen.clearScreen(); } // Distribute genesis funds IxiNumber genesisFunds = new IxiNumber(Config.genesisFunds); // Check if this is a genesis node if (genesisFunds > (long)0) { Logging.info(String.Format("Genesis {0} specified. Starting operation.", genesisFunds)); distributeGenesisFunds(genesisFunds); CoreNetworkUtils.seedTestNetNodes = new List <string[]>(); CoreNetworkUtils.seedNodes = new List <string[]>(); genesisNode = true; PresenceList.myPresenceType = 'M'; blockProcessor.resumeOperation(); serverStarted = true; if (!isMasterNode()) { Logging.info("Network server is not enabled in modes other than master node."); } else { NetworkServer.beginNetworkOperations(); } } else { if (File.Exists(Config.genesisFile)) { Block genesis = new Block(Crypto.stringToHash(File.ReadAllText(Config.genesisFile))); blockChain.setGenesisBlock(genesis); } ulong lastLocalBlockNum = storage.getHighestBlockInStorage(); if (lastLocalBlockNum > 6) { lastLocalBlockNum = lastLocalBlockNum - 6; } if (Config.lastGoodBlock > 0 && Config.lastGoodBlock < lastLocalBlockNum) { lastLocalBlockNum = Config.lastGoodBlock; } if (lastLocalBlockNum > 0) { Block b = blockChain.getBlock(lastLocalBlockNum, true); if (b != null) { ConsensusConfig.minRedactedWindowSize = ConsensusConfig.getRedactedWindowSize(b.version); ConsensusConfig.redactedWindowSize = ConsensusConfig.getRedactedWindowSize(b.version); } } // Start block sync ulong blockNum = WalletStateStorage.restoreWalletState(lastLocalBlockNum); if (blockNum > 0) { Block b = blockChain.getBlock(blockNum, true); if (b != null) { blockSync.onHelloDataReceived(blockNum, b.blockChecksum, b.version, b.walletStateChecksum, b.getSignatureCount(), lastLocalBlockNum); } else { walletState.clear(); } } else { blockSync.lastBlockToReadFromStorage = lastLocalBlockNum; walletState.clear(); if (CoreConfig.preventNetworkOperations) { Block b = storage.getBlock(lastLocalBlockNum); blockSync.onHelloDataReceived(b.blockNum, b.blockChecksum, b.version, b.walletStateChecksum, b.getSignatureCount(), lastLocalBlockNum); } } // Start the server for ping purposes serverStarted = true; if (!isMasterNode()) { Logging.info("Network server is not enabled in modes other than master node."); } else { NetworkServer.beginNetworkOperations(); } // Start the network client manager if (Config.recoverFromFile) { NetworkClientManager.start(0); } else { NetworkClientManager.start(1); } } PresenceList.startKeepAlive(); TLC = new ThreadLiveCheck(); // Start the maintenance thread maintenanceThread = new Thread(performMaintenance); maintenanceThread.Name = "Node_Maintenance_Thread"; maintenanceThread.Start(); }
// It takes dotCover to run it quite long, increased timeouts public async Task Can_process_mined_blocks() { int timeMultiplier = 1; // for debugging TimeSpan miningDelay = TimeSpan.FromMilliseconds(200 * timeMultiplier); /* logging & instrumentation */ // OneLoggerLogManager logger = new OneLoggerLogManager(new SimpleConsoleLogger(true)); ILogManager logManager = LimboLogs.Instance; ILogger logger = logManager.GetClassLogger(); /* spec */ FakeSealer sealer = new FakeSealer(TestItem.AddressA, miningDelay); RopstenSpecProvider specProvider = RopstenSpecProvider.Instance; /* state & storage */ StateDb codeDb = new StateDb(); StateDb stateDb = new StateDb(); StateProvider stateProvider = new StateProvider(stateDb, codeDb, logManager); StorageProvider storageProvider = new StorageProvider(stateDb, stateProvider, logManager); StateReader stateReader = new StateReader(stateDb, codeDb, logManager); /* store & validation */ EthereumEcdsa ecdsa = new EthereumEcdsa(specProvider.ChainId, logManager); MemColumnsDb <ReceiptsColumns> receiptsDb = new MemColumnsDb <ReceiptsColumns>(); TxPool.TxPool txPool = new TxPool.TxPool(NullTxStorage.Instance, Timestamper.Default, ecdsa, specProvider, new TxPoolConfig(), stateProvider, logManager); IReceiptStorage receiptStorage = new PersistentReceiptStorage(receiptsDb, specProvider, new ReceiptsRecovery()); var blockInfoDb = new MemDb(); BlockTree blockTree = new BlockTree(new MemDb(), new MemDb(), blockInfoDb, new ChainLevelInfoRepository(blockInfoDb), specProvider, txPool, NullBloomStorage.Instance, logManager); ITimestamper timestamper = Timestamper.Default; DifficultyCalculator difficultyCalculator = new DifficultyCalculator(specProvider); HeaderValidator headerValidator = new HeaderValidator(blockTree, sealer, specProvider, logManager); OmmersValidator ommersValidator = new OmmersValidator(blockTree, headerValidator, logManager); TxValidator txValidator = new TxValidator(specProvider.ChainId); BlockValidator blockValidator = new BlockValidator(txValidator, headerValidator, ommersValidator, specProvider, logManager); TestTransactionsGenerator generator = new TestTransactionsGenerator(txPool, ecdsa, TimeSpan.FromMilliseconds(50 * timeMultiplier), LimboLogs.Instance); generator.Start(); /* blockchain processing */ BlockhashProvider blockhashProvider = new BlockhashProvider(blockTree, LimboLogs.Instance); VirtualMachine virtualMachine = new VirtualMachine(stateProvider, storageProvider, blockhashProvider, specProvider, logManager); TransactionProcessor processor = new TransactionProcessor(specProvider, stateProvider, storageProvider, virtualMachine, logManager); RewardCalculator rewardCalculator = new RewardCalculator(specProvider); BlockProcessor blockProcessor = new BlockProcessor(specProvider, blockValidator, rewardCalculator, processor, stateDb, codeDb, stateProvider, storageProvider, txPool, receiptStorage, logManager); BlockchainProcessor blockchainProcessor = new BlockchainProcessor(blockTree, blockProcessor, new TxSignaturesRecoveryStep(ecdsa, NullTxPool.Instance, LimboLogs.Instance), logManager, BlockchainProcessor.Options.NoReceipts); /* load ChainSpec and init */ ChainSpecLoader loader = new ChainSpecLoader(new EthereumJsonSerializer()); string path = "chainspec.json"; logManager.GetClassLogger().Info($"Loading ChainSpec from {path}"); ChainSpec chainSpec = loader.Load(File.ReadAllText(path)); foreach (var allocation in chainSpec.Allocations) { stateProvider.CreateAccount(allocation.Key, allocation.Value.Balance); if (allocation.Value.Code != null) { Keccak codeHash = stateProvider.UpdateCode(allocation.Value.Code); stateProvider.UpdateCodeHash(allocation.Key, codeHash, specProvider.GenesisSpec); } } stateProvider.Commit(specProvider.GenesisSpec); chainSpec.Genesis.Header.StateRoot = stateProvider.StateRoot; chainSpec.Genesis.Header.Hash = chainSpec.Genesis.Header.CalculateHash(); if (chainSpec.Genesis.Hash != new Keccak("0xafbc3c327d2d18ff2b843e89226ef288fcee379542f854f982e4cfb85916d126")) { throw new Exception("Unexpected genesis hash"); } /* start processing */ blockTree.SuggestBlock(chainSpec.Genesis); blockchainProcessor.Start(); var transactionSelector = new TxPoolTxSource(txPool, stateReader, logManager); MinedBlockProducer minedBlockProducer = new MinedBlockProducer(transactionSelector, blockchainProcessor, sealer, blockTree, blockchainProcessor, stateProvider, timestamper, LimboLogs.Instance, difficultyCalculator); minedBlockProducer.Start(); ManualResetEventSlim manualResetEvent = new ManualResetEventSlim(false); blockTree.NewHeadBlock += (sender, args) => { if (args.Block.Number == 6) { manualResetEvent.Set(); } }; manualResetEvent.Wait(miningDelay * 100); await minedBlockProducer.StopAsync(); int previousCount = 0; int totalTx = 0; for (int i = 0; i < 6; i++) { Block block = blockTree.FindBlock(i, BlockTreeLookupOptions.None); Assert.That(block, Is.Not.Null, $"Block {i} not produced"); logger.Info($"Block {i} with {block.Transactions.Length} txs"); ManualResetEventSlim blockProcessedEvent = new ManualResetEventSlim(false); blockchainProcessor.ProcessingQueueEmpty += (sender, args) => blockProcessedEvent.Set(); blockchainProcessor.Enqueue(block, ProcessingOptions.ForceProcessing | ProcessingOptions.StoreReceipts | ProcessingOptions.ReadOnlyChain); blockProcessedEvent.Wait(miningDelay); GethStyleTracer gethStyleTracer = new GethStyleTracer(blockchainProcessor, receiptStorage, blockTree); int currentCount = receiptsDb.Keys.Count; logger.Info($"Current count of receipts {currentCount}"); logger.Info($"Previous count of receipts {previousCount}"); if (block.Transactions.Length > 0) { Assert.AreNotEqual(previousCount, currentCount, $"receipts at block {i}"); totalTx += block.Transactions.Length; } previousCount = currentCount; } Assert.AreNotEqual(0, totalTx, "no tx in blocks"); }
public void Test() { ISpecProvider specProvider = MainnetSpecProvider.Instance; DbProvider dbProvider = new DbProvider(DbModeHint.Mem); dbProvider.RegisterDb(DbNames.BlockInfos, new MemDb()); dbProvider.RegisterDb(DbNames.Blocks, new MemDb()); dbProvider.RegisterDb(DbNames.Headers, new MemDb()); dbProvider.RegisterDb(DbNames.State, new MemDb()); dbProvider.RegisterDb(DbNames.Code, new MemDb()); BlockTree blockTree = new BlockTree( dbProvider, new ChainLevelInfoRepository(dbProvider), specProvider, NullBloomStorage.Instance, LimboLogs.Instance); TrieStore trieStore = new TrieStore( dbProvider.RegisteredDbs[DbNames.State], NoPruning.Instance, Archive.Instance, LimboLogs.Instance); StateProvider stateProvider = new StateProvider( trieStore, dbProvider.RegisteredDbs[DbNames.Code], LimboLogs.Instance); StorageProvider storageProvider = new StorageProvider(trieStore, stateProvider, LimboLogs.Instance); BlockhashProvider blockhashProvider = new BlockhashProvider(blockTree, LimboLogs.Instance); VirtualMachine virtualMachine = new VirtualMachine( stateProvider, storageProvider, blockhashProvider, specProvider, LimboLogs.Instance); TransactionProcessor txProcessor = new TransactionProcessor( specProvider, stateProvider, storageProvider, virtualMachine, LimboLogs.Instance); BlockProcessor blockProcessor = new BlockProcessor( specProvider, Always.Valid, NoBlockRewards.Instance, txProcessor, stateProvider, storageProvider, NullTxPool.Instance, NullReceiptStorage.Instance, NullWitnessCollector.Instance, LimboLogs.Instance); BlockchainProcessor blockchainProcessor = new BlockchainProcessor( blockTree, blockProcessor, NullRecoveryStep.Instance, LimboLogs.Instance, BlockchainProcessor.Options.Default); BuildBlocksWhenRequested trigger = new BuildBlocksWhenRequested(); var timestamper = new ManualTimestamper(); DevBlockProducer devBlockProducer = new DevBlockProducer( EmptyTxSource.Instance, blockchainProcessor, stateProvider, blockTree, blockchainProcessor, trigger, timestamper, specProvider, new MiningConfig { Enabled = true }, LimboLogs.Instance); blockchainProcessor.Start(); devBlockProducer.Start(); AutoResetEvent autoResetEvent = new AutoResetEvent(false); blockTree.NewHeadBlock += (s, e) => autoResetEvent.Set(); blockTree.SuggestBlock(Build.A.Block.Genesis.TestObject); autoResetEvent.WaitOne(1000).Should().BeTrue("genesis"); trigger.BuildBlock(); autoResetEvent.WaitOne(1000).Should().BeTrue("1"); blockTree.Head.Number.Should().Be(1); }
public void Setup() { IDbProvider memDbProvider = TestMemDbProvider.Init(); TrieStore trieStore = new TrieStore(new MemDb(), LimboLogs.Instance); StateProvider stateProvider = new StateProvider(trieStore, memDbProvider.CodeDb, LimboLogs.Instance); StorageProvider storageProvider = new StorageProvider(trieStore, stateProvider, LimboLogs.Instance); ChainLevelInfoRepository chainLevelInfoRepository = new ChainLevelInfoRepository(memDbProvider); ISpecProvider specProvider = MainnetSpecProvider.Instance; IBloomStorage bloomStorage = NullBloomStorage.Instance; EthereumEcdsa ecdsa = new EthereumEcdsa(1, LimboLogs.Instance); ITransactionComparerProvider transactionComparerProvider = new TransactionComparerProvider(specProvider, _blockTree); _blockTree = new BlockTree( memDbProvider, chainLevelInfoRepository, specProvider, bloomStorage, new SyncConfig(), LimboLogs.Instance); TxPool.TxPool txPool = new TxPool.TxPool( NullTxStorage.Instance, ecdsa, new ChainHeadInfoProvider(specProvider, _blockTree, stateProvider), new TxPoolConfig(), new TxValidator(specProvider.ChainId), LimboLogs.Instance, transactionComparerProvider.GetDefaultComparer()); BlockhashProvider blockhashProvider = new BlockhashProvider(_blockTree, LimboLogs.Instance); VirtualMachine virtualMachine = new VirtualMachine( stateProvider, storageProvider, blockhashProvider, specProvider, LimboLogs.Instance); TransactionProcessor transactionProcessor = new TransactionProcessor( specProvider, stateProvider, storageProvider, virtualMachine, LimboLogs.Instance); BlockProcessor blockProcessor = new BlockProcessor( MainnetSpecProvider.Instance, Always.Valid, new RewardCalculator(specProvider), transactionProcessor, stateProvider, storageProvider, txPool, NullReceiptStorage.Instance, new WitnessCollector(memDbProvider.StateDb, LimboLogs.Instance), LimboLogs.Instance); _blockchainProcessor = new BlockchainProcessor( _blockTree, blockProcessor, new RecoverSignatures( ecdsa, txPool, specProvider, LimboLogs.Instance), LimboLogs.Instance, BlockchainProcessor.Options.Default); }
public void Setup() { _processor = new BlockProcessor(); }
/// <inheritdoc /> public override bool CanInterrupt(BlockProcessor processor, Block block) { return(!(block is ParagraphBlock)); // Indented code blocks can't interrupt paragraph blocks }
public override bool CanInterrupt(BlockProcessor processor, Block block) { return(true); }
public async Task Test() { TimeSpan miningDelay = TimeSpan.FromMilliseconds(50); /* logging & instrumentation */ var logger = new OneLoggerLogManager(new SimpleConsoleLogger()); /* spec */ var sealEngine = new FakeSealEngine(miningDelay); sealEngine.IsMining = true; var specProvider = RopstenSpecProvider.Instance; /* store & validation */ var blockTree = new BlockTree(new MemDb(), new MemDb(), new MemDb(), specProvider, logger); var difficultyCalculator = new DifficultyCalculator(specProvider); var headerValidator = new HeaderValidator(difficultyCalculator, blockTree, sealEngine, specProvider, logger); var ommersValidator = new OmmersValidator(blockTree, headerValidator, logger); var transactionValidator = new TransactionValidator(new SignatureValidator(ChainId.Ropsten)); var blockValidator = new BlockValidator(transactionValidator, headerValidator, ommersValidator, specProvider, logger); /* state & storage */ var codeDb = new MemDb(); var stateDb = new MemDb(); var stateTree = new StateTree(stateDb); var stateProvider = new StateProvider(stateTree, codeDb, logger); var storageDbProvider = new MemDbProvider(logger); var storageProvider = new StorageProvider(storageDbProvider, stateProvider, logger); /* blockchain processing */ var ethereumSigner = new EthereumSigner(specProvider, logger); var transactionStore = new TransactionStore(); var blockhashProvider = new BlockhashProvider(blockTree); var virtualMachine = new VirtualMachine(stateProvider, storageProvider, blockhashProvider, logger); var processor = new TransactionProcessor(specProvider, stateProvider, storageProvider, virtualMachine, NullTracer.Instance, logger); var rewardCalculator = new RewardCalculator(specProvider); var blockProcessor = new BlockProcessor(specProvider, blockValidator, rewardCalculator, processor, storageDbProvider, stateProvider, storageProvider, transactionStore, logger); var blockchainProcessor = new BlockchainProcessor(blockTree, sealEngine, transactionStore, difficultyCalculator, blockProcessor, ethereumSigner, logger); /* load ChainSpec and init */ ChainSpecLoader loader = new ChainSpecLoader(new UnforgivingJsonSerializer()); string path = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\Chains", "ropsten.json")); logger.GetClassLogger().Info($"Loading ChainSpec from {path}"); ChainSpec chainSpec = loader.Load(File.ReadAllBytes(path)); foreach (KeyValuePair <Address, BigInteger> allocation in chainSpec.Allocations) { stateProvider.CreateAccount(allocation.Key, allocation.Value); } stateProvider.Commit(specProvider.GenesisSpec); chainSpec.Genesis.Header.StateRoot = stateProvider.StateRoot; // TODO: shall it be HeaderSpec and not BlockHeader? chainSpec.Genesis.Header.Hash = BlockHeader.CalculateHash(chainSpec.Genesis.Header); if (chainSpec.Genesis.Hash != new Keccak("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")) { throw new Exception("Unexpected genesis hash"); } /* start processing */ blockTree.SuggestBlock(chainSpec.Genesis); blockchainProcessor.Start(); Thread.Sleep(miningDelay * 10); await blockchainProcessor.StopAsync(true).ContinueWith( t => { if (t.IsFaulted) { throw t.Exception; } Assert.GreaterOrEqual((int)blockTree.Head.Number, 6); }); }