예제 #1
0
        public void RetrieveDeposits_ReturnsSmallAndNormalDeposits_Scenario5()
        {
            // Create a "chain" of 20 blocks.
            this.blocks = ChainedHeadersHelper.CreateConsecutiveHeadersAndBlocks(20, true, this.mainChainNetwork);

            // Add 4 small deposits to blocks 5 through to 8 (the amounts are less than 10).
            for (int i = 5; i <= 8; i++)
            {
                this.blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, this.blocks[i].Block, Money.Coins(i), this.opReturnBytes);
            }

            // Add 6 normal deposits to block 11 through to 16.
            for (int i = 11; i <= 16; i++)
            {
                this.blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, this.blocks[i].Block, Money.Coins(i), this.opReturnBytes);
            }

            this.consensusManager.GetBlockData(Arg.Any <List <uint256> >()).Returns(delegate(CallInfo info)
            {
                var hashes = (List <uint256>)info[0];
                return(hashes.Select((hash) => this.blocks.Single(x => x.ChainedHeader.HashBlock == hash)).ToArray());
            });
            this.consensusManager.Tip.Returns(this.blocks.Last().ChainedHeader);

            var depositExtractor = new DepositExtractor(this.federatedPegSettings, this.network, this.opReturnDataReader);

            var maturedBlocksProvider = new MaturedBlocksProvider(this.consensusManager, depositExtractor, this.federatedPegSettings);

            SerializableResult <List <MaturedBlockDepositsModel> > depositsResult = maturedBlocksProvider.RetrieveDeposits(10);

            // Total deposits
            Assert.Equal(4, depositsResult.Value.SelectMany(b => b.Deposits).Count());
        }
예제 #2
0
        public void GetMaturedBlocksReturnsDeposits()
        {
            this.blocks = ChainedHeadersHelper.CreateConsecutiveHeadersAndBlocks(10, true, this.mainChainNetwork);

            ChainedHeader tip = this.blocks.Last().ChainedHeader;

            IFederatedPegSettings federatedPegSettings = Substitute.For <IFederatedPegSettings>();

            federatedPegSettings.MinimumConfirmationsNormalDeposits.Returns(0);

            var deposits = new List <IDeposit>()
            {
                new Deposit(new uint256(0), DepositRetrievalType.Normal, 100, "test", DestinationChain.STRAX, 0, new uint256(1))
            };

            // Set the first block up to return 100 normal deposits.
            IDepositExtractor depositExtractor = Substitute.For <IDepositExtractor>();

            depositExtractor.ExtractDepositsFromBlock(this.blocks.First().Block, this.blocks.First().ChainedHeader.Height, new[] { DepositRetrievalType.Normal }).ReturnsForAnyArgs(deposits);
            this.consensusManager.Tip.Returns(tip);

            // Makes every block a matured block.
            var maturedBlocksProvider = new MaturedBlocksProvider(this.consensusManager, depositExtractor, federatedPegSettings);

            SerializableResult <List <MaturedBlockDepositsModel> > depositsResult = maturedBlocksProvider.RetrieveDeposits(0);

            Assert.Equal(11, depositsResult.Value.Count);
        }
        public void GetMaturedBlocksReturnsDeposits()
        {
            List <ChainedHeaderBlock> blocks = ChainedHeadersHelper.CreateConsecutiveHeadersAndBlocks(10, null, true);

            ChainedHeader tip = blocks.Last().ChainedHeader;

            this.consensusManager.GetBlockData(Arg.Any <List <uint256> >()).Returns(delegate(CallInfo info)
            {
                var hashes = (List <uint256>)info[0];
                return(hashes.Select((hash) => blocks.Single(x => x.ChainedHeader.HashBlock == hash)).ToArray());
            });

            IFederatedPegSettings federatedPegSettings = Substitute.For <IFederatedPegSettings>();

            federatedPegSettings.MinimumConfirmationsNormalDeposits.Returns(0);

            var deposits = new List <IDeposit>()
            {
                new Deposit(new uint256(0), DepositRetrievalType.Normal, 100, "test", 0, new uint256(1))
            };

            IDepositExtractor depositExtractor = Substitute.For <IDepositExtractor>();

            depositExtractor.ExtractBlockDeposits(blocks.First(), DepositRetrievalType.Normal).ReturnsForAnyArgs(new MaturedBlockDepositsModel(new MaturedBlockInfoModel(), deposits));
            this.consensusManager.Tip.Returns(tip);

            // Makes every block a matured block.
            var maturedBlocksProvider = new MaturedBlocksProvider(this.consensusManager, depositExtractor, federatedPegSettings, this.loggerFactory);

            SerializableResult <List <MaturedBlockDepositsModel> > depositsResult = maturedBlocksProvider.RetrieveDeposits(0);

            // This will be double the amount of blocks because the mocked depositExtractor will always return a set of blocks
            // as that is how it has been configured.
            Assert.Equal(33, depositsResult.Value.Count);
        }
        public void RetrieveDeposits_ReturnsLargeDeposits_Scenario7()
        {
            // Create a "chain" of 40 blocks.
            List <ChainedHeaderBlock> blocks = ChainedHeadersHelper.CreateConsecutiveHeadersAndBlocks(40, null, true);

            // Add 4 small deposits to blocks 5 through to 8 (the amounts are less than 10).
            for (int i = 5; i <= 8; i++)
            {
                blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, blocks[i].Block, Money.Coins(i), this.opReturnBytes);
            }

            // Add 6 normal deposits to block 11 through to 16.
            for (int i = 11; i <= 16; i++)
            {
                blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, blocks[i].Block, Money.Coins(i), this.opReturnBytes);
            }

            // Add 6 large deposits to block 11 through to 16.
            for (int i = 11; i <= 16; i++)
            {
                blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, blocks[i].Block, Money.Coins((long)this.federatedPegSettings.NormalDepositThresholdAmount + 1), this.opReturnBytes);
            }

            this.consensusManager.GetBlockData(Arg.Any <List <uint256> >()).Returns(delegate(CallInfo info)
            {
                var hashes = (List <uint256>)info[0];
                return(hashes.Select((hash) => blocks.Single(x => x.ChainedHeader.HashBlock == hash)).ToArray());
            });
            this.consensusManager.Tip.Returns(blocks.Last().ChainedHeader);

            var depositExtractor = new DepositExtractor(this.loggerFactory, this.federatedPegSettings, this.opReturnDataReader);

            var maturedBlocksProvider = new MaturedBlocksProvider(this.consensusManager, depositExtractor, this.federatedPegSettings, this.loggerFactory);

            SerializableResult <List <MaturedBlockDepositsModel> > depositsResult = maturedBlocksProvider.RetrieveDeposits(10);

            // Total deposits
            Assert.Equal(12, depositsResult.Value.SelectMany(b => b.Deposits).Count());

            // Small Deposits
            Assert.Empty(depositsResult.Value.SelectMany(b => b.Deposits).Where(d => d.RetrievalType == DepositRetrievalType.Small));

            // Normal Deposits
            Assert.Equal(6, depositsResult.Value.SelectMany(b => b.Deposits).Where(d => d.RetrievalType == DepositRetrievalType.Normal).Count());

            // Large Deposits
            Assert.Equal(6, depositsResult.Value.SelectMany(b => b.Deposits).Where(d => d.RetrievalType == DepositRetrievalType.Large).Count());
        }
        public void RetrieveDeposits_ReturnsDataToAdvanceNextMaturedBlockHeight()
        {
            // Create a "chain" of 20 blocks.
            this.blocks = ChainedHeadersHelper.CreateConsecutiveHeadersAndBlocks(20, true, this.mainChainNetwork);

            // Add 6 normal deposits to block 11 through to 16.
            for (int i = 11; i < 17; i++)
            {
                this.blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, this.blocks[i].Block, Money.Coins(i), this.opReturnBytes);
            }

            // Add 4 faster deposits to blocks 5 through to 9 (the amounts are less than 10).
            for (int i = 5; i < 9; i++)
            {
                this.blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, this.blocks[i].Block, Money.Coins(i), this.opReturnBytes);
            }

            this.consensusManager.GetBlockData(Arg.Any<List<uint256>>()).Returns(delegate (CallInfo info)
            {
                var hashes = (List<uint256>)info[0];
                return hashes.Select((hash) => this.blocks.Single(x => x.ChainedHeader.HashBlock == hash && x.ChainedHeader.Height <= this.consensusManager.Tip.Height)).ToArray();
            });

            var depositExtractor = new DepositExtractor(this.federatedPegSettings, this.network, this.opReturnDataReader);

            var maturedBlocksProvider = new MaturedBlocksProvider(this.consensusManager, depositExtractor, this.federatedPegSettings);

            int nextMaturedBlockHeight = 1;
            for (int i = 1; i < this.blocks.Count; i++)
            {
                this.consensusManager.Tip.Returns(this.blocks[i].ChainedHeader);

                SerializableResult<List<MaturedBlockDepositsModel>> depositsResult = maturedBlocksProvider.RetrieveDeposits(nextMaturedBlockHeight);

                if (depositsResult?.Value != null && nextMaturedBlockHeight == depositsResult.Value.Min(b => b.BlockInfo.BlockHeight))
                {
                    nextMaturedBlockHeight = depositsResult.Value.Max(b => b.BlockInfo.BlockHeight) + 1;
                }
            }

            // Test whether the returned data is able to advance the NextMaturedBlockHeight.
            Assert.Equal(21, nextMaturedBlockHeight);
        }
예제 #6
0
        public void RetrieveDeposits_ReturnsLargeDeposits_Scenario6()
        {
            // Create a "chain" of 20 blocks.
            this.blocks = ChainedHeadersHelper.CreateConsecutiveHeadersAndBlocks(20, true, this.mainChainNetwork);

            // Add 4 small deposits to blocks 5 through to 8 (the amounts are less than 10).
            for (int i = 5; i <= 8; i++)
            {
                this.blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, this.blocks[i].Block, Money.Coins(i), this.opReturnBytes);
            }

            // Add 6 normal deposits to block 11 through to 16.
            for (int i = 11; i <= 16; i++)
            {
                this.blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, this.blocks[i].Block, Money.Coins(i), this.opReturnBytes);
            }

            // Add 6 large deposits to block 11 through to 16.
            for (int i = 11; i <= 16; i++)
            {
                this.blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, this.blocks[i].Block, Money.Coins((long)this.federatedPegSettings.NormalDepositThresholdAmount + 1), this.opReturnBytes);
            }

            this.consensusManager.Tip.Returns(this.blocks.Last().ChainedHeader);

            var depositExtractor = new DepositExtractor(this.federatedPegSettings, this.network, this.opReturnDataReader);

            var maturedBlocksProvider = new MaturedBlocksProvider(this.consensusManager, depositExtractor, this.federatedPegSettings);

            SerializableResult <List <MaturedBlockDepositsModel> > depositsResult = maturedBlocksProvider.RetrieveDeposits(10);

            // Total deposits
            Assert.Equal(4, depositsResult.Value.SelectMany(b => b.Deposits).Count());
        }
예제 #7
0
        public void RetrieveDeposits_ReturnsSmallAndNormalDeposits_Scenario3()
        {
            // Create a "chain" of 30 blocks.
            this.blocks = ChainedHeadersHelper.CreateConsecutiveHeadersAndBlocks(30, true, this.mainChainNetwork);

            // Add 6 small deposits to blocks 8 through to 13 (the amounts are less than 10).
            for (int i = 8; i <= 13; i++)
            {
                this.blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, this.blocks[i].Block, Money.Coins(8), this.opReturnBytes);
            }

            // Add 5 normal deposits to block 11 through to 15.
            for (int i = 11; i <= 15; i++)
            {
                this.blocks[i].Block.AddTransaction(new Transaction());
                CreateDepositTransaction(this.targetAddress, this.blocks[i].Block, Money.Coins(i), this.opReturnBytes);
            }

            this.consensusManager.Tip.Returns(this.blocks.Last().ChainedHeader);

            var depositExtractor = new DepositExtractor(this.federatedPegSettings, this.network, this.opReturnDataReader);

            var maturedBlocksProvider = new MaturedBlocksProvider(this.consensusManager, depositExtractor, this.federatedPegSettings);

            SerializableResult <List <MaturedBlockDepositsModel> > depositsResult = maturedBlocksProvider.RetrieveDeposits(5);

            // Total deposits
            Assert.Equal(11, depositsResult.Value.SelectMany(b => b.Deposits).Count());

            // Small Deposits
            Assert.Equal(6, depositsResult.Value.SelectMany(b => b.Deposits).Where(d => d.RetrievalType == DepositRetrievalType.Small).Count());

            // Normal Deposits
            Assert.Equal(5, depositsResult.Value.SelectMany(b => b.Deposits).Where(d => d.RetrievalType == DepositRetrievalType.Normal).Count());
        }