Example #1
0
 private ITxFilter CreateAuraTxFilterForProducer(IReadOnlyTxProcessorSource readOnlyTxProcessorSource, ISpecProvider specProvider) =>
 TxAuRaFilterBuilders.CreateAuRaTxFilterForProducer(
     NethermindApi.Config <IMiningConfig>(),
     _api,
     readOnlyTxProcessorSource,
     _minGasPricesContractDataStore,
     specProvider);
        protected override TxPool.TxPool CreateTxPool()
        {
            // This has to be different object than the _processingReadOnlyTransactionProcessorSource as this is in separate thread
            var txPoolReadOnlyTransactionProcessorSource = CreateReadOnlyTransactionProcessorSource();

            var(txPriorityContract, localDataSource) = TxAuRaFilterBuilders.CreateTxPrioritySources(_auraConfig, _api, txPoolReadOnlyTransactionProcessorSource !);

            ReportTxPriorityRules(txPriorityContract, localDataSource);

            var minGasPricesContractDataStore = TxAuRaFilterBuilders.CreateMinGasPricesDataStore(_api, txPriorityContract, localDataSource);

            ITxFilter txPoolFilter = TxAuRaFilterBuilders.CreateAuRaTxFilterForProducer(
                NethermindApi.Config <IMiningConfig>(),
                _api,
                txPoolReadOnlyTransactionProcessorSource,
                minGasPricesContractDataStore,
                _api.SpecProvider);

            return(new TxPool.TxPool(
                       _api.EthereumEcdsa,
                       new ChainHeadInfoProvider(_api.SpecProvider, _api.BlockTree, _api.StateReader),
                       NethermindApi.Config <ITxPoolConfig>(),
                       _api.TxValidator,
                       _api.LogManager,
                       CreateTxPoolTxComparer(txPriorityContract, localDataSource),
                       new TxFilterAdapter(_api.BlockTree, txPoolFilter, _api.LogManager)));
        }
Example #3
0
        private TxPoolTxSource CreateTxPoolTxSource(ReadOnlyTxProcessingEnv processingEnv, IReadOnlyTxProcessorSource readOnlyTxProcessorSource)
        {
            // We need special one for TxPriority as its following Head separately with events and we want rules from Head, not produced block
            IReadOnlyTxProcessorSource readOnlyTxProcessorSourceForTxPriority =
                new ReadOnlyTxProcessingEnv(_api.DbProvider, _api.ReadOnlyTrieStore, _api.BlockTree, _api.SpecProvider, _api.LogManager);

            (_txPriorityContract, _localDataSource) = TxAuRaFilterBuilders.CreateTxPrioritySources(_auraConfig, _api, readOnlyTxProcessorSourceForTxPriority);

            if (_txPriorityContract != null || _localDataSource != null)
            {
                _minGasPricesContractDataStore = TxAuRaFilterBuilders.CreateMinGasPricesDataStore(_api, _txPriorityContract, _localDataSource) !;
                _api.DisposeStack.Push(_minGasPricesContractDataStore);

                ContractDataStore <Address> whitelistContractDataStore = new ContractDataStoreWithLocalData <Address>(
                    new HashSetContractDataStoreCollection <Address>(),
                    _txPriorityContract?.SendersWhitelist,
                    _api.BlockTree,
                    _api.ReceiptFinder,
                    _api.LogManager,
                    _localDataSource?.GetWhitelistLocalDataSource() ?? new EmptyLocalDataSource <IEnumerable <Address> >());

                DictionaryContractDataStore <TxPriorityContract.Destination> prioritiesContractDataStore =
                    new DictionaryContractDataStore <TxPriorityContract.Destination>(
                        new TxPriorityContract.DestinationSortedListContractDataStoreCollection(),
                        _txPriorityContract?.Priorities,
                        _api.BlockTree,
                        _api.ReceiptFinder,
                        _api.LogManager,
                        _localDataSource?.GetPrioritiesLocalDataSource());

                _api.DisposeStack.Push(whitelistContractDataStore);
                _api.DisposeStack.Push(prioritiesContractDataStore);

                ITxFilter auraTxFilter =
                    CreateAuraTxFilterForProducer(readOnlyTxProcessorSource, _api.SpecProvider);
                ITxFilterPipeline txFilterPipeline = new TxFilterPipelineBuilder(_api.LogManager)
                                                     .WithCustomTxFilter(auraTxFilter)
                                                     .WithBaseFeeFilter(_api.SpecProvider)
                                                     .WithNullTxFilter()
                                                     .Build;


                return(new TxPriorityTxSource(
                           _api.TxPool,
                           processingEnv.StateReader,
                           _api.LogManager,
                           txFilterPipeline,
                           whitelistContractDataStore,
                           prioritiesContractDataStore,
                           _api.SpecProvider,
                           _api.TransactionComparerProvider));
            }
            else
            {
                return(CreateStandardTxPoolTxSource(processingEnv, readOnlyTxProcessorSource));
            }
        }
Example #4
0
        private ITxSource CreateTxSourceForProducer(ReadOnlyTxProcessingEnv processingEnv, IReadOnlyTxProcessorSource readOnlyTxProcessorSource, ITxSource?additionalTxSource)
        {
            bool CheckAddPosdaoTransactions(IList <ITxSource> list, long auRaPosdaoTransition)
            {
                if (auRaPosdaoTransition < AuRaParameters.TransitionDisabled && _validator is ITxSource validatorSource)
                {
                    list.Insert(0, validatorSource);
                    return(true);
                }

                return(false);
            }

            bool CheckAddRandomnessTransactions(IList <ITxSource> list, IDictionary <long, Address>?randomnessContractAddress, ISigner signer)
            {
                IList <IRandomContract> GetRandomContracts(
                    IDictionary <long, Address> randomnessContractAddressPerBlock,
                    IAbiEncoder abiEncoder,
                    IReadOnlyTxProcessorSource txProcessorSource,
                    ISigner signerLocal) =>
                randomnessContractAddressPerBlock
                .Select(kvp => new RandomContract(
                            abiEncoder,
                            kvp.Value,
                            txProcessorSource,
                            kvp.Key,
                            signerLocal))
                .ToArray <IRandomContract>();

                if (randomnessContractAddress?.Any() == true)
                {
                    RandomContractTxSource randomContractTxSource = new RandomContractTxSource(
                        GetRandomContracts(randomnessContractAddress, _api.AbiEncoder,
                                           readOnlyTxProcessorSource,
                                           signer),
                        new EciesCipher(_api.CryptoRandom),
                        signer,
                        _api.NodeKey,
                        _api.CryptoRandom,
                        _api.LogManager);

                    list.Insert(0, randomContractTxSource);
                    return(true);
                }

                return(false);
            }

            if (_api.ChainSpec == null)
            {
                throw new StepDependencyException(nameof(_api.ChainSpec));
            }
            if (_api.BlockTree == null)
            {
                throw new StepDependencyException(nameof(_api.BlockTree));
            }
            if (_api.EngineSigner == null)
            {
                throw new StepDependencyException(nameof(_api.EngineSigner));
            }

            IList <ITxSource> txSources = new List <ITxSource> {
                CreateStandardTxSourceForProducer(processingEnv, readOnlyTxProcessorSource)
            };
            bool needSigner = false;

            if (additionalTxSource is not null)
            {
                txSources.Insert(0, additionalTxSource);
            }
            needSigner |= CheckAddPosdaoTransactions(txSources, _api.ChainSpec.AuRa.PosdaoTransition);
            needSigner |= CheckAddRandomnessTransactions(txSources, _api.ChainSpec.AuRa.RandomnessContractAddress, _api.EngineSigner);

            ITxSource txSource = txSources.Count > 1 ? new CompositeTxSource(txSources.ToArray()) : txSources[0];

            if (needSigner)
            {
                TxSealer transactionSealer = new TxSealer(_api.EngineSigner, _api.Timestamper);
                txSource = new GeneratedTxSource(txSource, transactionSealer, processingEnv.StateReader, _api.LogManager);
            }

            ITxFilter?txPermissionFilter = TxAuRaFilterBuilders.CreateTxPermissionFilter(_api, readOnlyTxProcessorSource);

            if (txPermissionFilter != null)
            {
                // we now only need to filter generated transactions here, as regular ones are filtered on TxPoolTxSource filter based on CreateTxSourceFilter method
                txSource = new FilteredTxSource <GeneratedTransaction>(txSource, txPermissionFilter, _api.LogManager);
            }

            return(txSource);
        }
Example #5
0
        private BlockProcessor CreateBlockProcessor(ReadOnlyTxProcessingEnv changeableTxProcessingEnv, ReadOnlyTxProcessingEnv constantContractTxProcessingEnv)
        {
            if (_api.RewardCalculatorSource == null)
            {
                throw new StepDependencyException(nameof(_api.RewardCalculatorSource));
            }
            if (_api.ValidatorStore == null)
            {
                throw new StepDependencyException(nameof(_api.ValidatorStore));
            }
            if (_api.ChainSpec == null)
            {
                throw new StepDependencyException(nameof(_api.ChainSpec));
            }
            if (_api.BlockTree == null)
            {
                throw new StepDependencyException(nameof(_api.BlockTree));
            }
            if (_api.EngineSigner == null)
            {
                throw new StepDependencyException(nameof(_api.EngineSigner));
            }
            if (_api.SpecProvider == null)
            {
                throw new StepDependencyException(nameof(_api.SpecProvider));
            }
            if (_api.GasPriceOracle == null)
            {
                throw new StepDependencyException(nameof(_api.GasPriceOracle));
            }

            var chainSpecAuRa = _api.ChainSpec.AuRa;

            ITxFilter auRaTxFilter = TxAuRaFilterBuilders.CreateAuRaTxFilter(
                _api,
                constantContractTxProcessingEnv,
                _api.SpecProvider,
                new LocalTxFilter(_api.EngineSigner));

            _validator = new AuRaValidatorFactory(_api.AbiEncoder,
                                                  changeableTxProcessingEnv.StateProvider,
                                                  changeableTxProcessingEnv.TransactionProcessor,
                                                  changeableTxProcessingEnv.BlockTree,
                                                  constantContractTxProcessingEnv,
                                                  _api.ReceiptStorage,
                                                  _api.ValidatorStore,
                                                  _api.FinalizationManager,
                                                  NullTxSender.Instance,
                                                  NullTxPool.Instance,
                                                  NethermindApi.Config <IMiningConfig>(),
                                                  _api.LogManager,
                                                  _api.EngineSigner,
                                                  _api.SpecProvider,
                                                  _api.GasPriceOracle,
                                                  _api.ReportingContractValidatorCache, chainSpecAuRa.PosdaoTransition, true)
                         .CreateValidatorProcessor(chainSpecAuRa.Validators, _api.BlockTree.Head?.Header);

            if (_validator is IDisposable disposableValidator)
            {
                _api.DisposeStack.Push(disposableValidator);
            }

            IDictionary <long, IDictionary <Address, byte[]> > rewriteBytecode = chainSpecAuRa.RewriteBytecode;
            ContractRewriter?contractRewriter = rewriteBytecode?.Count > 0 ? new ContractRewriter(rewriteBytecode) : null;

            return(new AuRaBlockProcessor(
                       _api.SpecProvider,
                       _api.BlockValidator,
                       _api.RewardCalculatorSource.Get(changeableTxProcessingEnv.TransactionProcessor),
                       _api.BlockProducerEnvFactory.TransactionsExecutorFactory.Create(changeableTxProcessingEnv),
                       changeableTxProcessingEnv.StateProvider,
                       changeableTxProcessingEnv.StorageProvider,
                       _api.ReceiptStorage,
                       _api.LogManager,
                       changeableTxProcessingEnv.BlockTree,
                       auRaTxFilter,
                       CreateGasLimitCalculator(constantContractTxProcessingEnv) as AuRaContractGasLimitOverride,
                       contractRewriter)
            {
                AuRaValidator = _validator
            });
        }
        protected override BlockProcessor CreateBlockProcessor()
        {
            if (_api.SpecProvider == null)
            {
                throw new StepDependencyException(nameof(_api.SpecProvider));
            }
            if (_api.ChainHeadStateProvider == null)
            {
                throw new StepDependencyException(nameof(_api.ChainHeadStateProvider));
            }
            if (_api.BlockValidator == null)
            {
                throw new StepDependencyException(nameof(_api.BlockValidator));
            }
            if (_api.RewardCalculatorSource == null)
            {
                throw new StepDependencyException(nameof(_api.RewardCalculatorSource));
            }
            if (_api.TransactionProcessor == null)
            {
                throw new StepDependencyException(nameof(_api.TransactionProcessor));
            }
            if (_api.DbProvider == null)
            {
                throw new StepDependencyException(nameof(_api.DbProvider));
            }
            if (_api.StateProvider == null)
            {
                throw new StepDependencyException(nameof(_api.StateProvider));
            }
            if (_api.StorageProvider == null)
            {
                throw new StepDependencyException(nameof(_api.StorageProvider));
            }
            if (_api.TxPool == null)
            {
                throw new StepDependencyException(nameof(_api.TxPool));
            }
            if (_api.ReceiptStorage == null)
            {
                throw new StepDependencyException(nameof(_api.ReceiptStorage));
            }
            if (_api.BlockTree == null)
            {
                throw new StepDependencyException(nameof(_api.BlockTree));
            }
            if (_api.GasPriceOracle == null)
            {
                throw new StepDependencyException(nameof(_api.GasPriceOracle));
            }
            if (_api.ChainSpec == null)
            {
                throw new StepDependencyException(nameof(_api.ChainSpec));
            }

            var       processingReadOnlyTransactionProcessorSource = CreateReadOnlyTransactionProcessorSource();
            var       txPermissionFilterOnlyTxProcessorSource      = CreateReadOnlyTransactionProcessorSource();
            ITxFilter auRaTxFilter = TxAuRaFilterBuilders.CreateAuRaTxFilter(
                _api,
                txPermissionFilterOnlyTxProcessorSource,
                _api.SpecProvider,
                new ServiceTxFilter(_api.SpecProvider));

            IDictionary <long, IDictionary <Address, byte[]> > rewriteBytecode = _api.ChainSpec.AuRa.RewriteBytecode;
            ContractRewriter?contractRewriter = rewriteBytecode?.Count > 0 ? new ContractRewriter(rewriteBytecode) : null;

            var processor = (AuRaBlockProcessor)NewBlockProcessor(_api, auRaTxFilter, contractRewriter);

            var auRaValidator = CreateAuRaValidator(processor, processingReadOnlyTransactionProcessorSource);

            processor.AuRaValidator = auRaValidator;
            var reportingValidator = auRaValidator.GetReportingValidator();

            _api.ReportingValidator = reportingValidator;
            if (_sealValidator != null)
            {
                _sealValidator.ReportingValidator = reportingValidator;
            }

            return(processor);
        }
        protected override BlockProcessor CreateBlockProcessor()
        {
            if (_api.SpecProvider == null)
            {
                throw new StepDependencyException(nameof(_api.SpecProvider));
            }
            if (_api.ChainHeadStateProvider == null)
            {
                throw new StepDependencyException(nameof(_api.ChainHeadStateProvider));
            }
            if (_api.BlockValidator == null)
            {
                throw new StepDependencyException(nameof(_api.BlockValidator));
            }
            if (_api.RewardCalculatorSource == null)
            {
                throw new StepDependencyException(nameof(_api.RewardCalculatorSource));
            }
            if (_api.TransactionProcessor == null)
            {
                throw new StepDependencyException(nameof(_api.TransactionProcessor));
            }
            if (_api.DbProvider == null)
            {
                throw new StepDependencyException(nameof(_api.DbProvider));
            }
            if (_api.StateProvider == null)
            {
                throw new StepDependencyException(nameof(_api.StateProvider));
            }
            if (_api.StorageProvider == null)
            {
                throw new StepDependencyException(nameof(_api.StorageProvider));
            }
            if (_api.TxPool == null)
            {
                throw new StepDependencyException(nameof(_api.TxPool));
            }
            if (_api.ReceiptStorage == null)
            {
                throw new StepDependencyException(nameof(_api.ReceiptStorage));
            }
            if (_api.BlockTree == null)
            {
                throw new StepDependencyException(nameof(_api.BlockTree));
            }
            if (_api.GasPriceOracle == null)
            {
                throw new StepDependencyException(nameof(_api.GasPriceOracle));
            }

            var       processingReadOnlyTransactionProcessorSource = CreateReadOnlyTransactionProcessorSource();
            var       txPermissionFilterOnlyTxProcessorSource      = CreateReadOnlyTransactionProcessorSource();
            ITxFilter auRaTxFilter = TxAuRaFilterBuilders.CreateAuRaTxFilter(
                _api,
                txPermissionFilterOnlyTxProcessorSource,
                _api.SpecProvider,
                new ServiceTxFilter(_api.SpecProvider));

            var processor = new AuRaBlockProcessor(
                _api.SpecProvider,
                _api.BlockValidator,
                _api.RewardCalculatorSource.Get(_api.TransactionProcessor),
                new BlockProcessor.BlockValidationTransactionsExecutor(_api.TransactionProcessor, _api.StateProvider),
                _api.StateProvider,
                _api.StorageProvider,
                _api.ReceiptStorage,
                _api.LogManager,
                _api.BlockTree,
                auRaTxFilter,
                GetGasLimitCalculator());

            var auRaValidator = CreateAuRaValidator(processor, processingReadOnlyTransactionProcessorSource);

            processor.AuRaValidator = auRaValidator;
            var reportingValidator = auRaValidator.GetReportingValidator();

            _api.ReportingValidator = reportingValidator;
            if (_sealValidator != null)
            {
                _sealValidator.ReportingValidator = reportingValidator;
            }

            return(processor);
        }