Skip to content

Commit

Permalink
rusk: Integrate execution-core::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Aug 26, 2024
1 parent e4ab41d commit 24da333
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 84 deletions.
57 changes: 41 additions & 16 deletions rusk/src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::{fmt, io};

use dusk_bytes::Serializable;
use execution_core::{
signatures::bls::PublicKey as BlsPublicKey,
transfer::phoenix::Error as PhoenixError, BlsScalar, Dusk,
signatures::bls::PublicKey as BlsPublicKey, transfer::phoenix::CoreError,
BlsScalar, Dusk, Error as ExecErr,
};
use rusk_abi::PiecrustError;

Expand Down Expand Up @@ -37,8 +37,10 @@ pub enum Error {
OpeningNoteUndefined(u64),
/// Bytes Serialization Errors
Serialization(dusk_bytes::Error),
/// Originating from transaction-creation
Transaction(ExecErr),
/// Originating from Phoenix.
Phoenix(PhoenixError),
Phoenix(CoreError),
/// Piecrust VM internal Errors
Vm(PiecrustError),
/// IO Errors
Expand All @@ -47,9 +49,6 @@ pub enum Error {
CoinbaseBlockHeight(u64, u64),
/// Bad dusk spent in coinbase (got, expected).
CoinbaseDuskSpent(Dusk, Dusk),
/// Proof creation error
#[cfg(feature = "prover")]
ProofCreation(rusk_prover::ProverError),
/// Failed to produce proper state
#[cfg(feature = "node")]
InconsistentState(dusk_consensus::operations::VerificationOutput),
Expand All @@ -75,10 +74,39 @@ impl From<PiecrustError> for Error {
}
}

#[cfg(feature = "prover")]
impl From<rusk_prover::ProverError> for Error {
fn from(err: rusk_prover::ProverError) -> Self {
Error::ProofCreation(err)
impl From<execution_core::Error> for Error {
fn from(err: ExecErr) -> Self {
match err {
ExecErr::InsufficientBalance => {
Self::Transaction(ExecErr::InsufficientBalance)
}
ExecErr::Replay => Self::Transaction(ExecErr::Replay),
ExecErr::PhoenixOwnership => {
Self::Transaction(ExecErr::PhoenixOwnership)
}
ExecErr::PhoenixCircuit(e) => {
Self::Transaction(ExecErr::PhoenixCircuit(e))
}
ExecErr::PhoenixProver(e) => {
Self::Transaction(ExecErr::PhoenixProver(e))
}
ExecErr::InvalidData => {
Self::Serialization(dusk_bytes::Error::InvalidData)
}
ExecErr::BadLength(found, expected) => {
Self::Serialization(dusk_bytes::Error::BadLength {
found,
expected,
})
}
ExecErr::InvalidChar(ch, index) => {
Self::Serialization(dusk_bytes::Error::InvalidChar {
ch,
index,
})
}
ExecErr::Rkyv(e) => Self::Transaction(ExecErr::Rkyv(e)),
}
}
}

Expand All @@ -88,8 +116,8 @@ impl From<dusk_bytes::Error> for Error {
}
}

impl From<PhoenixError> for Error {
fn from(pe: PhoenixError) -> Self {
impl From<CoreError> for Error {
fn from(pe: CoreError) -> Self {
Self::Phoenix(pe)
}
}
Expand Down Expand Up @@ -123,6 +151,7 @@ impl fmt::Display for Error {
}
Error::Vm(err) => write!(f, "VM Error: {err}"),
Error::Io(err) => write!(f, "IO Error: {err}"),
Error::Transaction(err) => write!(f, "Transaction Error: {err}"),
Error::Phoenix(err) => write!(f, "Phoenix error: {err}"),
Error::Other(err) => write!(f, "Other error: {err}"),
Error::CoinbaseBlockHeight(got, expected) => write!(
Expand All @@ -145,10 +174,6 @@ impl fmt::Display for Error {
Error::InvalidCircuitArguments(inputs_len, outputs_len) => {
write!(f,"Expected: 0 < (inputs: {inputs_len}) < 5, 0 ≤ (outputs: {outputs_len}) < 3")
}
#[cfg(feature = "prover")]
Error::ProofCreation(e) => {
write!(f, "Proof creation error: {e}")
}
#[cfg(feature = "node")]
Error::InconsistentState(verification_output) => {
write!(
Expand Down
5 changes: 4 additions & 1 deletion rusk/src/lib/http/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use anyhow::anyhow;

use execution_core::transfer::phoenix::Prove;
use rusk_prover::LocalProver;

Expand All @@ -21,7 +23,8 @@ impl HandleRequest for LocalProver {
) -> anyhow::Result<ResponseData> {
let topic = request.event.topic.as_str();
let response = match topic {
"prove_tx_circuit" => LocalProver::prove(request.event_data())?,
"prove_execute" => LocalProver::prove(request.event_data())
.map_err(|e| anyhow!(e))?,
_ => anyhow::bail!("Unsupported"),
};
Ok(ResponseData::new(response))
Expand Down
34 changes: 1 addition & 33 deletions rusk/tests/common/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ use execution_core::{
stake::StakeData,
transfer::{
moonlight::AccountData,
phoenix::{
Note, Prove, Transaction as PhoenixTransaction, ViewKey,
NOTES_TREE_DEPTH,
},
Transaction,
phoenix::{Note, ViewKey, NOTES_TREE_DEPTH},
},
BlsScalar,
};
use futures::StreamExt;
use poseidon_merkle::Opening as PoseidonOpening;
use rusk::{Error, Result, Rusk};
use rusk_prover::LocalProver;
use test_wallet::{self as wallet, Store};
use tracing::info;

Expand Down Expand Up @@ -128,33 +123,6 @@ impl wallet::StateClient for TestStateClient {
}
}

#[derive(Default)]
pub struct TestProverClient();

impl Debug for TestProverClient {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Ok(())
}
}

impl wallet::ProverClient for TestProverClient {
type Error = Error;
/// Requests that a node prove the given transaction and later propagates it
fn compute_proof_and_propagate(
utx: &PhoenixTransaction,
) -> Result<Transaction, Self::Error> {
let circuit_bytes = &utx.proof()[..];
let proof_bytes = LocalProver::prove(circuit_bytes)?;
info!("circuit: {}", hex::encode(circuit_bytes));
let mut tx = utx.clone();
tx.replace_proof(proof_bytes);

//Propagate is not required yet

Ok(tx.into())
}
}

#[derive(Default, Debug, Clone)]
pub struct DummyCacheItem {
notes: Vec<(Note, u64)>,
Expand Down
18 changes: 6 additions & 12 deletions rusk/tests/rusk-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub fn rusk_state_finalized() -> Result<()> {
#[allow(dead_code)]
// #[tokio::test(flavor = "multi_thread")]
async fn generate_phoenix_txs() -> Result<(), Box<dyn std::error::Error>> {
use common::wallet::{TestProverClient, TestStateClient, TestStore};
use common::wallet::{TestStateClient, TestStore};
use std::io::Write;

common::logger();
Expand All @@ -189,11 +189,8 @@ async fn generate_phoenix_txs() -> Result<(), Box<dyn std::error::Error>> {
let cache =
Arc::new(std::sync::RwLock::new(std::collections::HashMap::new()));

let wallet = test_wallet::Wallet::new(
TestStore,
TestStateClient { rusk, cache },
TestProverClient::default(),
);
let wallet =
test_wallet::Wallet::new(TestStore, TestStateClient { rusk, cache });

const N_ADDRESSES: usize = 100;

Expand Down Expand Up @@ -240,7 +237,7 @@ async fn generate_phoenix_txs() -> Result<(), Box<dyn std::error::Error>> {
#[allow(dead_code)]
// #[tokio::test(flavor = "multi_thread")]
async fn generate_moonlight_txs() -> Result<(), Box<dyn std::error::Error>> {
use common::wallet::{TestProverClient, TestStateClient, TestStore};
use common::wallet::{TestStateClient, TestStore};
use std::io::Write;

common::logger();
Expand All @@ -254,11 +251,8 @@ async fn generate_moonlight_txs() -> Result<(), Box<dyn std::error::Error>> {
let cache =
Arc::new(std::sync::RwLock::new(std::collections::HashMap::new()));

let wallet = test_wallet::Wallet::new(
TestStore,
TestStateClient { rusk, cache },
TestProverClient::default(),
);
let wallet =
test_wallet::Wallet::new(TestStore, TestStateClient { rusk, cache });

const N_ADDRESSES: usize = 100;

Expand Down
7 changes: 3 additions & 4 deletions rusk/tests/services/contract_deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use tracing::info;

use crate::common::logger;
use crate::common::state::{generator_procedure, ExecuteResult};
use crate::common::wallet::{TestProverClient, TestStateClient, TestStore};
use crate::common::wallet::{TestStateClient, TestStore};

const BLOCK_HEIGHT: u64 = 1;
const BLOCK_GAS_LIMIT: u64 = 1_000_000_000_000;
Expand Down Expand Up @@ -107,7 +107,7 @@ fn bytecode_hash(bytecode: impl AsRef<[u8]>) -> ContractId {

fn make_and_execute_transaction_deploy(
rusk: &Rusk,
wallet: &wallet::Wallet<TestStore, TestStateClient, TestProverClient>,
wallet: &wallet::Wallet<TestStore, TestStateClient>,
bytecode: impl AsRef<[u8]>,
gas_limit: u64,
init_value: u8,
Expand Down Expand Up @@ -168,7 +168,7 @@ fn make_and_execute_transaction_deploy(

struct Fixture {
pub rusk: Rusk,
pub wallet: Wallet<TestStore, TestStateClient, TestProverClient>,
pub wallet: Wallet<TestStore, TestStateClient>,
pub bob_bytecode: Vec<u8>,
pub contract_id: ContractId,
pub path: PathBuf,
Expand All @@ -189,7 +189,6 @@ impl Fixture {
rusk: rusk.clone(),
cache,
},
TestProverClient::default(),
);

let original_root = rusk.state_root();
Expand Down
5 changes: 2 additions & 3 deletions rusk/tests/services/gas_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tracing::info;

use crate::common::logger;
use crate::common::state::{generator_procedure, new_state};
use crate::common::wallet::{TestProverClient, TestStateClient, TestStore};
use crate::common::wallet::{TestStateClient, TestStore};

const BLOCK_HEIGHT: u64 = 1;
const BLOCK_GAS_LIMIT: u64 = 1_000_000_000_000;
Expand All @@ -45,7 +45,7 @@ const SENDER_INDEX_1: u8 = 1;

fn make_transactions(
rusk: &Rusk,
wallet: &wallet::Wallet<TestStore, TestStateClient, TestProverClient>,
wallet: &wallet::Wallet<TestStore, TestStateClient>,
) {
let initial_balance_0 = wallet
.get_balance(SENDER_INDEX_0)
Expand Down Expand Up @@ -149,7 +149,6 @@ pub async fn erroring_tx_charged_full() -> Result<()> {
rusk: rusk.clone(),
cache,
},
TestProverClient::default(),
);

let original_root = rusk.state_root();
Expand Down
5 changes: 2 additions & 3 deletions rusk/tests/services/multi_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tracing::info;

use crate::common::logger;
use crate::common::state::{generator_procedure, new_state, ExecuteResult};
use crate::common::wallet::{TestProverClient, TestStateClient, TestStore};
use crate::common::wallet::{TestStateClient, TestStore};

const BLOCK_HEIGHT: u64 = 1;
// This is purposefully chosen to be low to trigger the discarding of a
Expand All @@ -39,7 +39,7 @@ fn initial_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
/// to be included due to exceeding the block gas limit
fn wallet_transfer(
rusk: &Rusk,
wallet: &wallet::Wallet<TestStore, TestStateClient, TestProverClient>,
wallet: &wallet::Wallet<TestStore, TestStateClient>,
amount: u64,
) {
// Generate a receiver pk
Expand Down Expand Up @@ -192,7 +192,6 @@ pub async fn multi_transfer() -> Result<()> {
rusk: rusk.clone(),
cache,
},
TestProverClient::default(),
);

let original_root = rusk.state_root();
Expand Down
9 changes: 3 additions & 6 deletions rusk/tests/services/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use test_wallet::{self as wallet};
use tracing::info;

use crate::common::state::{generator_procedure, new_state};
use crate::common::wallet::{TestProverClient, TestStateClient, TestStore};
use crate::common::wallet::{TestStateClient, TestStore};
use crate::common::*;

const BLOCK_HEIGHT: u64 = 1;
Expand Down Expand Up @@ -54,7 +54,7 @@ fn slash_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
/// stake and checking it is correctly withdrawn.
fn wallet_stake(
rusk: &Rusk,
wallet: &wallet::Wallet<TestStore, TestStateClient, TestProverClient>,
wallet: &wallet::Wallet<TestStore, TestStateClient>,
value: u64,
) {
let mut rng = StdRng::seed_from_u64(0xdead);
Expand Down Expand Up @@ -157,7 +157,6 @@ pub async fn stake() -> Result<()> {
rusk: rusk.clone(),
cache,
},
TestProverClient::default(),
);

let original_root = rusk.state_root();
Expand Down Expand Up @@ -188,7 +187,7 @@ pub async fn stake() -> Result<()> {
/// fails
fn wallet_reward(
rusk: &Rusk,
wallet: &wallet::Wallet<TestStore, TestStateClient, TestProverClient>,
wallet: &wallet::Wallet<TestStore, TestStateClient>,
) {
let mut rng = StdRng::seed_from_u64(0xdead);

Expand Down Expand Up @@ -251,7 +250,6 @@ pub async fn reward() -> Result<()> {
rusk: rusk.clone(),
cache,
},
TestProverClient::default(),
);

let original_root = rusk.state_root();
Expand Down Expand Up @@ -289,7 +287,6 @@ pub async fn slash() -> Result<()> {
rusk: rusk.clone(),
cache,
},
TestProverClient::default(),
);

let original_root = rusk.state_root();
Expand Down
5 changes: 2 additions & 3 deletions rusk/tests/services/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tracing::info;

use crate::common::logger;
use crate::common::state::{generator_procedure, new_state};
use crate::common::wallet::{TestProverClient, TestStateClient, TestStore};
use crate::common::wallet::{TestStateClient, TestStore};

const BLOCK_GAS_LIMIT: u64 = 100_000_000_000;
const INITIAL_BALANCE: u64 = 10_000_000_000;
Expand All @@ -37,7 +37,7 @@ fn initial_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
/// successfully.
fn wallet_transfer(
rusk: &Rusk,
wallet: &wallet::Wallet<TestStore, TestStateClient, TestProverClient>,
wallet: &wallet::Wallet<TestStore, TestStateClient>,
amount: u64,
block_height: u64,
) {
Expand Down Expand Up @@ -145,7 +145,6 @@ pub async fn wallet() -> Result<()> {
rusk: rusk.clone(),
cache: cache.clone(),
},
TestProverClient::default(),
);

let original_root = rusk.state_root();
Expand Down
Loading

0 comments on commit 24da333

Please sign in to comment.