diff --git a/CHANGELOG.md b/CHANGELOG.md index 09b90c97..79d212df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add balance display when offline +- Add `Wallet::sync` method for sync cache update +- Add `Wallet::register_sync` method for async cache update ### Changed -- Change `connect` and `connect_with_status`to specify if cache sync is required +- Change wallet to not sync automatically - Change spent notes to be in a different ColumnFamily - Change `StateClient::fetch_existing_nullifiers` to return empty data. - Change `fetch_notes` to use note's position instead of height [#190] diff --git a/src/bin/command.rs b/src/bin/command.rs index ccf93ab9..18bcb1f6 100644 --- a/src/bin/command.rs +++ b/src/bin/command.rs @@ -196,6 +196,10 @@ impl Command { ) -> anyhow::Result { match self { Command::Balance { addr, spendable } => { + // don't check result of wallet sync, since we support offline + // balance + let _ = wallet.sync().await; + let addr = match addr { Some(addr) => wallet.claim_as_address(addr)?, None => wallet.default_address(), @@ -227,6 +231,7 @@ impl Command { gas_limit, gas_price, } => { + wallet.sync().await?; let sender = match sndr { Some(addr) => wallet.claim_as_address(addr)?, None => wallet.default_address(), @@ -242,6 +247,7 @@ impl Command { gas_limit, gas_price, } => { + wallet.sync().await?; let addr = match addr { Some(addr) => wallet.claim_as_address(addr)?, None => wallet.default_address(), @@ -257,6 +263,7 @@ impl Command { gas_limit, gas_price, } => { + wallet.sync().await?; let addr = match addr { Some(addr) => wallet.claim_as_address(addr)?, None => wallet.default_address(), @@ -284,6 +291,7 @@ impl Command { gas_limit, gas_price, } => { + wallet.sync().await?; let addr = match addr { Some(addr) => wallet.claim_as_address(addr)?, None => wallet.default_address(), @@ -299,6 +307,7 @@ impl Command { gas_limit, gas_price, } => { + wallet.sync().await?; let addr = match addr { Some(addr) => wallet.claim_as_address(addr)?, None => wallet.default_address(), @@ -327,6 +336,7 @@ impl Command { Ok(RunResult::ExportedKeys(pub_key, key_pair)) } Command::History { addr } => { + wallet.sync().await?; let addr = match addr { Some(addr) => wallet.claim_as_address(addr)?, None => wallet.default_address(), diff --git a/src/bin/main.rs b/src/bin/main.rs index b91d3a18..1245f167 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -68,7 +68,6 @@ async fn connect( mut wallet: Wallet, settings: &Settings, status: fn(&str), - cache_sync: bool, ) -> Wallet where F: SecureWalletFile + std::fmt::Debug, @@ -78,7 +77,6 @@ where &settings.state.to_string(), &settings.prover.to_string(), status, - cache_sync, ) .await; @@ -275,10 +273,7 @@ async fn exec() -> anyhow::Result<()> { false => status::interactive, }; - // if any command is provided, cache must by synced before any operation - let cache_sync = cmd.is_some(); - - wallet = connect(wallet, &settings, status_cb, cache_sync).await; + wallet = connect(wallet, &settings, status_cb).await; // run command match cmd { @@ -331,6 +326,7 @@ async fn exec() -> anyhow::Result<()> { RunResult::Create() | RunResult::Restore() => {} }, None => { + wallet.register_sync().await?; interactive::run_loop(&mut wallet, &settings).await?; } } diff --git a/src/wallet.rs b/src/wallet.rs index d5241424..ebfaf953 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -235,7 +235,6 @@ impl Wallet { rusk_addr: S, prov_addr: S, status: fn(&str), - cache_sync: bool, ) -> Result<(), Error> where S: Into, @@ -273,17 +272,6 @@ impl Wallet { status, )?; - if cache_sync { - if state_status.is_ok() { - state.sync().await?; - } - } else { - let (sync_tx, sync_rx) = flume::unbounded::(); - state.register_sync(sync_tx).await?; - // set sync reciever to notify successful sync - self.sync_rx = Some(sync_rx); - } - // create wallet instance self.wallet = Some(WalletCore::new(self.store.clone(), state, prover)); @@ -293,6 +281,24 @@ impl Wallet { Ok(()) } + /// Sync wallet state + pub async fn sync(&self) -> Result<(), Error> { + self.connected_wallet().await?.state().sync().await + } + + /// Helper function to register for async-sync outside of connect + pub async fn register_sync(&mut self) -> Result<(), Error> { + match self.wallet.as_ref() { + Some(w) => { + let (sync_tx, sync_rx) = flume::unbounded::(); + w.state().register_sync(sync_tx).await?; + self.sync_rx = Some(sync_rx); + Ok(()) + } + None => Err(Error::Offline), + } + } + /// Checks if the wallet has an active connection to the network pub async fn is_online(&self) -> bool { match self.wallet.as_ref() {