Skip to content

Commit

Permalink
feat: Persist closure info and bumping event to VSS
Browse files Browse the repository at this point in the history
  • Loading branch information
duanyytop authored and jjyr committed Dec 11, 2024
1 parent fcfe0e1 commit 02372b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
20 changes: 18 additions & 2 deletions mutiny-core/src/event.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -728,14 +728,30 @@ impl<S: MutinyStorage> EventHandler<S> {
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,
timestamp: SystemTime::now()
.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,
});
}
}
Expand Down
26 changes: 25 additions & 1 deletion mutiny-core/src/ldkstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -397,7 +398,8 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
"{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()?;
Expand Down Expand Up @@ -431,6 +433,28 @@ impl<S: MutinyStorage> MutinyNodePersister<S> {
.collect::<Result<Vec<_>, _>>()
}

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.
///
Expand Down

0 comments on commit 02372b9

Please sign in to comment.