Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force_close_spend_delay filed for ChannelClosure #84

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions mutiny-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ impl<S: MutinyStorage> EventHandler<S> {
channel_id,
user_channel_id,
counterparty_node_id,
funding_txo,
..
} => {
log_debug!(
Expand All @@ -703,6 +704,31 @@ impl<S: MutinyStorage> EventHandler<S> {
"ERROR: Could not delete channel open params, but continuing: {e}"
);
}

let all_channels = self.channel_manager.list_channels();
let found_channel = all_channels.iter().find(|chan| {
chan.funding_txo.map(|a| a.into_bitcoin_outpoint()) == Some(funding_txo)
});
if let Some(channel) = found_channel {
let closure = ChannelClosure::new_placeholder(
user_channel_id,
channel_id,
funding_txo,
counterparty_node_id,
channel.force_close_spend_delay,
);
if let Err(e) = self
.persister
.persist_channel_closure(user_channel_id, closure)
{
log_error!(self.logger, "Failed to persist channel closure: {e}");
}
} else {
log_warn!(
self.logger,
"WARNING: Could not find channel with funding txo {funding_txo:?} when calling list_channels in ChannelPending event"
);
}
}
Event::HTLCIntercepted { .. } => {}
Event::BumpTransaction(event) => match &event {
Expand Down
9 changes: 9 additions & 0 deletions mutiny-core/src/ldkstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
"{CHANNEL_CLOSURE_PREFIX}{}",
user_channel_id.to_be_bytes().to_lower_hex_string()
));

let force_close_spend_delay = self
.storage
.get_channel_closure(&key)?
.and_then(|c| c.force_close_spend_delay);
let mut closure = closure;
closure.set_force_close_spend_delay(force_close_spend_delay);

self.storage
.write_data(key.clone(), &closure, Some(closure.timestamp as u32))?;

Expand Down Expand Up @@ -873,6 +881,7 @@ mod test {
reason: "This is a test.".to_string(),
timestamp: utils::now().as_secs(),
channel_funding_txo: None,
force_close_spend_delay: None,
};
let result = persister.persist_channel_closure(user_channel_id, closure.clone());
assert!(result.is_ok());
Expand Down
1 change: 1 addition & 0 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,7 @@ mod tests {
reason: "".to_string(),
timestamp: 1686258926,
channel_funding_txo: None,
force_close_spend_delay: None,
};
let closure_chan_id: u128 = 6969;
node.persister
Expand Down
31 changes: 31 additions & 0 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub struct MutinyChannel {
pub is_outbound: bool,
pub is_usable: bool,
pub is_anchor: bool,
pub force_close_spend_delay: Option<u16>,
}

impl From<&ChannelDetails> for MutinyChannel {
Expand Down Expand Up @@ -173,6 +174,7 @@ impl From<&ChannelDetails> for MutinyChannel {
is_outbound: c.is_outbound,
is_usable: c.is_usable,
is_anchor,
force_close_spend_delay: c.force_close_spend_delay,
}
}
}
Expand All @@ -187,6 +189,7 @@ pub struct ChannelClosure {
pub timestamp: u64,
#[serde(default)]
pub channel_funding_txo: Option<OutPoint>,
pub force_close_spend_delay: Option<u16>,
}

impl ChannelClosure {
Expand All @@ -206,6 +209,25 @@ impl ChannelClosure {
node_id,
reason: reason.to_string(),
timestamp: utils::now().as_secs(),
force_close_spend_delay: None,
}
}

pub fn new_placeholder(
user_channel_id: u128,
channel_id: ChannelId,
channel_funding_txo: OutPoint,
node_id: PublicKey,
force_close_spend_delay: Option<u16>,
) -> Self {
Self {
user_channel_id: Some(user_channel_id.to_be_bytes()),
channel_id: Some(channel_id.0),
channel_funding_txo: Some(channel_funding_txo),
node_id: Some(node_id),
reason: "".to_string(),
timestamp: 0, // Ensure that the real timestamp is used to update vss when the channel is shut down
force_close_spend_delay,
}
}

Expand All @@ -224,6 +246,14 @@ impl ChannelClosure {

Ok(())
}

pub fn set_force_close_spend_delay(&mut self, delay: Option<u16>) {
if self.force_close_spend_delay.is_some() {
return;
}

self.force_close_spend_delay = delay;
}
}

impl PartialOrd for ChannelClosure {
Expand Down Expand Up @@ -2395,6 +2425,7 @@ mod tests {
reason: "".to_string(),
timestamp: 1686258926,
channel_funding_txo: None,
force_close_spend_delay: None,
};

let tx1: TransactionDetails = TransactionDetails {
Expand Down
1 change: 1 addition & 0 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ impl MutinyWallet {
.list_channel_closures()
.await?
.into_iter()
.filter(|closure| closure.timestamp != 0) // filter out placeholder closures
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When channel_closeure stored as a placeholder, it can be filtered out when list_channel_closures is called from the front end. @Flouse @ShookLyngs

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i see

.map(Into::into)
.collect();
channel_closures.sort();
Expand Down
10 changes: 10 additions & 0 deletions mutiny-wasm/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ pub struct MutinyChannel {
pub is_outbound: bool,
pub is_usable: bool,
pub is_anchor: bool,
pub force_close_spend_delay: Option<u16>,
}

#[wasm_bindgen]
Expand Down Expand Up @@ -343,6 +344,7 @@ impl From<nodemanager::MutinyChannel> for MutinyChannel {
is_outbound: m.is_outbound,
is_usable: m.is_usable,
is_anchor: m.is_anchor,
force_close_spend_delay: m.force_close_spend_delay,
}
}
}
Expand All @@ -364,6 +366,7 @@ impl From<MutinyChannel> for nodemanager::MutinyChannel {
is_outbound: m.is_outbound,
is_usable: m.is_usable,
is_anchor: m.is_anchor,
force_close_spend_delay: m.force_close_spend_delay,
}
}
}
Expand All @@ -377,6 +380,7 @@ pub struct ChannelClosure {
reason: String,
pub timestamp: u64,
channel_funding_txo: Option<String>,
force_close_spend_delay: Option<u16>,
}

#[wasm_bindgen]
Expand Down Expand Up @@ -405,6 +409,11 @@ impl ChannelClosure {
pub fn channel_funding_txo(&self) -> Option<String> {
self.channel_funding_txo.clone()
}

#[wasm_bindgen(getter)]
pub fn force_close_spend_delay(&self) -> Option<u16> {
self.force_close_spend_delay
}
}

impl PartialOrd for ChannelClosure {
Expand Down Expand Up @@ -440,6 +449,7 @@ impl From<nodemanager::ChannelClosure> for ChannelClosure {
reason: c.reason,
timestamp: c.timestamp,
channel_funding_txo: c.channel_funding_txo.map(|txo| format!("{}", txo)),
force_close_spend_delay: c.force_close_spend_delay,
}
}
}
Expand Down
Loading