Skip to content

Commit

Permalink
Merge pull request #527 from getlipa/feature/fail-recovery-on-corrupt…
Browse files Browse the repository at this point in the history
…ed-monitor
  • Loading branch information
danielgranhao authored Jul 24, 2023
2 parents 0232f56 + 51e5b9f commit 8b2e8fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
9 changes: 6 additions & 3 deletions eel/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::errors::Result;
use crate::interfaces::RemoteStorage;
use crate::key_derivation;
use crate::keys_manager::init_keys_manager;
use crate::storage_persister::{has_local_install, StoragePersister};
use crate::storage_persister::{has_local_install, CorruptedMonitorPolicy, StoragePersister};
use log::info;
use perro::{invalid_input, MapToError};
use std::fs;
Expand Down Expand Up @@ -38,8 +38,11 @@ pub fn recover_lightning_node(
seed_first_half.copy_from_slice(&seed[..32]);
let keys_manager = Arc::new(init_keys_manager(&seed_first_half)?);

let remote_channel_monitors =
storage.fetch_remote_channel_monitors(&*keys_manager, &*keys_manager)?;
let remote_channel_monitors = storage.fetch_remote_channel_monitors(
&*keys_manager,
&*keys_manager,
CorruptedMonitorPolicy::Fail,
)?;
info!(
"Fetched {} channel monitors from remote storage during recovery procedure",
remote_channel_monitors.len()
Expand Down
26 changes: 18 additions & 8 deletions eel/src/storage_persister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use lightning::util::ser::{ReadableArgs, Writeable};
use lightning_persister::FilesystemPersister;
use log::{debug, error, warn};
use perro::Error::RuntimeError;
use perro::{invalid_input, permanent_failure, MapToError, ResultTrait};
use perro::{invalid_input, permanent_failure, runtime_error, MapToError, ResultTrait};
use std::fs;
use std::io::{BufReader, Cursor};
use std::ops::Deref;
Expand All @@ -45,6 +45,11 @@ static MANAGER_KEY: &str = "manager";
static GRAPH_KEY: &str = "network_graph";
static SCORER_KEY: &str = "scorer";

pub(crate) enum CorruptedMonitorPolicy {
Ignore,
Fail,
}

pub(crate) struct StoragePersister {
storage: Arc<Box<dyn RemoteStorage>>,
fs_persister: FilesystemPersister,
Expand Down Expand Up @@ -98,8 +103,11 @@ impl StoragePersister {
.map_to_permanent_failure("Failed to read channel monitors from disk")?;

// Fetch remote channel monitors to make sure remote state hasn't advanced
let mut remote_channel_monitors =
self.fetch_remote_channel_monitors(entropy_source, signer_provider)?;
let mut remote_channel_monitors = self.fetch_remote_channel_monitors(
entropy_source,
signer_provider,
CorruptedMonitorPolicy::Ignore,
)?;

Self::verify_local_state_is_latest_state::<SP>(
&mut local_channel_monitors,
Expand All @@ -114,6 +122,7 @@ impl StoragePersister {
&self,
entropy_source: ES,
signer_provider: SP,
corrupted_monitor_policy: CorruptedMonitorPolicy,
) -> Result<
Vec<(
BlockHash,
Expand Down Expand Up @@ -160,11 +169,12 @@ impl StoragePersister {
"Failed to deserialize remote ChannelMonitor `{}`: {}",
key, e
);
// TODO: Should we return this information to the caller?
// A corrupt remote ChannelMonitor could be harmless if in this case we
// load the local ChannelMonitors and the situation gets fixed. If this
// is a wallet recovery, we will need to load the remote ChannelMonitors,
// and this channel will be lost.
match corrupted_monitor_policy {
CorruptedMonitorPolicy::Ignore => {}
CorruptedMonitorPolicy::Fail => {
return Err(runtime_error(RuntimeErrorCode::RemoteStorageError, "Failed to deserialize a remote ChannelMonitor. Proceeding could cause this channel to be lost."));
}
}
}
}
}
Expand Down

0 comments on commit 8b2e8fc

Please sign in to comment.