src/Stratis.FederatedPeg.Tests/CrossChainTransferStoreTests.cs
/// <summary>
/// Tests whether the store merges signatures as expected.
/// </summary>
Tests whether the store merges signatures as expected.
Tests whether the store merges signatures as expected .
[Fact]
        public void StoreMergesSignaturesAsExpected()
        {
            var dataFolder = new DataFolder(CreateTestDir(this));
            this.Init(dataFolder);
            this.AddFunding();
            this.AppendBlocks(this.federationGatewaySettings.MinCoinMaturity);
            using (ICrossChainTransferStore crossChainTransferStore = this.CreateStore())
            {
                crossChainTransferStore.Initialize();
                crossChainTransferStore.Start();
                Assert.Equal(this.chain.Tip.HashBlock, crossChainTransferStore.TipHashAndHeight.HashBlock);
                Assert.Equal(this.chain.Tip.Height, crossChainTransferStore.TipHashAndHeight.Height);
                BitcoinAddress address = (new Key()).PubKey.Hash.GetAddress(this.network);
                var deposit = new Deposit(0, new Money(160m, MoneyUnit.BTC), address.ToString(), crossChainTransferStore.NextMatureDepositHeight, 1);
                MaturedBlockDepositsModel[] blockDeposits = new[] { new MaturedBlockDepositsModel(
                    new MaturedBlockInfoModel() {
                        BlockHash = 1,
                        BlockHeight = crossChainTransferStore.NextMatureDepositHeight },
                    new[] { deposit })
                };
                crossChainTransferStore.RecordLatestMatureDepositsAsync(blockDeposits).GetAwaiter().GetResult();
                ICrossChainTransfer crossChainTransfer = crossChainTransferStore.GetAsync(new[] { deposit.Id }).GetAwaiter().GetResult().SingleOrDefault();
                Assert.NotNull(crossChainTransfer);
                Transaction transaction = crossChainTransfer.PartialTransaction;
                Assert.True(crossChainTransferStore.ValidateTransaction(transaction));
                Transaction transaction2;
                var newTest = new CrossChainTransferStoreTests(this.network);
                var dataFolder2 = new DataFolder(CreateTestDir(this));
                newTest.federationKeys = this.federationKeys;
                newTest.SetExtendedKey(1);
                newTest.Init(dataFolder2);
                for (int i = 1; i <= this.chain.Height; i++)
                {
                    ChainedHeader header = this.chain.GetBlock(i);
                    Block block = this.blockDict[header.HashBlock];
                    newTest.AppendBlock(block);
                }
                using (ICrossChainTransferStore crossChainTransferStore2 = newTest.CreateStore())
                {
                    crossChainTransferStore2.Initialize();
                    crossChainTransferStore2.Start();
                    Assert.Equal(newTest.chain.Tip.HashBlock, crossChainTransferStore2.TipHashAndHeight.HashBlock);
                    Assert.Equal(newTest.chain.Tip.Height, crossChainTransferStore2.TipHashAndHeight.Height);
                    crossChainTransferStore2.RecordLatestMatureDepositsAsync(blockDeposits).GetAwaiter().GetResult();
                    ICrossChainTransfer crossChainTransfer2 = crossChainTransferStore2.GetAsync(new[] { deposit.Id }).GetAwaiter().GetResult().SingleOrDefault();
                    Assert.NotNull(crossChainTransfer2);
                    transaction2 = crossChainTransfer2.PartialTransaction;
                    Assert.True(crossChainTransferStore2.ValidateTransaction(transaction2));
                }
                crossChainTransferStore.MergeTransactionSignaturesAsync(deposit.Id, new[] { transaction2 }).GetAwaiter().GetResult();
                crossChainTransfer = crossChainTransferStore.GetAsync(new[] { deposit.Id }).GetAwaiter().GetResult().SingleOrDefault();
                Assert.NotNull(crossChainTransfer);
                Assert.Equal(CrossChainTransferStatus.FullySigned, crossChainTransfer.Status);
                Transaction signedTransaction = crossChainTransferStore.GetTransactionsByStatusAsync(CrossChainTransferStatus.FullySigned).GetAwaiter().GetResult().Values.SingleOrDefault();
                Assert.NotNull(signedTransaction);
                Assert.True(crossChainTransferStore.ValidateTransaction(signedTransaction, true));
            }
        }
[ Fact ] public void StoreMergesSignaturesAsExpected ( ) { var dataFolder = new DataFolder ( CreateTestDir ( this ) ) ; this . Init ( dataFolder ) ; this . AddFunding ( ) ; this . AppendBlocks ( this . federationGatewaySettings . MinCoinMaturity ) ; using ( ICrossChainTransferStore crossChainTransferStore = this . CreateStore ( ) ) { crossChainTransferStore . Initialize ( ) ; crossChainTransferStore . Start ( ) ; Assert . Equal ( this . chain . Tip . HashBlock , crossChainTransferStore . TipHashAndHeight . HashBlock ) ; Assert . Equal ( this . chain . Tip . Height , crossChainTransferStore . TipHashAndHeight . Height ) ; BitcoinAddress address = ( new Key ( ) ) . PubKey . Hash . GetAddress ( this . network ) ; var deposit = new Deposit ( 0 , new Money ( 160m , MoneyUnit . BTC ) , address . ToString ( ) , crossChainTransferStore . NextMatureDepositHeight , 1 ) ; MaturedBlockDepositsModel [ ] blockDeposits = new [ ] { new MaturedBlockDepositsModel ( new MaturedBlockInfoModel ( ) { BlockHash = 1 , BlockHeight = crossChainTransferStore . NextMatureDepositHeight } , new [ ] { deposit } ) } ; crossChainTransferStore . RecordLatestMatureDepositsAsync ( blockDeposits ) . GetAwaiter ( ) . GetResult ( ) ; ICrossChainTransfer crossChainTransfer = crossChainTransferStore . GetAsync ( new [ ] { deposit . Id } ) . GetAwaiter ( ) . GetResult ( ) . SingleOrDefault ( ) ; Assert . NotNull ( crossChainTransfer ) ; Transaction transaction = crossChainTransfer . PartialTransaction ; Assert . True ( crossChainTransferStore . ValidateTransaction ( transaction ) ) ; Transaction transaction2 ; var newTest = new CrossChainTransferStoreTests ( this . network ) ; var dataFolder2 = new DataFolder ( CreateTestDir ( this ) ) ; newTest . federationKeys = this . federationKeys ; newTest . SetExtendedKey ( 1 ) ; newTest . Init ( dataFolder2 ) ; for ( int i = 1 ; i <= this . chain . Height ; i ++ ) { ChainedHeader header = this . chain . GetBlock ( i ) ; Block block = this . blockDict [ header . HashBlock ] ; newTest . AppendBlock ( block ) ; } using ( ICrossChainTransferStore crossChainTransferStore2 = newTest . CreateStore ( ) ) { crossChainTransferStore2 . Initialize ( ) ; crossChainTransferStore2 . Start ( ) ; Assert . Equal ( newTest . chain . Tip . HashBlock , crossChainTransferStore2 . TipHashAndHeight . HashBlock ) ; Assert . Equal ( newTest . chain . Tip . Height , crossChainTransferStore2 . TipHashAndHeight . Height ) ; crossChainTransferStore2 . RecordLatestMatureDepositsAsync ( blockDeposits ) . GetAwaiter ( ) . GetResult ( ) ; ICrossChainTransfer crossChainTransfer2 = crossChainTransferStore2 . GetAsync ( new [ ] { deposit . Id } ) . GetAwaiter ( ) . GetResult ( ) . SingleOrDefault ( ) ; Assert . NotNull ( crossChainTransfer2 ) ; transaction2 = crossChainTransfer2 . PartialTransaction ; Assert . True ( crossChainTransferStore2 . ValidateTransaction ( transaction2 ) ) ; } crossChainTransferStore . MergeTransactionSignaturesAsync ( deposit . Id , new [ ] { transaction2 } ) . GetAwaiter ( ) . GetResult ( ) ; crossChainTransfer = crossChainTransferStore . GetAsync ( new [ ] { deposit . Id } ) . GetAwaiter ( ) . GetResult ( ) . SingleOrDefault ( ) ; Assert . NotNull ( crossChainTransfer ) ; Assert . Equal ( CrossChainTransferStatus . FullySigned , crossChainTransfer . Status ) ; Transaction signedTransaction = crossChainTransferStore . GetTransactionsByStatusAsync ( CrossChainTransferStatus . FullySigned ) . GetAwaiter ( ) . GetResult ( ) . Values . SingleOrDefault ( ) ; Assert . NotNull ( signedTransaction ) ; Assert . True ( crossChainTransferStore . ValidateTransaction ( signedTransaction , true ) ) ; } }
Tests whether the store merges signatures as expected.
Tests whether the store merges signatures as expected .
// Create a separate instance to generate another transaction. // Clone chain // Merges the transaction signatures. // Test the outcome. // Should be returned as signed. // Check ths signature.