Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #202 from dusk-network/connection_handling
Browse files Browse the repository at this point in the history
Refactor connection handling
  • Loading branch information
herr-seppia authored Sep 20, 2023
2 parents aa7a2fb + 6fd1c32 commit f3d735f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions src/bin/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ impl Command {
) -> anyhow::Result<RunResult> {
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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 2 additions & 6 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ async fn connect<F>(
mut wallet: Wallet<F>,
settings: &Settings,
status: fn(&str),
cache_sync: bool,
) -> Wallet<F>
where
F: SecureWalletFile + std::fmt::Debug,
Expand All @@ -78,7 +77,6 @@ where
&settings.state.to_string(),
&settings.prover.to_string(),
status,
cache_sync,
)
.await;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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?;
}
}
Expand Down
30 changes: 18 additions & 12 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
rusk_addr: S,
prov_addr: S,
status: fn(&str),
cache_sync: bool,
) -> Result<(), Error>
where
S: Into<String>,
Expand Down Expand Up @@ -273,17 +272,6 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
status,
)?;

if cache_sync {
if state_status.is_ok() {
state.sync().await?;
}
} else {
let (sync_tx, sync_rx) = flume::unbounded::<String>();
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));

Expand All @@ -293,6 +281,24 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
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::<String>();
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() {
Expand Down

0 comments on commit f3d735f

Please sign in to comment.