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 1617e11 commit f19a1c6
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 @@ -321,6 +321,14 @@ 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 @@ -107,6 +107,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
18 changes: 16 additions & 2 deletions dlc-manager/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::error::Error;
use crate::Signer;
use crate::{ChannelId, ContractId};
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 @@ -38,6 +38,7 @@ use secp256k1_zkp::{ecdsa::Signature, All, PublicKey, Secp256k1, SecretKey};
use std::collections::HashMap;
use std::ops::Deref;
use std::string::ToString;
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 @@ -897,6 +898,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
@@ -1,7 +1,7 @@
use std::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 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
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, Signer, Utxo, Wallet};
use lightning::chain::chaininterface::{ConfirmationTarget, FeeEstimator};
use secp256k1_zkp::{rand::thread_rng, All, PublicKey, Secp256k1, SecretKey};
Expand Down Expand Up @@ -318,6 +315,14 @@ where
fn import_address(&self, _: &Address) -> Result<()> {
Ok(())
}

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

Ok(())
}
}

impl<B: Deref, W: Deref> BatchOperations for SimpleWallet<B, W>
Expand Down

0 comments on commit f19a1c6

Please sign in to comment.