Skip to content

Commit

Permalink
f - use MontiorName as archive_persisted_channel param
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Jan 30, 2025
1 parent 681f587 commit 8edede0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
3 changes: 2 additions & 1 deletion fuzz/src/utils/test_persister.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use lightning::chain;
use lightning::chain::{chainmonitor, channelmonitor};
use lightning::util::persist::MonitorName;
use lightning::util::test_channel_signer::TestChannelSigner;

use std::sync::Mutex;
Expand All @@ -21,5 +22,5 @@ impl chainmonitor::Persist<TestChannelSigner> for TestPersister {
self.update_ret.lock().unwrap().clone()
}

fn archive_persisted_channel(&self, _: &channelmonitor::ChannelMonitor<TestChannelSigner>) {}
fn archive_persisted_channel(&self, _monitor_name: MonitorName) {}
}
5 changes: 3 additions & 2 deletions lightning/src/chain/chainmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::sign::ecdsa::EcdsaChannelSigner;
use crate::events::{self, Event, EventHandler, ReplayEvent};
use crate::util::logger::{Logger, WithContext};
use crate::util::errors::APIError;
use crate::util::persist::MonitorName;
use crate::util::wakers::{Future, Notifier};
use crate::ln::channel_state::ChannelDetails;

Expand Down Expand Up @@ -168,7 +169,7 @@ pub trait Persist<ChannelSigner: EcdsaChannelSigner> {
/// the archive process. Additionally, because the archive operation could be retried on
/// restart, this method must in that case be idempotent, ensuring it can handle scenarios where
/// the monitor already exists in the archive.
fn archive_persisted_channel(&self, monitor: &ChannelMonitor<ChannelSigner>);
fn archive_persisted_channel(&self, monitor_name: MonitorName);
}

struct MonitorHolder<ChannelSigner: EcdsaChannelSigner> {
Expand Down Expand Up @@ -654,7 +655,7 @@ where C::Target: chain::Filter,
"Archiving fully resolved ChannelMonitor for channel ID {}",
channel_id
);
self.persister.archive_persisted_channel(&monitor_holder.monitor);
self.persister.archive_persisted_channel(monitor_holder.monitor.persistence_key());
false
} else {
true
Expand Down
8 changes: 3 additions & 5 deletions lightning/src/util/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ impl<ChannelSigner: EcdsaChannelSigner, K: KVStore + ?Sized> Persist<ChannelSign
}
}

fn archive_persisted_channel(&self, monitor: &ChannelMonitor<ChannelSigner>) {
let monitor_name = monitor.persistence_key();
fn archive_persisted_channel(&self, monitor_name: MonitorName) {
let monitor = match self.read(
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
Expand Down Expand Up @@ -821,8 +820,7 @@ where
}
}

fn archive_persisted_channel(&self, monitor: &ChannelMonitor<ChannelSigner>) {
let monitor_name = monitor.persistence_key();
fn archive_persisted_channel(&self, monitor_name: MonitorName) {
let monitor_key = monitor_name.as_str().to_string();
let monitor = match self.read_channel_monitor_with_updates(monitor_key) {
Ok((_block_hash, monitor)) => monitor,
Expand Down Expand Up @@ -920,7 +918,7 @@ where
/// // Using MonitorName to generate a storage key
/// let storage_key = format!("channel_monitors/{}", monitor_name.as_str());
/// ```
#[derive(Debug)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct MonitorName(String);

/// The source of the [`MonitorName`], either an [`OutPoint`] for V1 channels or a [`ChannelId`] for
Expand Down
24 changes: 14 additions & 10 deletions lightning/src/util/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::sync::RwLock;
use crate::types::features::{ChannelFeatures, InitFeatures, NodeFeatures};
use crate::util::config::UserConfig;
use crate::util::logger::{Logger, Record};
use crate::util::persist::KVStore;
use crate::util::persist::{KVStore, MonitorName};
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
use crate::util::test_channel_signer::{EnforcementState, TestChannelSigner};

Expand Down Expand Up @@ -663,8 +663,11 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> Persist<Signer> for WatchtowerPers
res
}

fn archive_persisted_channel(&self, data: &ChannelMonitor<Signer>) {
self.persister.archive_persisted_channel(data);
fn archive_persisted_channel(&self, monitor_name: MonitorName) {
<TestPersister as Persist<TestChannelSigner>>::archive_persisted_channel(
&self.persister,
monitor_name,
);
}
}

Expand All @@ -674,10 +677,10 @@ pub struct TestPersister {
pub update_rets: Mutex<VecDeque<chain::ChannelMonitorUpdateStatus>>,
/// When we get an update_persisted_channel call *with* a ChannelMonitorUpdate, we insert the
/// [`ChannelMonitor::get_latest_update_id`] here.
pub offchain_monitor_updates: Mutex<HashMap<ChannelId, HashSet<u64>>>,
pub offchain_monitor_updates: Mutex<HashMap<MonitorName, HashSet<u64>>>,
/// When we get an update_persisted_channel call with no ChannelMonitorUpdate, we insert the
/// monitor's funding outpoint here.
pub chain_sync_monitor_persistences: Mutex<VecDeque<ChannelId>>,
pub chain_sync_monitor_persistences: Mutex<VecDeque<MonitorName>>,
}
impl TestPersister {
pub fn new() -> Self {
Expand Down Expand Up @@ -710,23 +713,24 @@ impl<Signer: sign::ecdsa::EcdsaChannelSigner> Persist<Signer> for TestPersister
ret = update_ret;
}

let monitor_name = data.persistence_key();
if let Some(update) = update {
self.offchain_monitor_updates
.lock()
.unwrap()
.entry(data.channel_id())
.entry(monitor_name)
.or_insert(new_hash_set())
.insert(update.update_id);
} else {
self.chain_sync_monitor_persistences.lock().unwrap().push_back(data.channel_id());
self.chain_sync_monitor_persistences.lock().unwrap().push_back(monitor_name);
}
ret
}

fn archive_persisted_channel(&self, data: &ChannelMonitor<Signer>) {
fn archive_persisted_channel(&self, monitor_name: MonitorName) {
// remove the channel from the offchain_monitor_updates and chain_sync_monitor_persistences.
self.offchain_monitor_updates.lock().unwrap().remove(&data.channel_id());
self.chain_sync_monitor_persistences.lock().unwrap().retain(|x| x != &data.channel_id());
self.offchain_monitor_updates.lock().unwrap().remove(&monitor_name);
self.chain_sync_monitor_persistences.lock().unwrap().retain(|x| x != &monitor_name);
}
}

Expand Down

0 comments on commit 8edede0

Please sign in to comment.