Exemple #1
0
        public async Task DoubleSpendMempoolCheckCleanUp()
        {
            //arrange
            cleanUpTxService.Pause();
            var cleanUpTxTriggeredSubscription = eventBus.Subscribe <CleanUpTxTriggeredEvent>();

            (List <Tx> txList, uint256 firstBlockHash) = await CreateAndInsertTxWithMempoolAsync(dsCheckMempool : true);

            WaitUntilEventBusIsIdle();

            var           doubleSpendTx = Transaction.Parse(Tx2Hex, Network.Main);
            List <byte[]> dsTxId        = new List <byte[]>
            {
                doubleSpendTx.GetHash().ToBytes()
            };
            var txsWithDSCheck = (await TxRepositoryPostgres.GetTxsForDSCheckAsync(dsTxId, true)).ToArray();

            var txPayload = HelperTools.HexStringToByteArray(tx2Hex);

            foreach (var dsTx in txsWithDSCheck)
            {
                await TxRepositoryPostgres.InsertMempoolDoubleSpendAsync(
                    dsTx.TxInternalId,
                    dsTx.TxExternalIdBytes,
                    txPayload);
            }
            WaitUntilEventBusIsIdle();
            var doubleSpends = (await TxRepositoryPostgres.GetTxsToSendMempoolDSNotificationsAsync()).ToList();

            Assert.AreEqual(1, doubleSpends.Count());

            foreach (var txDoubleSpend in doubleSpends)
            {
                await TxRepositoryPostgres.SetNotificationSendDateAsync(CallbackReason.DoubleSpendAttempt, txDoubleSpend.TxInternalId, -1, txDoubleSpend.DoubleSpendTxId, MockedClock.UtcNow);
            }

            doubleSpends = (await TxRepositoryPostgres.GetTxsToSendMempoolDSNotificationsAsync()).ToList();
            Assert.AreEqual(0, doubleSpends.Count());

            using (MockedClock.NowIs(DateTime.UtcNow.AddDays(cleanUpTxAfterDays)))
            {
                await ResumeAndWaitForCleanup(cleanUpTxTriggeredSubscription);

                // check if everything in db was cleared
                await CheckBlockNotPresentInDb(firstBlockHash);
                await CheckTxListNotPresentInDbAsync(txList);
            }
        }
        public async Task SubmitAndCheckMultipleDSRead()
        {
            var address = BitcoinAddress.Create(testAddress, Network.RegTest);

            var tx1 = BCash.Instance.Regtest.CreateTransaction();

            tx1.Inputs.Add(new TxIn(new OutPoint(new uint256(txC1Hash), 0)));
            tx1.Inputs.Add(new TxIn(new OutPoint(new uint256(txC1Hash), 1)));
            tx1.Outputs.Add(new TxOut(new Money(1000L), address));

            var tx2 = BCash.Instance.Regtest.CreateTransaction();

            tx2.Inputs.Add(new TxIn(new OutPoint(new uint256(txC2Hash), 0)));
            tx2.Inputs.Add(new TxIn(new OutPoint(new uint256(txC2Hash), 1)));
            tx2.Outputs.Add(new TxOut(new Money(100L), address));


            var txList = new List <Tx>()
            {
                CreateNewTx(tx1.GetHash().ToString(), tx1.ToHex(), false, null, true),
                CreateNewTx(tx2.GetHash().ToString(), tx2.ToHex(), false, null, true)
            };
            await TxRepositoryPostgres.InsertTxsAsync(txList, false);

            var txs = await TxRepositoryPostgres.GetTxsForDSCheckAsync(new List <byte[]> {
                tx1.GetHash().ToBytes(), tx2.GetHash().ToBytes()
            }, true);

            Assert.AreEqual(2, txs.Count());
            Assert.AreEqual(2, txs.First().TxIn.Count);
            Assert.AreEqual(2, txs.Last().TxIn.Count);

            var readTx1 = txs.SingleOrDefault(x => x.TxIn.Any(y => new uint256(y.PrevTxId) == new uint256(txC1Hash)));

            Assert.IsNotNull(readTx1);
            Assert.AreEqual(0, readTx1.OrderderInputs.First().N);
            Assert.AreEqual(1, readTx1.OrderderInputs.Last().N);

            var readTx2 = txs.SingleOrDefault(x => x.TxIn.Any(y => new uint256(y.PrevTxId) == new uint256(txC2Hash)));

            Assert.IsNotNull(readTx2);
            Assert.AreEqual(0, readTx2.OrderderInputs.First().N);
            Assert.AreEqual(1, readTx2.OrderderInputs.Last().N);
        }