public void On_pending_trigger_works()
        {
            int triggered = 0;
            BuildBlocksWhenRequested trigger1  = new BuildBlocksWhenRequested();
            BuildBlocksWhenRequested trigger2  = new BuildBlocksWhenRequested();
            IBlockProductionTrigger  composite = trigger1.Or(trigger2);

            composite.TriggerBlockProduction += (s, e) => triggered++;
            trigger1.BuildBlock();
            trigger2.BuildBlock();
            trigger1.BuildBlock();
            trigger2.BuildBlock();

            triggered.Should().Be(4);
        }
        public void Manual_trigger_works()
        {
            bool triggered = false;
            BuildBlocksWhenRequested trigger = new BuildBlocksWhenRequested();

            trigger.TriggerBlockProduction += (s, e) => triggered = true;
            trigger.BuildBlock();
            triggered.Should().BeTrue();
        }
        public void Does_not_trigger_when_empty(int txCount, bool shouldTrigger)
        {
            var pool = Substitute.For <ITxPool>();

            pool.GetPendingTransactions().Returns(new Transaction[txCount]);
            bool triggered = false;
            BuildBlocksWhenRequested trigger       = new BuildBlocksWhenRequested();
            IBlockProductionTrigger  withCondition = trigger.IfPoolIsNotEmpty(pool);

            withCondition.TriggerBlockProduction += (s, e) => triggered = true;
            trigger.BuildBlock();
            triggered.Should().Be(shouldTrigger);
        }
Esempio n. 4
0
        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);
        }