public async Task RemovedFromMempoolEventAsync(RemovedFromMempoolEvent e)
        {
            if (e.Message.Reason == RemovedFromMempoolMessage.Reasons.CollisionInBlockTx)
            {
                var removedTxId = new uint256(e.Message.TxId).ToBytes();

                var txWithDSCheck = (await txRepository.GetTxsForDSCheckAsync(new[] { removedTxId }, false)).ToArray();
                if (txWithDSCheck.Any())
                {
                    // Try to insert the block into DB. If block is already present in DB nothing will be done
                    await blockParser.NewBlockDiscoveredAsync(new NewBlockDiscoveredEvent()
                    {
                        CreationDate = clock.UtcNow(), BlockHash = e.Message.BlockHash
                    });

                    foreach (var tx in txWithDSCheck)
                    {
                        await txRepository.InsertBlockDoubleSpendAsync(
                            tx.TxInternalId,
                            new uint256(e.Message.BlockHash).ToBytes(),
                            new uint256(e.Message.CollidedWith.TxId).ToBytes(),
                            HelperTools.HexStringToByteArray(e.Message.CollidedWith.Hex));

                        var notificationEvent = new NewNotificationEvent()
                        {
                            CreationDate     = clock.UtcNow(),
                            NotificationType = CallbackReason.DoubleSpend,
                            TransactionId    = tx.TxExternalIdBytes
                        };
                        eventBus.Publish(notificationEvent);
                    }
                }
            }
        }
Esempio n. 2
0
        public async Task <ActionResult> QueryAsync(string txId)
        {
            if (!uint256.TryParse(txId, out uint256 uTxId))
            {
                return(AddBanScoreAndReturnResult("Invalid 'txid' format.", txId));
            }

            var tx = (await txRepository.GetTxsForDSCheckAsync(new byte[][] { uTxId.ToBytes() }, true)).SingleOrDefault();

            if (tx == null || !tx.DSCheck)
            {
                logger.LogDebug($"Setting not interested information for {txId}");
                // Set response header that we are not interested in DS check
                this.Response.Headers.Add(DSHeader, "0");

                if (tx == null)
                {
                    transactionRequestsCheck.LogUnknownTransactionRequest(Request.Host.Host);
                }
                else
                {
                    transactionRequestsCheck.LogKnownTransactionId(Request.Host.Host, uTxId);
                }

                if (banList.IsHostBanned(Request.Host.Host))
                {
                    return(BadRequest("Query for unknown transaction id requested too many times. (banned)"));
                }
                return(Ok());
            }

            logger.LogDebug($"Setting interested information for {txId}");

            // Set response header that we are interested in DS check
            this.Response.Headers.Add(DSHeader, "1");
            transactionRequestsCheck.LogKnownTransactionId(Request.Host.Host, uTxId);
            if (banList.IsHostBanned(Request.Host.Host))
            {
                return(BadRequest("Query for known transaction id requested too many times. (banned)"));
            }

            transactionRequestsCheck.LogQueriedTransactionId(Request.Host.Host, uTxId);

            return(Ok());
        }