Skip to content

Commit

Permalink
ReplacementTransactionBuilder bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
omurovch committed Mar 22, 2024
1 parent ecd9a8b commit 07c8099
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ data class ReplacementTransaction(
)

data class ReplacementTransactionInfo(
val originalTransactionSize: Long,
val replacementTransactionMinSize: Long,
val feeRange: LongRange
)
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 }
}

Expand All @@ -361,15 +361,15 @@ class ReplacementTransactionBuilder(
lockingScriptPayload = type.address.lockingScriptPayload
)
)
originalSize = sizeCalculator.transactionSize(fixedUtxo, fixedOutputs)
replacementTxMinSize = sizeCalculator.transactionSize(fixedUtxo, fixedOutputs)
removableOutputsValue = originalFullInfo.outputs.sumOf { it.value } - dustValue
}
}

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 07c8099

Please sign in to comment.