From cd650459a2349f2515cd5c86ab79362e819e4bd8 Mon Sep 17 00:00:00 2001 From: moana Date: Fri, 23 Aug 2024 13:56:57 +0200 Subject: [PATCH] rusk: Integrate wallet-core --- rusk/Cargo.toml | 2 +- rusk/src/lib/http/prover.rs | 6 +++-- rusk/src/lib/verifier.rs | 8 +++--- rusk/tests/common/wallet.rs | 29 +++++++++++----------- rusk/tests/rusk-state.rs | 10 ++++---- rusk/tests/services/contract_deployment.rs | 14 +++++------ rusk/tests/services/gas_behavior.rs | 18 ++++++++------ rusk/tests/services/multi_transfer.rs | 4 ++- rusk/tests/services/stake.rs | 26 +++++++++++++------ rusk/tests/services/transfer.rs | 4 ++- rusk/tests/services/unspendable.rs | 26 ++++++++++--------- 11 files changed, 84 insertions(+), 63 deletions(-) diff --git a/rusk/Cargo.toml b/rusk/Cargo.toml index 3eba219178..4139d1095e 100644 --- a/rusk/Cargo.toml +++ b/rusk/Cargo.toml @@ -61,7 +61,7 @@ async-trait = "0.1" execution-core = { version = "0.1.0", path = "../execution-core", features = ["zk"] } rusk-profile = { version = "0.6", path = "../rusk-profile" } rusk-abi = { version = "0.13.0-rc", path = "../rusk-abi", default-features = false, features = ["host"] } -rusk-prover = { version = "0.3", path = "../rusk-prover", optional = true } +rusk-prover = { version = "0.3", path = "../rusk-prover", features = ["std"], optional = true } ## node dependencies node = { version = "0.1", path = "../node", optional = true } diff --git a/rusk/src/lib/http/prover.rs b/rusk/src/lib/http/prover.rs index 69bb82610f..da64734eb9 100644 --- a/rusk/src/lib/http/prover.rs +++ b/rusk/src/lib/http/prover.rs @@ -4,7 +4,8 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use rusk_prover::{LocalProver, Prover}; +use execution_core::transfer::phoenix::Prove; +use rusk_prover::LocalProver; use super::*; @@ -20,7 +21,8 @@ impl HandleRequest for LocalProver { ) -> anyhow::Result { let topic = request.event.topic.as_str(); let response = match topic { - "prove_execute" => self.prove_execute(request.event_data())?, + // Moana: can I change the string content? + "prove_execute" => LocalProver::prove(request.event_data())?, _ => anyhow::bail!("Unsupported"), }; Ok(ResponseData::new(response)) diff --git a/rusk/src/lib/verifier.rs b/rusk/src/lib/verifier.rs index c3b6541d15..1b323d5123 100644 --- a/rusk/src/lib/verifier.rs +++ b/rusk/src/lib/verifier.rs @@ -18,16 +18,16 @@ use rusk_profile::Circuit as CircuitProfile; use std::sync::LazyLock; pub static VD_EXEC_1_2: LazyLock> = - LazyLock::new(|| fetch_verifier("ExecuteCircuitOneTwo")); + LazyLock::new(|| fetch_verifier("TxCircuitOneTwo")); pub static VD_EXEC_2_2: LazyLock> = - LazyLock::new(|| fetch_verifier("ExecuteCircuitTwoTwo")); + LazyLock::new(|| fetch_verifier("TxCircuitTwoTwo")); pub static VD_EXEC_3_2: LazyLock> = - LazyLock::new(|| fetch_verifier("ExecuteCircuitThreeTwo")); + LazyLock::new(|| fetch_verifier("TxCircuitThreeTwo")); pub static VD_EXEC_4_2: LazyLock> = - LazyLock::new(|| fetch_verifier("ExecuteCircuitFourTwo")); + LazyLock::new(|| fetch_verifier("TxCircuitFourTwo")); /// Verifies the proof of the incoming transaction. pub fn verify_proof(tx: &PhoenixTransaction) -> Result { diff --git a/rusk/tests/common/wallet.rs b/rusk/tests/common/wallet.rs index 5e514df10b..4712d047aa 100644 --- a/rusk/tests/common/wallet.rs +++ b/rusk/tests/common/wallet.rs @@ -10,14 +10,16 @@ use std::sync::{Arc, RwLock}; use crate::common::block::Block as BlockAwait; -use dusk_bytes::{DeserializableSlice, Serializable}; +use dusk_bytes::Serializable; use execution_core::{ - plonk::Proof, signatures::bls::PublicKey as BlsPublicKey, stake::StakeData, transfer::{ moonlight::AccountData, - phoenix::{Note, ViewKey, NOTES_TREE_DEPTH}, + phoenix::{ + Note, Prove, Transaction as PhoenixTransaction, ViewKey, + NOTES_TREE_DEPTH, + }, Transaction, }, BlsScalar, @@ -25,8 +27,8 @@ use execution_core::{ use futures::StreamExt; use poseidon_merkle::Opening as PoseidonOpening; use rusk::{Error, Result, Rusk}; -use rusk_prover::{LocalProver, Prover}; -use test_wallet::{self as wallet, Store, UnprovenTransaction}; +use rusk_prover::LocalProver; +use test_wallet::{self as wallet, Store}; use tracing::info; #[derive(Debug, Clone)] @@ -127,9 +129,7 @@ impl wallet::StateClient for TestStateClient { } #[derive(Default)] -pub struct TestProverClient { - pub prover: LocalProver, -} +pub struct TestProverClient(); impl Debug for TestProverClient { fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -141,14 +141,13 @@ 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( - &self, - utx: &UnprovenTransaction, + utx: &PhoenixTransaction, ) -> Result { - let utx_bytes = &utx.to_var_bytes()[..]; - let proof = self.prover.prove_execute(utx_bytes)?; - info!("UTX: {}", hex::encode(utx_bytes)); - let proof = Proof::from_slice(&proof).map_err(Error::Serialization)?; - let tx = utx.clone().gen_transaction(proof); + 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 diff --git a/rusk/tests/rusk-state.rs b/rusk/tests/rusk-state.rs index ecdb1982a3..c34b87238a 100644 --- a/rusk/tests/rusk-state.rs +++ b/rusk/tests/rusk-state.rs @@ -204,12 +204,12 @@ async fn generate_phoenix_txs() -> Result<(), Box> { let mut txs_file = std::fs::File::create("phoenix-txs")?; - for sender_index in 0..N_ADDRESSES as u64 { + 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 u64; - let receiver = wallet.public_key(receiver_index).unwrap(); + 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 @@ -269,10 +269,10 @@ async fn generate_moonlight_txs() -> Result<(), Box> { let mut txs_file = std::fs::File::create("moonlight-txs")?; - for sender_index in 0..N_ADDRESSES as u64 { + for sender_index in 0..N_ADDRESSES as u8 { let wallet = wallet.clone(); - let receiver_index = (sender_index + 1) % N_ADDRESSES as u64; + 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 || { diff --git a/rusk/tests/services/contract_deployment.rs b/rusk/tests/services/contract_deployment.rs index 59db166d7d..aa4269770d 100644 --- a/rusk/tests/services/contract_deployment.rs +++ b/rusk/tests/services/contract_deployment.rs @@ -30,12 +30,12 @@ use crate::common::wallet::{TestProverClient, TestStateClient, TestStore}; const BLOCK_HEIGHT: u64 = 1; const BLOCK_GAS_LIMIT: u64 = 1_000_000_000_000; const GAS_LIMIT: u64 = 200_000_000; -const GAS_LIMIT_NOT_ENOUGH_TO_SPEND: u64 = 11_000_000; -const GAS_LIMIT_NOT_ENOUGH_TO_SPEND_AND_DEPLOY: u64 = 12_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 POINT_LIMIT: u64 = 0x10000000; -const SENDER_INDEX: u64 = 0; +const SENDER_INDEX: u8 = 0; const ALICE_CONTRACT_ID: ContractId = { let mut bytes = [0u8; 32]; @@ -122,6 +122,10 @@ fn make_and_execute_transaction_deploy( let tx = wallet .phoenix_execute( &mut rng, + SENDER_INDEX, + gas_limit, + GAS_PRICE, + 0u64, ContractExec::Deploy(ContractDeploy { bytecode: ContractBytecode { hash, @@ -131,10 +135,6 @@ fn make_and_execute_transaction_deploy( constructor_args, nonce: 0, }), - SENDER_INDEX, - gas_limit, - GAS_PRICE, - 0u64, ) .expect("Making transaction should succeed"); diff --git a/rusk/tests/services/gas_behavior.rs b/rusk/tests/services/gas_behavior.rs index d6f1ce9b4f..c8fa0e7ad7 100644 --- a/rusk/tests/services/gas_behavior.rs +++ b/rusk/tests/services/gas_behavior.rs @@ -29,6 +29,8 @@ 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 DEPOSIT: u64 = 0; // Creates the Rusk initial state for the tests below fn initial_state>(dir: P) -> Result { @@ -38,8 +40,8 @@ fn initial_state>(dir: P) -> Result { new_state(dir, &snapshot, BLOCK_GAS_LIMIT) } -const SENDER_INDEX_0: u64 = 0; -const SENDER_INDEX_1: u64 = 1; +const SENDER_INDEX_0: u8 = 0; +const SENDER_INDEX_1: u8 = 1; fn make_transactions( rusk: &Rusk, @@ -77,11 +79,11 @@ fn make_transactions( let tx_0 = wallet .phoenix_execute( &mut rng, - ContractExec::Call(contract_call.clone()), SENDER_INDEX_0, GAS_LIMIT_0, - 1, - 0, + GAS_PRICE, + DEPOSIT, + ContractExec::Call(contract_call.clone()), ) .expect("Making the transaction should succeed"); @@ -91,11 +93,11 @@ fn make_transactions( let tx_1 = wallet .phoenix_execute( &mut rng, - contract_call, SENDER_INDEX_1, GAS_LIMIT_1, - 1, - 0, + GAS_PRICE, + DEPOSIT, + contract_call, ) .expect("Making the transaction should succeed"); diff --git a/rusk/tests/services/multi_transfer.rs b/rusk/tests/services/multi_transfer.rs index a51c0c0439..92d0aa5fe5 100644 --- a/rusk/tests/services/multi_transfer.rs +++ b/rusk/tests/services/multi_transfer.rs @@ -43,7 +43,9 @@ fn wallet_transfer( amount: u64, ) { // Generate a receiver pk - let receiver = wallet.public_key(3).expect("Failed to get public key"); + let receiver = wallet + .phoenix_public_key(3) + .expect("Failed to get public key"); let mut rng = StdRng::seed_from_u64(0xdead); diff --git a/rusk/tests/services/stake.rs b/rusk/tests/services/stake.rs index 790e973de9..28888db522 100644 --- a/rusk/tests/services/stake.rs +++ b/rusk/tests/services/stake.rs @@ -14,12 +14,13 @@ use execution_core::{ stake::{StakeAmount, STAKE_CONTRACT}, transfer::contract_exec::ContractCall, }; + use rand::prelude::*; use rand::rngs::StdRng; use rusk::{Result, Rusk}; use std::collections::HashMap; use tempfile::tempdir; -use test_wallet::{self as wallet, Store}; +use test_wallet::{self as wallet}; use tracing::info; use crate::common::state::{generator_procedure, new_state}; @@ -29,6 +30,8 @@ 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; // Creates the Rusk initial state for the tests below fn stake_state>(dir: P) -> Result { @@ -68,11 +71,11 @@ fn wallet_stake( .expect("stakeinfo to be found") .amount .is_none(), - "stake amount to be found" + "stake amount not to be found" ); let tx = wallet - .phoenix_stake(&mut rng, 0, 2, value, GAS_LIMIT, 1) + .phoenix_stake(&mut rng, 0, 2, value, GAS_LIMIT, GAS_PRICE) .expect("Failed to create a stake transaction"); let executed_txs = generator_procedure( rusk, @@ -103,7 +106,7 @@ fn wallet_stake( .expect("stake amount to be found"); let tx = wallet - .phoenix_unstake(&mut rng, 0, 0, GAS_LIMIT, 1) + .phoenix_unstake(&mut rng, 0, 0, GAS_LIMIT, GAS_PRICE) .expect("Failed to unstake"); let spent_txs = generator_procedure( rusk, @@ -121,7 +124,7 @@ fn wallet_stake( assert_eq!(stake.amount, None); let tx = wallet - .phoenix_withdraw(&mut rng, 0, 1, GAS_LIMIT, 1) + .phoenix_stake_withdraw(&mut rng, 0, 1, GAS_LIMIT, GAS_PRICE) .expect("failed to withdraw reward"); generator_procedure( rusk, @@ -170,6 +173,7 @@ pub async fn stake() -> Result<()> { "New root after the 1st transfer: {:?}", hex::encode(new_root) ); + info!("arrived at end of stake test, before assertion",); assert_ne!(original_root, new_root, "Root should have changed"); // let recv = kadcast_recv.try_recv(); @@ -177,6 +181,7 @@ pub async fn stake() -> Result<()> { // propagated"); assert_eq!(h, 0, "Transaction locally propagated with // wrong height"); + info!("arrived at end of stake test, after assertion",); Ok(()) } @@ -189,7 +194,7 @@ fn wallet_reward( ) { let mut rng = StdRng::seed_from_u64(0xdead); - let stake_sk = wallet.store().fetch_account_secret_key(2).unwrap(); + let stake_sk = wallet.account_secret_key(2).unwrap(); let stake_pk = BlsPublicKey::from(&stake_sk); let reward_calldata = (stake_pk, 6u32); @@ -203,7 +208,14 @@ fn wallet_reward( ) .expect("calldata should serialize"); let tx = wallet - .phoenix_execute(&mut rng, contract_call, 0, GAS_LIMIT, 1, 0) + .phoenix_execute( + &mut rng, + 0, + GAS_LIMIT, + GAS_PRICE, + DEPOSIT, + contract_call, + ) .expect("Failed to create a reward transaction"); let executed_txs = generator_procedure( rusk, diff --git a/rusk/tests/services/transfer.rs b/rusk/tests/services/transfer.rs index 6bd77a7d03..f4978efcff 100644 --- a/rusk/tests/services/transfer.rs +++ b/rusk/tests/services/transfer.rs @@ -42,7 +42,9 @@ fn wallet_transfer( block_height: u64, ) { // Generate a receiver pk - let receiver_pk = wallet.public_key(1).expect("Failed to get public key"); + let receiver_pk = wallet + .phoenix_public_key(1) + .expect("Failed to get public key"); let mut rng = StdRng::seed_from_u64(0xdead); diff --git a/rusk/tests/services/unspendable.rs b/rusk/tests/services/unspendable.rs index 2f34669a07..67a565ed23 100644 --- a/rusk/tests/services/unspendable.rs +++ b/rusk/tests/services/unspendable.rs @@ -30,6 +30,8 @@ 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 DEPOSIT: u64 = 0; // Creates the Rusk initial state for the tests below fn initial_state>(dir: P) -> Result { @@ -39,9 +41,9 @@ fn initial_state>(dir: P) -> Result { new_state(dir, &snapshot, BLOCK_GAS_LIMIT) } -const SENDER_INDEX_0: u64 = 0; -const SENDER_INDEX_1: u64 = 1; -const SENDER_INDEX_2: u64 = 2; +const SENDER_INDEX_0: u8 = 0; +const SENDER_INDEX_1: u8 = 1; +const SENDER_INDEX_2: u8 = 2; fn make_transactions( rusk: &Rusk, @@ -89,11 +91,11 @@ fn make_transactions( let tx_0 = wallet .phoenix_execute( &mut rng, - ContractExec::Call(contract_call.clone()), SENDER_INDEX_0, GAS_LIMIT_0, - 1, - 0, + GAS_PRICE, + DEPOSIT, + ContractExec::Call(contract_call.clone()), ) .expect("Making the transaction should succeed"); @@ -103,11 +105,11 @@ fn make_transactions( let tx_1 = wallet .phoenix_execute( &mut rng, - ContractExec::Call(contract_call.clone()), SENDER_INDEX_1, GAS_LIMIT_1, - 1, - 0, + GAS_PRICE, + DEPOSIT, + ContractExec::Call(contract_call.clone()), ) .expect("Making the transaction should succeed"); @@ -117,11 +119,11 @@ fn make_transactions( let tx_2 = wallet .phoenix_execute( &mut rng, - contract_call, SENDER_INDEX_2, GAS_LIMIT_2, - 1, - 0, + GAS_PRICE, + DEPOSIT, + contract_call, ) .expect("Making the transaction should succeed");