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

rusk-wallet: Add is_sycned() function call to estimate sync completion #2540

Merged
merged 9 commits into from
Oct 1, 2024
56 changes: 48 additions & 8 deletions rusk-wallet/src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub(crate) async fn run_loop(
addr: wallet.bls_public_key(addr.index()?),
};

let is_synced = wallet.is_synced().await?;

loop {
// get balance for this address
prompt::hide_cursor()?;
Expand All @@ -82,25 +84,46 @@ pub(crate) async fn run_loop(
let spendable = phoenix_bal.spendable.into();
let total: Dusk = phoenix_bal.value.into();

let mut spendable_str = format!("{}", spendable);
let mut total_str = format!("{}", total);
let mut moonlight_bal_str = format!("{}", moonlight_bal);

// display placeholders if not synced yet
if !is_synced {
moonlight_bal_str = String::from("syncing...");
spendable_str = moonlight_bal_str.clone();
total_str = moonlight_bal_str.clone();
}
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved

prompt::hide_cursor()?;

// display address information
println!();
println!();
println!("{0: <20} - Total: {moonlight_bal}", "Moonlight Balance");
println!(
"{0: <20} - Total: {moonlight_bal_str}",
"Moonlight Balance"
);
println!("{0: <20} {moonlight}", "Moonlight Address");

println!();
println!("{0: <20} - Spendable: {spendable}", "Phoenix Balance",);
println!("{0: <20} - Total: {total}", "");
println!(
"{0: <20} - Spendable: {spendable_str}",
"Phoenix Balance",
);
println!("{0: <20} - Total: {total_str}", "");
println!("{0: <20} {addr}", "Phoenix Address");
println!();

// request operation to perform
let op = match wallet.is_online().await {
true => {
menu_op(addr.clone(), spendable, moonlight_bal, settings)
}
true => menu_op(
addr.clone(),
spendable,
moonlight_bal,
settings,
is_synced,
),
false => menu_op_offline(addr.clone(), settings),
};

Expand Down Expand Up @@ -417,10 +440,11 @@ fn menu_op(
phoenix_balance: Dusk,
moonlight_balance: Dusk,
settings: &Settings,
is_synced: bool,
) -> anyhow::Result<AddrOp> {
use CommandMenuItem as CMI;

let cmd_menu = Menu::new()
let mut cmd_menu = Menu::new()
.add(CMI::StakeInfo, "Check Existing Stake")
.add(CMI::PhoenixTransactions, "Phoenix Transactions")
.add(CMI::MoonlightTransactions, "Moonlight Transactions")
Expand All @@ -431,8 +455,24 @@ fn menu_op(
.add(CMI::Back, "Back")
.separator();

let msg;

if !is_synced {
cmd_menu = Menu::new()
.separator()
.add(CMI::StakeInfo, "Check Existing Stake")
.add(CMI::Export, "Export provisioner key-pair")
.separator()
.add(CMI::Back, "Back")
.separator();

msg = "Not Synced yet, Come back after its done."
} else {
msg = "What do you want to do?"
}
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved

let q = Question::select("theme")
.message("What would you like to do?")
.message(msg)
.choices(cmd_menu.clone())
.build();

Expand Down
19 changes: 19 additions & 0 deletions rusk-wallet/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,25 @@ impl State {
Ok(branch)
}

/// Queries the transfer contract for the latest network position.
pub async fn fetch_network_pos(&self) -> Result<u64, Error> {
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved
let status = self.status;
status("Fetching latest note position...");

let data = self
.client
.contract_query::<_, _, { u64::SIZE }>(
TRANSFER_CONTRACT,
"num_notes",
&(),
)
.await?;

let res: u64 = rkyv::from_bytes(&data).map_err(|_| Error::Rkyv)?;

Ok(res)
}

pub fn close(&mut self) {
// UNWRAP: its okay to panic here because we're closing the database
// if there's an error we want an exception to happen
Expand Down
17 changes: 13 additions & 4 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,15 +1027,15 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
) -> Result<Transaction, Error> {
let state = self.state()?;
let sender_index = sender.index()?;
let pk = sender.apk()?;
let nonce = state.fetch_account(pk).await?.nonce + 1;
let pk = &self.bls_public_key(sender_index);
let nonce = state.fetch_account(&pk).await?.nonce + 1;
let chain_id = state.fetch_chain_id().await?;

let mut sender_sk = self.bls_secret_key(sender_index);

let deploy = moonlight_deployment(
&sender_sk, bytes_code, pk, init_args, gas.limit, gas.price, nonce,
0, chain_id,
&sender_sk, bytes_code, &pk, init_args, gas.limit, gas.price,
nonce, 0, chain_id,
)?;

sender_sk.zeroize();
Expand Down Expand Up @@ -1151,6 +1151,15 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
}
}

/// Check if the wallet is synced
pub async fn is_synced(&mut self) -> Result<bool, Error> {
let state = self.state()?;
let db_pos = state.cache().last_pos()?.unwrap_or(0);
let network_last_pos = state.fetch_network_pos().await? - 1;
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved

Ok(network_last_pos == db_pos)
}

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