mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-21 22:21:53 +01:00
Add invariant to make sure spendingTxId is different than the txid (#4019)
This commit is contained in:
parent
b918cf78b7
commit
71711ca582
3 changed files with 32 additions and 1 deletions
|
@ -0,0 +1,23 @@
|
|||
package org.bitcoins.core.api.wallet.db
|
||||
|
||||
import org.bitcoins.crypto.{DoubleSha256DigestBE, ECPrivateKey}
|
||||
import org.bitcoins.testkitcore.util.{BitcoinSUnitTest, TransactionTestUtil}
|
||||
|
||||
class SpendingInfoDbTest extends BitcoinSUnitTest {
|
||||
|
||||
behavior of "SpendingInfoDbTest"
|
||||
|
||||
it must "throw an exception if the outpoint txid is different than the txid" in {
|
||||
assertThrows[IllegalArgumentException] {
|
||||
TransactionTestUtil.spendingInfoDb.copy(txid =
|
||||
DoubleSha256DigestBE.fromBytes(ECPrivateKey.freshPrivateKey.bytes))
|
||||
}
|
||||
}
|
||||
|
||||
it must "throw an exception if the spending txid is the same as txid" in {
|
||||
assertThrows[IllegalArgumentException] {
|
||||
TransactionTestUtil.spendingInfoDb.copy(spendingTxIdOpt =
|
||||
Some(DoubleSha256DigestBE.empty))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -158,7 +158,13 @@ sealed trait SpendingInfoDb extends DbRowAutoInc[SpendingInfoDb] {
|
|||
/** TxId of the transaction that this output was spent by */
|
||||
def spendingTxIdOpt: Option[DoubleSha256DigestBE]
|
||||
|
||||
require(
|
||||
spendingTxIdOpt.map(_ != txid).getOrElse(true),
|
||||
s"txid and the spendingTxId cannot be the same, txid=${txid.hex} spendingTxId=${spendingTxIdOpt.get.hex}"
|
||||
)
|
||||
|
||||
/** Converts the UTXO to the canonical `txid:vout` format */
|
||||
|
||||
def toHumanReadableString: String =
|
||||
s"${outPoint.txId.flip.hex}:${outPoint.vout.toInt}"
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.bitcoins.core.wallet.utxo.TxoState
|
|||
import org.bitcoins.crypto.{
|
||||
DoubleSha256Digest,
|
||||
DoubleSha256DigestBE,
|
||||
ECPrivateKey,
|
||||
ECPublicKey,
|
||||
ECPublicKeyBytes
|
||||
}
|
||||
|
@ -284,7 +285,8 @@ trait TransactionTestUtil {
|
|||
scriptWitness = EmptyScriptWitness,
|
||||
txid = DoubleSha256DigestBE.empty,
|
||||
state = TxoState.PendingConfirmationsSpent,
|
||||
spendingTxIdOpt = Some(DoubleSha256DigestBE.empty)
|
||||
spendingTxIdOpt =
|
||||
Some(DoubleSha256DigestBE.fromBytes(ECPrivateKey.freshPrivateKey.bytes))
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue