Skip to content

Commit

Permalink
fix: Use function on sub channel to get the dlc channel id
Browse files Browse the repository at this point in the history
It appears the channel_id is not always correct. Using the `get_dlc_channel_id(0)` seems to cover more edge cases.
  • Loading branch information
holzeis committed Aug 21, 2023
1 parent 71cde71 commit f88b34b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
13 changes: 13 additions & 0 deletions crates/ln-dlc-node/src/ln/dlc_channel_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use serde::Serializer;
pub struct DlcChannelDetails {
#[serde(serialize_with = "channel_id_as_hex")]
pub channel_id: ChannelId,
#[serde(serialize_with = "optional_channel_id_as_hex")]
pub dlc_channel_id: Option<ChannelId>,
#[serde(serialize_with = "pk_as_hex")]
pub counter_party: PublicKey,
pub update_idx: u64,
Expand Down Expand Up @@ -41,6 +43,7 @@ impl From<SubChannel> for DlcChannelDetails {
fn from(sc: SubChannel) -> Self {
DlcChannelDetails {
channel_id: sc.channel_id,
dlc_channel_id: sc.get_dlc_channel_id(0),
counter_party: sc.counter_party,
update_idx: sc.update_idx,
state: SubChannelState::from(sc.state),
Expand Down Expand Up @@ -73,6 +76,16 @@ impl From<dlc_manager::subchannel::SubChannelState> for SubChannelState {
}
}

fn optional_channel_id_as_hex<S>(channel_id: &Option<ChannelId>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match channel_id {
Some(channel_id) => s.serialize_str(&channel_id.to_hex()),
None => s.serialize_none(),
}
}

fn channel_id_as_hex<S>(channel_id: &ChannelId, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down
1 change: 1 addition & 0 deletions crates/tests-e2e/src/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct DlcChannels {
#[derive(Deserialize, Debug)]
pub struct DlcChannel {
pub channel_id: String,
pub dlc_channel_id: Option<String>,
pub counter_party: String,
pub state: SubChannelState,
}
Expand Down
41 changes: 21 additions & 20 deletions mobile/native/src/ln_dlc/node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::db;
use crate::trade::order;
use crate::trade::position;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use bdk::bitcoin::secp256k1::PublicKey;
Expand All @@ -21,9 +22,8 @@ use ln_dlc_node::channel::Channel;
use ln_dlc_node::channel::FakeScid;
use ln_dlc_node::node;
use ln_dlc_node::node::dlc_message_name;
use ln_dlc_node::node::rust_dlc_manager::contract::signed_contract::SignedContract;
use ln_dlc_node::node::rust_dlc_manager::contract::Contract;
use ln_dlc_node::node::rust_dlc_manager::Storage as _;
use ln_dlc_node::node::rust_dlc_manager::Storage;
use ln_dlc_node::node::sub_channel_message_name;
use ln_dlc_node::node::NodeInfo;
use ln_dlc_node::node::PaymentDetails;
Expand Down Expand Up @@ -203,26 +203,27 @@ impl Node {
channel_id, ..
})) = msg
{
let contracts = self.inner.dlc_manager.get_store().get_contracts()?;

let accept_collateral = contracts
.iter()
// Taking the first `Confirmed` contract we find is just a
// heuristic. Ideally we would be able to match against the
// `ContractId` or the `ChannelId`, but the information is not
// guaranteed to be there
.find_map(|contract| match contract {
Contract::Confirmed(SignedContract {
accepted_contract, ..
}) => Some(accepted_contract.accept_params.collateral),
_ => None,
})
.with_context(|| {
format!(
let storage = self.inner.dlc_manager.get_store();
let sub_channel = storage.get_sub_channel(channel_id)?.with_context(|| {
format!(
"Could not find sub channel by channel id {}",
channel_id.to_hex()
)
})?;
let dlc_channel_id = sub_channel
.get_dlc_channel_id(0)
.context("Could not fetch dlc channel id")?;

let accept_collateral =
match self.inner.get_contract_by_dlc_channel_id(dlc_channel_id)? {
Contract::Confirmed(contract) => {
contract.accepted_contract.accept_params.collateral
}
_ => bail!(
"Confirmed contract not found for channel ID: {}",
hex::encode(channel_id)
)
})?;
),
};

let filled_order = order::handler::order_filled()
.context("Cannot mark order as filled for confirmed DLC")?;
Expand Down

0 comments on commit f88b34b

Please sign in to comment.