From 07c8099666339c9ab2d0ded28b549feeb74cad50 Mon Sep 17 00:00:00 2001 From: chyngyz Date: Thu, 21 Mar 2024 15:44:07 +0600 Subject: [PATCH] ReplacementTransactionBuilder bug fixes --- .../bitcoincore/rbf/ReplacementTransaction.kt | 2 +- .../bitcoincore/rbf/ReplacementTransactionBuilder.kt | 8 ++++---- .../transactions/TransactionSizeCalculator.kt | 9 +++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/rbf/ReplacementTransaction.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/rbf/ReplacementTransaction.kt index 963a5fc2..150023db 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/rbf/ReplacementTransaction.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/rbf/ReplacementTransaction.kt @@ -10,6 +10,6 @@ data class ReplacementTransaction( ) data class ReplacementTransactionInfo( - val originalTransactionSize: Long, + val replacementTransactionMinSize: Long, val feeRange: LongRange ) diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/rbf/ReplacementTransactionBuilder.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/rbf/ReplacementTransactionBuilder.kt index b2088fe4..34c7378c 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/rbf/ReplacementTransactionBuilder.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/rbf/ReplacementTransactionBuilder.kt @@ -329,7 +329,7 @@ class ReplacementTransactionBuilder( val descendantTransactions = storage.getDescendantTransactionsFullInfo(transactionHash.toReversedByteArray()) val absoluteFee = descendantTransactions.sumOf { it.metadata.fee ?: 0 } - val originalSize: Long + val replacementTxMinSize: Long val removableOutputsValue: Long when (type) { @@ -345,7 +345,7 @@ class ReplacementTransactionBuilder( sortedOutputs = sortedOutputs.dropLast(1) } - originalSize = sizeCalculator.transactionSize(fixedUtxo, fixedOutputs) + replacementTxMinSize = sizeCalculator.transactionSize(fixedUtxo, fixedOutputs) removableOutputsValue = sortedOutputs.sumOf { it.value } } @@ -361,7 +361,7 @@ class ReplacementTransactionBuilder( lockingScriptPayload = type.address.lockingScriptPayload ) ) - originalSize = sizeCalculator.transactionSize(fixedUtxo, fixedOutputs) + replacementTxMinSize = sizeCalculator.transactionSize(fixedUtxo, fixedOutputs) removableOutputsValue = originalFullInfo.outputs.sumOf { it.value } - dustValue } } @@ -369,7 +369,7 @@ class ReplacementTransactionBuilder( val confirmedUtxoTotalValue = unspentOutputProvider.getConfirmedSpendableUtxo().sumOf { it.output.value } val feeRange = LongRange(absoluteFee, originalFee + removableOutputsValue + confirmedUtxoTotalValue) - return ReplacementTransactionInfo(originalSize, feeRange) + return ReplacementTransactionInfo(replacementTxMinSize, feeRange) } sealed class BuildError : Throwable() { diff --git a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionSizeCalculator.kt b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionSizeCalculator.kt index bd62945a..a91a1a5d 100644 --- a/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionSizeCalculator.kt +++ b/bitcoincore/src/main/kotlin/io/horizontalsystems/bitcoincore/transactions/TransactionSizeCalculator.kt @@ -29,7 +29,8 @@ class TransactionSizeCalculator { ScriptType.P2SH to 23, ScriptType.P2WPKH to 22, ScriptType.P2WSH to 34, - ScriptType.P2WPKHSH to 23 + ScriptType.P2WPKHSH to 23, + ScriptType.P2TR to 34 ) fun outputSize(scripType: ScriptType): Int { @@ -83,10 +84,10 @@ class TransactionSizeCalculator { var outputWeight = 0 for (output in outputs) { - when (output.scriptType) { - ScriptType.NULL_DATA -> outputWeight += outputSize(lockingScriptSize = output.lockingScript.size) * 4 + outputWeight += when (output.scriptType) { + ScriptType.NULL_DATA -> outputSize(lockingScriptSize = output.lockingScript.size) * 4 ScriptType.UNKNOWN -> throw IllegalStateException("Unknown output script type") - else -> outputSize(outputSize(output.scriptType)) * 4 + else -> outputSize(output.scriptType) * 4 } }