Fix bug where we couldn't execute a DLC twice (#3426)

This commit is contained in:
benthecarman 2021-07-20 10:43:27 -05:00 committed by GitHub
parent 59963156bc
commit a295b363bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -217,6 +217,18 @@ class DLCExecutionTest extends BitcoinSDualWalletTest {
}
}
it must "execute a DLC twice and get the same transaction" in { wallets =>
val wallet = wallets._1.wallet
for {
contractId <- getContractId(wallet)
status <- getDLCStatus(wallet)
(sig, _) = getSigs(status.contractInfo)
tx1 <- wallet.executeDLC(contractId, sig)
tx2 <- wallet.executeDLC(contractId, sig)
} yield assert(tx1 == tx2)
}
it must "execute a losing dlc" in { wallets =>
val dlcA = wallets._1.wallet

View File

@ -1199,6 +1199,28 @@ abstract class DLCWallet
contractId: ByteVector,
oracleSigs: Vector[OracleSignatures]): Future[Transaction] = {
require(oracleSigs.nonEmpty, "Must provide at least one oracle signature")
dlcDAO.findByContractId(contractId).flatMap {
case None =>
Future.failed(
new IllegalArgumentException(
s"No DLC found with contractId $contractId"))
case Some(db) =>
db.closingTxIdOpt match {
case Some(txId) =>
transactionDAO.findByTxId(txId).flatMap {
case Some(tx) => Future.successful(tx.transaction)
case None => createDLCExecutionTx(contractId, oracleSigs)
}
case None =>
createDLCExecutionTx(contractId, oracleSigs)
}
}
}
private def createDLCExecutionTx(
contractId: ByteVector,
oracleSigs: Vector[OracleSignatures]): Future[Transaction] = {
require(oracleSigs.nonEmpty, "Must provide at least one oracle signature")
for {
(executor, setup) <- executorAndSetupFromDb(contractId)