private void SetupRulesEngine(ChainIndexer chainIndexer) { var dateTimeProvider = new DateTimeProvider(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in this.Network.Consensus.ConsensusRules.HeaderValidationRules) { consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in this.Network.Consensus.ConsensusRules.PartialValidationRules) { consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule); } foreach (var ruleType in this.Network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } var asyncProvider = new AsyncProvider(this.LoggerFactory.Object, new Mock <ISignals>().Object, new NodeLifetime()); var powConsensusRules = new PowConsensusRuleEngine(this.testNet, this.LoggerFactory.Object, this.dateTimeProvider.Object, chainIndexer, new NodeDeployments(this.testNet, chainIndexer), new ConsensusSettings(new NodeSettings(this.testNet)), new Checkpoints(), new Mock <ICoinView>().Object, new Mock <IChainState>().Object, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, LoggerFactory.Object), asyncProvider, consensusRulesContainer); powConsensusRules.SetupRulesEngineParent(); this.consensusManager.SetupGet(x => x.ConsensusRules).Returns(powConsensusRules); }
private MempoolManager CreateTestMempool(NodeSettings settings, out TxMempool txMemPool) { var mempoolSettings = new MempoolSettings(settings); IDateTimeProvider dateTimeProvider = DateTimeProvider.Default; NodeSettings nodeSettings = NodeSettings.Default(settings.Network); ILoggerFactory loggerFactory = nodeSettings.LoggerFactory; var consensusSettings = new ConsensusSettings(nodeSettings); txMemPool = new TxMempool(dateTimeProvider, new BlockPolicyEstimator(new MempoolSettings(nodeSettings), loggerFactory, nodeSettings), loggerFactory, nodeSettings); var mempoolLock = new MempoolSchedulerLock(); var coins = new InMemoryCoinView(settings.Network.GenesisHash); var chain = new ChainIndexer(settings.Network); var chainState = new ChainState(); var mempoolPersistence = new MempoolPersistence(settings, loggerFactory); this.network.Consensus.Options = new PosConsensusOptions(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in this.network.Consensus.ConsensusRules.HeaderValidationRules) { consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in this.network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } var asyncProvider = new AsyncProvider(nodeSettings.LoggerFactory, new Mock <ISignals>().Object, new NodeLifetime()); ConsensusRuleEngine consensusRules = new PowConsensusRuleEngine(this.network, loggerFactory, dateTimeProvider, chain, new NodeDeployments(this.network, chain), consensusSettings, new Checkpoints(), coins, chainState, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, loggerFactory), asyncProvider, consensusRulesContainer).SetupRulesEngineParent(); // The mempool rule constructors aren't parameterless, so we have to manually inject the dependencies for every rule var mempoolRules = new List <MempoolRule> { new CheckConflictsMempoolRule(network, txMemPool, mempoolSettings, chain, loggerFactory), new CheckCoinViewMempoolRule(network, txMemPool, mempoolSettings, chain, loggerFactory), new CreateMempoolEntryMempoolRule(network, txMemPool, mempoolSettings, chain, consensusRules, loggerFactory), new CheckSigOpsMempoolRule(network, txMemPool, mempoolSettings, chain, loggerFactory), new CheckFeeMempoolRule(network, txMemPool, mempoolSettings, chain, loggerFactory), new CheckRateLimitMempoolRule(network, txMemPool, mempoolSettings, chain, loggerFactory), new CheckAncestorsMempoolRule(network, txMemPool, mempoolSettings, chain, loggerFactory), new CheckReplacementMempoolRule(network, txMemPool, mempoolSettings, chain, loggerFactory), new CheckAllInputsMempoolRule(network, txMemPool, mempoolSettings, chain, consensusRules, loggerFactory) }; // We also have to check that the manually instantiated rules match the ones in the network, or the test isn't valid for (int i = 0; i < this.network.Consensus.MempoolRules.Count; i++) { if (network.Consensus.MempoolRules[i] != mempoolRules[i].GetType()) { throw new Exception("Mempool rule type mismatch"); } } var mempoolValidator = new MempoolValidator(txMemPool, mempoolLock, dateTimeProvider, mempoolSettings, chain, coins, loggerFactory, settings, consensusRules, mempoolRules, new NodeDeployments(this.network, chain)); return(new MempoolManager(mempoolLock, txMemPool, mempoolValidator, dateTimeProvider, mempoolSettings, mempoolPersistence, coins, loggerFactory, settings.Network)); }
public ConsensusRulesPerformanceCounter(ConsensusRulesContainer consensusRules) { this.registeredRules = new List <RuleItem>(); this.RegisterRulesCollection(consensusRules.HeaderValidationRules.Select(x => x as IConsensusRuleBase), RuleType.Header); this.RegisterRulesCollection(consensusRules.IntegrityValidationRules.Select(x => x as IConsensusRuleBase), RuleType.Integrity); this.RegisterRulesCollection(consensusRules.PartialValidationRules.Select(x => x as IConsensusRuleBase), RuleType.Partial); this.RegisterRulesCollection(consensusRules.FullValidationRules.Select(x => x as IConsensusRuleBase), RuleType.Full); this.currentSnapshot = new ConsensusRulesPerformanceSnapshot(this.registeredRules); }
public PowConsensusRuleEngine(Network network, ILoggerFactory loggerFactory, IDateTimeProvider dateTimeProvider, ChainIndexer chainIndexer, NodeDeployments nodeDeployments, ConsensusSettings consensusSettings, ICheckpoints checkpoints, ICoinView utxoSet, IChainState chainState, IInvalidBlockHashStore invalidBlockHashStore, INodeStats nodeStats, IAsyncProvider asyncProvider, ConsensusRulesContainer consensusRulesContainer) : base(network, loggerFactory, dateTimeProvider, chainIndexer, nodeDeployments, consensusSettings, checkpoints, chainState, invalidBlockHashStore, nodeStats, consensusRulesContainer) { this.logger = loggerFactory.CreateLogger(GetType().FullName); this.UtxoSet = utxoSet; this.prefetcher = new CoinviewPrefetcher(this.UtxoSet, chainIndexer, loggerFactory, asyncProvider); }
public PosConsensusRuleEngine(Network network, ILoggerFactory loggerFactory, IDateTimeProvider dateTimeProvider, ChainIndexer chainIndexer, NodeDeployments nodeDeployments, ConsensusSettings consensusSettings, ICheckpoints checkpoints, ICoinView utxoSet, IStakeChain stakeChain, IStakeValidator stakeValidator, IChainState chainState, IInvalidBlockHashStore invalidBlockHashStore, INodeStats nodeStats, IRewindDataIndexCache rewindDataIndexCache, IAsyncProvider asyncProvider, ConsensusRulesContainer consensusRulesContainer) : base(network, loggerFactory, dateTimeProvider, chainIndexer, nodeDeployments, consensusSettings, checkpoints, utxoSet, chainState, invalidBlockHashStore, nodeStats, asyncProvider, consensusRulesContainer) { this.StakeChain = stakeChain; this.StakeValidator = stakeValidator; this.RewindDataIndexCache = rewindDataIndexCache; }
protected ConsensusRuleEngine( Network network, ILoggerFactory loggerFactory, IDateTimeProvider dateTimeProvider, ChainIndexer chainIndexer, NodeDeployments nodeDeployments, ConsensusSettings consensusSettings, ICheckpoints checkpoints, IChainState chainState, IInvalidBlockHashStore invalidBlockHashStore, INodeStats nodeStats, ConsensusRulesContainer consensusRules) { Guard.NotNull(network, nameof(network)); Guard.NotNull(loggerFactory, nameof(loggerFactory)); Guard.NotNull(dateTimeProvider, nameof(dateTimeProvider)); Guard.NotNull(chainIndexer, nameof(chainIndexer)); Guard.NotNull(nodeDeployments, nameof(nodeDeployments)); Guard.NotNull(consensusSettings, nameof(consensusSettings)); Guard.NotNull(checkpoints, nameof(checkpoints)); Guard.NotNull(chainState, nameof(chainState)); Guard.NotNull(invalidBlockHashStore, nameof(invalidBlockHashStore)); Guard.NotNull(nodeStats, nameof(nodeStats)); this.Network = network; this.ChainIndexer = chainIndexer; this.ChainState = chainState; this.NodeDeployments = nodeDeployments; this.LoggerFactory = loggerFactory; this.ConsensusSettings = consensusSettings; this.Checkpoints = checkpoints; this.ConsensusParams = this.Network.Consensus; this.ConsensusSettings = consensusSettings; this.DateTimeProvider = dateTimeProvider; this.invalidBlockHashStore = invalidBlockHashStore; this.consensusRules = consensusRules; this.LoggerFactory = loggerFactory; this.logger = loggerFactory.CreateLogger(this.GetType().FullName); this.NodeDeployments = nodeDeployments; if (nodeStats.DisplayBenchStats) { nodeStats.RegisterStats(this.AddBenchStats, StatsType.Benchmark, this.GetType().Name, 500); } }
/// <summary> /// Create the MempoolManager used for testing whether transactions are accepted to the memory pool. /// </summary> private void CreateMempoolManager() { this.mempoolSettings = new MempoolSettings(this.nodeSettings); this.consensusSettings = new ConsensusSettings(this.nodeSettings); this.txMemPool = new TxMempool(this.dateTimeProvider, new BlockPolicyEstimator( new MempoolSettings(this.nodeSettings), this.loggerFactory, this.nodeSettings), this.loggerFactory, this.nodeSettings); this.chainIndexer = new ChainIndexer(this.Network); this.nodeDeployments = new NodeDeployments(this.Network, this.chainIndexer); this.MockCoinView(); this.MockStakeChain(); this.MockStakeValidator(); // Create POS consensus rules engine. var checkpoints = new Mock <ICheckpoints>(); var chainState = new ChainState(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in this.Network.Consensus.ConsensusRules.HeaderValidationRules) { consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in this.Network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } ConsensusRuleEngine consensusRuleEngine = new PosConsensusRuleEngine(this.Network, this.loggerFactory, this.dateTimeProvider, this.chainIndexer, this.nodeDeployments, this.consensusSettings, checkpoints.Object, this.coinView.Object, this.stakeChain.Object, this.stakeValidator.Object, chainState, new InvalidBlockHashStore(this.dateTimeProvider), new Mock <INodeStats>().Object, new Mock <IRewindDataIndexCache>().Object, this.asyncProvider, consensusRulesContainer) .SetupRulesEngineParent(); // Create mempool validator. var mempoolLock = new MempoolSchedulerLock(); var mempoolValidator = new MempoolValidator(this.txMemPool, mempoolLock, this.dateTimeProvider, this.mempoolSettings, this.chainIndexer, this.coinView.Object, this.loggerFactory, this.nodeSettings, consensusRuleEngine); // Create mempool manager. var mempoolPersistence = new Mock <IMempoolPersistence>(); this.mempoolManager = new MempoolManager(mempoolLock, this.txMemPool, mempoolValidator, this.dateTimeProvider, this.mempoolSettings, mempoolPersistence.Object, this.coinView.Object, this.loggerFactory, this.Network); }
private void SetupRulesEngine(ChainIndexer chainIndexer) { var dateTimeProvider = new DateTimeProvider(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in this.Network.Consensus.ConsensusRules.HeaderValidationRules) { consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in this.Network.Consensus.ConsensusRules.PartialValidationRules) { consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule); } foreach (var ruleType in this.Network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } var posConsensusRules = new PosConsensusRuleEngine( this.Network, this.LoggerFactory.Object, this.dateTimeProvider.Object, chainIndexer, new NodeDeployments(this.Network, chainIndexer), new ConsensusSettings(new NodeSettings(this.Network)), new Checkpoints(), new Mock <ICoinView>().Object, new Mock <IStakeChain>().Object, new Mock <IStakeValidator>().Object, new Mock <IChainState>().Object, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, NodeSettings.Default(this.Network), new Mock <IVersionProvider>().Object), new Mock <IRewindDataIndexCache>().Object, this.CreateAsyncProvider(), consensusRulesContainer); posConsensusRules.SetupRulesEngineParent(); this.consensusManager.SetupGet(x => x.ConsensusRules).Returns(posConsensusRules); }
private void SetupRulesEngine(ChainIndexer chainIndexer) { var dateTimeProvider = new DateTimeProvider(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in this.Network.Consensus.ConsensusRules.HeaderValidationRules) { consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in this.Network.Consensus.ConsensusRules.PartialValidationRules) { consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule); } foreach (var ruleType in this.Network.Consensus.ConsensusRules.FullValidationRules) { FullValidationConsensusRule rule = null; if (ruleType == typeof(FlushCoinviewRule)) { rule = new FlushCoinviewRule(new Mock <IInitialBlockDownloadState>().Object); } else { rule = Activator.CreateInstance(ruleType) as FullValidationConsensusRule; } consensusRulesContainer.FullValidationRules.Add(rule); } var asyncProvider = new AsyncProvider(this.LoggerFactory.Object, new Mock <ISignals>().Object); var powConsensusRules = new PowConsensusRuleEngine(this.testNet, this.LoggerFactory.Object, this.dateTimeProvider.Object, chainIndexer, new NodeDeployments(this.testNet, chainIndexer), new ConsensusSettings(new NodeSettings(this.testNet)), new Checkpoints(), new Mock <ICoinView>().Object, new Mock <IChainState>().Object, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, NodeSettings.Default(this.Network), new Mock <IVersionProvider>().Object), asyncProvider, consensusRulesContainer); powConsensusRules.SetupRulesEngineParent(); this.consensusManager.SetupGet(x => x.ConsensusRules).Returns(powConsensusRules); }
private MempoolManager CreateTestMempool(NodeSettings settings, out TxMempool txMemPool) { var mempoolSettings = new MempoolSettings(settings); IDateTimeProvider dateTimeProvider = DateTimeProvider.Default; NodeSettings nodeSettings = NodeSettings.Default(settings.Network); ILoggerFactory loggerFactory = nodeSettings.LoggerFactory; var consensusSettings = new ConsensusSettings(nodeSettings); txMemPool = new TxMempool(dateTimeProvider, new BlockPolicyEstimator(new MempoolSettings(nodeSettings), loggerFactory, nodeSettings), loggerFactory, nodeSettings); var mempoolLock = new MempoolSchedulerLock(); var coins = new InMemoryCoinView(settings.Network.GenesisHash); var chain = new ChainIndexer(settings.Network); var chainState = new ChainState(); var mempoolPersistence = new MempoolPersistence(settings, loggerFactory); this.network.Consensus.Options = new PosConsensusOptions(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in this.network.Consensus.ConsensusRules.HeaderValidationRules) { consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in this.network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } var asyncProvider = new AsyncProvider(nodeSettings.LoggerFactory, new Mock <ISignals>().Object, new NodeLifetime()); ConsensusRuleEngine consensusRules = new PowConsensusRuleEngine(this.network, loggerFactory, dateTimeProvider, chain, new NodeDeployments(this.network, chain), consensusSettings, new Checkpoints(), coins, chainState, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, loggerFactory), asyncProvider, consensusRulesContainer).SetupRulesEngineParent(); var mempoolValidator = new MempoolValidator(txMemPool, mempoolLock, dateTimeProvider, mempoolSettings, chain, coins, loggerFactory, settings, consensusRules); return(new MempoolManager(mempoolLock, txMemPool, mempoolValidator, dateTimeProvider, mempoolSettings, mempoolPersistence, coins, loggerFactory, settings.Network)); }
public PoAConsensusRuleEngine(Network network, ILoggerFactory loggerFactory, IDateTimeProvider dateTimeProvider, ChainIndexer chainIndexer, NodeDeployments nodeDeployments, ConsensusSettings consensusSettings, ICheckpoints checkpoints, ICoinView utxoSet, IChainState chainState, IInvalidBlockHashStore invalidBlockHashStore, INodeStats nodeStats, ISlotsManager slotsManager, PoABlockHeaderValidator poaHeaderValidator, VotingManager votingManager, IFederationManager federationManager, IAsyncProvider asyncProvider, ConsensusRulesContainer consensusRulesContainer) : base(network, loggerFactory, dateTimeProvider, chainIndexer, nodeDeployments, consensusSettings, checkpoints, utxoSet, chainState, invalidBlockHashStore, nodeStats, asyncProvider, consensusRulesContainer) { this.SlotsManager = slotsManager; this.PoaHeaderValidator = poaHeaderValidator; this.VotingManager = votingManager; this.FederationManager = federationManager; }
/// <summary> /// Creates the consensus manager used by <see cref="PosCoinViewRuleFailsAsync"/>. /// </summary> /// <param name="unspentOutputs">The dictionary used to mock up the <see cref="ICoinView"/>.</param> /// <returns>The constructed consensus manager.</returns> private async Task <ConsensusManager> CreateConsensusManagerAsync(Dictionary <OutPoint, UnspentOutput> unspentOutputs) { this.consensusSettings = new ConsensusSettings(Configuration.NodeSettings.Default(this.network)); var initialBlockDownloadState = new InitialBlockDownloadState(this.chainState.Object, this.network, this.consensusSettings, new Checkpoints(), DateTimeProvider.Default); var signals = new Signals.Signals(this.loggerFactory.Object, null); var asyncProvider = new AsyncProvider(this.loggerFactory.Object, signals); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in this.network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } // Register POS consensus rules. // new FullNodeBuilderConsensusExtension.PosConsensusRulesRegistration().RegisterRules(this.network.Consensus); var consensusRuleEngine = new PosConsensusRuleEngine(this.network, this.loggerFactory.Object, DateTimeProvider.Default, this.ChainIndexer, this.nodeDeployments, this.consensusSettings, this.checkpoints.Object, this.coinView.Object, this.stakeChain.Object, this.stakeValidator.Object, this.chainState.Object, new InvalidBlockHashStore(this.dateTimeProvider.Object), new Mock <INodeStats>().Object, this.rewindDataIndexStore.Object, this.asyncProvider, consensusRulesContainer) .SetupRulesEngineParent(); var headerValidator = new HeaderValidator(consensusRuleEngine, this.loggerFactory.Object); var integrityValidator = new IntegrityValidator(consensusRuleEngine, this.loggerFactory.Object); var partialValidator = new PartialValidator(asyncProvider, consensusRuleEngine, this.loggerFactory.Object); var fullValidator = new FullValidator(consensusRuleEngine, this.loggerFactory.Object); // Create the chained header tree. var chainedHeaderTree = new ChainedHeaderTree(this.network, this.loggerFactory.Object, headerValidator, this.checkpoints.Object, this.chainState.Object, new Mock <IFinalizedBlockInfoRepository>().Object, this.consensusSettings, new InvalidBlockHashStore(new DateTimeProvider()), new ChainWorkComparer()); // Create consensus manager. var consensus = new ConsensusManager(chainedHeaderTree, this.network, this.loggerFactory.Object, this.chainState.Object, integrityValidator, partialValidator, fullValidator, consensusRuleEngine, new Mock <IFinalizedBlockInfoRepository>().Object, signals, new Mock <IPeerBanning>().Object, initialBlockDownloadState, this.ChainIndexer, new Mock <IBlockPuller>().Object, new Mock <IBlockStore>().Object, new Mock <IConnectionManager>().Object, new Mock <INodeStats>().Object, new Mock <INodeLifetime>().Object, this.consensusSettings, this.dateTimeProvider.Object); // Mock the coinviews "FetchCoinsAsync" method. We will use the "unspentOutputs" dictionary to track spendable outputs. this.coinView.Setup(d => d.FetchCoins(It.IsAny <OutPoint[]>())) .Returns((OutPoint[] txIds) => { var result = new FetchCoinsResponse(); for (int i = 0; i < txIds.Length; i++) { unspentOutputs.TryGetValue(txIds[i], out UnspentOutput unspent); result.UnspentOutputs.Add(txIds[i], unspent); } return(result); }); // Mock the coinviews "GetTipHashAsync" method. this.coinView.Setup(d => d.GetTipHash()).Returns(() => { return(new HashHeightPair(this.ChainIndexer.Tip)); }); // Since we are mocking the stake validator ensure that GetNextTargetRequired returns something sensible. Otherwise we get the "bad-diffbits" error. this.stakeValidator.Setup(s => s.GetNextTargetRequired(It.IsAny <IStakeChain>(), It.IsAny <ChainedHeader>(), It.IsAny <IConsensus>(), It.IsAny <bool>())) .Returns(this.network.Consensus.PowLimit); // Skip validation of signature in the proven header this.stakeValidator.Setup(s => s.VerifySignature(It.IsAny <UnspentOutput>(), It.IsAny <Transaction>(), 0, It.IsAny <ScriptVerify>())) .Returns(true); // Skip validation of stake kernel this.stakeValidator.Setup(s => s.CheckStakeKernelHash(It.IsAny <PosRuleContext>(), It.IsAny <uint>(), It.IsAny <uint256>(), It.IsAny <UnspentOutput>(), It.IsAny <OutPoint>(), It.IsAny <uint>())) .Returns(true); // Since we are mocking the stakechain ensure that the Get returns a BlockStake. Otherwise this results in "previous stake is not found". this.stakeChain.Setup(d => d.Get(It.IsAny <uint256>())).Returns(new BlockStake() { Flags = BlockFlag.BLOCK_PROOF_OF_STAKE, StakeModifierV2 = 0, StakeTime = (this.ChainIndexer.Tip.Header.Time + 60) & ~PosConsensusOptions.StakeTimestampMask }); // Since we are mocking the chainState ensure that the BlockStoreTip returns a usable value. this.chainState.Setup(d => d.BlockStoreTip).Returns(this.ChainIndexer.Tip); // Since we are mocking the chainState ensure that the ConsensusTip returns a usable value. this.chainState.Setup(d => d.ConsensusTip).Returns(this.ChainIndexer.Tip); // Initialize the consensus manager. await consensus.InitializeAsync(this.ChainIndexer.Tip); return(consensus); }
/// <summary> /// Create the MempoolManager used for testing whether transactions are accepted to the memory pool. /// </summary> private void CreateMempoolManager() { this.mempoolSettings = new MempoolSettings(this.nodeSettings); this.consensusSettings = new ConsensusSettings(this.nodeSettings); this.txMemPool = new TxMempool(this.dateTimeProvider, new BlockPolicyEstimator( new MempoolSettings(this.nodeSettings), this.loggerFactory, this.nodeSettings), this.loggerFactory, this.nodeSettings); this.chainIndexer = new ChainIndexer(this.Network); this.nodeDeployments = new NodeDeployments(this.Network, this.chainIndexer); this.MockCoinView(); this.MockStakeChain(); this.MockStakeValidator(); // Create POS consensus rules engine. var checkpoints = new Mock <ICheckpoints>(); var chainState = new ChainState(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in this.Network.Consensus.ConsensusRules.HeaderValidationRules) { consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in this.Network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } ConsensusRuleEngine consensusRuleEngine = new PosConsensusRuleEngine(this.Network, this.loggerFactory, this.dateTimeProvider, this.chainIndexer, this.nodeDeployments, this.consensusSettings, checkpoints.Object, this.coinView.Object, this.stakeChain.Object, this.stakeValidator.Object, chainState, new InvalidBlockHashStore(this.dateTimeProvider), new Mock <INodeStats>().Object, new Mock <IRewindDataIndexCache>().Object, this.asyncProvider, consensusRulesContainer) .SetupRulesEngineParent(); // Create mempool validator. var mempoolLock = new MempoolSchedulerLock(); // The mempool rule constructors aren't parameterless, so we have to manually inject the dependencies for every rule var mempoolRules = new List <MempoolRule> { new CheckConflictsMempoolRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, this.loggerFactory), new CheckCoinViewMempoolRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, this.loggerFactory), new CreateMempoolEntryMempoolRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, consensusRuleEngine, this.loggerFactory), new CheckSigOpsMempoolRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, this.loggerFactory), new CheckFeeMempoolRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, this.loggerFactory), new CheckRateLimitMempoolRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, this.loggerFactory), new CheckAncestorsMempoolRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, this.loggerFactory), new CheckReplacementMempoolRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, this.loggerFactory), new CheckAllInputsMempoolRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, consensusRuleEngine, this.loggerFactory), new CheckTxOutDustRule(this.Network, this.txMemPool, this.mempoolSettings, this.chainIndexer, this.loggerFactory) }; // We also have to check that the manually instantiated rules match the ones in the network, or the test isn't valid for (int i = 0; i < this.Network.Consensus.MempoolRules.Count; i++) { if (this.Network.Consensus.MempoolRules[i] != mempoolRules[i].GetType()) { throw new Exception("Mempool rule type mismatch"); } } Assert.Equal(this.Network.Consensus.MempoolRules.Count, mempoolRules.Count); var mempoolValidator = new MempoolValidator(this.txMemPool, mempoolLock, this.dateTimeProvider, this.mempoolSettings, this.chainIndexer, this.coinView.Object, this.loggerFactory, this.nodeSettings, consensusRuleEngine, mempoolRules, new NodeDeployments(this.Network, this.chainIndexer)); // Create mempool manager. var mempoolPersistence = new Mock <IMempoolPersistence>(); this.mempoolManager = new MempoolManager(mempoolLock, this.txMemPool, mempoolValidator, this.dateTimeProvider, this.mempoolSettings, mempoolPersistence.Object, this.coinView.Object, this.loggerFactory, this.Network); }
public RuleRegistrationHelper(ConsensusRuleEngine ruleEngine, ConsensusRulesContainer consensusRulesContainer) { this.ruleEngine = ruleEngine; this.consensusRulesContainer = consensusRulesContainer; }
public TestConsensusRules(Network network, ILoggerFactory loggerFactory, IDateTimeProvider dateTimeProvider, ChainIndexer chainIndexer, NodeDeployments nodeDeployments, ConsensusSettings consensusSettings, ICheckpoints checkpoints, IChainState chainState, IInvalidBlockHashStore invalidBlockHashStore, INodeStats nodeStats, ConsensusRulesContainer consensusRulesContainer) : base(network, loggerFactory, dateTimeProvider, chainIndexer, nodeDeployments, consensusSettings, checkpoints, chainState, invalidBlockHashStore, nodeStats, consensusRulesContainer) { this.ruleRegistrationHelper = new RuleRegistrationHelper(this, consensusRulesContainer); }
public static async Task <ITestChainContext> CreatePosAsync(Network network, Script scriptPubKey, string dataDir) { var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dataDir}" }); ILoggerFactory loggerFactory = nodeSettings.LoggerFactory; IDateTimeProvider dateTimeProvider = DateTimeProvider.Default; network.Consensus.Options = new ConsensusOptions(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in network.Consensus.ConsensusRules.HeaderValidationRules) { // Dont check PoW of a header in this test. if (ruleType == typeof(CheckDifficultyPowRule)) { continue; } consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } foreach (var ruleType in network.Consensus.ConsensusRules.PartialValidationRules) { consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule); } var consensusSettings = new ConsensusSettings(nodeSettings); var chain = new ChainIndexer(network); var inMemoryCoinView = new InMemoryCoinView(chain.Tip.HashBlock); var chainState = new ChainState(); var deployments = new NodeDeployments(network, chain); ConsensusRuleEngine consensusRules; var asyncProvider = new AsyncProvider(loggerFactory, new Mock <ISignals>().Object, new NodeLifetime()); var stakeChain = new StakeChainStore(network, chain, null, loggerFactory); consensusRules = new PosConsensusRuleEngine(network, loggerFactory, dateTimeProvider, chain, deployments, consensusSettings, new Checkpoints(), inMemoryCoinView, stakeChain, new StakeValidator(network, stakeChain, chain, inMemoryCoinView, loggerFactory), chainState, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, loggerFactory), new RewindDataIndexCache(dateTimeProvider, network), asyncProvider, consensusRulesContainer).SetupRulesEngineParent(); ConsensusManager consensus = ConsensusManagerHelper.CreateConsensusManager(network, dataDir, chainState, chainIndexer: chain, consensusRules: consensusRules, inMemoryCoinView: inMemoryCoinView); var genesis = new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0); chainState.BlockStoreTip = genesis; await consensus.InitializeAsync(genesis).ConfigureAwait(false); var mempoolSettings = new MempoolSettings(nodeSettings); var blockPolicyEstimator = new BlockPolicyEstimator(mempoolSettings, loggerFactory, nodeSettings); var mempool = new TxMempool(dateTimeProvider, blockPolicyEstimator, loggerFactory, nodeSettings); var mempoolLock = new MempoolSchedulerLock(); // The mempool rule constructors aren't parameterless, so we have to manually inject the dependencies for every rule var mempoolRules = new List <MempoolRule> { new CheckConflictsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckCoinViewMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CreateMempoolEntryMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, loggerFactory), new CheckSigOpsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckFeeMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckRateLimitMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckAncestorsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckReplacementMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckAllInputsMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, loggerFactory), new CheckTxOutDustRule(network, mempool, mempoolSettings, chain, loggerFactory), }; // We also have to check that the manually instantiated rules match the ones in the network, or the test isn't valid for (int i = 0; i < network.Consensus.MempoolRules.Count; i++) { if (network.Consensus.MempoolRules[i] != mempoolRules[i].GetType()) { throw new Exception("Mempool rule type mismatch"); } } var nodeDeployments = new NodeDeployments(network, chain); var mempoolValidator = new MempoolValidator(mempool, mempoolLock, dateTimeProvider, new MempoolSettings(nodeSettings), chain, inMemoryCoinView, loggerFactory, nodeSettings, consensusRules, mempoolRules, nodeDeployments); return(new TestChainContext { MempoolValidator = mempoolValidator }); }
public static async Task <ITestChainContext> CreatePosAsync(Network network, Script scriptPubKey, string dataDir) { var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dataDir}" }); ILoggerFactory loggerFactory = nodeSettings.LoggerFactory; IDateTimeProvider dateTimeProvider = DateTimeProvider.Default; network.Consensus.Options = new ConsensusOptions(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in network.Consensus.ConsensusRules.HeaderValidationRules) { // Dont check PoW of a header in this test. if (ruleType == typeof(CheckDifficultyPowRule)) { continue; } consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } foreach (var ruleType in network.Consensus.ConsensusRules.PartialValidationRules) { consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule); } var consensusSettings = new ConsensusSettings(nodeSettings); var chain = new ChainIndexer(network); var inMemoryCoinView = new InMemoryCoinView(chain.Tip.HashBlock); var chainState = new ChainState(); var deployments = new NodeDeployments(network, chain); ConsensusRuleEngine consensusRules; var asyncProvider = new AsyncProvider(loggerFactory, new Mock <ISignals>().Object, new NodeLifetime()); var stakeChain = new StakeChainStore(network, chain, null, loggerFactory); consensusRules = new PosConsensusRuleEngine(network, loggerFactory, dateTimeProvider, chain, deployments, consensusSettings, new Checkpoints(), inMemoryCoinView, stakeChain, new StakeValidator(network, stakeChain, chain, inMemoryCoinView, loggerFactory), chainState, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, loggerFactory), new RewindDataIndexCache(dateTimeProvider, network), asyncProvider, consensusRulesContainer).SetupRulesEngineParent(); ConsensusManager consensus = ConsensusManagerHelper.CreateConsensusManager(network, dataDir, chainState, chainIndexer: chain, consensusRules: consensusRules, inMemoryCoinView: inMemoryCoinView); var genesis = new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0); chainState.BlockStoreTip = genesis; await consensus.InitializeAsync(genesis).ConfigureAwait(false); var blockPolicyEstimator = new BlockPolicyEstimator(new MempoolSettings(nodeSettings), loggerFactory, nodeSettings); var mempool = new TxMempool(dateTimeProvider, blockPolicyEstimator, loggerFactory, nodeSettings); var mempoolLock = new MempoolSchedulerLock(); var mempoolValidator = new MempoolValidator(mempool, mempoolLock, dateTimeProvider, new MempoolSettings(nodeSettings), chain, inMemoryCoinView, loggerFactory, nodeSettings, consensusRules); return(new TestChainContext { MempoolValidator = mempoolValidator }); }
/// <summary> /// Creates the test chain with some default blocks and txs. /// </summary> /// <param name="network">Network to create the chain on.</param> /// <param name="scriptPubKey">Public key to create blocks/txs with.</param> /// <returns>Context object representing the test chain.</returns> public static async Task <ITestChainContext> CreateAsync(Network network, Script scriptPubKey, string dataDir) { var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dataDir}" }); ILoggerFactory loggerFactory = nodeSettings.LoggerFactory; IDateTimeProvider dateTimeProvider = DateTimeProvider.Default; network.Consensus.Options = new ConsensusOptions(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in network.Consensus.ConsensusRules.HeaderValidationRules) { // Dont check PoW of a header in this test. if (ruleType == typeof(CheckDifficultyPowRule)) { continue; } consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } foreach (var ruleType in network.Consensus.ConsensusRules.PartialValidationRules) { consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule); } var consensusSettings = new ConsensusSettings(nodeSettings); var chain = new ChainIndexer(network); var inMemoryCoinView = new InMemoryCoinView(chain.Tip.HashBlock); var asyncProvider = new AsyncProvider(loggerFactory, new Mock <ISignals>().Object, new NodeLifetime()); var chainState = new ChainState(); var deployments = new NodeDeployments(network, chain); ConsensusRuleEngine consensusRules = new PowConsensusRuleEngine(network, loggerFactory, dateTimeProvider, chain, deployments, consensusSettings, new Checkpoints(), inMemoryCoinView, chainState, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, loggerFactory), asyncProvider, consensusRulesContainer).SetupRulesEngineParent(); ConsensusManager consensus = ConsensusManagerHelper.CreateConsensusManager(network, dataDir, chainState, chainIndexer: chain, consensusRules: consensusRules, inMemoryCoinView: inMemoryCoinView); var genesis = new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0); chainState.BlockStoreTip = genesis; await consensus.InitializeAsync(genesis).ConfigureAwait(false); var blockPolicyEstimator = new BlockPolicyEstimator(new MempoolSettings(nodeSettings), loggerFactory, nodeSettings); var mempool = new TxMempool(dateTimeProvider, blockPolicyEstimator, loggerFactory, nodeSettings); var mempoolLock = new MempoolSchedulerLock(); var minerSettings = new MinerSettings(nodeSettings); // Simple block creation, nothing special yet: var blockDefinition = new PowBlockDefinition(consensus, dateTimeProvider, loggerFactory, mempool, mempoolLock, minerSettings, network, consensusRules); BlockTemplate newBlock = blockDefinition.Build(chain.Tip, scriptPubKey); await consensus.BlockMinedAsync(newBlock.Block); List <BlockInfo> blockinfo = CreateBlockInfoList(); // We can't make transactions until we have inputs therefore, load 100 blocks. var blocks = new List <Block>(); var srcTxs = new List <Transaction>(); for (int i = 0; i < blockinfo.Count; ++i) { Block currentBlock = Block.Load(newBlock.Block.ToBytes(network.Consensus.ConsensusFactory), network.Consensus.ConsensusFactory); currentBlock.Header.HashPrevBlock = chain.Tip.HashBlock; currentBlock.Header.Version = 1; currentBlock.Header.Time = Utils.DateTimeToUnixTime(chain.Tip.GetMedianTimePast()) + 1; Transaction txCoinbase = network.CreateTransaction(currentBlock.Transactions[0].ToBytes()); txCoinbase.Inputs.Clear(); txCoinbase.Version = 1; txCoinbase.AddInput(new TxIn(new Script(new[] { Op.GetPushOp(blockinfo[i].extraNonce), Op.GetPushOp(chain.Height) }))); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this) txCoinbase.AddOutput(new TxOut(Money.Zero, new Script())); currentBlock.Transactions[0] = txCoinbase; if (srcTxs.Count < 4) { srcTxs.Add(currentBlock.Transactions[0]); } currentBlock.UpdateMerkleRoot(); currentBlock.Header.Nonce = blockinfo[i].nonce; chain.SetTip(currentBlock.Header); } // Just to make sure we can still make simple blocks blockDefinition = new PowBlockDefinition(consensus, dateTimeProvider, loggerFactory, mempool, mempoolLock, minerSettings, network, consensusRules); blockDefinition.Build(chain.Tip, scriptPubKey); var mempoolValidator = new MempoolValidator(mempool, mempoolLock, dateTimeProvider, new MempoolSettings(nodeSettings), chain, inMemoryCoinView, loggerFactory, nodeSettings, consensusRules); var outputs = new List <UnspentOutputs>(); foreach (Transaction tx in srcTxs) { var output = new UnspentOutputs(0, tx); outputs.Add(output); } inMemoryCoinView.SaveChanges(outputs, new List <TxOut[]>(), chain.GetHeader(1).HashBlock, chain.GetHeader(2).HashBlock, chain.GetHeader(0).Height); return(new TestChainContext { MempoolValidator = mempoolValidator, SrcTxs = srcTxs }); }
public TestPosConsensusRules(Network network, ILoggerFactory loggerFactory, IDateTimeProvider dateTimeProvider, ChainIndexer chainIndexer, NodeDeployments nodeDeployments, ConsensusSettings consensusSettings, ICheckpoints checkpoints, ICoinView uxtoSet, IStakeChain stakeChain, IStakeValidator stakeValidator, IChainState chainState, IInvalidBlockHashStore invalidBlockHashStore, INodeStats nodeStats, IRewindDataIndexCache rewindDataIndexCache, IAsyncProvider asyncProvider, ConsensusRulesContainer consensusRulesContainer) : base(network, loggerFactory, dateTimeProvider, chainIndexer, nodeDeployments, consensusSettings, checkpoints, uxtoSet, stakeChain, stakeValidator, chainState, invalidBlockHashStore, nodeStats, rewindDataIndexCache, asyncProvider, consensusRulesContainer) { this.ruleRegistrationHelper = new RuleRegistrationHelper(this, consensusRulesContainer); }
/// <summary> /// Creates the test chain with some default blocks and txs. /// </summary> /// <param name="network">Network to create the chain on.</param> /// <param name="scriptPubKey">Public key to create blocks/txs with.</param> /// <param name="requireStandard">By default testnet and regtest networks do not require transactions to be standard. This changes that default.</param> /// <returns>Context object representing the test chain.</returns> public static async Task <ITestChainContext> CreateAsync(Network network, Script scriptPubKey, string dataDir, bool requireStandard = true) { var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dataDir}" }); ILoggerFactory loggerFactory = nodeSettings.LoggerFactory; IDateTimeProvider dateTimeProvider = DateTimeProvider.Default; network.Consensus.Options = new ConsensusOptions(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (Type ruleType in network.Consensus.ConsensusRules.HeaderValidationRules) { // Don't check PoW of a header in this test. if (ruleType == typeof(CheckDifficultyPowRule)) { continue; } consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (Type ruleType in network.Consensus.ConsensusRules.PartialValidationRules) { consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule); } foreach (var ruleType in network.Consensus.ConsensusRules.FullValidationRules) { FullValidationConsensusRule rule = null; if (ruleType == typeof(FlushCoinviewRule)) { rule = new FlushCoinviewRule(new Mock <IInitialBlockDownloadState>().Object); } else { rule = Activator.CreateInstance(ruleType) as FullValidationConsensusRule; } consensusRulesContainer.FullValidationRules.Add(rule); } var consensusSettings = new ConsensusSettings(nodeSettings); var chain = new ChainIndexer(network); var inMemoryCoinView = new InMemoryCoinView(new HashHeightPair(chain.Tip)); var asyncProvider = new AsyncProvider(loggerFactory, new Mock <ISignals>().Object, new NodeLifetime()); var chainState = new ChainState(); var deployments = new NodeDeployments(network, chain); ConsensusRuleEngine consensusRules = new PowConsensusRuleEngine(network, loggerFactory, dateTimeProvider, chain, deployments, consensusSettings, new Checkpoints(), inMemoryCoinView, chainState, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, loggerFactory), asyncProvider, consensusRulesContainer).SetupRulesEngineParent(); ConsensusManager consensus = ConsensusManagerHelper.CreateConsensusManager(network, dataDir, chainState, chainIndexer: chain, consensusRules: consensusRules, inMemoryCoinView: inMemoryCoinView); var genesis = new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0); chainState.BlockStoreTip = genesis; await consensus.InitializeAsync(genesis).ConfigureAwait(false); var blockPolicyEstimator = new BlockPolicyEstimator(new MempoolSettings(nodeSettings), loggerFactory, nodeSettings); var mempool = new TxMempool(dateTimeProvider, blockPolicyEstimator, loggerFactory, nodeSettings); var mempoolLock = new MempoolSchedulerLock(); var minerSettings = new MinerSettings(nodeSettings); // Simple block creation, nothing special yet: var blockDefinition = new PowBlockDefinition(consensus, dateTimeProvider, loggerFactory, mempool, mempoolLock, minerSettings, network, consensusRules, deployments); BlockTemplate newBlock = blockDefinition.Build(chain.Tip, scriptPubKey); await consensus.BlockMinedAsync(newBlock.Block); List <BlockInfo> blockinfo = CreateBlockInfoList(); // We can't make transactions until we have inputs therefore, load 100 blocks. var srcTxs = new List <Transaction>(); for (int i = 0; i < blockinfo.Count; ++i) { Block currentBlock = Block.Load(newBlock.Block.ToBytes(network.Consensus.ConsensusFactory), network.Consensus.ConsensusFactory); currentBlock.Header.HashPrevBlock = chain.Tip.HashBlock; currentBlock.Header.Version = 1; currentBlock.Header.Time = Utils.DateTimeToUnixTime(chain.Tip.GetMedianTimePast()) + 1; Transaction txCoinbase = network.CreateTransaction(currentBlock.Transactions[0].ToBytes()); txCoinbase.Inputs.Clear(); txCoinbase.Version = 1; txCoinbase.AddInput(new TxIn(new Script(new[] { Op.GetPushOp(blockinfo[i].extraNonce), Op.GetPushOp(chain.Height) }))); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this) txCoinbase.AddOutput(new TxOut(Money.Zero, new Script())); currentBlock.Transactions[0] = txCoinbase; currentBlock.UpdateMerkleRoot(); currentBlock.Header.Nonce = blockinfo[i].nonce; chain.SetTip(currentBlock.Header); srcTxs.Add(currentBlock.Transactions[0]); inMemoryCoinView.SaveChanges(new List <UnspentOutput>() { new UnspentOutput(new OutPoint(currentBlock.Transactions[0], 0), new Coins((uint)(i + 1), currentBlock.Transactions[0].Outputs.First(), currentBlock.Transactions[0].IsCoinBase)) }, new HashHeightPair(chain.Tip.Previous), new HashHeightPair(chain.Tip)); } // Just to make sure we can still make simple blocks blockDefinition = new PowBlockDefinition(consensus, dateTimeProvider, loggerFactory, mempool, mempoolLock, minerSettings, network, consensusRules, deployments); blockDefinition.Build(chain.Tip, scriptPubKey); var mempoolSettings = new MempoolSettings(nodeSettings) { RequireStandard = requireStandard }; // The mempool rule constructors aren't parameterless, so we have to manually inject the dependencies for every rule var mempoolRules = new List <MempoolRule> { new CheckConflictsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckCoinViewMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CreateMempoolEntryMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, loggerFactory), new CheckSigOpsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckFeeMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckRateLimitMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckAncestorsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckReplacementMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckAllInputsMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, deployments, loggerFactory), new CheckTxOutDustRule(network, mempool, mempoolSettings, chain, loggerFactory), }; // We also have to check that the manually instantiated rules match the ones in the network, or the test isn't valid for (int i = 0; i < network.Consensus.MempoolRules.Count; i++) { if (network.Consensus.MempoolRules[i] != mempoolRules[i].GetType()) { throw new Exception("Mempool rule type mismatch"); } } Assert.Equal(network.Consensus.MempoolRules.Count, mempoolRules.Count); var mempoolValidator = new MempoolValidator(mempool, mempoolLock, dateTimeProvider, mempoolSettings, chain, inMemoryCoinView, loggerFactory, nodeSettings, consensusRules, mempoolRules, deployments); return(new TestChainContext { MempoolValidator = mempoolValidator, MempoolSettings = mempoolSettings, ChainIndexer = chain, SrcTxs = srcTxs }); }
public async Task InitializeAsync() { this.blockinfo = new List <Blockinfo>(); List <long> lst = blockinfoarr.Cast <long>().ToList(); for (int i = 0; i < lst.Count; i += 2) { this.blockinfo.Add(new Blockinfo { extranonce = (int)lst[i], nonce = (uint)lst[i + 1] }); } // Note that by default, these tests run with size accounting enabled. this.network = KnownNetworks.RegTest; byte[] hex = Encoders.Hex.DecodeData("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); this.scriptPubKey = new Script(new[] { Op.GetPushOp(hex), OpcodeType.OP_CHECKSIG }); this.entry = new TestMemPoolEntryHelper(); this.ChainIndexer = new ChainIndexer(this.network); this.network.Consensus.Options = new ConsensusOptions(); IDateTimeProvider dateTimeProvider = DateTimeProvider.Default; var loggerFactory = ExtendedLoggerFactory.Create(); var nodeSettings = new NodeSettings(this.network, args: new string[] { "-checkpoints" }); var consensusSettings = new ConsensusSettings(nodeSettings); var inMemoryCoinView = new InMemoryCoinView(new HashHeightPair(this.ChainIndexer.Tip)); var nodeStats = new NodeStats(dateTimeProvider, loggerFactory); this.cachedCoinView = new CachedCoinView(this.network, new Checkpoints(), inMemoryCoinView, dateTimeProvider, new LoggerFactory(), nodeStats, consensusSettings); var signals = new Signals.Signals(loggerFactory, null); var asyncProvider = new AsyncProvider(loggerFactory, signals, new NodeLifetime()); var deployments = new NodeDeployments(this.network, this.ChainIndexer); var genesis = this.network.GetGenesis(); var chainState = new ChainState() { BlockStoreTip = new ChainedHeader(genesis.Header, genesis.GetHash(), 0) }; var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in this.network.Consensus.ConsensusRules.HeaderValidationRules) { consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (var ruleType in this.network.Consensus.ConsensusRules.FullValidationRules) { FullValidationConsensusRule rule = null; if (ruleType == typeof(FlushUtxosetRule)) { rule = new FlushUtxosetRule(new Mock <IInitialBlockDownloadState>().Object); } else { rule = Activator.CreateInstance(ruleType) as FullValidationConsensusRule; } consensusRulesContainer.FullValidationRules.Add(rule); } this.ConsensusRules = new PowConsensusRuleEngine(this.network, loggerFactory, dateTimeProvider, this.ChainIndexer, deployments, consensusSettings, new Checkpoints(), this.cachedCoinView, chainState, new InvalidBlockHashStore(dateTimeProvider), nodeStats, asyncProvider, consensusRulesContainer).SetupRulesEngineParent(); this.consensus = ConsensusManagerHelper.CreateConsensusManager(this.network, chainState: chainState, inMemoryCoinView: inMemoryCoinView, chainIndexer: this.ChainIndexer, consensusRules: this.ConsensusRules); await this.consensus.InitializeAsync(chainState.BlockStoreTip); this.entry.Fee(11); this.entry.Height(11); var dateTimeProviderSet = new DateTimeProviderSet { time = dateTimeProvider.GetTime(), timeutc = dateTimeProvider.GetUtcNow() }; this.DateTimeProvider = dateTimeProviderSet; this.mempool = new TxMempool(dateTimeProvider, new BlockPolicyEstimator(new MempoolSettings(nodeSettings), loggerFactory, nodeSettings), loggerFactory, nodeSettings); this.mempoolLock = new MempoolSchedulerLock(); // We can't make transactions until we have inputs // Therefore, load 100 blocks :) this.baseheight = 0; var blocks = new List <Block>(); this.txFirst = new List <Transaction>(); this.nonce = 0; for (int i = 0; i < this.blockinfo.Count; ++i) { Block block = this.network.CreateBlock(); block.Header.HashPrevBlock = this.consensus.Tip.HashBlock; block.Header.Version = 1; block.Header.Time = Utils.DateTimeToUnixTime(this.ChainIndexer.Tip.GetMedianTimePast()) + 1; Transaction txCoinbase = this.network.CreateTransaction(); txCoinbase.Version = 1; txCoinbase.AddInput(new TxIn(new Script(new[] { Op.GetPushOp(this.blockinfo[i].extranonce), Op.GetPushOp(this.ChainIndexer.Height) }))); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this) txCoinbase.AddOutput(new TxOut(Money.Zero, new Script())); block.AddTransaction(txCoinbase); if (this.txFirst.Count == 0) { this.baseheight = this.ChainIndexer.Height; } if (this.txFirst.Count < 4) { this.txFirst.Add(block.Transactions[0]); } block.Header.Bits = block.Header.GetWorkRequired(this.network, this.ChainIndexer.Tip); block.UpdateMerkleRoot(); while (!block.CheckProofOfWork()) { block.Header.Nonce = ++this.nonce; } // Serialization sets the BlockSize property. block = Block.Load(block.ToBytes(), this.network.Consensus.ConsensusFactory); var res = await this.consensus.BlockMinedAsync(block); if (res == null) { throw new InvalidOperationException(); } blocks.Add(block); } // Just to make sure we can still make simple blocks this.newBlock = AssemblerForTest(this).Build(this.ChainIndexer.Tip, this.scriptPubKey); Assert.NotNull(this.newBlock); }
public static async Task <ITestChainContext> CreatePosAsync(Network network, Script scriptPubKey, string dataDir, bool requireStandard = true) { var nodeSettings = new NodeSettings(network, args: new string[] { $"-datadir={dataDir}" }); ILoggerFactory loggerFactory = nodeSettings.LoggerFactory; IDateTimeProvider dateTimeProvider = DateTimeProvider.Default; network.Consensus.Options = new ConsensusOptions(); var consensusRulesContainer = new ConsensusRulesContainer(); foreach (var ruleType in network.Consensus.ConsensusRules.HeaderValidationRules) { // Don't check PoW of a header in this test. if (ruleType == typeof(CheckDifficultyPowRule)) { continue; } consensusRulesContainer.HeaderValidationRules.Add(Activator.CreateInstance(ruleType) as HeaderValidationConsensusRule); } foreach (Type ruleType in network.Consensus.ConsensusRules.FullValidationRules) { consensusRulesContainer.FullValidationRules.Add(Activator.CreateInstance(ruleType) as FullValidationConsensusRule); } foreach (Type ruleType in network.Consensus.ConsensusRules.PartialValidationRules) { consensusRulesContainer.PartialValidationRules.Add(Activator.CreateInstance(ruleType) as PartialValidationConsensusRule); } var consensusSettings = new ConsensusSettings(nodeSettings); var chain = new ChainIndexer(network); var inMemoryCoinView = new InMemoryCoinView(new HashHeightPair(chain.Tip)); var chainState = new ChainState(); var deployments = new NodeDeployments(network, chain); var asyncProvider = new AsyncProvider(loggerFactory, new Mock <ISignals>().Object, new NodeLifetime()); var stakeChain = new StakeChainStore(network, chain, null, loggerFactory); ConsensusRuleEngine consensusRules = new PosConsensusRuleEngine(network, loggerFactory, dateTimeProvider, chain, deployments, consensusSettings, new Checkpoints(), inMemoryCoinView, stakeChain, new StakeValidator(network, stakeChain, chain, inMemoryCoinView, loggerFactory), chainState, new InvalidBlockHashStore(dateTimeProvider), new NodeStats(dateTimeProvider, loggerFactory), new RewindDataIndexCache(dateTimeProvider, network, new FinalizedBlockInfoRepository(new HashHeightPair()), new Checkpoints()), asyncProvider, consensusRulesContainer).SetupRulesEngineParent(); ConsensusManager consensus = ConsensusManagerHelper.CreateConsensusManager(network, dataDir, chainState, chainIndexer: chain, consensusRules: consensusRules, inMemoryCoinView: inMemoryCoinView); var genesis = new ChainedHeader(network.GetGenesis().Header, network.GenesisHash, 0); chainState.BlockStoreTip = genesis; await consensus.InitializeAsync(genesis).ConfigureAwait(false); var mempoolSettings = new MempoolSettings(nodeSettings) { RequireStandard = requireStandard }; var blockPolicyEstimator = new BlockPolicyEstimator(mempoolSettings, loggerFactory, nodeSettings); var mempool = new TxMempool(dateTimeProvider, blockPolicyEstimator, loggerFactory, nodeSettings); var mempoolLock = new MempoolSchedulerLock(); // The mempool rule constructors aren't parameterless, so we have to manually inject the dependencies for every rule var mempoolRules = new List <MempoolRule> { new CheckConflictsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckCoinViewMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CreateMempoolEntryMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, loggerFactory), new CheckSigOpsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckFeeMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckRateLimitMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckAncestorsMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckReplacementMempoolRule(network, mempool, mempoolSettings, chain, loggerFactory), new CheckAllInputsMempoolRule(network, mempool, mempoolSettings, chain, consensusRules, deployments, loggerFactory), new CheckTxOutDustRule(network, mempool, mempoolSettings, chain, loggerFactory), }; // We also have to check that the manually instantiated rules match the ones in the network, or the test isn't valid for (int i = 0; i < network.Consensus.MempoolRules.Count; i++) { if (network.Consensus.MempoolRules[i] != mempoolRules[i].GetType()) { throw new Exception("Mempool rule type mismatch"); } } var mempoolValidator = new MempoolValidator(mempool, mempoolLock, dateTimeProvider, mempoolSettings, chain, inMemoryCoinView, loggerFactory, nodeSettings, consensusRules, mempoolRules, deployments); var blocks = new List <Block>(); var srcTxs = new List <Transaction>(); DateTimeOffset now = DateTimeOffset.UtcNow; for (int i = 0; i < 50; i++) { uint nonce = 0; Block block = network.CreateBlock(); block.Header.HashPrevBlock = chain.Tip.HashBlock; block.Header.Bits = block.Header.GetWorkRequired(network, chain.Tip); block.Header.UpdateTime(now, network, chain.Tip); Transaction coinbase = network.CreateTransaction(); coinbase.AddInput(TxIn.CreateCoinbase(chain.Height + 1)); coinbase.AddOutput(new TxOut(network.Consensus.ProofOfWorkReward, scriptPubKey)); block.AddTransaction(coinbase); block.UpdateMerkleRoot(); while (!block.CheckProofOfWork()) { block.Header.Nonce = ++nonce; } block.Header.PrecomputeHash(); blocks.Add(block); chain.SetTip(block.Header); srcTxs.Add(block.Transactions[0]); inMemoryCoinView.SaveChanges(new List <UnspentOutput>() { new UnspentOutput(new OutPoint(block.Transactions[0], 0), new Coins((uint)(i + 1), block.Transactions[0].Outputs.First(), block.Transactions[0].IsCoinBase)) }, new HashHeightPair(chain.Tip.Previous), new HashHeightPair(chain.Tip)); } return(new TestChainContext { MempoolValidator = mempoolValidator, MempoolSettings = mempoolSettings, ChainIndexer = chain, SrcTxs = srcTxs }); }