From badef45f7a5bc564b060f82c846a3b1122198727 Mon Sep 17 00:00:00 2001 From: chyngyz Date: Mon, 11 Mar 2024 11:08:52 +0600 Subject: [PATCH] testing --- .../transactions/PendingTransactionProcessor.kt | 5 ++++- .../TransactionConflictsResolver.kt | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/PendingTransactionProcessor.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/PendingTransactionProcessor.kt index 13f91024..ced706d8 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/PendingTransactionProcessor.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/PendingTransactionProcessor.kt @@ -1,5 +1,6 @@ package io.horizontalsystems.bitcoincore.transactions +import android.util.Log import io.horizontalsystems.bitcoincore.WatchedTransactionManager import io.horizontalsystems.bitcoincore.blocks.IBlockchainDataListener import io.horizontalsystems.bitcoincore.core.IPublicKeyManager @@ -27,9 +28,11 @@ class PendingTransactionProcessor( private fun resolveConflicts(transaction: FullTransaction, updated: MutableList) { val conflictingTransactions = conflictsResolver.getTransactionsConflictingWithPendingTransaction(transaction) - + Log.e("e", "resolveConflicts for ${transaction.header.hash.toReversedHex()}, conflictingTxsCount = ${conflictingTransactions.size}") for (conflictingTransaction in conflictingTransactions) { + Log.e("e", "conflicting tx ${conflictingTransaction.hash.toReversedHex()}") for (descendantTransaction in storage.getDescendantTransactions(conflictingTransaction.hash)) { + Log.e("e", "conflicting tx descendant ${descendantTransaction.hash.toReversedHex()}") descendantTransaction.conflictingTxHash = transaction.header.hash storage.updateTransaction(descendantTransaction) updated.add(descendantTransaction) diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionConflictsResolver.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionConflictsResolver.kt index 47b3279a..80fb3312 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionConflictsResolver.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionConflictsResolver.kt @@ -1,6 +1,8 @@ package io.horizontalsystems.bitcoincore.transactions +import android.util.Log import io.horizontalsystems.bitcoincore.core.IStorage +import io.horizontalsystems.bitcoincore.extensions.toReversedHex import io.horizontalsystems.bitcoincore.models.Transaction import io.horizontalsystems.bitcoincore.storage.FullTransaction @@ -12,29 +14,42 @@ class TransactionConflictsResolver(private val storage: IStorage) { } fun getTransactionsConflictingWithPendingTransaction(transaction: FullTransaction): List { + Log.e("e", "1 ${transaction.header.hash.toReversedHex()}") val conflictingTransactions = getConflictingTransactionsForTransaction(transaction) + Log.e("e", "2 ${conflictingTransactions.joinToString { it.hash.toReversedHex()}}") + if (conflictingTransactions.isEmpty()) return listOf() + Log.e("e", "3") // If any of conflicting transactions is already in a block, then current transaction is invalid and non of them is conflicting with it. if (conflictingTransactions.any { it.blockHash != null }) return listOf() + Log.e("e", "4") + val conflictingFullTransactions = storage.getFullTransactions(conflictingTransactions) - return conflictingFullTransactions + val filtered = conflictingFullTransactions // If an existing transaction has a conflicting input with higher sequence, // then mempool transaction most probably has been received before // and the existing transaction is a replacement transaction that is not relayed in mempool yet. // Other cases are theoretically possible, but highly unlikely .filter { !existingHasHigherSequence(mempoolTransaction = transaction, existingTransaction = it) } .map { it.header } + + Log.e("e", "5 filtered = ${filtered.joinToString { it.hash.toReversedHex() }}") + return filtered } private fun existingHasHigherSequence(mempoolTransaction: FullTransaction, existingTransaction: FullTransaction): Boolean { + Log.e("e", "existingTransaction inputs" + "\n" + + existingTransaction.inputs.joinToString { "${it.previousOutputTxHash.toReversedHex()}, ${it.previousOutputIndex}, seq= ${it.sequence}" } ) + existingTransaction.inputs.forEach { existingInput -> val mempoolInput = mempoolTransaction.inputs.firstOrNull { mempoolInput -> mempoolInput.previousOutputTxHash.contentEquals(existingInput.previousOutputTxHash) && mempoolInput.previousOutputIndex == existingInput.previousOutputIndex } + Log.e("e", "mempoolInput.seq ${mempoolInput?.sequence}, existingInput.seq ${existingInput.sequence}") if (mempoolInput != null && mempoolInput.sequence < existingInput.sequence) return true }