From d914419745991e45d42f712069d6de67149e8733 Mon Sep 17 00:00:00 2001 From: moana Date: Fri, 6 Sep 2024 08:33:48 +0200 Subject: [PATCH] rusk: Replace `test-wallet` with `rusk-wallet` for testing --- rusk/Cargo.toml | 2 +- rusk/tests/common/test_wallet.dat | Bin 0 -> 108 bytes rusk/tests/common/wallet.rs | 158 ++++--------------- rusk/tests/rusk-state.rs | 97 +++++------- rusk/tests/services/contract_deployment.rs | 105 +++++++------ rusk/tests/services/gas_behavior.rs | 63 ++++---- rusk/tests/services/multi_transfer.rs | 73 ++++----- rusk/tests/services/owner_calls.rs | 21 +-- rusk/tests/services/stake.rs | 171 ++++++++++----------- rusk/tests/services/transfer.rs | 59 ++++--- rusk/tests/services/unspendable.rs | 80 +++++----- 11 files changed, 350 insertions(+), 479 deletions(-) create mode 100644 rusk/tests/common/test_wallet.dat diff --git a/rusk/Cargo.toml b/rusk/Cargo.toml index 935ceaea9e..dbb667fd74 100644 --- a/rusk/Cargo.toml +++ b/rusk/Cargo.toml @@ -80,7 +80,7 @@ rusk-recovery = { version = "0.6", path = "../rusk-recovery", optional = true } futures = { version = "0.3", optional = true } [dev-dependencies] -test-wallet = { version = "0.1.0", path = "../test-wallet" } +rusk-wallet = { version = "0.1.0", path = "../rusk-wallet" } test-context = "0.1" reqwest = "0.12" rusk-recovery = { version = "0.6", path = "../rusk-recovery", features = ["state"] } diff --git a/rusk/tests/common/test_wallet.dat b/rusk/tests/common/test_wallet.dat new file mode 100644 index 0000000000000000000000000000000000000000..9f7bebf5092557667040430264e088b2496575fc GIT binary patch literal 108 zcmV-y0F(a!a&v0}000000098}w4dyN;0QubpeU0>@`VYd%lD9kc(v^G>vq)6hJ_=G zc;>_W-i+9o@%c3G5-g>OV2 O>&}e%=r52L>jR+V-#3K- literal 0 HcmV?d00001 diff --git a/rusk/tests/common/wallet.rs b/rusk/tests/common/wallet.rs index af5cf34dad..0d70d8da51 100644 --- a/rusk/tests/common/wallet.rs +++ b/rusk/tests/common/wallet.rs @@ -4,137 +4,47 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use std::collections::HashMap; -use std::fmt::Debug; -use std::sync::{Arc, RwLock}; - -use crate::common::block::Block as BlockAwait; - -use dusk_bytes::Serializable; -use execution_core::{ - signatures::bls::PublicKey as BlsPublicKey, - stake::StakeData, - transfer::{ - moonlight::AccountData, - phoenix::{Note, NoteOpening, ViewKey}, - }, - BlsScalar, -}; -use futures::StreamExt; -use rusk::{Error, Result, Rusk}; -use test_wallet::{self as wallet, Store}; -use tracing::info; +use rusk::{Error, Result}; +use rusk_wallet::{SecureWalletFile, Wallet, WalletPath}; #[derive(Debug, Clone)] -pub struct TestStore; - -impl Store for TestStore { - type Error = (); - - fn get_seed(&self) -> Result<[u8; 64], Self::Error> { - Ok([0; 64]) - } -} - -#[derive(Clone)] -pub struct TestStateClient { - pub rusk: Rusk, - pub cache: Arc, DummyCacheItem>>>, +#[allow(dead_code)] +pub struct WalletFile { + path: WalletPath, + pwd: Vec, } -impl std::fmt::Debug for TestStateClient { - fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - Ok(()) +impl SecureWalletFile for WalletFile { + fn path(&self) -> &WalletPath { + &self.path } -} -impl wallet::StateClient for TestStateClient { - type Error = Error; - - /// Find notes for a view key, starting from the given block height. - fn fetch_notes( - &self, - vk: &ViewKey, - ) -> Result, Self::Error> { - let cache_read = self.cache.read().unwrap(); - let mut vk_cache = if cache_read.contains_key(&vk.to_bytes().to_vec()) { - cache_read.get(&vk.to_bytes().to_vec()).unwrap().clone() - } else { - DummyCacheItem::default() - }; - - info!("Requesting notes from height {}", vk_cache.last_height); - let vk_bytes = vk.to_bytes(); - - let stream = self - .rusk - .get_notes(vk_bytes.as_ref(), vk_cache.last_height) - .wait()?; - - let response_notes = stream.collect::>().wait(); - - for (note, block_height) in response_notes { - // Filter out duplicated notes and update the last - vk_cache.add(note, block_height) - } - drop(cache_read); - self.cache - .write() - .unwrap() - .insert(vk.to_bytes().to_vec(), vk_cache.clone()); - - Ok(vk_cache.notes) - } - - /// Fetch the current root of the state. - fn fetch_root(&self) -> Result { - self.rusk.tree_root() - } - - fn fetch_existing_nullifiers( - &self, - nullifiers: &[BlsScalar], - ) -> Result, Self::Error> { - self.rusk.existing_nullifiers(&nullifiers.to_vec()) - } - - /// Queries the node to find the opening for a specific note. - fn fetch_opening(&self, note: &Note) -> Result { - self.rusk - .tree_opening(*note.pos())? - .ok_or(Error::OpeningPositionNotFound(*note.pos())) - } - - fn fetch_stake(&self, pk: &BlsPublicKey) -> Result { - let stake = self.rusk.provisioner(pk)?.unwrap_or(StakeData::EMPTY); - Ok(stake) - } - - fn fetch_account( - &self, - pk: &BlsPublicKey, - ) -> Result { - let account = self.rusk.account(pk)?; - Ok(account) - } - - fn fetch_chain_id(&self) -> Result { - let chain_id = self.rusk.chain_id()?; - Ok(chain_id) + fn pwd(&self) -> &[u8] { + &self.pwd } } -#[derive(Default, Debug, Clone)] -pub struct DummyCacheItem { - notes: Vec<(Note, u64)>, - last_height: u64, -} - -impl DummyCacheItem { - fn add(&mut self, note: Note, block_height: u64) { - if !self.notes.contains(&(note.clone(), block_height)) { - self.notes.push((note.clone(), block_height)); - self.last_height = block_height; - } - } +#[allow(dead_code)] +pub fn test_wallet() -> Result, Error> { + // the file 'test_wallet.dat' needs to be stored in the current dir + let wallet_path = WalletPath::from( + std::env::current_dir()? + .as_path() + .join("tests") + .join("common") + .join("test_wallet.dat"), + ); + let wallet_file = WalletFile { + path: wallet_path, + pwd: blake3::hash(b"mypassword").as_bytes().to_vec(), + }; + + // Load wallet from the wallet-file, which is a wallet with the seed of + // an array of `0u8`. Check 'rusk-wallet/tests/generate_test_wallet.rs' + // on how to generate such a wallet-file. + println!("the path is: {}", wallet_file.path); + let wallet = + Wallet::from_file(wallet_file).map_err(|_| Error::RestoreFailed)?; + + Ok(wallet) } diff --git a/rusk/tests/rusk-state.rs b/rusk/tests/rusk-state.rs index 72e6eab0fd..ee8f76f3f4 100644 --- a/rusk/tests/rusk-state.rs +++ b/rusk/tests/rusk-state.rs @@ -14,6 +14,7 @@ use std::sync::{mpsc, Arc}; use execution_core::{ transfer::{ + data::TransactionData, phoenix::{ Note, NoteLeaf, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey, @@ -29,6 +30,7 @@ use rand::rngs::StdRng; use rusk::node::{Rusk, RuskTip}; use rusk::Result; use rusk_abi::VM; +use rusk_wallet::gas::Gas; use tempfile::tempdir; use tracing::info; @@ -176,22 +178,11 @@ pub fn rusk_state_finalized() -> Result<()> { #[allow(dead_code)] // #[tokio::test(flavor = "multi_thread")] async fn generate_phoenix_txs() -> Result<(), Box> { - use common::wallet::{TestStateClient, TestStore}; use std::io::Write; common::logger(); - let tmp = tempdir()?; - let snapshot = toml::from_str(include_str!("./config/bench.toml")) - .expect("Cannot deserialize config"); - - let rusk = new_state(&tmp, &snapshot, 100_000_000_000)?; - - let cache = - Arc::new(std::sync::RwLock::new(std::collections::HashMap::new())); - - let wallet = - test_wallet::Wallet::new(TestStore, TestStateClient { rusk, cache }); + let wallet = common::wallet::test_wallet()?; const N_ADDRESSES: usize = 100; @@ -204,25 +195,24 @@ async fn generate_phoenix_txs() -> Result<(), Box> { for sender_index in 0..N_ADDRESSES as u8 { let wallet = wallet.clone(); - let mut rng = StdRng::seed_from_u64(0xdead); let receiver_index = (sender_index + 1) % N_ADDRESSES as u8; - let receiver = wallet.phoenix_public_key(receiver_index).unwrap(); - - let task = tokio::task::spawn_blocking(move || { - wallet - .phoenix_transfer( - &mut rng, - sender_index, - &receiver, - TRANSFER_VALUE, - GAS_LIMIT, - LUX, - ) - .expect("Making a transfer TX should succeed") - }); - - let tx = task.await.expect("Joining should succeed"); + let sender_addr = wallet.addresses()[sender_index as usize].clone(); + let receiver_addr = wallet.addresses()[receiver_index as usize].clone(); + + let tx = wallet + .phoenix_transfer( + &sender_addr, + &receiver_addr, + TRANSFER_VALUE.into(), + rusk_wallet::gas::Gas { + limit: GAS_LIMIT, + price: LUX, + }, + ) + .await + .expect("Making a transfer TX should succeed"); + txs_file.write(hex::encode(tx.to_var_bytes()).as_bytes())?; txs_file.write(b"\n")?; } @@ -236,24 +226,13 @@ async fn generate_phoenix_txs() -> Result<(), Box> { // - run the test 'generate_moonlight_txs' // - move the resulting "moonlight-txs" file under "benches/moonlight-txs" #[allow(dead_code)] -// #[tokio::test(flavor = "multi_thread")] -async fn generate_moonlight_txs() -> Result<(), Box> { - use common::wallet::{TestStateClient, TestStore}; +// #[test] +fn generate_moonlight_txs() -> Result<(), Box> { use std::io::Write; common::logger(); - let tmp = tempdir()?; - let snapshot = toml::from_str(include_str!("./config/bench.toml")) - .expect("Cannot deserialize config"); - - let rusk = new_state(&tmp, &snapshot, 100_000_000_000)?; - - let cache = - Arc::new(std::sync::RwLock::new(std::collections::HashMap::new())); - - let wallet = - test_wallet::Wallet::new(TestStore, TestStateClient { rusk, cache }); + let wallet = common::wallet::test_wallet()?; const N_ADDRESSES: usize = 100; @@ -266,23 +245,25 @@ async fn generate_moonlight_txs() -> Result<(), Box> { for sender_index in 0..N_ADDRESSES as u8 { let wallet = wallet.clone(); + let sender = &wallet.addresses()[sender_index as usize]; let receiver_index = (sender_index + 1) % N_ADDRESSES as u8; - let receiver = wallet.account_public_key(receiver_index).unwrap(); - - let task = tokio::task::spawn_blocking(move || { - wallet - .moonlight_transfer( - sender_index, - receiver, - TRANSFER_VALUE, - GAS_LIMIT, - LUX, - ) - .expect("Making a transfer TX should succeed") - }); - - let tx = task.await.expect("Joining should succeed"); + let receiver = wallet.bls_public_key(receiver_index); + + let tx = wallet + .moonlight_transaction( + sender, + Some(receiver), + TRANSFER_VALUE.into(), + 0.into(), + Gas { + limit: GAS_LIMIT, + price: LUX, + }, + None::, + ) + .expect("Making a transfer TX should succeed"); + txs_file.write(hex::encode(tx.to_var_bytes()).as_bytes())?; txs_file.write(b"\n")?; } diff --git a/rusk/tests/services/contract_deployment.rs b/rusk/tests/services/contract_deployment.rs index 0aa17d5751..2a8ad8b127 100644 --- a/rusk/tests/services/contract_deployment.rs +++ b/rusk/tests/services/contract_deployment.rs @@ -4,28 +4,24 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use std::collections::HashMap; use std::path::{Path, PathBuf}; -use std::sync::{Arc, RwLock}; use execution_core::{ transfer::data::{ContractBytecode, ContractDeploy, TransactionData}, ContractId, }; -use rand::prelude::*; -use rand::rngs::StdRng; use rusk::gen_id::gen_contract_id; use rusk::{Result, Rusk}; use rusk_abi::{ContractData, PiecrustError}; use rusk_recovery_tools::state; +use rusk_wallet::{currency::Lux, gas::Gas, Wallet}; use tempfile::tempdir; -use test_wallet::{self as wallet, Wallet}; use tokio::sync::broadcast; use tracing::info; use crate::common::logger; use crate::common::state::{generator_procedure, ExecuteResult}; -use crate::common::wallet::{TestStateClient, TestStore}; +use crate::common::wallet::{test_wallet, WalletFile}; const BLOCK_HEIGHT: u64 = 1; const BLOCK_GAS_LIMIT: u64 = 1_000_000_000_000; @@ -33,9 +29,9 @@ const GAS_LIMIT: u64 = 200_000_000; const GAS_LIMIT_NOT_ENOUGH_TO_SPEND: u64 = 10_000_000; const GAS_LIMIT_NOT_ENOUGH_TO_SPEND_AND_DEPLOY: u64 = 11_000_000; const GAS_LIMIT_NOT_ENOUGH_TO_DEPLOY: u64 = 1_200_000; -const GAS_PRICE: u64 = 2; +const GAS_PRICE: Lux = 2; const POINT_LIMIT: u64 = 0x10000000; -const SENDER_INDEX: u8 = 0; +const DEPOSIT: u64 = 0; const ALICE_CONTRACT_ID: ContractId = { let mut bytes = [0u8; 32]; @@ -81,7 +77,7 @@ fn initial_state>(dir: P, deploy_bob: bool) -> Result { bob_bytecode, ContractData::builder() .owner(OWNER) - .constructor_arg(&BOB_INIT_VALUE) + .init_arg(&BOB_INIT_VALUE) .contract_id(gen_contract_id( &bob_bytecode, 0u64, @@ -107,27 +103,27 @@ fn bytecode_hash(bytecode: impl AsRef<[u8]>) -> ContractId { ContractId::from_bytes(hash.into()) } -fn make_and_execute_transaction_deploy( +async fn make_and_execute_transaction_deploy( rusk: &Rusk, - wallet: &wallet::Wallet, + wallet: &Wallet, bytecode: impl AsRef<[u8]>, gas_limit: u64, init_value: u8, should_fail: bool, should_discard: bool, ) { - let mut rng = StdRng::seed_from_u64(0xcafe); - + let sender_addr = wallet.default_address(); let constructor_args = Some(vec![init_value]); let hash = bytecode_hash(bytecode.as_ref()).to_bytes(); let tx = wallet .phoenix_execute( - &mut rng, - SENDER_INDEX, - gas_limit, - GAS_PRICE, - 0u64, + sender_addr, + DEPOSIT.into(), + Gas { + limit: gas_limit, + price: GAS_PRICE, + }, TransactionData::Deploy(ContractDeploy { bytecode: ContractBytecode { hash, @@ -138,6 +134,7 @@ fn make_and_execute_transaction_deploy( nonce: 0, }), ) + .await .expect("Making transaction should succeed"); let expected = ExecuteResult { @@ -170,7 +167,7 @@ fn make_and_execute_transaction_deploy( struct Fixture { pub rusk: Rusk, - pub wallet: Wallet, + pub wallet: Wallet, pub bob_bytecode: Vec, pub contract_id: ContractId, pub path: PathBuf, @@ -183,15 +180,8 @@ impl Fixture { let rusk = initial_state(&tmp, deploy_bob) .expect("Initializing should succeed"); - let cache = Arc::new(RwLock::new(HashMap::new())); - - let wallet = wallet::Wallet::new( - TestStore, - TestStateClient { - rusk: rusk.clone(), - cache, - }, - ); + let wallet = + test_wallet().expect("test-wallet should be possible to create"); let original_root = rusk.state_root(); @@ -255,9 +245,10 @@ impl Fixture { ); } - pub fn wallet_balance(&self) -> u64 { + pub async fn wallet_balance(&self) -> u64 { self.wallet - .get_balance(0) + .get_balance(&self.wallet.addresses()[0]) + .await .expect("Getting wallet's balance should succeed") .value } @@ -273,7 +264,7 @@ pub async fn contract_deploy() { let f = Fixture::build(false); f.assert_bob_contract_is_not_deployed(); - let before_balance = f.wallet_balance(); + let before_balance = f.wallet_balance().await; make_and_execute_transaction_deploy( &f.rusk, &f.wallet, @@ -282,8 +273,9 @@ pub async fn contract_deploy() { BOB_INIT_VALUE, false, false, - ); - let after_balance = f.wallet_balance(); + ) + .await; + let after_balance = f.wallet_balance().await; f.assert_bob_contract_is_deployed(); let funds_spent = before_balance - after_balance; assert!(funds_spent < GAS_LIMIT * GAS_PRICE); @@ -298,7 +290,7 @@ pub async fn contract_already_deployed() { let f = Fixture::build(true); f.assert_bob_contract_is_deployed(); - let before_balance = f.wallet_balance(); + let before_balance = f.wallet_balance().await; make_and_execute_transaction_deploy( &f.rusk, &f.wallet, @@ -307,8 +299,9 @@ pub async fn contract_already_deployed() { BOB_INIT_VALUE, true, false, - ); - let after_balance = f.wallet_balance(); + ) + .await; + let after_balance = f.wallet_balance().await; let funds_spent = before_balance - after_balance; assert_eq!(funds_spent, GAS_LIMIT * GAS_PRICE); } @@ -325,7 +318,7 @@ pub async fn contract_deploy_corrupted_bytecode() { f.bob_bytecode = f.bob_bytecode[4..].to_vec(); f.assert_bob_contract_is_not_deployed(); - let before_balance = f.wallet_balance(); + let before_balance = f.wallet_balance().await; make_and_execute_transaction_deploy( &f.rusk, &f.wallet, @@ -334,8 +327,9 @@ pub async fn contract_deploy_corrupted_bytecode() { BOB_INIT_VALUE, true, false, - ); - let after_balance = f.wallet_balance(); + ) + .await; + let after_balance = f.wallet_balance().await; let funds_spent = before_balance - after_balance; assert_eq!(funds_spent, GAS_LIMIT * GAS_PRICE); } @@ -351,7 +345,7 @@ pub async fn contract_deploy_charge() { "../../../target/dusk/wasm32-unknown-unknown/release/license_contract.wasm" ); - let before_balance = f.wallet_balance(); + let before_balance = f.wallet_balance().await; make_and_execute_transaction_deploy( &f.rusk, &f.wallet, @@ -360,8 +354,9 @@ pub async fn contract_deploy_charge() { BOB_INIT_VALUE, false, false, - ); - let after_bob_balance = f.wallet_balance(); + ) + .await; + let after_bob_balance = f.wallet_balance().await; make_and_execute_transaction_deploy( &f.rusk, &f.wallet, @@ -370,8 +365,9 @@ pub async fn contract_deploy_charge() { 0, false, false, - ); - let after_license_balance = f.wallet_balance(); + ) + .await; + let after_license_balance = f.wallet_balance().await; let bob_deployment_cost = before_balance - after_bob_balance; let license_deployment_cost = after_bob_balance - after_license_balance; assert!(license_deployment_cost > bob_deployment_cost); @@ -387,7 +383,7 @@ pub async fn contract_deploy_not_enough_to_spend() { let f = Fixture::build(false); f.assert_bob_contract_is_not_deployed(); - let before_balance = f.wallet_balance(); + let before_balance = f.wallet_balance().await; make_and_execute_transaction_deploy( &f.rusk, &f.wallet, @@ -396,8 +392,9 @@ pub async fn contract_deploy_not_enough_to_spend() { BOB_INIT_VALUE, false, true, - ); - let after_balance = f.wallet_balance(); + ) + .await; + let after_balance = f.wallet_balance().await; f.assert_bob_contract_is_not_deployed(); let funds_spent = before_balance - after_balance; assert_eq!(funds_spent, 0); @@ -413,7 +410,7 @@ pub async fn contract_deploy_not_enough_to_spend_and_deploy() { let f = Fixture::build(false); f.assert_bob_contract_is_not_deployed(); - let before_balance = f.wallet_balance(); + let before_balance = f.wallet_balance().await; make_and_execute_transaction_deploy( &f.rusk, &f.wallet, @@ -422,8 +419,9 @@ pub async fn contract_deploy_not_enough_to_spend_and_deploy() { BOB_INIT_VALUE, true, false, - ); - let after_balance = f.wallet_balance(); + ) + .await; + let after_balance = f.wallet_balance().await; f.assert_bob_contract_is_not_deployed(); let funds_spent = before_balance - after_balance; assert_eq!( @@ -441,7 +439,7 @@ pub async fn contract_deploy_not_enough_to_deploy() { let f = Fixture::build(false); f.assert_bob_contract_is_not_deployed(); - let before_balance = f.wallet_balance(); + let before_balance = f.wallet_balance().await; make_and_execute_transaction_deploy( &f.rusk, &f.wallet, @@ -450,8 +448,9 @@ pub async fn contract_deploy_not_enough_to_deploy() { BOB_INIT_VALUE, false, true, - ); - let after_balance = f.wallet_balance(); + ) + .await; + let after_balance = f.wallet_balance().await; f.assert_bob_contract_is_not_deployed(); let funds_spent = before_balance - after_balance; assert_eq!(funds_spent, 0); diff --git a/rusk/tests/services/gas_behavior.rs b/rusk/tests/services/gas_behavior.rs index bbe50d8c02..695ff57b0b 100644 --- a/rusk/tests/services/gas_behavior.rs +++ b/rusk/tests/services/gas_behavior.rs @@ -4,24 +4,20 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use std::collections::HashMap; use std::path::Path; -use std::sync::{Arc, RwLock}; use execution_core::transfer::{ data::{ContractCall, TransactionData}, TRANSFER_CONTRACT, }; -use rand::prelude::*; -use rand::rngs::StdRng; use rusk::{Result, Rusk}; +use rusk_wallet::{currency::Lux, gas::Gas}; use tempfile::tempdir; -use test_wallet::{self as wallet}; use tracing::info; use crate::common::logger; use crate::common::state::{generator_procedure, new_state}; -use crate::common::wallet::{TestStateClient, TestStore}; +use crate::common::wallet::{test_wallet, WalletFile}; const BLOCK_HEIGHT: u64 = 1; const BLOCK_GAS_LIMIT: u64 = 1_000_000_000_000; @@ -29,7 +25,7 @@ const INITIAL_BALANCE: u64 = 10_000_000_000; const GAS_LIMIT_0: u64 = 100_000_000; const GAS_LIMIT_1: u64 = 300_000_000; -const GAS_PRICE: u64 = 1; +const GAS_PRICE: Lux = 1; const DEPOSIT: u64 = 0; // Creates the Rusk initial state for the tests below @@ -43,17 +39,22 @@ fn initial_state>(dir: P) -> Result { const SENDER_INDEX_0: u8 = 0; const SENDER_INDEX_1: u8 = 1; -fn make_transactions( +async fn make_transactions( rusk: &Rusk, - wallet: &wallet::Wallet, + wallet: &rusk_wallet::Wallet, ) { + let sender_addr_0 = &wallet.addresses()[SENDER_INDEX_0 as usize]; + let sender_addr_1 = &wallet.addresses()[SENDER_INDEX_1 as usize]; + let initial_balance_0 = wallet - .get_balance(SENDER_INDEX_0) + .get_balance(sender_addr_0) + .await .expect("Getting initial balance should succeed") .value; let initial_balance_1 = wallet - .get_balance(SENDER_INDEX_1) + .get_balance(sender_addr_1) + .await .expect("Getting initial balance should succeed") .value; @@ -66,8 +67,6 @@ fn make_transactions( "The sender should have the given initial balance" ); - let mut rng = StdRng::seed_from_u64(0xdead); - // The first transaction will be a `wallet.execute` to the transfer // contract, querying for the root of the tree. This will be given too // little gas to execute correctly and error, consuming all gas provided. @@ -78,13 +77,15 @@ fn make_transactions( }; let tx_0 = wallet .phoenix_execute( - &mut rng, - SENDER_INDEX_0, - GAS_LIMIT_0, - GAS_PRICE, - DEPOSIT, + sender_addr_0, + DEPOSIT.into(), + Gas { + limit: GAS_LIMIT_0, + price: GAS_PRICE, + }, TransactionData::Call(contract_call.clone()), ) + .await .expect("Making the transaction should succeed"); // The second transaction will also be a `wallet.execute` to the transfer @@ -92,13 +93,15 @@ fn make_transactions( // gas cost. let tx_1 = wallet .phoenix_execute( - &mut rng, - SENDER_INDEX_1, - GAS_LIMIT_1, - GAS_PRICE, - DEPOSIT, - contract_call, + sender_addr_1, + DEPOSIT.into(), + Gas { + limit: GAS_LIMIT_1, + price: GAS_PRICE, + }, + TransactionData::Call(contract_call), ) + .await .expect("Making the transaction should succeed"); let spent_transactions = generator_procedure( @@ -140,22 +143,14 @@ pub async fn erroring_tx_charged_full() -> Result<()> { let tmp = tempdir().expect("Should be able to create temporary directory"); let rusk = initial_state(&tmp)?; - let cache = Arc::new(RwLock::new(HashMap::new())); - // Create a wallet - let wallet = wallet::Wallet::new( - TestStore, - TestStateClient { - rusk: rusk.clone(), - cache, - }, - ); + let wallet = test_wallet()?; let original_root = rusk.state_root(); info!("Original Root: {:?}", hex::encode(original_root)); - make_transactions(&rusk, &wallet); + make_transactions(&rusk, &wallet).await; // Check the state's root is changed from the original one let new_root = rusk.state_root(); diff --git a/rusk/tests/services/multi_transfer.rs b/rusk/tests/services/multi_transfer.rs index f3cf71cc56..60256b7cd2 100644 --- a/rusk/tests/services/multi_transfer.rs +++ b/rusk/tests/services/multi_transfer.rs @@ -4,20 +4,16 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use std::collections::HashMap; use std::path::Path; -use std::sync::{Arc, RwLock}; -use rand::prelude::*; -use rand::rngs::StdRng; use rusk::{Result, Rusk}; +use rusk_wallet::{gas::Gas, Wallet}; use tempfile::tempdir; -use test_wallet::{self as wallet}; use tracing::info; use crate::common::logger; use crate::common::state::{generator_procedure, new_state, ExecuteResult}; -use crate::common::wallet::{TestStateClient, TestStore}; +use crate::common::wallet::{test_wallet, WalletFile}; const BLOCK_HEIGHT: u64 = 1; // This is purposefully chosen to be low to trigger the discarding of a @@ -37,28 +33,29 @@ fn initial_state>(dir: P) -> Result { /// Executes three different transactions in the same block, expecting only two /// to be included due to exceeding the block gas limit -fn wallet_transfer( +async fn wallet_transfer( rusk: &Rusk, - wallet: &wallet::Wallet, + wallet: &Wallet, amount: u64, ) { - // Generate a receiver pk - let receiver = wallet - .phoenix_public_key(3) - .expect("Failed to get public key"); - - let mut rng = StdRng::seed_from_u64(0xdead); + let sender_addr_0 = &wallet.addresses()[0]; + let sender_addr_1 = &wallet.addresses()[1]; + let sender_addr_2 = &wallet.addresses()[2]; + let receiver_addr = &wallet.addresses()[3]; let initial_balance_0 = wallet - .get_balance(0) + .get_balance(sender_addr_0) + .await .expect("Failed to get the balance") .value; let initial_balance_1 = wallet - .get_balance(1) + .get_balance(sender_addr_1) + .await .expect("Failed to get the balance") .value; let initial_balance_2 = wallet - .get_balance(2) + .get_balance(sender_addr_2) + .await .expect("Failed to get the balance") .value; @@ -79,7 +76,8 @@ fn wallet_transfer( // Check the receiver initial balance is zero assert_eq!( wallet - .get_balance(3) + .get_balance(receiver_addr) + .await .expect("Failed to get the balance") .value, 0, @@ -88,10 +86,19 @@ fn wallet_transfer( let mut txs = Vec::with_capacity(3); - for i in 0..3 { + for sender_addr in [sender_addr_0, sender_addr_1, sender_addr_2] { let tx = wallet - .phoenix_transfer(&mut rng, i, &receiver, amount, GAS_LIMIT, 1) - .expect("Failed to transfer"); + .phoenix_transfer( + sender_addr, + receiver_addr, + amount.into(), + Gas { + limit: GAS_LIMIT, + price: 1, + }, + ) + .await + .expect("Failed to create transaction"); txs.push(tx); } @@ -113,7 +120,8 @@ fn wallet_transfer( // Check the receiver's balance is changed accordingly assert_eq!( wallet - .get_balance(3) + .get_balance(receiver_addr) + .await .expect("Failed to get the balance") .value, 2 * amount, @@ -121,7 +129,8 @@ fn wallet_transfer( ); let final_balance_0 = wallet - .get_balance(0) + .get_balance(sender_addr_0) + .await .expect("Failed to get the balance") .value; let gas_limit_0 = txs[0].gas_limit(); @@ -129,7 +138,8 @@ fn wallet_transfer( let fee_0 = gas_limit_0 * gas_price_0; let final_balance_1 = wallet - .get_balance(1) + .get_balance(sender_addr_1) + .await .expect("Failed to get the balance") .value; let gas_limit_1 = txs[1].gas_limit(); @@ -167,7 +177,8 @@ fn wallet_transfer( // Check the discarded transaction didn't change the balance assert_eq!( wallet - .get_balance(2) + .get_balance(sender_addr_2) + .await .expect("Failed to get the balance") .value, initial_balance_2, @@ -183,22 +194,14 @@ pub async fn multi_transfer() -> Result<()> { let tmp = tempdir().expect("Should be able to create temporary directory"); let rusk = initial_state(&tmp)?; - let cache = Arc::new(RwLock::new(HashMap::new())); - // Create a wallet - let wallet = wallet::Wallet::new( - TestStore, - TestStateClient { - rusk: rusk.clone(), - cache, - }, - ); + let wallet = test_wallet()?; let original_root = rusk.state_root(); info!("Original Root: {:?}", hex::encode(original_root)); - wallet_transfer(&rusk, &wallet, 1_000); + wallet_transfer(&rusk, &wallet, 1_000).await; // Check the state's root is changed from the original one let new_root = rusk.state_root(); diff --git a/rusk/tests/services/owner_calls.rs b/rusk/tests/services/owner_calls.rs index d3a932bdc5..b0f17d1238 100644 --- a/rusk/tests/services/owner_calls.rs +++ b/rusk/tests/services/owner_calls.rs @@ -10,9 +10,7 @@ use rand::rngs::StdRng; use rand::SeedableRng; use rkyv::validation::validators::DefaultValidator; use rkyv::{Archive, Deserialize, Infallible, Serialize}; -use std::collections::HashMap; use std::path::{Path, PathBuf}; -use std::sync::{Arc, RwLock}; use execution_core::{ signatures::bls::{ @@ -25,13 +23,13 @@ use rusk::gen_id::gen_contract_id; use rusk::{Error, Result, Rusk}; use rusk_abi::{CallReceipt, ContractData, Session}; use rusk_recovery_tools::state; +use rusk_wallet::Wallet; use tempfile::tempdir; -use test_wallet::{self as wallet, Wallet}; use tokio::sync::broadcast; use tracing::info; use crate::common::logger; -use crate::common::wallet::{TestStateClient, TestStore}; +use crate::common::wallet::{test_wallet, WalletFile}; const BLOCK_GAS_LIMIT: u64 = 1_000_000_000_000; const POINT_LIMIT: u64 = 0x10000000; @@ -64,7 +62,7 @@ fn initial_state>( bob_bytecode, ContractData::builder() .owner(owner.as_ref()) - .constructor_arg(&BOB_INIT_VALUE) + .init_arg(&BOB_INIT_VALUE) .contract_id(gen_contract_id(&bob_bytecode, 0u64, owner)), POINT_LIMIT, ) @@ -83,7 +81,7 @@ fn initial_state>( #[allow(dead_code)] struct Fixture { pub rusk: Rusk, - pub wallet: Wallet, + pub wallet: Wallet, pub bob_bytecode: Vec, pub contract_id: ContractId, pub path: PathBuf, @@ -97,15 +95,8 @@ impl Fixture { let rusk = initial_state(&tmp, owner.as_ref()) .expect("Initializing should succeed"); - let cache = Arc::new(RwLock::new(HashMap::new())); - - let wallet = wallet::Wallet::new( - TestStore, - TestStateClient { - rusk: rusk.clone(), - cache, - }, - ); + let wallet = + test_wallet().expect("test_wallet should be possible to generate"); let original_root = rusk.state_root(); diff --git a/rusk/tests/services/stake.rs b/rusk/tests/services/stake.rs index 603a5b5a5a..f0c4e23912 100644 --- a/rusk/tests/services/stake.rs +++ b/rusk/tests/services/stake.rs @@ -5,33 +5,30 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use std::path::Path; -use std::sync::{Arc, RwLock}; use execution_core::stake::MINIMUM_STAKE; use execution_core::{ dusk, - signatures::bls::PublicKey as BlsPublicKey, stake::{StakeAmount, STAKE_CONTRACT}, - transfer::data::ContractCall, }; +use rusk_wallet::{currency::Lux, gas::Gas, Wallet}; -use rand::prelude::*; -use rand::rngs::StdRng; use rusk::{Result, Rusk}; -use std::collections::HashMap; use tempfile::tempdir; -use test_wallet::{self as wallet}; use tracing::info; use crate::common::state::{generator_procedure, new_state}; -use crate::common::wallet::{TestStateClient, TestStore}; +use crate::common::wallet::{test_wallet, WalletFile}; use crate::common::*; const BLOCK_HEIGHT: u64 = 1; const BLOCK_GAS_LIMIT: u64 = 100_000_000_000; const GAS_LIMIT: u64 = 10_000_000_000; -const GAS_PRICE: u64 = 1; -const DEPOSIT: u64 = 0; +const GAS_PRICE: Lux = 1; + +const SENDER_INDEX_0: u8 = 0; +const SENDER_INDEX_1: u8 = 1; +const SENDER_INDEX_2: u8 = 2; // Creates the Rusk initial state for the tests below fn stake_state>(dir: P) -> Result { @@ -52,30 +49,38 @@ fn slash_state>(dir: P) -> Result { /// Stakes an amount Dusk and produces a block with this single transaction, /// checking the stake is set successfully. It then proceeds to withdraw the /// stake and checking it is correctly withdrawn. -fn wallet_stake( - rusk: &Rusk, - wallet: &wallet::Wallet, - value: u64, -) { - let mut rng = StdRng::seed_from_u64(0xdead); +async fn wallet_stake(rusk: &Rusk, wallet: &Wallet, value: u64) { + let sender_addr_0 = &wallet.addresses()[SENDER_INDEX_0 as usize]; wallet - .get_stake(0) + .stake_info(SENDER_INDEX_0) + .await .expect("stakeinfo to be found") + .expect("stake to be Some") .amount .expect("stake amount to be found"); assert!( wallet - .get_stake(2) + .stake_info(SENDER_INDEX_2) + .await .expect("stakeinfo to be found") + .expect("stake to be Some") .amount .is_none(), "stake amount not to be found" ); let tx = wallet - .phoenix_stake(&mut rng, 0, 2, value, GAS_LIMIT, GAS_PRICE) + .phoenix_stake( + sender_addr_0, + value.into(), + Gas { + limit: GAS_LIMIT, + price: GAS_PRICE, + }, + ) + .await .expect("Failed to create a stake transaction"); let executed_txs = generator_procedure( rusk, @@ -94,19 +99,32 @@ fn wallet_stake( panic!("Stake transaction failed due to {e}") } - let stake = wallet.get_stake(2).expect("stake to be found"); + let stake = wallet + .stake_info(SENDER_INDEX_2) + .await + .expect("stake to be found") + .expect("stake to be Some"); let stake_value = stake.amount.expect("stake should have an amount").value; assert_eq!(stake_value, value); wallet - .get_stake(0) + .stake_info(SENDER_INDEX_0) + .await .expect("stakeinfo to be found") + .expect("stake to be Some") .amount .expect("stake amount to be found"); let tx = wallet - .phoenix_unstake(&mut rng, 0, 0, GAS_LIMIT, GAS_PRICE) + .phoenix_unstake( + sender_addr_0, + Gas { + limit: GAS_LIMIT, + price: GAS_PRICE, + }, + ) + .await .expect("Failed to unstake"); let spent_txs = generator_procedure( rusk, @@ -120,11 +138,23 @@ fn wallet_stake( let spent_tx = spent_txs.first().expect("Unstake tx to be included"); assert_eq!(spent_tx.err, None, "unstake to be successfull"); - let stake = wallet.get_stake(0).expect("stake should still be state"); + let stake = wallet + .stake_info(SENDER_INDEX_0) + .await + .expect("stake should still be state") + .expect("stake to be Some"); assert_eq!(stake.amount, None); let tx = wallet - .phoenix_stake_withdraw(&mut rng, 0, 1, GAS_LIMIT, GAS_PRICE) + .phoenix_stake_withdraw( + sender_addr_0, + 1, + Gas { + limit: GAS_LIMIT, + price: GAS_PRICE, + }, + ) + .await .expect("failed to withdraw reward"); generator_procedure( rusk, @@ -136,7 +166,11 @@ fn wallet_stake( ) .expect("generator procedure to succeed"); - let stake = wallet.get_stake(1).expect("stake should still be state"); + let stake = wallet + .stake_info(SENDER_INDEX_1) + .await + .expect("stake should still be state") + .expect("Stake to be some"); assert_eq!(stake.reward, 0); } @@ -148,23 +182,15 @@ pub async fn stake() -> Result<()> { let tmp = tempdir().expect("Should be able to create temporary directory"); let rusk = stake_state(&tmp)?; - let cache = Arc::new(RwLock::new(HashMap::new())); - // Create a wallet - let wallet = wallet::Wallet::new( - TestStore, - TestStateClient { - rusk: rusk.clone(), - cache, - }, - ); + let wallet = test_wallet()?; let original_root = rusk.state_root(); info!("Original Root: {:?}", hex::encode(original_root)); // Perform some staking actions. - wallet_stake(&rusk, &wallet, MINIMUM_STAKE); + wallet_stake(&rusk, &wallet, MINIMUM_STAKE).await; // Check the state's root is changed from the original one let new_root = rusk.state_root(); @@ -185,34 +211,19 @@ pub async fn stake() -> Result<()> { /// Attempt to submit a management transaction intending it to fail. Verify that /// the reward amount remains unchanged and confirm that the transaction indeed /// fails -fn wallet_reward( - rusk: &Rusk, - wallet: &wallet::Wallet, -) { - let mut rng = StdRng::seed_from_u64(0xdead); - - let stake_sk = wallet.account_secret_key(2).unwrap(); - let stake_pk = BlsPublicKey::from(&stake_sk); - let reward_calldata = (stake_pk, 6u32); +async fn wallet_reward(rusk: &Rusk, wallet: &Wallet) { + let sender_addr_0 = &wallet.addresses()[SENDER_INDEX_0 as usize]; - let stake = wallet.get_stake(2).expect("stake to be found"); - assert_eq!(stake.reward, 0, "stake reward must be empty"); - - let contract_call = ContractCall::new( - STAKE_CONTRACT.to_bytes(), - "reward", - &reward_calldata, - ) - .expect("calldata should serialize"); let tx = wallet - .phoenix_execute( - &mut rng, - 0, - GAS_LIMIT, - GAS_PRICE, - DEPOSIT, - contract_call, + .phoenix_stake_withdraw( + sender_addr_0, + SENDER_INDEX_2, + Gas { + limit: GAS_LIMIT, + price: GAS_PRICE, + }, ) + .await .expect("Failed to create a reward transaction"); let executed_txs = generator_procedure( rusk, @@ -229,7 +240,11 @@ fn wallet_reward( .err .as_ref() .expect("reward transaction to fail"); - let stake = wallet.get_stake(2).expect("stake to be found"); + let stake = wallet + .stake_info(SENDER_INDEX_2) + .await + .expect("stake to be found") + .expect("stake to be some"); assert_eq!(stake.reward, 0, "stake reward must be empty"); } @@ -241,23 +256,15 @@ pub async fn reward() -> Result<()> { let tmp = tempdir().expect("Should be able to create temporary directory"); let rusk = stake_state(&tmp)?; - let cache = Arc::new(RwLock::new(HashMap::new())); - // Create a wallet - let wallet = wallet::Wallet::new( - TestStore, - TestStateClient { - rusk: rusk.clone(), - cache, - }, - ); + let wallet = test_wallet()?; let original_root = rusk.state_root(); info!("Original Root: {:?}", hex::encode(original_root)); // Perform some staking actions. - wallet_reward(&rusk, &wallet); + wallet_reward(&rusk, &wallet).await; // Check the state's root is changed from the original one let new_root = rusk.state_root(); @@ -278,16 +285,8 @@ pub async fn slash() -> Result<()> { let tmp = tempdir().expect("Should be able to create temporary directory"); let rusk = slash_state(&tmp)?; - let cache = Arc::new(RwLock::new(HashMap::new())); - // Create a wallet - let wallet = wallet::Wallet::new( - TestStore, - TestStateClient { - rusk: rusk.clone(), - cache, - }, - ); + let wallet = test_wallet()?; let original_root = rusk.state_root(); @@ -296,8 +295,8 @@ pub async fn slash() -> Result<()> { let contract_balance = rusk .contract_balance(STAKE_CONTRACT) .expect("balance to exists"); - let to_slash = wallet.account_public_key(0).unwrap(); - let stake = wallet.get_stake(0).unwrap(); + let to_slash = wallet.bls_public_key(SENDER_INDEX_0); + let stake = wallet.stake_info(SENDER_INDEX_0).await.unwrap().unwrap(); let initial_stake_value = dusk(20.0); assert_eq!(stake.reward, dusk(3.0)); assert_eq!( @@ -344,7 +343,7 @@ pub async fn slash() -> Result<()> { let prev_stake = prev.amount.unwrap().value; let slashed_amount = prev_stake / 10; - let after_slash = wallet.get_stake(0).unwrap(); + let after_slash = wallet.stake_info(SENDER_INDEX_0).await.unwrap().unwrap(); assert_eq!(after_slash.reward, dusk(3.0)); assert_eq!( after_slash.amount, @@ -385,7 +384,7 @@ pub async fn slash() -> Result<()> { // 20% slash let slashed_amount = prev_stake / 10 * 2; - let after_slash = wallet.get_stake(0).unwrap(); + let after_slash = wallet.stake_info(SENDER_INDEX_0).await.unwrap().unwrap(); assert_eq!(after_slash.reward, dusk(3.0)); assert_eq!( after_slash.amount, @@ -434,7 +433,7 @@ pub async fn slash() -> Result<()> { let prev_locked = prev.amount.unwrap().locked; // 30% slash let slashed_amount = prev_stake / 10 * 3; - let after_slash = wallet.get_stake(0).unwrap(); + let after_slash = wallet.stake_info(SENDER_INDEX_0).await.unwrap().unwrap(); assert_eq!(after_slash.reward, dusk(3.0)); assert_eq!( @@ -453,7 +452,7 @@ pub async fn slash() -> Result<()> { &[], 9001, BLOCK_GAS_LIMIT, - vec![wallet.account_public_key(1).unwrap()], + vec![wallet.bls_public_key(SENDER_INDEX_1)], None, ) .expect_err("Slashing a public key that never staked must fail"); diff --git a/rusk/tests/services/transfer.rs b/rusk/tests/services/transfer.rs index 0b6ec460d0..ab431a233d 100644 --- a/rusk/tests/services/transfer.rs +++ b/rusk/tests/services/transfer.rs @@ -4,21 +4,17 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use std::collections::HashMap; use std::path::Path; -use std::sync::{Arc, RwLock}; use node_data::ledger::SpentTransaction; -use rand::prelude::*; -use rand::rngs::StdRng; use rusk::{Result, Rusk}; +use rusk_wallet::{gas::Gas, Wallet}; use tempfile::tempdir; -use test_wallet::{self as wallet}; use tracing::info; use crate::common::logger; use crate::common::state::{generator_procedure, new_state}; -use crate::common::wallet::{TestStateClient, TestStore}; +use crate::common::wallet::{test_wallet, WalletFile}; const BLOCK_GAS_LIMIT: u64 = 100_000_000_000; const INITIAL_BALANCE: u64 = 10_000_000_000; @@ -35,22 +31,19 @@ fn initial_state>(dir: P) -> Result { /// Transacts between two accounts on the in the same wallet and produces a /// block with a single transaction, checking balances are transferred /// successfully. -fn wallet_transfer( +async fn wallet_transfer( rusk: &Rusk, - wallet: &wallet::Wallet, + wallet: &Wallet, amount: u64, block_height: u64, ) { - // Generate a receiver pk - let receiver_pk = wallet - .phoenix_public_key(1) - .expect("Failed to get public key"); - - let mut rng = StdRng::seed_from_u64(0xdead); + let sender_addr_0 = &wallet.addresses()[0]; + let sender_addr_1 = &wallet.addresses()[1]; // Store the sender initial balance let sender_initial_balance = wallet - .get_balance(0) + .get_balance(sender_addr_0) + .await .expect("Failed to get the balance") .value; @@ -64,7 +57,8 @@ fn wallet_transfer( // Check the receiver initial balance is zero assert_eq!( wallet - .get_balance(1) + .get_balance(sender_addr_1) + .await .expect("Failed to get the balance") .value, 0, @@ -72,8 +66,18 @@ fn wallet_transfer( ); // Execute a transfer + let receiver_addr = sender_addr_1; let tx = wallet - .phoenix_transfer(&mut rng, 0, &receiver_pk, amount, 1_000_000_000, 2) + .phoenix_transfer( + sender_addr_0, + receiver_addr, + amount.into(), + Gas { + limit: 1_000_000_000, + price: 2, + }, + ) + .await .expect("Failed to transfer"); info!("Tx: {}", hex::encode(tx.to_var_bytes())); @@ -107,7 +111,8 @@ fn wallet_transfer( // Check the receiver's balance is changed accordingly assert_eq!( wallet - .get_balance(1) + .get_balance(&receiver_addr) + .await .expect("Failed to get the balance") .value, amount, @@ -116,7 +121,8 @@ fn wallet_transfer( // Check the sender's balance is changed accordingly let sender_final_balance = wallet - .get_balance(0) + .get_balance(&sender_addr_0) + .await .expect("Failed to get the balance") .value; let fee = gas_spent * tx.inner.inner.gas_price(); @@ -136,22 +142,14 @@ pub async fn wallet() -> Result<()> { let tmp = tempdir().expect("Should be able to create temporary directory"); let rusk = initial_state(&tmp)?; - let cache = Arc::new(RwLock::new(HashMap::new())); - // Create a wallet - let wallet = wallet::Wallet::new( - TestStore, - TestStateClient { - rusk: rusk.clone(), - cache: cache.clone(), - }, - ); + let wallet = test_wallet()?; let original_root = rusk.state_root(); info!("Original Root: {:?}", hex::encode(original_root)); - wallet_transfer(&rusk, &wallet, 1_000, 2); + wallet_transfer(&rusk, &wallet, 1_000, 2).await; // Check the state's root is changed from the original one let new_root = rusk.state_root(); @@ -164,13 +162,12 @@ pub async fn wallet() -> Result<()> { // Revert the state rusk.revert_to_base_root() .expect("Reverting should succeed"); - cache.write().unwrap().clear(); // Check the state's root is back to the original one info!("Root after reset: {:?}", hex::encode(rusk.state_root())); assert_eq!(original_root, rusk.state_root(), "Root be the same again"); - wallet_transfer(&rusk, &wallet, 1_000, 2); + wallet_transfer(&rusk, &wallet, 1_000, 2).await; // Check the state's root is back to the original one info!( diff --git a/rusk/tests/services/unspendable.rs b/rusk/tests/services/unspendable.rs index 85dd513f2e..f0439866e2 100644 --- a/rusk/tests/services/unspendable.rs +++ b/rusk/tests/services/unspendable.rs @@ -4,24 +4,20 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use std::collections::HashMap; use std::path::Path; -use std::sync::{Arc, RwLock}; use execution_core::transfer::{ data::{ContractCall, TransactionData}, TRANSFER_CONTRACT, }; -use rand::prelude::*; -use rand::rngs::StdRng; use rusk::{Result, Rusk}; +use rusk_wallet::{currency::Lux, gas::Gas, Wallet}; use tempfile::tempdir; -use test_wallet::{self as wallet}; use tracing::info; use crate::common::logger; use crate::common::state::{generator_procedure, new_state, ExecuteResult}; -use crate::common::wallet::{TestStateClient, TestStore}; +use crate::common::wallet::{test_wallet, WalletFile}; const BLOCK_HEIGHT: u64 = 1; const BLOCK_GAS_LIMIT: u64 = 1_000_000_000_000; @@ -30,7 +26,7 @@ const INITIAL_BALANCE: u64 = 10_000_000_000; const GAS_LIMIT_0: u64 = 20_000_000; // Enough to spend, but OOG during ICC const GAS_LIMIT_1: u64 = 1_000; // Not enough to spend const GAS_LIMIT_2: u64 = 300_000_000; // All ok -const GAS_PRICE: u64 = 1; +const GAS_PRICE: Lux = 1; const DEPOSIT: u64 = 0; // Creates the Rusk initial state for the tests below @@ -45,22 +41,26 @@ const SENDER_INDEX_0: u8 = 0; const SENDER_INDEX_1: u8 = 1; const SENDER_INDEX_2: u8 = 2; -fn make_transactions( - rusk: &Rusk, - wallet: &wallet::Wallet, -) { +async fn make_transactions(rusk: &Rusk, wallet: &Wallet) { + let sender_addr_0 = &wallet.addresses()[SENDER_INDEX_0 as usize]; + let sender_addr_1 = &wallet.addresses()[SENDER_INDEX_1 as usize]; + let sender_addr_2 = &wallet.addresses()[SENDER_INDEX_2 as usize]; + let initial_balance_0 = wallet - .get_balance(SENDER_INDEX_0) + .get_balance(sender_addr_0) + .await .expect("Getting initial balance should succeed") .value; let initial_balance_1 = wallet - .get_balance(SENDER_INDEX_1) + .get_balance(sender_addr_1) + .await .expect("Getting initial balance should succeed") .value; let initial_balance_2 = wallet - .get_balance(SENDER_INDEX_2) + .get_balance(sender_addr_2) + .await .expect("Getting initial balance should succeed") .value; @@ -78,8 +78,6 @@ fn make_transactions( "The sender should have the given initial balance" ); - let mut rng = StdRng::seed_from_u64(0xdead); - // The first transaction will be a `wallet.execute` to the transfer // contract, querying for the root of the tree. This will be given too // little gas to execute correctly and error, consuming all gas provided. @@ -90,13 +88,15 @@ fn make_transactions( }; let tx_0 = wallet .phoenix_execute( - &mut rng, - SENDER_INDEX_0, - GAS_LIMIT_0, - GAS_PRICE, - DEPOSIT, + sender_addr_0, + DEPOSIT.into(), + Gas { + limit: GAS_LIMIT_0, + price: GAS_PRICE, + }, TransactionData::Call(contract_call.clone()), ) + .await .expect("Making the transaction should succeed"); // The second transaction will also be a `wallet.execute` to the transfer @@ -104,13 +104,15 @@ fn make_transactions( // discarded let tx_1 = wallet .phoenix_execute( - &mut rng, - SENDER_INDEX_1, - GAS_LIMIT_1, - GAS_PRICE, - DEPOSIT, + sender_addr_1, + DEPOSIT.into(), + Gas { + limit: GAS_LIMIT_1, + price: GAS_PRICE, + }, TransactionData::Call(contract_call.clone()), ) + .await .expect("Making the transaction should succeed"); // The third transaction transaction will also be a `wallet.execute` to the @@ -118,13 +120,15 @@ fn make_transactions( // tested for gas cost. let tx_2 = wallet .phoenix_execute( - &mut rng, - SENDER_INDEX_2, - GAS_LIMIT_2, - GAS_PRICE, - DEPOSIT, - contract_call, + sender_addr_2, + DEPOSIT.into(), + Gas { + limit: GAS_LIMIT_2, + price: GAS_PRICE, + }, + TransactionData::Call(contract_call), ) + .await .expect("Making the transaction should succeed"); let expected = ExecuteResult { @@ -170,22 +174,14 @@ pub async fn unspendable() -> Result<()> { let tmp = tempdir().expect("Should be able to create temporary directory"); let rusk = initial_state(&tmp)?; - let cache = Arc::new(RwLock::new(HashMap::new())); - // Create a wallet - let wallet = wallet::Wallet::new( - TestStore, - TestStateClient { - rusk: rusk.clone(), - cache, - }, - ); + let wallet = test_wallet()?; let original_root = rusk.state_root(); info!("Original Root: {:?}", hex::encode(original_root)); - make_transactions(&rusk, &wallet); + make_transactions(&rusk, &wallet).await; // Check the state's root is changed from the original one let new_root = rusk.state_root();