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

Small improvements #242

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add support for `WALLET_MAX_ADDR` lower than `6` [#244]

### Changed

- Change rusk-wallet to wait for tx to be included

### Fixed

- Fix tx history to avoid useless calls [#243]

## [0.22.0] - 2024-2-28

### Changed
Expand Down Expand Up @@ -480,6 +492,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Implementation of `Store` trait from `wallet-core`
- Implementation of `State` and `Prover` traits from `wallet-core`

[#244]: https://github.com/dusk-network/wallet-cli/issues/244
[#243]: https://github.com/dusk-network/wallet-cli/issues/243
[#238]: https://github.com/dusk-network/wallet-cli/issues/238
[#235]: https://github.com/dusk-network/wallet-cli/issues/235
[#231]: https://github.com/dusk-network/wallet-cli/issues/231
Expand Down
15 changes: 6 additions & 9 deletions src/bin/command/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt::{self, Display};

use dusk_plonk::prelude::BlsScalar;
use dusk_wallet::DecodedNote;
use dusk_wallet_core::Transaction;
use rusk_abi::dusk;
use rusk_abi::hash::Hasher;

use crate::io::{self, GraphQL};
use crate::settings::Settings;
Expand All @@ -23,7 +21,7 @@ pub struct TransactionHistory {
amount: f64,
fee: u64,
pub tx: Transaction,
id: BlsScalar,
id: String,
}

impl TransactionHistory {
Expand Down Expand Up @@ -52,7 +50,7 @@ impl Display for TransactionHistory {
}
};

let tx_id = hex::encode(self.id.to_bytes());
let tx_id = &self.id;
let heigth = self.height;

write!(
Expand Down Expand Up @@ -96,11 +94,11 @@ pub(crate) async fn transaction_from_notes(

let note_hash = decoded_note.note.hash();
// Looking for the transaction which created the note
let note_creator = txs.iter().find(|(t, _)| {
let note_creator = txs.iter().find(|(t, _, _)| {
t.outputs().iter().any(|&n| n.hash().eq(&note_hash))
});

if let Some((t, gas_spent)) = note_creator {
if let Some((t, tx_id, gas_spent)) = note_creator {
let inputs_amount: f64 = t
.nullifiers()
.iter()
Expand All @@ -113,16 +111,15 @@ pub(crate) async fn transaction_from_notes(
true => TransactionDirection::Out,
false => TransactionDirection::In,
};
let hash_to_find = Hasher::digest(t.to_hash_input_bytes());
match ret.iter_mut().find(|th| th.id == hash_to_find) {
match ret.iter_mut().find(|th| &th.id == tx_id) {
Some(tx) => tx.amount += note_amount,
None => ret.push(TransactionHistory {
direction,
height: decoded_note.block_height,
amount: note_amount - inputs_amount,
fee: gas_spent * t.fee().gas_price,
tx: t.clone(),
id: hash_to_find,
id: tx_id.clone(),
}),
}
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ fn menu_addr(wallet: &Wallet<WalletFile>) -> anyhow::Result<AddrSelect> {
.add(AddrSelect::Address(Box::new(addr.clone())), preview);
}

let remaining_addresses =
MAX_ADDRESSES.saturating_sub(wallet.addresses().len());
let mut action_menu = Menu::new()
.separator()
.add(AddrSelect::NewAddress, "New address");

// show warning early on at 250 addresses
if wallet.addresses().len() >= MAX_ADDRESSES - 5 {
// show warning if less than
if remaining_addresses < 5 {
action_menu = action_menu.separator().separator_msg(format!(
"\x1b[93m{}\x1b[0m This wallet only supports up to {MAX_ADDRESSES} addresses, you have {} addresses ",
"Warning:",
Expand Down
47 changes: 15 additions & 32 deletions src/bin/io/gql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,18 @@ pub struct GraphQL {
status: fn(&str),
}

// helper structs to deserialize response
#[derive(Deserialize)]
struct SpentTx {
pub err: Option<String>,
#[serde(alias = "gasSpent", default)]
pub gas_spent: f64,
}

#[derive(Deserialize)]
struct Tx {
pub id: String,
#[serde(default)]
pub raw: String,
pub err: Option<String>,
#[serde(alias = "gasSpent", default)]
pub gas_spent: f64,
}
#[derive(Deserialize)]
struct Block {
pub transactions: Vec<Tx>,
pub transactions: Vec<SpentTx>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -71,24 +66,17 @@ impl GraphQL {

/// Wait for a transaction to be confirmed (included in a block)
pub async fn wait_for(&self, tx_id: &str) -> anyhow::Result<()> {
const TIMEOUT_SECS: i32 = 30;
let mut i = 1;
while i <= TIMEOUT_SECS {
loop {
let status = self.tx_status(tx_id).await?;

match status {
TxStatus::Ok => break,
TxStatus::Error(err) => return Err(Error::Transaction(err))?,
TxStatus::NotFound => {
(self.status)(
format!(
"Waiting for confirmation... ({}/{})",
i, TIMEOUT_SECS
)
.as_str(),
"Waiting for tx to be included into a block...",
);
sleep(Duration::from_millis(1000)).await;
i += 1;
}
}
}
Expand All @@ -101,7 +89,7 @@ impl GraphQL {
tx_id: &str,
) -> anyhow::Result<TxStatus, GraphQLError> {
let query =
"query { tx(hash: \"####\") { err }}".replace("####", tx_id);
"query { tx(hash: \"####\") { id, err }}".replace("####", tx_id);
let response = self.query(&query).await?;
let response = serde_json::from_slice::<SpentTxResponse>(&response)?.tx;

Expand All @@ -116,8 +104,8 @@ impl GraphQL {
pub async fn txs_for_block(
&self,
block_height: u64,
) -> anyhow::Result<Vec<(Transaction, u64)>, GraphQLError> {
let query = "query { block(height: ####) { transactions {id, raw}}}"
) -> anyhow::Result<Vec<(Transaction, String, u64)>, GraphQLError> {
let query = "query { block(height: ####) { transactions {id, raw, gasSpent, err}}}"
.replace("####", block_height.to_string().as_str());

let response = self.query(&query).await?;
Expand All @@ -126,17 +114,11 @@ impl GraphQL {
let block = response.ok_or(GraphQLError::BlockInfo)?;
let mut ret = vec![];

for tx in block.transactions {
let tx_raw =
hex::decode(&tx.raw).map_err(|_| GraphQLError::TxStatus)?;
for spent_tx in block.transactions {
let tx_raw = hex::decode(&spent_tx.raw)
.map_err(|_| GraphQLError::TxStatus)?;
let ph_tx = Transaction::from_slice(&tx_raw).unwrap();
let query = "query { tx(hash: \"####\") { gasSpent, err }}"
.replace("####", &tx.id);
let response = self.query(&query).await?;
let response =
serde_json::from_slice::<SpentTxResponse>(&response)?.tx;
let spent_tx = response.ok_or(GraphQLError::TxStatus)?;
ret.push((ph_tx, spent_tx.gas_spent as u64));
ret.push((ph_tx, spent_tx.id, spent_tx.gas_spent as u64));
}
Ok(ret)
}
Expand Down Expand Up @@ -194,9 +176,10 @@ async fn test() -> Result<(), Box<dyn std::error::Error>> {
)
.await?;
let block_txs = gql.txs_for_block(90).await?;
block_txs.iter().for_each(|(t, _)| {
block_txs.into_iter().for_each(|(t, chain_txid, _)| {
let hash = rusk_abi::hash::Hasher::digest(t.to_hash_input_bytes());
let tx_id = hex::encode(hash.to_bytes());
assert_eq!(chain_txid, tx_id);
println!("txid: {tx_id}");
});
Ok(())
Expand Down
10 changes: 1 addition & 9 deletions src/bin/io/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,9 @@ use std::time::Duration;

use tracing::info;

const STATUS_SIZE: usize = 35;

/// Prints an interactive status message
pub(crate) fn interactive(status: &str) {
let filln = STATUS_SIZE - status.len();
let fill = if filln > 0 {
" ".repeat(filln)
} else {
"".to_string()
};
print!("{}{}\r", status, fill);
print!("\r{status: <50}\r");
let mut stdout = stdout();
stdout.flush().unwrap();
thread::sleep(Duration::from_millis(85));
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ const DEFAULT_MAX_ADDRESSES: usize = 25;
const fn get_max_addresses() -> usize {
match option_env!("WALLET_MAX_ADDR") {
Some(v) => match konst::primitive::parse_usize(v) {
Ok(e) if e > DEFAULT_MAX_ADDRESSES => {
Ok(e) if e > 255 => {
panic!("WALLET_MAX_ADDR must be lower or equal to 255")
}
Ok(e) if e > 5 => e,
Ok(_) => panic!("WALLET_MAX_ADDR must be greater than 5"),
Err(_) => panic!("Invalid WALLET_MAX_ADDR"),
Ok(e) if e > 0 => e,
_ => panic!("Invalid WALLET_MAX_ADDR"),
},
None => DEFAULT_MAX_ADDRESSES,
}
Expand Down