Skip to content

Commit

Permalink
save lock
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapk00 committed Jul 14, 2022
1 parent 5618a2d commit ce69377
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ impl Help for SaveCommand {
impl<P: consensus::Parameters + Send + Sync + 'static> Command<P> for SaveCommand {
fn exec(&self, _args: &[&str], lightclient: &LightClient<P>) -> String {
RT.block_on(async move {
match lightclient.do_save().await {
match lightclient.do_save(true).await {
Ok(_) => {
let r = object! { "result" => "success" };
r.pretty(2)
Expand Down Expand Up @@ -1305,7 +1305,7 @@ impl Help for QuitCommand {
impl<P: consensus::Parameters + Send + Sync + 'static> Command<P> for QuitCommand {
fn exec(&self, _args: &[&str], lightclient: &LightClient<P>) -> String {
RT.block_on(async move {
match lightclient.do_save().await {
match lightclient.do_save(true).await {
Ok(_) => "".to_string(),
Err(e) => e,
}
Expand Down
37 changes: 21 additions & 16 deletions lib/src/lightclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {
info!("Created LightClient to {}", &config.server);

// Save
l.do_save()
l.do_save(true)
.await
.map_err(|s| io::Error::new(ErrorKind::PermissionDenied, s))?;

Expand Down Expand Up @@ -289,7 +289,9 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {
};

l.set_wallet_initial_state(birthday).await;
l.do_save().await.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
l.do_save(true)
.await
.map_err(|e| Error::new(ErrorKind::InvalidData, e))?;

info!("Created new wallet!");

Expand Down Expand Up @@ -472,7 +474,7 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {
}
}

pub async fn do_save(&self) -> Result<(), String> {
pub async fn do_save(&self, grab_lock: bool) -> Result<(), String> {
// On mobile platforms, disable the save, because the saves will be handled by the native layer, and not in rust
if cfg!(all(not(target_os = "ios"), not(target_os = "android"))) {
// If the wallet is encrypted but unlocked, lock it again.
Expand All @@ -491,7 +493,11 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {

{
// Prevent any overlapping syncs during save, and don't save in the middle of a sync
let _lock = self.sync_lock.lock().await;
let _lock = if (grab_lock) {
Some(self.sync_lock.lock().await)
} else {
None
};

let mut wallet_bytes = vec![];
match self.wallet.write(&mut wallet_bytes).await {
Expand Down Expand Up @@ -929,7 +935,7 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {
addr
};

self.do_save().await?;
self.do_save(true).await?;

Ok(array![new_address])
}
Expand Down Expand Up @@ -964,7 +970,7 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {
return Err(e);
}

self.do_save().await?;
self.do_save(true).await?;
Ok(array![address])
}

Expand All @@ -986,7 +992,7 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {
addr
};

self.do_save().await?;
self.do_save(true).await?;

Ok(array![new_address])
}
Expand All @@ -1009,7 +1015,7 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {
addr
};

self.do_save().await?;
self.do_save(true).await?;

Ok(array![new_address])
}
Expand Down Expand Up @@ -1037,7 +1043,7 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {
let response = self.do_sync(true).await;

if response.is_ok() {
self.do_save().await?;
self.do_save(true).await?;
}

info!("Rescan finished");
Expand Down Expand Up @@ -1238,6 +1244,10 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {
/// Start syncing in batches with the max size, so we don't consume memory more than
// wha twe can handle.
async fn start_sync(&self) -> Result<JsonValue, String> {
// We can only do one sync at a time because we sync blocks in serial order
// If we allow multiple syncs, they'll all get jumbled up.
let _lock = self.sync_lock.lock().await;

// The top of the wallet
let last_scanned_height = self.wallet.last_scanned_height().await;

Expand Down Expand Up @@ -1293,14 +1303,9 @@ impl<P: consensus::Parameters + Send + Sync + 'static> LightClient<P> {

let mut res = Err("No batches were run!".to_string());
for (batch_num, batch_latest_block) in latest_block_batches.into_iter().enumerate() {
{
// We can only do one sync at a time because we sync blocks in serial order
// If we allow multiple syncs, they'll all get jumbled up.
let _lock = self.sync_lock.lock().await;
res = self.start_sync_batch(batch_latest_block, batch_num).await;
}
res = self.start_sync_batch(batch_latest_block, batch_num).await;

self.do_save().await?;
self.do_save(false).await?;
if res.is_err() {
return res;
}
Expand Down

0 comments on commit ce69377

Please sign in to comment.