From 02372b96907d934491058e5b5c20d6c1273b2213 Mon Sep 17 00:00:00 2001 From: Dylan Duan Date: Tue, 10 Dec 2024 21:23:46 +0800 Subject: [PATCH] feat: Persist closure info and bumping event to VSS --- mutiny-core/src/event.rs | 20 ++++++++++++++++++-- mutiny-core/src/ldkstorage.rs | 26 +++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/mutiny-core/src/event.rs b/mutiny-core/src/event.rs index d6123d0e8..3d7af733d 100644 --- a/mutiny-core/src/event.rs +++ b/mutiny-core/src/event.rs @@ -1,7 +1,7 @@ use crate::ldkstorage::{MutinyNodePersister, PhantomChannelManager}; use crate::logging::MutinyLogger; use crate::lsp::{AnyLsp, Lsp}; -use crate::messagehandler::{CommonLnEvent, CommonLnEventCallback}; +use crate::messagehandler::{BumpChannelClosureTransaction, CommonLnEvent, CommonLnEventCallback}; use crate::node::BumpTxEventHandler; use crate::nodemanager::ChannelClosure; use crate::onchain::OnChainWallet; @@ -728,7 +728,7 @@ impl EventHandler { self.bump_tx_event_handler.handle_event(&event); } if let Some(cb) = self.ln_event_callback.as_ref() { - cb.trigger(CommonLnEvent::BumpChannelCloseTransaction { + let closure_bumping_event = BumpChannelClosureTransaction { channel_id: format!("{channel_id}"), txid, hex_tx, @@ -736,6 +736,22 @@ impl EventHandler { .duration_since(UNIX_EPOCH) .expect("current time must be greater than epoch anchor") .as_secs(), + }; + + if let Err(e) = self + .persister + .persist_channel_closure_bumping_event(&closure_bumping_event) + { + log_error!( + self.logger, + "Failed to persist channel closure bumping event: {e}" + ); + } + cb.trigger(CommonLnEvent::BumpChannelCloseTransaction { + channel_id: closure_bumping_event.channel_id, + txid: closure_bumping_event.txid, + hex_tx: closure_bumping_event.hex_tx, + timestamp: closure_bumping_event.timestamp, }); } } diff --git a/mutiny-core/src/ldkstorage.rs b/mutiny-core/src/ldkstorage.rs index 035fb6f9f..96bfab263 100644 --- a/mutiny-core/src/ldkstorage.rs +++ b/mutiny-core/src/ldkstorage.rs @@ -3,6 +3,7 @@ use crate::fees::MutinyFeeEstimator; use crate::gossip::PROB_SCORER_KEY; use crate::keymanager::PhantomKeysManager; use crate::logging::MutinyLogger; +use crate::messagehandler::BumpChannelClosureTransaction; use crate::node::Router; use crate::node::{default_user_config, ChainMonitor}; use crate::nodemanager::ChannelClosure; @@ -397,7 +398,8 @@ impl MutinyNodePersister { "{CHANNEL_CLOSURE_PREFIX}{}", user_channel_id.to_be_bytes().to_lower_hex_string() )); - self.storage.write_data(key.clone(), &closure, None)?; + self.storage + .write_data(key.clone(), &closure, Some(closure.timestamp as u32))?; let index = self.storage.activity_index(); let mut index = index.try_write()?; @@ -431,6 +433,28 @@ impl MutinyNodePersister { .collect::, _>>() } + pub(crate) fn persist_channel_closure_bumping_event( + &self, + closure: &BumpChannelClosureTransaction, + ) -> Result<(), MutinyError> { + let key = self.get_key(&format!( + "{CHANNEL_CLOSURE_BUMP_PREFIX}{}", + closure.channel_id + )); + self.storage + .write_data(key.clone(), closure, Some(closure.timestamp as u32))?; + + let index = self.storage.activity_index(); + let mut index = index.try_write()?; + index.retain(|i| i.key != key); // remove old version + index.insert(IndexItem { + timestamp: Some(closure.timestamp), + key, + }); + + Ok(()) + } + /// Persists the failed spendable outputs to storage. /// Previously failed spendable outputs are not overwritten. ///