Remove SpendingInfoDb.txid parameter (#4199)

This commit is contained in:
Chris Stewart 2022-03-19 15:21:36 -05:00 committed by GitHub
parent 8d2a749df6
commit d5807daeab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 64 additions and 39 deletions

View file

@ -1,19 +1,12 @@
package org.bitcoins.core.api.wallet.db
import org.bitcoins.crypto.{DoubleSha256DigestBE, ECPrivateKey}
import org.bitcoins.crypto.DoubleSha256DigestBE
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 =

View file

@ -139,7 +139,6 @@ class CoinSelectorTest extends BitcoinSUnitTest {
output = output,
privKeyPath = path,
scriptWitness = scriptWitness,
txid = EmptyTransactionOutPoint.txIdBE,
state = TxoState.ConfirmedReceived,
spendingTxIdOpt = None
)

View file

@ -32,7 +32,6 @@ case class SegwitV0SpendingInfo(
output: TransactionOutput,
privKeyPath: SegWitHDPath,
scriptWitness: ScriptWitness,
txid: DoubleSha256DigestBE,
state: TxoState,
spendingTxIdOpt: Option[DoubleSha256DigestBE],
id: Option[Long] = None
@ -61,7 +60,6 @@ case class LegacySpendingInfo(
output: TransactionOutput,
privKeyPath: LegacyHDPath,
state: TxoState,
txid: DoubleSha256DigestBE,
spendingTxIdOpt: Option[DoubleSha256DigestBE],
id: Option[Long] = None
) extends SpendingInfoDb {
@ -92,7 +90,6 @@ case class NestedSegwitV0SpendingInfo(
privKeyPath: NestedSegWitHDPath,
redeemScript: ScriptPubKey,
scriptWitness: ScriptWitness,
txid: DoubleSha256DigestBE,
state: TxoState,
spendingTxIdOpt: Option[DoubleSha256DigestBE],
id: Option[Long] = None
@ -149,11 +146,7 @@ sealed trait SpendingInfoDb extends DbRowAutoInc[SpendingInfoDb] {
def state: TxoState
/** The TXID of the transaction this output was received in */
def txid: DoubleSha256DigestBE
require(
txid == outPoint.txIdBE,
s"Cannot have different outpoint txId and txId outpoint=${outPoint.txIdBE.hex} txId=${txid.hex}")
def txid: DoubleSha256DigestBE = outPoint.txIdBE
/** TxId of the transaction that this output was spent by */
def spendingTxIdOpt: Option[DoubleSha256DigestBE]
@ -232,7 +225,6 @@ object SpendingInfoDb {
hdPath.asInstanceOf[NestedSegWitHDPath],
redeemScriptOpt.get,
scriptWitnessOpt.get,
txId,
state,
spendingTxIdOpt,
id)
@ -244,7 +236,6 @@ object SpendingInfoDb {
output = output,
privKeyPath = hdPath.asInstanceOf[LegacyHDPath],
state = state,
txid = txId,
spendingTxIdOpt = spendingTxIdOpt,
id = id)
}
@ -259,7 +250,6 @@ object SpendingInfoDb {
output = output,
privKeyPath = hdPath.asInstanceOf[SegWitHDPath],
scriptWitness = scriptWitnessOpt.get,
txid = txId,
state = state,
spendingTxIdOpt = spendingTxIdOpt,
id = id
@ -272,7 +262,6 @@ object SpendingInfoDb {
output = output,
privKeyPath = hdPath.asInstanceOf[LegacyHDPath],
state = state,
txid = txId,
spendingTxIdOpt = spendingTxIdOpt,
id = id)
}

View file

@ -22,7 +22,6 @@ import org.bitcoins.crypto.DoubleSha256DigestBE
case class UTXORecord(
outpoint: TransactionOutPoint,
txid: DoubleSha256DigestBE, // TXID
state: TxoState, // state
scriptPubKeyId: Long, // output SPK
value: CurrencyUnit, // output value
@ -46,7 +45,6 @@ case class UTXORecord(
scriptWitness = scriptWitness,
id = id,
state = state,
txid = txid,
spendingTxIdOpt = spendingTxIdOpt
)
@ -56,8 +54,7 @@ case class UTXORecord(
privKeyPath = path,
id = id,
state = state,
spendingTxIdOpt = spendingTxIdOpt,
txid = txid)
spendingTxIdOpt = spendingTxIdOpt)
case (path: NestedSegWitHDPath, Some(redeemScript), Some(scriptWitness))
if WitnessScriptPubKey.isValidAsm(redeemScript.asm) =>
@ -67,7 +64,6 @@ case class UTXORecord(
privKeyPath = path,
redeemScript = redeemScript,
scriptWitness = scriptWitness,
txid = txid,
state = state,
spendingTxIdOpt = spendingTxIdOpt,
id = id
@ -86,7 +82,6 @@ object UTXORecord {
scriptPubKeyId: Long): UTXORecord =
UTXORecord(
spendingInfoDb.outPoint,
spendingInfoDb.txid, // TXID
spendingInfoDb.state, // state
scriptPubKeyId, // output SPK
spendingInfoDb.output.value, // output value

View file

@ -296,7 +296,6 @@ trait TransactionTestUtil {
output = output,
privKeyPath = SegWitHDPath(HDCoinType.Testnet, 0, HDChainType.External, 0),
scriptWitness = EmptyScriptWitness,
txid = DoubleSha256DigestBE.empty,
state = TxoState.PendingConfirmationsSpent,
spendingTxIdOpt =
Some(DoubleSha256DigestBE.fromBytes(ECPrivateKey.freshPrivateKey.bytes))

View file

@ -132,7 +132,6 @@ object WalletTestUtil {
SegwitV0SpendingInfo(
state = state,
txid = outpoint.txIdBE,
outPoint = outpoint,
output = output,
privKeyPath = privkeyPath,
@ -158,7 +157,6 @@ object WalletTestUtil {
}
LegacySpendingInfo(state = state,
txid = outpoint.txIdBE,
outPoint = outpoint,
output = output,
privKeyPath = privKeyPath,
@ -185,7 +183,6 @@ object WalletTestUtil {
NestedSegwitV0SpendingInfo(
state = state,
txid = outpoint.txIdBE,
outPoint = outpoint,
output = output,
privKeyPath = privkeyPath,

View file

@ -31,7 +31,6 @@ class CoinSelectorTest extends BitcoinSWalletTest {
val outpoint1 = TransactionGenerators.outPoint.sampleSome
val utxo1 = SegwitV0SpendingInfo(
txid = outpoint1.txIdBE,
state = TxoState.PendingConfirmationsReceived,
id = Some(1),
outPoint = outpoint1,
@ -42,7 +41,6 @@ class CoinSelectorTest extends BitcoinSWalletTest {
)
val outPoint2 = TransactionGenerators.outPoint.sampleSome
val utxo2 = SegwitV0SpendingInfo(
txid = outPoint2.txIdBE,
state = TxoState.ConfirmedReceived,
id = Some(2),
outPoint = outPoint2,
@ -54,7 +52,6 @@ class CoinSelectorTest extends BitcoinSWalletTest {
val outPoint3 = TransactionGenerators.outPoint.sampleSome
val utxo3 = SegwitV0SpendingInfo(
txid = outPoint3.txIdBE,
state = TxoState.ConfirmedReceived,
id = Some(3),
outPoint = outPoint3,

View file

@ -252,7 +252,6 @@ private[wallet] trait UtxoHandling extends WalletLogger {
case segwitAddr: SegWitAddressDb =>
SegwitV0SpendingInfo(
state = state,
txid = tx.txIdBE,
outPoint = outPoint,
output = output,
privKeyPath = segwitAddr.path,
@ -261,7 +260,6 @@ private[wallet] trait UtxoHandling extends WalletLogger {
)
case LegacyAddressDb(path, _, _, _, _) =>
LegacySpendingInfo(state = state,
txid = tx.txIdBE,
outPoint = outPoint,
output = output,
privKeyPath = path,
@ -273,7 +271,6 @@ private[wallet] trait UtxoHandling extends WalletLogger {
privKeyPath = nested.path,
redeemScript = P2WPKHWitnessSPKV0(nested.ecPublicKey),
scriptWitness = P2WPKHWitnessV0(nested.ecPublicKey),
txid = tx.txIdBE,
state = state,
spendingTxIdOpt = None,
id = None

View file

@ -609,7 +609,65 @@ case class SpendingInfoDAO()(implicit
targetTableQuery = txTable)(_.txIdBE)
}
def * =
private val fromTuple: (
(
TransactionOutPoint,
DoubleSha256DigestBE,
TxoState,
Long,
CurrencyUnit,
HDPath,
Option[ScriptPubKey],
Option[ScriptWitness],
Option[DoubleSha256DigestBE],
Option[Long])) => UTXORecord = {
case (outpoint,
_,
state,
scriptPubKeyId,
value,
path,
redeemScriptOpt,
scriptWitOpt,
spendingTxIdOpt,
idOpt) =>
UTXORecord(outpoint,
state,
scriptPubKeyId,
value,
path,
redeemScriptOpt,
scriptWitOpt,
spendingTxIdOpt,
idOpt)
}
private val toTuple: UTXORecord => Option[
(
TransactionOutPoint,
DoubleSha256DigestBE,
TxoState,
Long,
CurrencyUnit,
HDPath,
Option[ScriptPubKey],
Option[ScriptWitness],
Option[DoubleSha256DigestBE],
Option[Long])] = { case utxo: UTXORecord =>
Some(
(utxo.outpoint,
utxo.outpoint.txIdBE,
utxo.state,
utxo.scriptPubKeyId,
utxo.value,
utxo.path,
utxo.redeemScript,
utxo.scriptWitness,
utxo.spendingTxIdOpt,
utxo.id))
}
override def * = {
(outPoint,
txid,
state,
@ -619,6 +677,7 @@ case class SpendingInfoDAO()(implicit
redeemScriptOpt,
scriptWitnessOpt,
spendingTxIdOpt,
id.?).<>((UTXORecord.apply _).tupled, UTXORecord.unapply)
id.?).<>(fromTuple, toTuple)
}
}
}