public async Task TxCommitQuantumTest(KeyPair alphaKeyPair, ConnectionState state, Type excpectedException)
        {
            context.AppState.State = ApplicationState.Ready;

            var clientConnection = new AuditorWebSocketConnection(context, new FakeAuditorConnectionInfo(new FakeWebSocket()))
            {
                ConnectionState = state
            };

            var ledgerNotification = new TxNotification
            {
                TxCursor = 0,
                Payments = new List <PaymentBase>()
            };

            var envelope = new TxCommitQuantum
            {
                Source = ledgerNotification.CreateEnvelope()
            }.CreateEnvelope();

            envelope.Sign(alphaKeyPair);
            using var writer = new XdrBufferWriter();
            var inMessage = envelope.ToIncomingMessage(writer);

            await AssertMessageHandling(clientConnection, inMessage, excpectedException);
        }
        public async Task TxCommitQuantumTest(KeyPair alphaKeyPair, ConnectionState state, Type excpectedException)
        {
            Global.AppState.State = ApplicationState.Ready;

            var clientConnection = new AuditorWebSocketConnection(new FakeWebSocket(), null)
            {
                ConnectionState = state
            };

            var ledgerNotification = new TxNotification
            {
                TxCursor = 0,
                Payments = new List <PaymentBase>()
            };

            var envelope = new TxCommitQuantum
            {
                Source = ledgerNotification.CreateEnvelope()
            }.CreateEnvelope();

            envelope.Sign(alphaKeyPair);

            await AssertMessageHandling(clientConnection, envelope, excpectedException);
        }
        public async Task TxCommitQuantumTest(int cursor, int amount, int asset, Type excpectedException)
        {
            context.AppState.State = ApplicationState.Ready;

            long apex = context.QuantumStorage.CurrentApex;

            var client1StartBalanceAmount = (long)0;

            var account1 = context.AccountStorage.GetAccount(TestEnvironment.Client1KeyPair).Account;

            var clientAccountBalance = account1.GetBalance(asset);

            var withdrawalDest = KeyPair.Random();
            var txHash         = new byte[] { };

            if (clientAccountBalance != null && amount > 0)
            {
                client1StartBalanceAmount = clientAccountBalance.Amount;


                context.Constellation.TryFindAssetSettings(asset, out var assetSettings);

                var account   = new stellar_dotnet_sdk.Account(TestEnvironment.Client1KeyPair.AccountId, 1);
                var txBuilder = new TransactionBuilder(account);
                txBuilder.SetFee(10_000);
                txBuilder.AddTimeBounds(new stellar_dotnet_sdk.TimeBounds(DateTimeOffset.UtcNow, new TimeSpan(0, 5, 0)));
                txBuilder.AddOperation(
                    new PaymentOperation.Builder(withdrawalDest, assetSettings.ToAsset(), Amount.FromXdr(amount).ToString())
                    .SetSourceAccount((KeyPair)context.Constellation.Vault)
                    .Build()
                    );
                var tx = txBuilder.Build();
                txHash = tx.Hash();

                var txV1     = tx.ToXdrV1();
                var txStream = new XdrDataOutputStream();
                stellar_dotnet_sdk.xdr.Transaction.Encode(txStream, txV1);

                var accountWrapper = context.AccountStorage.GetAccount(TestEnvironment.Client1KeyPair);

                var withdrawal = new WithdrawalRequest
                {
                    Account        = accountWrapper.Account.Id,
                    TransactionXdr = txStream.ToArray(),
                    RequestId      = DateTime.UtcNow.Ticks,
                    AccountWrapper = accountWrapper
                };

                MessageEnvelope quantum = withdrawal.CreateEnvelope();
                quantum.Sign(TestEnvironment.Client1KeyPair);
                if (!context.IsAlpha)
                {
                    quantum = new RequestQuantum {
                        Apex = ++apex, RequestEnvelope = quantum, Timestamp = DateTime.UtcNow.Ticks
                    }.CreateEnvelope();
                    quantum.Sign(TestEnvironment.AlphaKeyPair);
                }
                //create withdrawal
                await context.QuantumHandler.HandleAsync(quantum);
            }

            var depositAmount = new Random().Next(10, 1000);

            var ledgerNotification = new TxNotification
            {
                TxCursor = (uint)cursor,
                Payments = new List <PaymentBase>
                {
                    new Deposit
                    {
                        Amount      = depositAmount,
                        Destination = TestEnvironment.Client1KeyPair,
                        Asset       = asset
                    },
                    new Withdrawal
                    {
                        TransactionHash = txHash,
                        PaymentResult   = PaymentResults.Success
                    }
                }
            };
            var ledgerNotificationEnvelope = ledgerNotification.CreateEnvelope();

            ledgerNotificationEnvelope.Sign(TestEnvironment.Auditor1KeyPair);

            var ledgerCommitEnv = new TxCommitQuantum
            {
                Source = ledgerNotificationEnvelope,
                Apex   = ++apex
            }.CreateEnvelope();

            if (!context.IsAlpha)
            {
                var msg = ((TxCommitQuantum)ledgerCommitEnv.Message);
                msg.Timestamp   = DateTime.UtcNow.Ticks;
                ledgerCommitEnv = msg.CreateEnvelope().Sign(TestEnvironment.AlphaKeyPair);
            }

            await AssertQuantumHandling(ledgerCommitEnv, excpectedException);

            if (excpectedException == null)
            {
                Assert.AreEqual(context.TxCursorManager.TxCursor, ledgerNotification.TxCursor);

                Assert.AreEqual(account1.GetBalance(asset).Liabilities, 0);
                Assert.AreEqual(account1.GetBalance(asset).Amount, client1StartBalanceAmount - amount + depositAmount); //acc balance + deposit - withdrawal
            }
        }