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

Wipe storage cache in case of data corrupted issue #566

Merged
merged 3 commits into from
Nov 14, 2024
Merged
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
61 changes: 52 additions & 9 deletions lib/core/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fs;
use std::io::Write;
use std::{str::FromStr, sync::Arc};
use std::{path::Path, str::FromStr, sync::Arc};

use anyhow::{anyhow, Result};
use async_trait::async_trait;
Expand Down Expand Up @@ -94,6 +95,7 @@ pub(crate) struct LiquidOnchainWallet {
config: Config,
persister: Arc<Persister>,
wallet: Arc<Mutex<Wollet>>,
working_dir: String,
pub(crate) signer: SdkLwkSigner,
}

Expand All @@ -104,20 +106,47 @@ impl LiquidOnchainWallet {
persister: Arc<Persister>,
user_signer: Arc<Box<dyn Signer>>,
) -> Result<Self> {
let signer = crate::signer::SdkLwkSigner::new(user_signer)?;
let descriptor = LiquidOnchainWallet::get_descriptor(&signer, config.network)?;
let elements_network: ElementsNetwork = config.network.into();
let signer = crate::signer::SdkLwkSigner::new(user_signer.clone())?;
let wollet = Self::create_wallet(&config, working_dir, &signer)?;

let lwk_persister = FsPersister::new(working_dir, elements_network, &descriptor)?;
let wollet = Wollet::new(elements_network, lwk_persister, descriptor)?;
Ok(Self {
config,
persister,
wallet: Arc::new(Mutex::new(wollet)),
working_dir: working_dir.clone(),
signer,
})
}

fn create_wallet<P: AsRef<Path>>(
config: &Config,
working_dir: P,
signer: &SdkLwkSigner,
) -> Result<Wollet> {
let elements_network: ElementsNetwork = config.network.into();
let descriptor = LiquidOnchainWallet::get_descriptor(signer, config.network)?;
let mut lwk_persister =
FsPersister::new(working_dir.as_ref(), elements_network, &descriptor)?;

match Wollet::new(elements_network, lwk_persister, descriptor.clone()) {
Ok(wollet) => Ok(wollet),
Err(lwk_wollet::Error::UpdateHeightTooOld { .. }) => {
warn!("Update height too old, wipping storage and retrying");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
warn!("Update height too old, wipping storage and retrying");
warn!("Update height too old, wiping storage and retrying");

let mut path = working_dir.as_ref().to_path_buf();
path.push(elements_network.as_str());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the passed working_dir is already formatted with the network/fingerprint here:

let working_dir = config.get_wallet_working_dir(fingerprint_hex)?;

Do we need to add it again?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes but the lwk wallet dir is a nested directory under that directory.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah right, it's the ElementsNetwork, sorry

fs::remove_dir_all(&path)?;
warn!("Wiping wallet in path: {:?}", path);
lwk_persister = FsPersister::new(working_dir, elements_network, &descriptor)?;
Ok(Wollet::new(
elements_network,
lwk_persister,
descriptor.clone(),
)?)
}
Err(e) => Err(e.into()),
}
}

fn get_descriptor(
signer: &SdkLwkSigner,
network: LiquidNetwork,
Expand Down Expand Up @@ -297,12 +326,26 @@ impl OnchainWallet for LiquidOnchainWallet {
.persister
.get_last_derivation_index()?
.unwrap_or_default();
lwk_wollet::full_scan_to_index_with_electrum_client(
match lwk_wollet::full_scan_to_index_with_electrum_client(
&mut wallet,
index,
&mut electrum_client,
)?;
Ok(())
) {
Ok(()) => Ok(()),
Err(lwk_wollet::Error::UpdateHeightTooOld { .. }) => {
warn!("Full scan failed with update height too old, wiping storage and retrying");
let mut new_wallet =
Self::create_wallet(&self.config, &self.working_dir, &self.signer)?;
lwk_wollet::full_scan_to_index_with_electrum_client(
&mut new_wallet,
index,
&mut electrum_client,
)?;
*wallet = new_wallet;
Ok(())
}
Err(e) => Err(e.into()),
}
}

fn sign_message(&self, message: &str) -> Result<String> {
Expand Down
Loading