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 16, 2024
1 parent 12ee7b7 commit 38c91be
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
7 changes: 7 additions & 0 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ impl Wallet for BitcoinCoreProvider {
.import_address(address, None, Some(false))
.map_err(rpc_err_to_manager_err)
}

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 @@ -112,6 +112,8 @@ pub trait Wallet: Signer {
) -> Result<Vec<Utxo>, Error>;
/// Import the provided address.
fn import_address(&self, address: &Address) -> Result<(), Error>;
/// Unlock reserved utxo
fn unreserve_utxos(&self, outpoints: &[OutPoint]) -> Result<(), Error>;
}

/// Blockchain trait provides access to the bitcoin blockchain.
Expand Down
14 changes: 13 additions & 1 deletion dlc-manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::subchannel::{ClosingSubChannel, SubChannel, SubChannelState};
use crate::utils::get_object_in_state;
use crate::{ContractId, DlcChannelId, Signer};
use bitcoin::consensus::encode::serialize_hex;
use bitcoin::Address;
use bitcoin::{Address, OutPoint};
use bitcoin::Transaction;
use bitcoin::hashes::hex::ToHex;
use dlc_messages::channel::{
Expand All @@ -43,6 +43,7 @@ use std::collections::HashMap;
use std::ops::Deref;
use std::string::ToString;
use std::sync::Mutex;
use bitcoin::consensus::Decodable;

/// The number of confirmations required before moving the the confirmed state.
pub const NB_CONFIRMATIONS: u32 = 1;
Expand Down Expand Up @@ -826,6 +827,7 @@ where
pub fn reject_channel(&self, channel_id: &DlcChannelId) -> 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>)?;

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

Expand Down Expand Up @@ -2006,6 +2008,16 @@ where
Channel::Offered(offered_channel) => {
let offered_contract = get_contract_in_state!(self, &offered_channel.offered_contract_id, Offered, None as Option<PublicKey>)?;

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)?;

// remove rejected channel, since nothing has been confirmed on chain yet.
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
@@ -1,7 +1,7 @@
use std::{ops::Deref, rc::Rc};

use bitcoin::psbt::PartiallySignedTransaction;
use bitcoin::{Address, PackedLockTime, Script, Transaction, TxOut};
use bitcoin::{Address, OutPoint, PackedLockTime, Script, Transaction, TxOut};
use dlc_manager::{error::Error, Blockchain, Signer, Utxo, Wallet};
use lightning::chain::chaininterface::BroadcasterInterface;
use secp256k1_zkp::{rand::seq::SliceRandom, SecretKey};
Expand Down Expand Up @@ -111,6 +111,10 @@ impl Wallet for MockWallet {
fn import_address(&self, _address: &Address) -> Result<(), dlc_manager::error::Error> {
Ok(())
}

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

fn get_address() -> Address {
Expand Down
12 changes: 9 additions & 3 deletions simple-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use bdk::{
FeeRate, KeychainKind, LocalUtxo, Utxo as BdkUtxo, WeightedUtxo,
};
use bitcoin::psbt::PartiallySignedTransaction;
use bitcoin::{
Address, Network, PackedLockTime, Script, Sequence, Transaction, TxIn, TxOut, Txid, Witness,
};
use bitcoin::{Address, Network, PackedLockTime, Script, Sequence, Transaction, TxIn, TxOut, Txid, Witness, OutPoint};
use dlc_manager::{error::Error, Blockchain, Signer, Utxo, Wallet};
use lightning::chain::chaininterface::{ConfirmationTarget, FeeEstimator};
use rust_bitcoin_coin_selection::select_coins;
Expand Down Expand Up @@ -285,6 +283,14 @@ where
fn import_address(&self, _: &Address) -> Result<()> {
Ok(())
}

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

Ok(())
}
}

#[derive(Clone)]
Expand Down

0 comments on commit 38c91be

Please sign in to comment.