Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
omurovch committed Mar 11, 2024
1 parent ac5a6c1 commit 232a511
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,9 +28,11 @@ class PendingTransactionProcessor(

private fun resolveConflicts(transaction: FullTransaction, updated: MutableList<Transaction>) {
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,29 +14,42 @@ class TransactionConflictsResolver(private val storage: IStorage) {
}

fun getTransactionsConflictingWithPendingTransaction(transaction: FullTransaction): List<Transaction> {
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
}
Expand Down

0 comments on commit 232a511

Please sign in to comment.