Require utxos aren't spent when sending from outpoints (#1817)

This commit is contained in:
Ben Carman 2020-08-14 08:43:18 -05:00 committed by GitHub
parent 1c21c8837f
commit 403ed33dbd
2 changed files with 22 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import org.bitcoins.core.protocol.transaction.TransactionOutput
import org.bitcoins.core.script.constant.{BytesToPushOntoStack, ScriptConstant}
import org.bitcoins.core.script.control.OP_RETURN
import org.bitcoins.core.wallet.fee.SatoshisPerByte
import org.bitcoins.core.wallet.utxo.TxoState
import org.bitcoins.crypto.CryptoUtil
import org.bitcoins.testkit.wallet.BitcoinSWalletTest
import org.bitcoins.testkit.wallet.FundWalletUtil.FundedWallet
@ -223,6 +224,22 @@ class WalletSendingTest extends BitcoinSWalletTest {
}
}
it should "fail to send from outpoints when already spent" in {
fundedWallet =>
val wallet = fundedWallet.wallet
for {
allUtxos <- wallet.listUtxos()
// Make one already spent
spent = allUtxos.head.copyWithState(TxoState.PendingConfirmationsSpent)
_ <- wallet.spendingInfoDAO.update(spent)
test <- recoverToSucceededIf[IllegalArgumentException](
wallet.sendFromOutPoints(allUtxos.map(_.outPoint),
testAddress,
amountToSend,
feeRateOpt))
} yield test
}
def testSendWithAlgo(
wallet: Wallet,
algo: CoinSelectionAlgo): Future[Assertion] = {

View file

@ -385,6 +385,11 @@ abstract class Wallet
diff = utxoDbs.map(_.outPoint).diff(outPoints)
_ = require(diff.isEmpty,
s"Not all OutPoints belong to this wallet, diff $diff")
spentUtxos =
utxoDbs.filterNot(utxo => TxoState.receivedStates.contains(utxo.state))
_ = require(
spentUtxos.isEmpty,
s"Some out points given have already been spent, ${spentUtxos.map(_.outPoint)}")
prevTxFs = utxoDbs.map(utxo =>
transactionDAO.findByOutPoint(utxo.outPoint).map(_.get.transaction))