From 36df5080e890c177058c58698c39f0e539402c92 Mon Sep 17 00:00:00 2001 From: moana Date: Mon, 26 Aug 2024 15:01:48 +0200 Subject: [PATCH] test-wallet: Integrate `execution-core::Error` --- test-wallet/Cargo.toml | 1 + test-wallet/src/imp.rs | 162 ++++++++++++++++------------------------- test-wallet/src/lib.rs | 20 +---- 3 files changed, 65 insertions(+), 118 deletions(-) diff --git a/test-wallet/Cargo.toml b/test-wallet/Cargo.toml index f9b8b25eaf..40e9870d70 100644 --- a/test-wallet/Cargo.toml +++ b/test-wallet/Cargo.toml @@ -18,6 +18,7 @@ zeroize = { version = "1", default-features = false, features = ["derive"] } # rusk dependencies execution-core = { version = "0.1.0", path = "../execution-core" } wallet-core = { version = "0.1", path = "../wallet-core/" } +rusk-prover = { version = "0.3", path = "../rusk-prover/" } [dev-dependencies] rand = "^0.8" diff --git a/test-wallet/src/imp.rs b/test-wallet/src/imp.rs index 2ed5c01e64..6297bce503 100644 --- a/test-wallet/src/imp.rs +++ b/test-wallet/src/imp.rs @@ -4,7 +4,7 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use crate::{ProverClient, StateClient, Store}; +use crate::{StateClient, Store}; use core::convert::Infallible; @@ -27,20 +27,20 @@ use execution_core::{ contract_exec::ContractExec, moonlight::{AccountData, Transaction as MoonlightTransaction}, phoenix::{ - Error as PhoenixError, Note, PublicKey as PhoenixPublicKey, - SecretKey as PhoenixSecretKey, ViewKey as PhoenixViewKey, - NOTES_TREE_DEPTH, + Note, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey, + ViewKey as PhoenixViewKey, NOTES_TREE_DEPTH, }, Transaction, }, - BlsScalar, + BlsScalar, Error as ExecutionError, }; +use rusk_prover::LocalProver; use wallet_core::{ keys::{derive_bls_sk, derive_phoenix_sk}, phoenix_balance, transaction::{ - phoenix_stake, phoenix_transaction, phoenix_unstake, - phoenix_withdraw_stake_reward, + phoenix as phoenix_transaction, phoenix_stake, phoenix_stake_reward, + phoenix_unstake, }, BalanceInfo, }; @@ -56,13 +56,11 @@ type SerializerError = CompositeSerializerError< /// The error type returned by this crate. #[derive(Debug)] #[allow(clippy::large_enum_variant)] -pub enum Error { +pub enum Error { /// Underlying store error. Store(S::Error), /// Error originating from the state client. State(SC::Error), - /// Error originating from the prover client. - Prover(PC::Error), /// Rkyv serialization. Rkyv, /// Random number generator error. @@ -71,10 +69,8 @@ pub enum Error { Bytes(BytesError), /// Bytes were meant to be utf8 but aren't. Utf8(FromUtf8Error), - /// Originating from the phoenix transaction model. - Phoenix(PhoenixError), - /// Not enough balance to perform phoenix transaction. - NotEnoughBalance, + /// Originating from the execution-core error. + Execution(ExecutionError), /// Note combination for the given value is impossible given the maximum /// amount if inputs in a phoenix transaction. NoteCombinationProblem, @@ -104,7 +100,7 @@ pub enum Error { }, } -impl Error { +impl Error { /// Returns an error from the underlying store error. pub fn from_store_err(se: S::Error) -> Self { Self::Store(se) @@ -113,57 +109,43 @@ impl Error { pub fn from_state_err(se: SC::Error) -> Self { Self::State(se) } - /// Returns an error from the underlying prover client. - pub fn from_prover_err(pe: PC::Error) -> Self { - Self::Prover(pe) - } } -impl From - for Error -{ +impl From for Error { fn from(_: SerializerError) -> Self { Self::Rkyv } } -impl - From> for Error +impl From> + for Error { fn from(_: CheckDeserializeError) -> Self { Self::Rkyv } } -impl From - for Error -{ +impl From for Error { fn from(re: RngError) -> Self { Self::Rng(re) } } -impl From - for Error -{ +impl From for Error { fn from(be: BytesError) -> Self { Self::Bytes(be) } } -impl From - for Error -{ +impl From for Error { fn from(err: FromUtf8Error) -> Self { Self::Utf8(err) } } -impl From - for Error -{ - fn from(pe: PhoenixError) -> Self { - Self::Phoenix(pe) +impl From for Error { + fn from(ee: ExecutionError) -> Self { + Self::Execution(ee) } } @@ -171,20 +153,15 @@ impl From /// /// This is responsible for holding the keys, and performing operations like /// creating transactions. -pub struct Wallet { +pub struct Wallet { store: S, state: SC, - prover: PC, } -impl Wallet { +impl Wallet { /// Create a new wallet given the underlying store and node client. - pub const fn new(store: S, state: SC, prover: PC) -> Self { - Self { - store, - state, - prover, - } + pub const fn new(store: S, state: SC) -> Self { + Self { store, state } } /// Return the inner Store reference @@ -196,24 +173,18 @@ impl Wallet { pub const fn state(&self) -> &SC { &self.state } - - /// Return the inner Prover reference - pub const fn prover(&self) -> &PC { - &self.prover - } } -impl Wallet +impl Wallet where S: Store, SC: StateClient, - PC: ProverClient, { /// Retrieve the secret key with the given index. pub fn phoenix_secret_key( &self, index: u8, - ) -> Result> { + ) -> Result> { self.store .phoenix_secret_key(index) .map_err(Error::from_store_err) @@ -223,7 +194,7 @@ where pub fn phoenix_public_key( &self, index: u8, - ) -> Result> { + ) -> Result> { self.store .phoenix_public_key(index) .map_err(Error::from_store_err) @@ -233,7 +204,7 @@ where pub fn account_secret_key( &self, index: u8, - ) -> Result> { + ) -> Result> { self.store .account_secret_key(index) .map_err(Error::from_store_err) @@ -243,7 +214,7 @@ where pub fn account_public_key( &self, index: u8, - ) -> Result> { + ) -> Result> { self.store .account_public_key(index) .map_err(Error::from_store_err) @@ -254,7 +225,7 @@ where fn unspent_notes_and_nullifiers( &self, sk: &PhoenixSecretKey, - ) -> Result, Error> { + ) -> Result, Error> { let vk = PhoenixViewKey::from(sk); let notes: Vec = self @@ -291,7 +262,7 @@ where &self, sender_sk: &PhoenixSecretKey, transaction_cost: u64, - ) -> Result, Error> { + ) -> Result, Error> { let sender_vk = PhoenixViewKey::from(sender_sk); // decrypt the value of all unspent note @@ -302,13 +273,15 @@ where let mut accumulated_value = 0; for (note, nullifier) in unspent_notes_nullifiers { - let val = note.value(Some(&sender_vk))?; + let val = note + .value(Some(&sender_vk)) + .map_err(|_| ExecutionError::PhoenixOwnership)?; accumulated_value += val; notes_values_nullifiers.push((note, val, nullifier)); } if accumulated_value < transaction_cost { - return Err(Error::NotEnoughBalance); + return Err(ExecutionError::InsufficientBalance.into()); } // pick the four smallest notes that cover the costs @@ -330,7 +303,7 @@ where transaction_cost: u64, ) -> Result< Vec<(Note, Opening<(), NOTES_TREE_DEPTH>, BlsScalar)>, - Error, + Error, > { let notes_and_nullifiers = self.input_notes_nullifiers(sender_sk, transaction_cost)?; @@ -355,8 +328,7 @@ where &self, sender_sk: &PhoenixSecretKey, transaction_cost: u64, - ) -> Result)>, Error> - { + ) -> Result)>, Error> { let notes_and_nullifiers = self.input_notes_nullifiers(sender_sk, transaction_cost)?; @@ -383,7 +355,7 @@ where gas_price: u64, deposit: u64, exec: impl Into, - ) -> Result> { + ) -> Result> { let mut sender_sk = self.phoenix_secret_key(sender_index)?; let receiver_pk = self.phoenix_public_key(sender_index)?; let change_pk = receiver_pk; @@ -398,7 +370,7 @@ where let transfer_value = 0; let obfuscated_transaction = false; - let utx = phoenix_transaction( + let tx = phoenix_transaction::( rng, &sender_sk, &change_pk, @@ -411,12 +383,11 @@ where gas_limit, gas_price, Some(exec), - ); + )?; sender_sk.zeroize(); - PC::compute_proof_and_propagate(&utx) - .map_err(|e| Error::from_prover_err(e)) + Ok(tx) } /// Transfer Dusk in the form of Phoenix notes from one key to another. @@ -429,7 +400,7 @@ where transfer_value: u64, gas_limit: u64, gas_price: u64, - ) -> Result> { + ) -> Result> { let mut sender_sk = self.phoenix_secret_key(sender_index)?; let change_pk = self.phoenix_public_key(sender_index)?; @@ -445,7 +416,7 @@ where let exec: Option = None; - let utx = phoenix_transaction( + let tx = phoenix_transaction::( rng, &sender_sk, &change_pk, @@ -458,12 +429,11 @@ where gas_limit, gas_price, exec, - ); + )?; sender_sk.zeroize(); - PC::compute_proof_and_propagate(&utx) - .map_err(|e| Error::from_prover_err(e)) + Ok(tx) } /// Stakes an amount of Dusk using Phoenix notes. @@ -476,7 +446,7 @@ where stake_value: u64, gas_limit: u64, gas_price: u64, - ) -> Result> { + ) -> Result> { let mut phoenix_sender_sk = self.phoenix_secret_key(sender_index)?; let mut stake_sk = self.account_secret_key(staker_index)?; @@ -495,7 +465,7 @@ where .map_err(Error::from_state_err)? .nonce; - let utx = phoenix_stake( + let tx = phoenix_stake::( rng, &phoenix_sender_sk, &stake_sk, @@ -505,13 +475,12 @@ where gas_price, stake_value, current_nonce, - ); + )?; stake_sk.zeroize(); phoenix_sender_sk.zeroize(); - PC::compute_proof_and_propagate(&utx) - .map_err(|e| Error::from_prover_err(e)) + Ok(tx) } /// Unstakes a key from the stake contract, using Phoenix notes. @@ -522,7 +491,7 @@ where staker_index: u8, gas_limit: u64, gas_price: u64, - ) -> Result> { + ) -> Result> { let mut phoenix_sender_sk = self.phoenix_secret_key(sender_index)?; let mut stake_sk = self.account_secret_key(staker_index)?; @@ -548,7 +517,7 @@ where })? .value; - let utx = phoenix_unstake( + let tx = phoenix_unstake::( rng, &phoenix_sender_sk, &stake_sk, @@ -557,13 +526,12 @@ where staked_amount, gas_limit, gas_price, - ); + )?; stake_sk.zeroize(); phoenix_sender_sk.zeroize(); - PC::compute_proof_and_propagate(&utx) - .map_err(|e| Error::from_prover_err(e)) + Ok(tx) } /// Withdraw the accumulated staking reward for a key, into Phoenix notes. @@ -575,7 +543,7 @@ where staker_index: u8, gas_limit: u64, gas_price: u64, - ) -> Result> { + ) -> Result> { let mut phoenix_sender_sk = self.phoenix_secret_key(sender_index)?; let mut stake_sk = self.account_secret_key(staker_index)?; @@ -594,7 +562,7 @@ where .map_err(Error::from_state_err)? .reward; - let utx = phoenix_withdraw_stake_reward( + let tx = phoenix_stake_reward::( rng, &phoenix_sender_sk, &stake_sk, @@ -603,13 +571,12 @@ where stake_reward, gas_limit, gas_price, - ); + )?; stake_sk.zeroize(); phoenix_sender_sk.zeroize(); - PC::compute_proof_and_propagate(&utx) - .map_err(|e| Error::from_prover_err(e)) + Ok(tx) } /// Transfer Dusk from one account to another using moonlight. @@ -620,7 +587,7 @@ where value: u64, gas_limit: u64, gas_price: u64, - ) -> Result> { + ) -> Result> { let deposit = 0; let exec: Option = None; @@ -646,7 +613,7 @@ where gas_limit: u64, gas_price: u64, exec: Option>, - ) -> Result> { + ) -> Result> { let mut seed = self.store.get_seed().map_err(Error::from_store_err)?; let mut from_sk = derive_bls_sk(&seed, from_index); let from_account = BlsPublicKey::from(&from_sk); @@ -660,7 +627,7 @@ where // the network with transactions that are unspendable. let max_value = value + deposit + gas_limit * gas_price; if max_value > account.balance { - return Err(Error::NotEnoughBalance); + return Err(ExecutionError::InsufficientBalance.into()); } let nonce = account.nonce + 1; @@ -679,7 +646,7 @@ where pub fn get_balance( &self, sk_index: u8, - ) -> Result> { + ) -> Result> { let mut seed = self.store.get_seed().map_err(Error::from_store_err)?; let mut phoenix_sk = derive_phoenix_sk(&seed, sk_index); let phoenix_vk = PhoenixViewKey::from(&phoenix_sk); @@ -698,10 +665,7 @@ where } /// Gets the stake and the expiration of said stake for a key. - pub fn get_stake( - &self, - sk_index: u8, - ) -> Result> { + pub fn get_stake(&self, sk_index: u8) -> Result> { let mut seed = self.store.get_seed().map_err(Error::from_store_err)?; let mut account_sk = derive_bls_sk(&seed, sk_index); @@ -722,7 +686,7 @@ where pub fn get_account( &self, sk_index: u8, - ) -> Result> { + ) -> Result> { let mut seed = self.store.get_seed().map_err(Error::from_store_err)?; let mut account_sk = derive_bls_sk(&seed, sk_index); diff --git a/test-wallet/src/lib.rs b/test-wallet/src/lib.rs index 6a25b4c903..32d4fbe7bd 100644 --- a/test-wallet/src/lib.rs +++ b/test-wallet/src/lib.rs @@ -25,10 +25,8 @@ use execution_core::{ moonlight::AccountData, phoenix::{ Note, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey, - Transaction as PhoenixTransaction, ViewKey as PhoenixViewKey, - NOTES_TREE_DEPTH, + ViewKey as PhoenixViewKey, NOTES_TREE_DEPTH, }, - Transaction, }, BlsScalar, }; @@ -39,22 +37,6 @@ pub use wallet_core::keys::{ pub use imp::*; -/// Types that are client of the prover. -pub trait ProverClient { - /// Error returned by the node client. - type Error; - - /// Requests that a node prove the given unproven [`PhoenixTransaction`] and - /// later propagates it. - /// - /// # Errors - /// This function will error if the proof can not be generated from the - /// unproven transaction. - fn compute_proof_and_propagate( - utx: &PhoenixTransaction, - ) -> Result; -} - /// Stores the cryptographic material necessary to derive cryptographic keys. pub trait Store { /// The error type returned from the store.