Skip to content

Commit

Permalink
rusk-wallet: Add method to store last block height in cache
Browse files Browse the repository at this point in the history
Add sync status enum to display correct sync status to user and current synced block height from the cache
  • Loading branch information
Daksh14 committed Oct 8, 2024
1 parent 1d3d25f commit 3ed82f6
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
37 changes: 29 additions & 8 deletions rusk-wallet/src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,36 @@ fn menu_addr(wallet: &Wallet<WalletFile>) -> anyhow::Result<AddrSelect> {
));
}

use rusk_wallet::SyncStatus;

if let Some(rx) = &wallet.state()?.sync_rx {
if let Ok(status) = rx.try_recv() {
action_menu = action_menu
.separator()
.separator_msg(format!("Sync Status: {}", status));
} else {
action_menu = action_menu
.separator()
.separator_msg("Waiting for Sync to complete..".to_string());
match rx.try_recv() {
Ok(status) => {
let last_height = wallet.last_block_height()?;

action_menu = action_menu.separator().separator_msg(format!(
"Synced at last block height: {}",
last_height
));

match status {
SyncStatus::Synced => (),
SyncStatus::NotSynced => {
action_menu = action_menu
.separator_msg("Syncing in progress..".to_string());
}
SyncStatus::Err(e) => {
action_menu = action_menu.separator().separator_msg(
format!("Sync failed with err: {:?}", e),
)
}
}
}
Err(e) => {
action_menu = action_menu
.separator()
.separator_msg(format!("Sync failed with err: {:?}", e))
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions rusk-wallet/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ impl Cache {
let data = rkyv::to_bytes::<NoteLeaf, TREE_LEAF>(&leaf)
.map_err(|_| Error::Rkyv)?;

// keep track of max block height while inserting notes
self.insert_last_block_height(block_height)?;

let key = nullifier.to_bytes();

self.db.put_cf(&cf, key, data)?;
Expand Down Expand Up @@ -98,6 +101,9 @@ impl Cache {
.map_err(|_| Error::Rkyv)?;
let key = nullifier.to_bytes();

// keep track of max block height while inserting notes
self.insert_last_block_height(block_height)?;

self.db.put_cf(&cf, key, data)?;

Ok(())
Expand Down Expand Up @@ -155,6 +161,26 @@ impl Cache {
}))
}

pub(crate) fn insert_last_block_height(
&self,
block_height: u64,
) -> Result<(), Error> {
let last_block_height = self.last_block_height()?.unwrap_or(0);
let max = std::cmp::max(last_block_height, block_height);

self.db.put(b"last_block_height", max.to_be_bytes())?;

Ok(())
}

pub(crate) fn last_block_height(&self) -> Result<Option<u64>, Error> {
Ok(self.db.get(b"last_block_height")?.map(|x| {
let buff: [u8; 8] = x.try_into().expect("Invalid u64 in cache db");

u64::from_be_bytes(buff)
}))
}

/// Returns an iterator over all unspent notes nullifier for the given pk.
pub(crate) fn unspent_notes_id(
&self,
Expand Down
10 changes: 5 additions & 5 deletions rusk-wallet/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub struct State {
client: RuesHttpClient,
prover: RuskHttpClient,
store: LocalStore,
pub sync_rx: Option<Receiver<String>>,
pub sync_rx: Option<Receiver<SyncStatus>>,
sync_join_handle: Option<JoinHandle<()>>,
}

Expand Down Expand Up @@ -121,7 +121,7 @@ impl State {
}

pub async fn register_sync(&mut self) -> Result<(), Error> {
let (sync_tx, sync_rx) = flume::unbounded::<String>();
let (sync_tx, sync_rx) = flume::unbounded::<SyncStatus>();

self.sync_rx = Some(sync_rx);

Expand All @@ -134,11 +134,11 @@ impl State {

let handle = tokio::spawn(async move {
loop {
let _ = sync_tx.send("Syncing..".to_string());
let _ = sync_tx.send(SyncStatus::NotSynced);

let _ = match sync_db(&client, &cache, &store, status).await {
Ok(_) => sync_tx.send("Syncing Complete".to_string()),
Err(e) => sync_tx.send(format!("Error during sync:.. {e}")),
Ok(_) => sync_tx.send(SyncStatus::Synced),
Err(e) => sync_tx.send(SyncStatus::Err(e)),
};

sleep(Duration::from_secs(SYNC_INTERVAL_SECONDS)).await;
Expand Down
12 changes: 12 additions & 0 deletions rusk-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ use execution_core::{
BlsScalar,
};

/// The enum that the sync status reciever emits so the binary can report
/// on the sync status
#[derive(Debug)]
pub enum SyncStatus {
/// Syncing is complete
Synced,
/// Syncing is in progress
NotSynced,
/// An error occurred while syncing
Err(Error),
}

use currency::Dusk;

/// The largest amount of Dusk that is possible to convert
Expand Down
7 changes: 7 additions & 0 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,13 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
Ok(network_last_pos == db_pos)
}

/// Fetch the last block height from the database
pub fn last_block_height(&self) -> Result<u64, Error> {
let state = self.state()?;

Ok(state.cache().last_block_height()?.unwrap_or(0))
}

/// Close the wallet and zeroize the seed
pub fn close(&mut self) {
self.store.inner_mut().zeroize();
Expand Down

0 comments on commit 3ed82f6

Please sign in to comment.