Add invariant to make sure spendingTxId is different than the txid (#4019)

This commit is contained in:
Chris Stewart 2022-01-27 09:52:12 -06:00 committed by GitHub
parent b918cf78b7
commit 71711ca582
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 1 deletions

View file

@ -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))
}
}
}

View file

@ -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}"

View file

@ -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))
)
}