public void ReceiveCurrentBlockTip_Should_Call_LeaderProdvider_Update()
        {
            var controller = new FederationGatewayController(
                this.loggerFactory,
                this.maturedBlockReceiver,
                this.maturedBlocksRequester,
                this.leaderProvider,
                this.chain,
                GetMaturedBlocksProvider(),
                this.depositExtractor,
                this.leaderReceiver);

            var model = new BlockTipModel(TestingValues.GetUint256(), TestingValues.GetPositiveInt(), TestingValues.GetPositiveInt());

            var leaderProviderCallCount = 0;

            this.leaderProvider.When(x => x.Update(Arg.Any <BlockTipModel>())).Do(info =>
            {
                leaderProviderCallCount++;
            });

            IActionResult result = controller.ReceiveCurrentBlockTip(model);

            result.Should().BeOfType <OkResult>();
            leaderProviderCallCount.Should().Be(1);
        }
Exemplo n.º 2
0
        public void PersistNewMaturedBlockDeposits_Should_Call_Store_And_Pass_Deposits_Upon_New_Block_Arrival_And_No_Deposits_In_Block()
        {
            int depositCount = 0;
            var deposits     = new List <IMaturedBlockDeposits[]>();

            deposits.Add(new[] { new MaturedBlockDepositsModel(new MaturedBlockModel()
                {
                    BlockHash   = 0,
                    BlockHeight = 0
                }, TestingValues.GetMaturedBlockDeposits(depositCount, new HashHeightPair(0, 0)).Deposits) });

            IObservable <IMaturedBlockDeposits[]> maturedBlockStream = deposits.ToObservable();

            this.maturedBlockReceiver.MaturedBlockDepositStream.Returns(maturedBlockStream);

            this.eventsPersister = new EventsPersister(this.loggerFactory, this.store, this.maturedBlockReceiver, this.maturedBlocksRequester);

            var indexedCallArguments = this.mockStore.Invocations
                                       .Where(i => i.Method.Name == nameof(ICrossChainTransferStore.RecordLatestMatureDepositsAsync))
                                       .Select((c, i) => new { Index = i, Deposits = ((IMaturedBlockDeposits[])c.Arguments[0])[0].Deposits }).ToList();

            indexedCallArguments.ForEach(
                ca =>
            {
                ca.Deposits.Count.Should().Be(depositCount);
                ca.Deposits.Select(d => d).Should().BeEquivalentTo(deposits[ca.Index][0].Deposits);
            });
        }
        public void ReceiveMaturedBlock_Should_Call_ReceivedMatureBlockDeposits()
        {
            var controller = new FederationGatewayController(
                this.loggerFactory,
                this.maturedBlockReceiver,
                this.maturedBlocksRequester,
                this.leaderProvider,
                this.chain,
                GetMaturedBlocksProvider(),
                this.depositExtractor,
                this.leaderReceiver);

            HashHeightPair hashHeightPair = TestingValues.GetHashHeightPair();
            var            deposits       = new MaturedBlockDepositsModel(new MaturedBlockModel()
            {
                BlockHash = hashHeightPair.Hash, BlockHeight = hashHeightPair.Height
            },
                                                                          new[] { new Deposit(0, Money.COIN * 10000, "TTMM7qGGxD5c77pJ8puBg7sTLAm2zZNBwK",
                                                                                              hashHeightPair.Height, hashHeightPair.Hash) });

            var callCount = 0;

            this.maturedBlockReceiver.When(x => x.ReceiveMaturedBlockDeposits(Arg.Any <IMaturedBlockDeposits[]>())).Do(info =>
            {
                callCount++;
            });

            controller.ReceiveMaturedBlock(deposits);
            callCount.Should().Be(1);
        }
        public BlockObserverTests()
        {
            this.minimumDepositConfirmations = 10;

            this.federationGatewaySettings = Substitute.For <IFederationGatewaySettings>();
            this.federationGatewaySettings.MinimumDepositConfirmations.Returns(this.minimumDepositConfirmations);

            this.federationWalletSyncManager = Substitute.For <IFederationWalletSyncManager>();
            this.federationGatewayClient     = Substitute.For <IFederationGatewayClient>();
            this.chain                = Substitute.ForPartsOf <ConcurrentChain>();
            this.loggerFactory        = Substitute.For <ILoggerFactory>();
            this.opReturnDataReader   = Substitute.For <IOpReturnDataReader>();
            this.consensusManager     = Substitute.For <IConsensusManager>();
            this.withdrawalExtractor  = Substitute.For <IWithdrawalExtractor>();
            this.extractedWithdrawals = TestingValues.GetWithdrawals(2);
            this.withdrawalExtractor.ExtractWithdrawalsFromBlock(null, 0).ReturnsForAnyArgs(this.extractedWithdrawals);

            this.withdrawalReceiver = Substitute.For <IWithdrawalReceiver>();

            this.signals = Substitute.For <ISignals>();
            this.signals.OnBlockConnected.Returns(Substitute.For <EventNotifier <ChainedHeaderBlock> >());

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

            this.blockObserver = new BlockObserver(
                this.federationWalletSyncManager,
                this.depositExtractor,
                this.withdrawalExtractor,
                this.withdrawalReceiver,
                this.federationGatewayClient,
                this.signals);
        }
        public async Task SendBlockTip_Should_Log_Error_When_Failing_To_Send_IBlockTipAsync()
        {
            var blockTip = new BlockTipModel(TestingValues.GetUint256(), TestingValues.GetPositiveInt(), TestingValues.GetPositiveInt());

            HttpResponseMessage result = await this.createClient(true).PushCurrentBlockTipAsync(blockTip).ConfigureAwait(false);

            Assert.False(result.IsSuccessStatusCode);
        }
        public async Task SendBlockTip_Should_Be_Able_To_Send_IBlockTipAsync()
        {
            var blockTip = new BlockTipModel(TestingValues.GetUint256(), TestingValues.GetPositiveInt(), TestingValues.GetPositiveInt());

            await this.createClient().PushCurrentBlockTipAsync(blockTip).ConfigureAwait(false);

            this.logger.Received(0).Log <object>(LogLevel.Error, 0, Arg.Any <object>(), Arg.Any <Exception>(), Arg.Any <Func <object, Exception, string> >());
        }
        public void ShouldSerialiseAsJson()
        {
            var maturedBlockDeposits = new MaturedBlockRequestModel(TestingValues.GetPositiveInt(), TestingValues.GetPositiveInt());
            var asJson = maturedBlockDeposits.ToString();

            var reconverted = JsonConvert.DeserializeObject <MaturedBlockRequestModel>(asJson);

            reconverted.BlockHeight.Should().Be(maturedBlockDeposits.BlockHeight);
            reconverted.MaxBlocksToSend.Should().Be(maturedBlockDeposits.MaxBlocksToSend);
        }
Exemplo n.º 8
0
        public static IBlockTip PrepareBlockTip()
        {
            uint256 blockHash          = TestingValues.GetUint256();
            int     blockHeight        = TestingValues.GetPositiveInt();
            int     matureConfirmation = TestingValues.GetPositiveInt();

            var blockTip = new BlockTipModel(blockHash, blockHeight, matureConfirmation);

            return(blockTip);
        }
Exemplo n.º 9
0
        public void ShouldSerialiseAsJson()
        {
            var maturedBlockDeposits = TestingValues.GetMaturedBlockDeposits(3);
            var asJson = maturedBlockDeposits.ToString();

            var reconverted = JsonConvert.DeserializeObject <MaturedBlockDepositsModel>(asJson);

            reconverted.Block.BlockHash.Should().Be(maturedBlockDeposits.Block.BlockHash);
            reconverted.Block.BlockHeight.Should().Be(maturedBlockDeposits.Block.BlockHeight);
            reconverted.Deposits.Should().BeEquivalentTo(maturedBlockDeposits.Deposits);
        }
Exemplo n.º 10
0
        public async Task SendBlockTip_Should_Log_Error_When_Failing_To_Send_IBlockTipAsync()
        {
            TestingHttpClient.PrepareFailingHttpClient(ref this.messageHandler, ref this.httpClient, ref this.httpClientFactory);

            var restSender = new RestBlockTipSender(this.loggerFactory, this.federationSettings, this.httpClientFactory);

            var blockTip = new BlockTipModel(TestingValues.GetUint256(), TestingValues.GetPositiveInt(), TestingValues.GetPositiveInt());

            await restSender.SendBlockTipAsync(blockTip).ConfigureAwait(false);

            this.logger.Received(1).Log <object>(LogLevel.Error, 0, Arg.Any <object>(), Arg.Is <Exception>(e => e != null), Arg.Any <Func <object, Exception, string> >());
        }
Exemplo n.º 11
0
        public async Task SendMaturedBlockDeposits_Should_Log_Error_When_Failing_To_Send_MaturedBlockDepositAsync()
        {
            TestingHttpClient.PrepareFailingHttpClient(ref this.messageHandler, ref this.httpClient, ref this.httpClientFactory);

            var maturedBlockDeposits = TestingValues.GetMaturedBlockDeposits();

            var restSender = new RestMaturedBlockSender(this.loggerFactory, this.federationSettings, this.httpClientFactory);

            await restSender.SendMaturedBlockDepositsAsync(maturedBlockDeposits).ConfigureAwait(false);

            this.logger.Received(1).Log <object>(LogLevel.Error, 0, Arg.Any <object>(), Arg.Is <Exception>(e => e != null), Arg.Any <Func <object, Exception, string> >());
        }
Exemplo n.º 12
0
        public void ShouldSerialiseAsJson()
        {
            IWithdrawal withdrawal = TestingValues.GetWithdrawal();

            string asJson      = withdrawal.ToString();
            var    reconverted = JsonConvert.DeserializeObject <Withdrawal>(asJson);

            reconverted.BlockHash.Should().Be(withdrawal.BlockHash);
            reconverted.Amount.Satoshi.Should().Be(withdrawal.Amount.Satoshi);
            reconverted.BlockNumber.Should().Be(withdrawal.BlockNumber);
            reconverted.Id.Should().Be(withdrawal.Id);
            reconverted.DepositId.Should().Be(withdrawal.DepositId);
            reconverted.TargetAddress.Should().Be(withdrawal.TargetAddress);
        }
Exemplo n.º 13
0
        AddWithdrawalToBlock(Block block)
        {
            var  targetScript               = this.addressHelper.GetNewTargetChainPaymentScript();
            var  opReturnDepositId          = TestingValues.GetUint256();
            long amount                     = 22 * Money.COIN;
            var  validWithdrawalTransaction = this.transactionBuilder.GetWithdrawalOutOfMultisigTo(
                targetScript,
                opReturnDepositId.ToBytes(),
                amount,
                true);

            block.AddTransaction(validWithdrawalTransaction);
            this.opReturnDataReader.TryGetTransactionId(validWithdrawalTransaction).Returns(opReturnDepositId.ToString());
            return(targetScript, opReturnDepositId, amount, validWithdrawalTransaction);
        }
Exemplo n.º 14
0
        public void ReceiveWithdrawals_Should_Push_An_Item_In_NewWithdrawalsOnTargetChainStream()
        {
            this.withdrawalReceiver = new WithdrawalReceiver();

            var receivedWithdrawalListCount = 0;

            this.streamSubscription = this.withdrawalReceiver.NewWithdrawalsOnTargetChainStream.Subscribe(
                _ => { Interlocked.Increment(ref receivedWithdrawalListCount); });

            var withdrawals = TestingValues.GetWithdrawals(3);

            this.withdrawalReceiver.ReceiveWithdrawals(withdrawals);

            receivedWithdrawalListCount.Should().Be(1);
        }
Exemplo n.º 15
0
        public BlockObserverTests()
        {
            this.minimumDepositConfirmations = 10;

            this.federationGatewaySettings = Substitute.For <IFederationGatewaySettings>();
            this.federationGatewaySettings.MinimumDepositConfirmations.Returns(this.minimumDepositConfirmations);

            this.leaderProvider = Substitute.For <ILeaderProvider>();
            this.federationWalletSyncManager = Substitute.For <IFederationWalletSyncManager>();
            this.fullNode              = Substitute.For <IFullNode>();
            this.maturedBlockSender    = Substitute.For <IMaturedBlockSender>();
            this.maturedBlocksProvider = Substitute.For <IMaturedBlocksProvider>();
            this.blockTipSender        = Substitute.For <IBlockTipSender>();
            this.chain = Substitute.ForPartsOf <ConcurrentChain>();
            this.fullNode.NodeService <ConcurrentChain>().Returns(this.chain);
            this.loggerFactory      = Substitute.For <ILoggerFactory>();
            this.opReturnDataReader = Substitute.For <IOpReturnDataReader>();

            this.withdrawalExtractor  = Substitute.For <IWithdrawalExtractor>();
            this.extractedWithdrawals = TestingValues.GetWithdrawals(2);
            this.withdrawalExtractor.ExtractWithdrawalsFromBlock(null, 0)
            .ReturnsForAnyArgs(this.extractedWithdrawals);

            this.withdrawalReceiver = Substitute.For <IWithdrawalReceiver>();

            this.depositExtractor = new DepositExtractor(
                this.loggerFactory,
                this.federationGatewaySettings,
                this.opReturnDataReader,
                this.fullNode);

            this.maturedBlocksProvider = new MaturedBlocksProvider(
                this.loggerFactory,
                this.chain,
                this.depositExtractor,
                Substitute.For <IBlockRepository>());

            this.blockObserver = new BlockObserver(
                this.federationWalletSyncManager,
                this.depositExtractor,
                this.withdrawalExtractor,
                this.withdrawalReceiver,
                this.maturedBlockSender,
                this.maturedBlocksProvider,
                this.blockTipSender);
        }
        public void ReceiveCurrentBlockTip_Should_Call_LeaderProdvider_Update()
        {
            FederationGatewayController controller = this.CreateController();

            var model = new BlockTipModel(TestingValues.GetUint256(), TestingValues.GetPositiveInt(), TestingValues.GetPositiveInt());

            int leaderProviderCallCount = 0;

            this.leaderProvider.When(x => x.Update(Arg.Any <BlockTipModel>())).Do(info =>
            {
                leaderProviderCallCount++;
            });

            IActionResult result = controller.PushCurrentBlockTip(model);

            result.Should().BeOfType <OkResult>();
            leaderProviderCallCount.Should().Be(1);
        }
Exemplo n.º 17
0
        public void PersistNewMaturedBlockDeposits_Should_Call_Store_And_Pass_Deposits_For_Each_Incomming_Matured_Block()
        {
            int blocksCount = 10;
            var deposits    = new List <IMaturedBlockDeposits[]>();

            for (int i = 0; i < blocksCount; i++)
            {
                deposits.Add(new[] { new MaturedBlockDepositsModel(new MaturedBlockModel()
                    {
                        BlockHash   = new uint256((ulong)i),
                        BlockHeight = i
                    }, TestingValues.GetMaturedBlockDeposits(i, new HashHeightPair((uint)i, i)).Deposits) });
            }

            IObservable <IMaturedBlockDeposits[]> maturedBlockStream = deposits.ToObservable();

            this.maturedBlockReceiver.MaturedBlockDepositStream.Returns(maturedBlockStream);

            int blockNum = 0;

            this.mockStore.SetupGet(o => o.NextMatureDepositHeight).Returns(() => blockNum);
            this.mockStore.Setup(mock => mock.RecordLatestMatureDepositsAsync(It.IsAny <IMaturedBlockDeposits[]>()))
            .Returns(() => Task <bool> .Run(() => { blockNum++; return(true); }));

            this.eventsPersister = new EventsPersister(this.loggerFactory, this.store, this.maturedBlockReceiver, this.maturedBlocksRequester);

            var indexedCallArguments = this.mockStore.Invocations
                                       .Where(i => i.Method.Name == nameof(ICrossChainTransferStore.RecordLatestMatureDepositsAsync))
                                       .Select((c, i) => new { Index = i, Deposits = ((IMaturedBlockDeposits[])c.Arguments[0])[0].Deposits }).ToList();

            indexedCallArguments.ForEach(
                ca =>
            {
                ca.Deposits.Count.Should().Be(ca.Index);
                ca.Deposits.Select(d => d).Should().BeEquivalentTo(deposits[ca.Index][0].Deposits);
            });
        }