Fix bugs where we were building internally inconsistent SpendingInfoDb (#4016)

* Fix bugs where we were building internally inconsistent SpendingInfoDb

* Fix error message
This commit is contained in:
Chris Stewart 2022-01-27 07:22:09 -06:00 committed by GitHub
parent bd5bcfef3a
commit b918cf78b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 17 deletions

View file

@ -8,7 +8,7 @@ import org.bitcoins.core.protocol.script._
import org.bitcoins.core.protocol.transaction._ import org.bitcoins.core.protocol.transaction._
import org.bitcoins.core.wallet.fee._ import org.bitcoins.core.wallet.fee._
import org.bitcoins.core.wallet.utxo.TxoState import org.bitcoins.core.wallet.utxo.TxoState
import org.bitcoins.crypto.{DoubleSha256DigestBE, ECPublicKey} import org.bitcoins.crypto.ECPublicKey
import org.bitcoins.testkitcore.gen.FeeUnitGen import org.bitcoins.testkitcore.gen.FeeUnitGen
import org.bitcoins.testkitcore.util.BitcoinSUnitTest import org.bitcoins.testkitcore.util.BitcoinSUnitTest
@ -124,7 +124,7 @@ class CoinSelectorTest extends BitcoinSUnitTest {
def createSpendingInfoDbs( def createSpendingInfoDbs(
amounts: Vector[CurrencyUnit]): Vector[SpendingInfoDb] = { amounts: Vector[CurrencyUnit]): Vector[SpendingInfoDb] = {
amounts.map { amt => val result = amounts.map { amt =>
val key = ECPublicKey.freshPublicKey val key = ECPublicKey.freshPublicKey
val spk = P2WPKHWitnessSPKV0(key) val spk = P2WPKHWitnessSPKV0(key)
val output = TransactionOutput(amt, spk) val output = TransactionOutput(amt, spk)
@ -139,11 +139,12 @@ class CoinSelectorTest extends BitcoinSUnitTest {
output = output, output = output,
privKeyPath = path, privKeyPath = path,
scriptWitness = scriptWitness, scriptWitness = scriptWitness,
txid = DoubleSha256DigestBE.empty, txid = EmptyTransactionOutPoint.txIdBE,
state = TxoState.ConfirmedReceived, state = TxoState.ConfirmedReceived,
spendingTxIdOpt = None spendingTxIdOpt = None
) )
} }
result
} }
/** The test assumes feeRate is greater than longTermFeeRate /** The test assumes feeRate is greater than longTermFeeRate

View file

@ -151,6 +151,10 @@ sealed trait SpendingInfoDb extends DbRowAutoInc[SpendingInfoDb] {
/** The TXID of the transaction this output was received in */ /** The TXID of the transaction this output was received in */
def txid: DoubleSha256DigestBE def txid: DoubleSha256DigestBE
require(
txid == outPoint.txIdBE,
s"Cannot have different outpoint txId and txId outpoint=${outPoint.txIdBE.hex} txId=${txid.hex}")
/** TxId of the transaction that this output was spent by */ /** TxId of the transaction that this output was spent by */
def spendingTxIdOpt: Option[DoubleSha256DigestBE] def spendingTxIdOpt: Option[DoubleSha256DigestBE]

View file

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

View file

@ -9,11 +9,7 @@ import org.bitcoins.core.wallet.fee.{FeeUnit, SatoshisPerByte}
import org.bitcoins.core.wallet.utxo.TxoState import org.bitcoins.core.wallet.utxo.TxoState
import org.bitcoins.testkit.wallet.{BitcoinSWalletTest, WalletTestUtil} import org.bitcoins.testkit.wallet.{BitcoinSWalletTest, WalletTestUtil}
import org.bitcoins.testkitcore.Implicits._ import org.bitcoins.testkitcore.Implicits._
import org.bitcoins.testkitcore.gen.{ import org.bitcoins.testkitcore.gen.{TransactionGenerators, WitnessGenerators}
CryptoGenerators,
TransactionGenerators,
WitnessGenerators
}
import org.scalatest.FutureOutcome import org.scalatest.FutureOutcome
class CoinSelectorTest extends BitcoinSWalletTest { class CoinSelectorTest extends BitcoinSWalletTest {
@ -33,31 +29,35 @@ class CoinSelectorTest extends BitcoinSWalletTest {
val output = TransactionOutput(99.sats, ScriptPubKey.empty) val output = TransactionOutput(99.sats, ScriptPubKey.empty)
val feeRate = SatoshisPerByte(CurrencyUnits.zero) val feeRate = SatoshisPerByte(CurrencyUnits.zero)
val outpoint1 = TransactionGenerators.outPoint.sampleSome
val utxo1 = SegwitV0SpendingInfo( val utxo1 = SegwitV0SpendingInfo(
txid = CryptoGenerators.doubleSha256Digest.sampleSome.flip, txid = outpoint1.txIdBE,
state = TxoState.DoesNotExist, state = TxoState.DoesNotExist,
id = Some(1), id = Some(1),
outPoint = TransactionGenerators.outPoint.sampleSome, outPoint = outpoint1,
output = TransactionOutput(10.sats, ScriptPubKey.empty), output = TransactionOutput(10.sats, ScriptPubKey.empty),
privKeyPath = WalletTestUtil.sampleSegwitPath, privKeyPath = WalletTestUtil.sampleSegwitPath,
scriptWitness = WitnessGenerators.scriptWitness.sampleSome, scriptWitness = WitnessGenerators.scriptWitness.sampleSome,
spendingTxIdOpt = None spendingTxIdOpt = None
) )
val outPoint2 = TransactionGenerators.outPoint.sampleSome
val utxo2 = SegwitV0SpendingInfo( val utxo2 = SegwitV0SpendingInfo(
txid = CryptoGenerators.doubleSha256Digest.sampleSome.flip, txid = outPoint2.txIdBE,
state = TxoState.DoesNotExist, state = TxoState.DoesNotExist,
id = Some(2), id = Some(2),
outPoint = TransactionGenerators.outPoint.sampleSome, outPoint = outPoint2,
output = TransactionOutput(90.sats, ScriptPubKey.empty), output = TransactionOutput(90.sats, ScriptPubKey.empty),
privKeyPath = WalletTestUtil.sampleSegwitPath, privKeyPath = WalletTestUtil.sampleSegwitPath,
scriptWitness = WitnessGenerators.scriptWitness.sampleSome, scriptWitness = WitnessGenerators.scriptWitness.sampleSome,
spendingTxIdOpt = None spendingTxIdOpt = None
) )
val outPoint3 = TransactionGenerators.outPoint.sampleSome
val utxo3 = SegwitV0SpendingInfo( val utxo3 = SegwitV0SpendingInfo(
txid = CryptoGenerators.doubleSha256Digest.sampleSome.flip, txid = outPoint3.txIdBE,
state = TxoState.DoesNotExist, state = TxoState.DoesNotExist,
id = Some(3), id = Some(3),
outPoint = TransactionGenerators.outPoint.sampleSome, outPoint = outPoint3,
output = TransactionOutput(20.sats, ScriptPubKey.empty), output = TransactionOutput(20.sats, ScriptPubKey.empty),
privKeyPath = WalletTestUtil.sampleSegwitPath, privKeyPath = WalletTestUtil.sampleSegwitPath,
scriptWitness = WitnessGenerators.scriptWitness.sampleSome, scriptWitness = WitnessGenerators.scriptWitness.sampleSome,