Example #1
0
        public void TestUnsubscribe()
        {
            // initialize ChainStateMonitor
            using (var chainStateMonitor = new ChainStateMonitor(LogManager.CreateNullLogger()))
                using (chainStateMonitor.Start())
                {
                    try
                    {
                        // mock IChainStateVisitor
                        var visitor = new Mock <IChainStateVisitor>();

                        // keep track of BeginBlock calls
                        var wasVisited = false;
                        var visitEvent = new AutoResetEvent(false);
                        visitor.Setup(x => x.BeginBlock(It.IsAny <ChainedHeader>())).Callback(() => visitEvent.Set());

                        // subscribe visitor
                        using (var subscription = chainStateMonitor.Subscribe(visitor.Object))
                        {
                            // verify no BeginBlock calls
                            wasVisited = visitEvent.WaitOne(10);
                            Assert.IsFalse(wasVisited);

                            // call BeginBlock
                            chainStateMonitor.BeginBlock(null);

                            // verify BeginBlock call
                            wasVisited = visitEvent.WaitOne(100);
                            Assert.IsTrue(wasVisited);
                        }

                        // call BeginBlock, after unsubscribe
                        chainStateMonitor.BeginBlock(null);

                        // verify no additional BeginBlock call
                        wasVisited = visitEvent.WaitOne(10);
                        Assert.IsFalse(wasVisited);
                    }
                    finally
                    {
                        // wait for monitor
                        chainStateMonitor.CompleteAdding();
                        chainStateMonitor.WaitToComplete();
                    }
                }
        }
Example #2
0
        public ChainStateBuilder(ChainBuilder chain, Utxo parentUtxo, Logger logger, IKernel kernel, IBlockchainRules rules, BlockHeaderCache blockHeaderCache, BlockCache blockCache, SpentTransactionsCache spentTransactionsCache, SpentOutputsCache spentOutputsCache)
        {
            this.logger                 = logger;
            this.sha256                 = new SHA256Managed();
            this.rules                  = rules;
            this.blockHeaderCache       = blockHeaderCache;
            this.blockCache             = blockCache;
            this.spentTransactionsCache = spentTransactionsCache;
            this.spentOutputsCache      = spentOutputsCache;

            this.chainStateMonitor = new ChainStateMonitor(this.logger);
            this.scriptValidator   = new ScriptValidator(this.logger, this.rules);
            this.chainStateMonitor.Subscribe(this.scriptValidator);

            this.chain = chain;
            this.chainStateBuilderStorage = kernel.Get <IChainStateBuilderStorage>(new ConstructorArgument("parentUtxo", parentUtxo.Storage));

            this.spentTransactions = ImmutableList.CreateBuilder <KeyValuePair <UInt256, SpentTx> >();
            this.spentOutputs      = ImmutableList.CreateBuilder <KeyValuePair <TxOutputKey, TxOutput> >();

            this.stats = new BuilderStats();
        }