public void ProcessBlock_NewBlock__BlockOnBestChain_ReOrgWalletManagerUsingBlockStoreCache()
        {
            (ChainIndexer Chain, List <Block> Blocks)result = WalletTestsHelpers.GenerateChainAndBlocksWithHeight(5, KnownNetworks.StratisMain);
            this.chainIndexer = result.Chain;
            List <Block> blocks            = result.Blocks;
            var          walletSyncManager = new WalletSyncManagerOverride(this.LoggerFactory.Object, this.walletManager.Object, this.chainIndexer, KnownNetworks.StratisMain,
                                                                           this.blockStore.Object, this.storeSettings, this.signals, this.asyncProvider);

            // setup blockstore to return blocks on the chain.
            this.blockStore.Setup(b => b.GetBlock(It.IsAny <uint256>()))
            .Returns((uint256 hashblock) =>
            {
                return(blocks.Single(b => b.GetHash() == hashblock));
            });

            // set 2nd block as tip
            walletSyncManager.SetWalletTip(this.chainIndexer.GetHeader(2));
            //process 4th block in the list does not have same prevhash as which is loaded
            Block blockToProcess = blocks[3];

            blockToProcess.SetPrivatePropertyValue("BlockSize", 1L);

            walletSyncManager.ProcessBlock(blockToProcess);

            this.AssertTipBlockHash(walletSyncManager, 4);

            //verify manager processes each missing block until caught up.
            // height 3
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(blocks[2]), ExpectChainedBlock(this.chainIndexer.GetHeader(3))));
            // height 4
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(blocks[3]), ExpectChainedBlock(this.chainIndexer.GetHeader(4))), Times.Exactly(2));
        }
Ejemplo n.º 2
0
        public void ProcessBlock_NewBlock__BlockOnBestChain_ReOrgWalletManagerUsingBlockStoreCache()
        {
            (ConcurrentChain Chain, List <Block> Blocks)result = WalletTestsHelpers.GenerateChainAndBlocksWithHeight(5, Network.StratisMain);
            this.chain = result.Chain;
            List <Block> blocks            = result.Blocks;
            var          walletSyncManager = new WalletSyncManagerOverride(this.LoggerFactory.Object, this.walletManager.Object, this.chain, Network.StratisMain,
                                                                           this.blockStoreCache.Object, this.storeSettings, this.nodeLifetime.Object);

            // setup blockstorecache to return blocks on the chain.
            this.blockStoreCache.Setup(b => b.GetBlockAsync(It.IsAny <uint256>()))
            .ReturnsAsync((uint256 hashblock) =>
            {
                return(blocks.Single(b => b.GetHash() == hashblock));
            });

            // set 2nd block as tip
            walletSyncManager.SetWalletTip(this.chain.GetBlock(2));
            //process 4th block in the list does not have same prevhash as which is loaded
            Block blockToProcess = blocks[3];

            walletSyncManager.ProcessBlock(blockToProcess);

            uint256 expectedBlockHash = this.chain.GetBlock(4).Header.GetHash();

            Assert.Equal(expectedBlockHash, walletSyncManager.WalletTip.Header.GetHash());
            //verify manager processes each missing block until caught up.
            // height 3
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(blocks[2]), ExpectChainedBlock(this.chain.GetBlock(3))));
            // height 4
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(blocks[3]), ExpectChainedBlock(this.chain.GetBlock(4))), Times.Exactly(2));
        }
        public void ProcessBlock_With_No_Wallet_Processing_Is_Ignored()
        {
            (ChainIndexer Chain, List <Block> Blocks)result = WalletTestsHelpers.GenerateChainAndBlocksWithHeight(1, KnownNetworks.StratisMain);

            this.chainIndexer = result.Chain;

            this.walletManager.Setup(w => w.ContainsWallets).Returns(false);

            var walletSyncManager = new WalletSyncManagerOverride(this.LoggerFactory.Object, this.walletManager.Object, this.chainIndexer, KnownNetworks.StratisMain,
                                                                  this.blockStore.Object, this.storeSettings, this.signals, this.asyncProvider);

            walletSyncManager.SetWalletTip(this.chainIndexer.GetHeader(1));

            walletSyncManager.ProcessBlock(result.Blocks[0]);

            this.walletManager.Verify(w => w.ProcessBlock(It.IsAny <Block>(), It.IsAny <ChainedHeader>()), Times.Never);
        }
Ejemplo n.º 4
0
        public void ProcessBlock_NewBlock_BlockArrivesLateInBlockStoreCache_ReOrgWalletManagerUsingBlockStoreCache()
        {
            var result = WalletTestsHelpers.GenerateChainAndBlocksWithHeight(5, Network.PurpleMain);

            this.chain = result.Chain;
            var blocks            = result.Blocks;
            var walletSyncManager = new WalletSyncManagerOverride(this.LoggerFactory.Object, this.walletManager.Object, this.chain, Network.PurpleMain,
                                                                  this.blockStoreCache.Object, this.storeSettings, this.nodeLifetime.Object);
            var blockEmptyCounters = new Dictionary <uint256, int>();

            // setup blockstorecache to return blocks on the chain but postpone by 3 rounds for each block.
            this.blockStoreCache.Setup(b => b.GetBlockAsync(It.IsAny <uint256>()))
            .ReturnsAsync((uint256 hashblock) =>
            {
                if (!blockEmptyCounters.ContainsKey(hashblock))
                {
                    blockEmptyCounters.Add(hashblock, 0);
                }

                if (blockEmptyCounters[hashblock] < 3)
                {
                    blockEmptyCounters[hashblock] += 1;
                    return(null);
                }
                else
                {
                    return(blocks.Single(b => b.GetHash() == hashblock));
                }
            });

            // set 2nd block as tip
            walletSyncManager.SetWalletTip(this.chain.GetBlock(2));
            //process 4th block in the list  does not have same prevhash as which is loaded
            var blockToProcess = blocks[3];

            walletSyncManager.ProcessBlock(blockToProcess);

            var expectedBlockHash = this.chain.GetBlock(4).Header.GetHash();

            Assert.Equal(expectedBlockHash, walletSyncManager.WalletTip.Header.GetHash());
            //verify manager processes each missing block until caught up.
            // height 3
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(blocks[2]), ExpectChainedBlock(this.chain.GetBlock(3))));
            // height 4
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(blocks[3]), ExpectChainedBlock(this.chain.GetBlock(4))), Times.Exactly(2));
        }
Ejemplo n.º 5
0
        public void ProcessBlock_NewBlock_BlockArrivesLateInBlockStoreCache_ReOrgWalletManagerUsingBlockStoreCache()
        {
            (ConcurrentChain Chain, List <Block> Blocks)result = WalletTestsHelpers.GenerateChainAndBlocksWithHeight(5, KnownNetworks.StratisMain);
            this.chain = result.Chain;
            List <Block> blocks            = result.Blocks;
            var          walletSyncManager = new WalletSyncManagerOverride(this.LoggerFactory.Object, this.walletManager.Object, this.chain, KnownNetworks.StratisMain,
                                                                           this.blockStore.Object, this.storeSettings, this.signals);
            var blockEmptyCounters = new Dictionary <uint256, int>();

            // setup blockstore to return blocks on the chain but postpone by 3 rounds for each block.
            this.blockStore.Setup(b => b.GetBlockAsync(It.IsAny <uint256>()))
            .ReturnsAsync((uint256 hashblock) =>
            {
                if (!blockEmptyCounters.ContainsKey(hashblock))
                {
                    blockEmptyCounters.Add(hashblock, 0);
                }

                if (blockEmptyCounters[hashblock] < 3)
                {
                    blockEmptyCounters[hashblock] += 1;
                    return(null);
                }
                else
                {
                    return(blocks.Single(b => b.GetHash() == hashblock));
                }
            });

            // set 2nd block as tip
            walletSyncManager.SetWalletTip(this.chain.GetBlock(2));
            //process 4th block in the list  does not have same prevhash as which is loaded
            Block blockToProcess = blocks[3];

            blockToProcess.SetPrivatePropertyValue("BlockSize", 1L);

            walletSyncManager.ProcessBlock(blockToProcess);

            this.AssertTipBlockHash(walletSyncManager, 4);

            //verify manager processes each missing block until caught up.
            // height 3
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(blocks[2]), ExpectChainedBlock(this.chain.GetBlock(3))));
            // height 4
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(blocks[3]), ExpectChainedBlock(this.chain.GetBlock(4))), Times.Exactly(2));
        }
Ejemplo n.º 6
0
        public void ProcessBlock_NewBlock_PreviousHashSameAsWalletTip_PassesBlockToManagerWithoutReorg()
        {
            (ConcurrentChain Chain, List <Block> Blocks)result = WalletTestsHelpers.GenerateChainAndBlocksWithHeight(5, Network.StratisMain);
            this.chain = result.Chain;
            List <Block> blocks            = result.Blocks;
            var          walletSyncManager = new WalletSyncManagerOverride(this.LoggerFactory.Object, this.walletManager.Object, this.chain, Network.StratisMain,
                                                                           this.blockStoreCache.Object, this.storeSettings, this.nodeLifetime.Object);

            walletSyncManager.SetWalletTip(this.chain.GetBlock(3));

            Block blockToProcess = blocks[3];

            walletSyncManager.ProcessBlock(blockToProcess); //4th block in the list has same prevhash as which is loaded

            uint256 expectedBlockHash = this.chain.GetBlock(4).Header.GetHash();

            Assert.Equal(expectedBlockHash, walletSyncManager.WalletTip.Header.GetHash());
            this.walletManager.Verify(w => w.ProcessBlock(It.Is <Block>(b => b.GetHash() == blockToProcess.GetHash()), It.Is <ChainedHeader>(c => c.Header.GetHash() == expectedBlockHash)));
        }
        public void ProcessBlock_NewBlock_BlockNotOnBestChain_ReOrgWalletManagerUsingBlockStoreCache()
        {
            (ChainIndexer LeftChain, ChainIndexer RightChain, List <Block> LeftForkBlocks, List <Block> RightForkBlocks)result = WalletTestsHelpers.GenerateForkedChainAndBlocksWithHeight(5, KnownNetworks.StratisMain, 2);
            // left side chain containing the 'old' fork.
            ChainIndexer leftChainIndexer = result.LeftChain;

            // right side chain containing the 'new' fork. Work on this.
            this.chainIndexer = result.RightChain;
            var walletSyncManager = new WalletSyncManagerOverride(this.LoggerFactory.Object, this.walletManager.Object, this.chainIndexer, KnownNetworks.StratisMain,
                                                                  this.blockStore.Object, this.storeSettings, this.signals, this.asyncProvider);

            // setup blockstore to return blocks on the chain.
            this.blockStore.Setup(b => b.GetBlock(It.IsAny <uint256>()))
            .Returns((uint256 hashblock) =>
            {
                return(result.LeftForkBlocks.Union(result.RightForkBlocks).Single(b => b.GetHash() == hashblock));
            });

            // set 4th block of the old chain as tip. 2 ahead of the fork thus not being on the right chain.
            walletSyncManager.SetWalletTip(leftChainIndexer.GetHeader(result.LeftForkBlocks[3].Header.GetHash()));
            //process 5th block from the right side of the fork in the list does not have same prevhash as which is loaded.
            Block blockToProcess = result.RightForkBlocks[4];

            blockToProcess.SetPrivatePropertyValue("BlockSize", 1L);

            walletSyncManager.ProcessBlock(blockToProcess);

            this.AssertTipBlockHash(walletSyncManager, 5);

            // walletmanager removes all blocks up to the fork.
            this.walletManager.Verify(w => w.RemoveBlocks(ExpectChainedBlock(this.chainIndexer.GetHeader(2))));

            //verify manager processes each missing block until caught up.
            // height 3
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(result.RightForkBlocks[2]), ExpectChainedBlock(this.chainIndexer.GetHeader(3))));
            // height 4
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(result.RightForkBlocks[3]), ExpectChainedBlock(this.chainIndexer.GetHeader(4))));
            // height 5
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(result.RightForkBlocks[4]), ExpectChainedBlock(this.chainIndexer.GetHeader(5))), Times.Exactly(2));
        }
        public void ProcessBlock_NewBlock_PreviousHashSameAsWalletTip_PassesBlockToManagerWithoutReorg()
        {
            (ChainIndexer Chain, List <Block> Blocks)result = WalletTestsHelpers.GenerateChainAndBlocksWithHeight(5, KnownNetworks.StratisMain);
            this.chainIndexer = result.Chain;
            List <Block> blocks            = result.Blocks;
            var          walletSyncManager = new WalletSyncManagerOverride(this.LoggerFactory.Object, this.walletManager.Object, this.chainIndexer, KnownNetworks.StratisMain,
                                                                           this.blockStore.Object, this.storeSettings, this.signals, this.asyncProvider);

            walletSyncManager.SetWalletTip(this.chainIndexer.GetHeader(3));

            Block blockToProcess = blocks[3];

            blockToProcess.SetPrivatePropertyValue("BlockSize", 1L);

            walletSyncManager.ProcessBlock(blockToProcess); //4th block in the list has same prevhash as which is loaded

            uint256 expectedBlockHash = this.AssertTipBlockHash(walletSyncManager, 4);

            this.AssertTipBlockHash(walletSyncManager, 4);

            this.walletManager.Verify(w => w.ProcessBlock(It.Is <Block>(b => b.GetHash() == blockToProcess.GetHash()), It.Is <ChainedHeader>(c => c.Header.GetHash() == expectedBlockHash)));
        }
Ejemplo n.º 9
0
        public void ProcessBlock_NewBlock_BlockNotOnBestChain_ReOrgWalletManagerUsingBlockStoreCache()
        {
            (ConcurrentChain LeftChain, ConcurrentChain RightChain, List <Block> LeftForkBlocks, List <Block> RightForkBlocks)result = WalletTestsHelpers.GenerateForkedChainAndBlocksWithHeight(5, Network.StratisMain, 2);
            // left side chain containing the 'old' fork.
            ConcurrentChain leftChain = result.LeftChain;

            // right side chain containing the 'new' fork. Work on this.
            this.chain = result.RightChain;
            var walletSyncManager = new WalletSyncManagerOverride(this.LoggerFactory.Object, this.walletManager.Object, this.chain, Network.StratisMain,
                                                                  this.blockStoreCache.Object, this.storeSettings, this.nodeLifetime.Object);

            // setup blockstorecache to return blocks on the chain.
            this.blockStoreCache.Setup(b => b.GetBlockAsync(It.IsAny <uint256>()))
            .ReturnsAsync((uint256 hashblock) =>
            {
                return(result.LeftForkBlocks.Union(result.RightForkBlocks).Single(b => b.GetHash() == hashblock));
            });

            // set 4th block of the old chain as tip. 2 ahead of the fork thus not being on the right chain.
            walletSyncManager.SetWalletTip(leftChain.GetBlock(result.LeftForkBlocks[3].Header.GetHash()));
            //process 5th block from the right side of the fork in the list does not have same prevhash as which is loaded.
            Block blockToProcess = result.RightForkBlocks[4];

            walletSyncManager.ProcessBlock(blockToProcess);

            // walletmanager removes all blocks up to the fork.
            this.walletManager.Verify(w => w.RemoveBlocks(ExpectChainedBlock(this.chain.GetBlock(2))));
            uint256 expectedBlockHash = this.chain.GetBlock(5).Header.GetHash();

            Assert.Equal(expectedBlockHash, walletSyncManager.WalletTip.Header.GetHash());

            //verify manager processes each missing block until caught up.
            // height 3
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(result.RightForkBlocks[2]), ExpectChainedBlock(this.chain.GetBlock(3))));
            // height 4
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(result.RightForkBlocks[3]), ExpectChainedBlock(this.chain.GetBlock(4))));
            // height 5
            this.walletManager.Verify(w => w.ProcessBlock(ExpectBlock(result.RightForkBlocks[4]), ExpectChainedBlock(this.chain.GetBlock(5))), Times.Exactly(2));
        }