Skip to content

Commit

Permalink
rusk: Integrate wallet-core
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Aug 23, 2024
1 parent bbd63ff commit cd65045
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 63 deletions.
2 changes: 1 addition & 1 deletion rusk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 4 additions & 2 deletions rusk/src/lib/http/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand All @@ -20,7 +21,8 @@ impl HandleRequest for LocalProver {
) -> anyhow::Result<ResponseData> {
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))
Expand Down
8 changes: 4 additions & 4 deletions rusk/src/lib/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ use rusk_profile::Circuit as CircuitProfile;
use std::sync::LazyLock;

pub static VD_EXEC_1_2: LazyLock<Vec<u8>> =
LazyLock::new(|| fetch_verifier("ExecuteCircuitOneTwo"));
LazyLock::new(|| fetch_verifier("TxCircuitOneTwo"));

pub static VD_EXEC_2_2: LazyLock<Vec<u8>> =
LazyLock::new(|| fetch_verifier("ExecuteCircuitTwoTwo"));
LazyLock::new(|| fetch_verifier("TxCircuitTwoTwo"));

pub static VD_EXEC_3_2: LazyLock<Vec<u8>> =
LazyLock::new(|| fetch_verifier("ExecuteCircuitThreeTwo"));
LazyLock::new(|| fetch_verifier("TxCircuitThreeTwo"));

pub static VD_EXEC_4_2: LazyLock<Vec<u8>> =
LazyLock::new(|| fetch_verifier("ExecuteCircuitFourTwo"));
LazyLock::new(|| fetch_verifier("TxCircuitFourTwo"));

/// Verifies the proof of the incoming transaction.
pub fn verify_proof(tx: &PhoenixTransaction) -> Result<bool> {
Expand Down
29 changes: 14 additions & 15 deletions rusk/tests/common/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ 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,
};
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)]
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Transaction, Self::Error> {
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

Expand Down
10 changes: 5 additions & 5 deletions rusk/tests/rusk-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ async fn generate_phoenix_txs() -> Result<(), Box<dyn std::error::Error>> {

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
Expand Down Expand Up @@ -269,10 +269,10 @@ async fn generate_moonlight_txs() -> Result<(), Box<dyn std::error::Error>> {

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 || {
Expand Down
14 changes: 7 additions & 7 deletions rusk/tests/services/contract_deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand All @@ -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");

Expand Down
18 changes: 10 additions & 8 deletions rusk/tests/services/gas_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
Expand All @@ -38,8 +40,8 @@ fn initial_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
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,
Expand Down Expand Up @@ -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");

Expand All @@ -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");

Expand Down
4 changes: 3 additions & 1 deletion rusk/tests/services/multi_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
26 changes: 19 additions & 7 deletions rusk/tests/services/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -170,13 +173,15 @@ 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();
// let (_, _, h) = recv.expect("Transaction has not been locally
// propagated"); assert_eq!(h, 0, "Transaction locally propagated with
// wrong height");

info!("arrived at end of stake test, after assertion",);
Ok(())
}

Expand All @@ -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);

Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion rusk/tests/services/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading

0 comments on commit cd65045

Please sign in to comment.