Skip to content

Commit

Permalink
feat: Unreserve utxos after offered channel gets rejected
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Feb 15, 2024
1 parent dac9f3e commit 19b0e87
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
8 changes: 8 additions & 0 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ impl Wallet for BitcoinCoreProvider {

Ok(())
}

fn unreserve_utxos(&self, outpoints: &[OutPoint]) -> Result<(), ManagerError> {
match self.client.lock().unwrap().unlock_unspent(outpoints).map_err(rpc_err_to_manager_err)? {
true => Ok(()),
false => Err(ManagerError::StorageError(format!("Failed to unlock utxos: {outpoints:?}")))
}

}
}

impl Blockchain for BitcoinCoreProvider {
Expand Down
2 changes: 2 additions & 0 deletions dlc-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ pub trait Wallet {
psbt: &mut PartiallySignedTransaction,
input_index: usize,
) -> Result<(), Error>;
/// Unlock reserved utxo
fn unreserve_utxos(&self, outpoints: &[OutPoint]) -> Result<(), Error>;
}

/// Blockchain trait provides access to the bitcoin blockchain.
Expand Down
18 changes: 16 additions & 2 deletions dlc-manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::contract_updater::{accept_contract, verify_accepted_and_sign_contract
use crate::error::Error;
use crate::{ChannelId, ContractId, ContractSignerProvider};
use bitcoin::locktime::Height;
use bitcoin::Transaction;
use bitcoin::hashes::hex::ToHex;
use bitcoin::{OutPoint, Transaction};
use bitcoin::hashes::hex::{ToHex};
use bitcoin::{locktime, Address, LockTime};
use dlc_messages::channel::{
AcceptChannel, CollaborativeCloseOffer, OfferChannel, Reject, RenewAccept, RenewConfirm,
Expand All @@ -40,6 +40,7 @@ use std::collections::HashMap;
use std::ops::Deref;
use std::string::ToString;
use std::sync::Arc;
use bitcoin::consensus::Decodable;

/// The number of confirmations required before moving the the confirmed state.
pub const NB_CONFIRMATIONS: u32 = 6;
Expand Down Expand Up @@ -930,6 +931,19 @@ where
pub fn reject_channel(&self, channel_id: &ChannelId) -> Result<(Reject, PublicKey), Error> {
let offered_channel = get_channel_in_state!(self, channel_id, Offered, None as Option<PublicKey>)?;
let offered_contract = get_contract_in_state!(self, &offered_channel.offered_contract_id, Offered, None as Option<PublicKey>)?;

if offered_channel.is_offer_party {
let utxos = offered_contract.funding_inputs_info.iter().map(|funding_input_info| {
let txid = Transaction::consensus_decode(&mut funding_input_info.funding_input.prev_tx.as_slice())
.expect("Transaction Decode Error")
.txid();
let vout = funding_input_info.funding_input.prev_tx_vout;
OutPoint{txid, vout}
}).collect::<Vec<_>>();

self.wallet.unreserve_utxos(&utxos)?;
}

let counterparty = offered_channel.counter_party;
self.store.upsert_channel(Channel::Cancelled(offered_channel), Some(Contract::Rejected(offered_contract)))?;

Expand Down
6 changes: 5 additions & 1 deletion mocks/src/mock_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::rc::Rc;

use bitcoin::psbt::PartiallySignedTransaction;
use bitcoin::secp256k1::PublicKey;
use bitcoin::{Address, PackedLockTime, Script, Transaction, TxOut};
use bitcoin::{Address, OutPoint, PackedLockTime, Script, Transaction, TxOut};
use dlc_manager::{error::Error, Blockchain, ContractSignerProvider, SimpleSigner, Utxo, Wallet};
use secp256k1_zkp::{rand::seq::SliceRandom, SecretKey};

Expand Down Expand Up @@ -115,6 +115,10 @@ impl Wallet for MockWallet {
fn sign_psbt_input(&self, _: &mut PartiallySignedTransaction, _: usize) -> Result<(), Error> {
Ok(())
}

fn unreserve_utxos(&self, _outpoints: &[OutPoint]) -> Result<(), Error> {
Ok(())
}
}

fn get_address() -> Address {
Expand Down
13 changes: 9 additions & 4 deletions simple-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use bdk::{
FeeRate, KeychainKind, LocalUtxo, Utxo as BdkUtxo, WeightedUtxo,
};
use bitcoin::psbt::PartiallySignedTransaction;
use bitcoin::{
hashes::Hash, Address, Network, PackedLockTime, Script, Sequence, Transaction, TxIn, TxOut,
Txid, Witness,
};
use bitcoin::{hashes::Hash, Address, Network, PackedLockTime, Script, Sequence, Transaction, TxIn, TxOut, Txid, Witness, OutPoint};
use dlc_manager::{
error::Error, Blockchain, ContractSignerProvider, KeysId, SimpleSigner, Utxo, Wallet,
};
Expand Down Expand Up @@ -297,6 +294,14 @@ where
Ok(())
}

fn unreserve_utxos(&self, outputs: &[OutPoint]) -> std::result::Result<(), Error> {
for outpoint in outputs {
self.storage.unreserve_utxo(&outpoint.txid, outpoint.vout)?;
}

Ok(())
}

fn sign_psbt_input(
&self,
psbt: &mut PartiallySignedTransaction,
Expand Down

0 comments on commit 19b0e87

Please sign in to comment.