Skip to content

Commit

Permalink
rusk-wallet: Adjust to added utx support in wallet-core
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Sep 16, 2024
1 parent 2a9fba4 commit af6406f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 67 deletions.
56 changes: 24 additions & 32 deletions rusk-wallet/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use execution_core::{
signatures::bls::PublicKey as AccountPublicKey,
transfer::{
moonlight::AccountData,
phoenix::{Note, NoteLeaf, Prove},
phoenix::{
Note, NoteLeaf, Transaction as PhoenixTransaction,
UnprovenTransaction,
},
Transaction,
},
Error as ExecutionCoreError,
Expand Down Expand Up @@ -56,20 +59,6 @@ const SYNC_INTERVAL_SECONDS: u64 = 3;
/// SIZE of the tree leaf
pub const TREE_LEAF: usize = std::mem::size_of::<ArchivedNoteLeaf>();

/// A prover struct that has the `Prove` trait from executio-core implemented.
/// It currently uses a hardcoded prover which delegates the proving to the
/// `prove_execute`
pub struct Prover;

impl Prove for Prover {
fn prove(
&self,
tx_circuit_vec_bytes: &[u8],
) -> Result<Vec<u8>, ExecutionCoreError> {
Ok(tx_circuit_vec_bytes.to_vec())
}
}

/// The state struct is responsible for managing the state of the wallet
pub struct State {
cache: Mutex<Arc<Cache>>,
Expand Down Expand Up @@ -152,33 +141,36 @@ impl State {
sync_db(&self.client, &self.cache(), &self.store, self.status).await
}

/// Requests that a node prove the given transaction and later propagates it
/// Skips writing the proof for non phoenix transactions
pub async fn prove_and_propagate(
/// Requests that a node prove the given phoenix-transaction.
pub async fn prove_unproven(
&self,
tx: Transaction,
utx: UnprovenTransaction,
) -> Result<Transaction, Error> {
let status = self.status;
let prover = &self.prover;
let mut tx = tx;

if let Transaction::Phoenix(utx) = &mut tx {
let status = self.status;
let proof = utx.proof();
status("Attempt to prove unproven tx...");

status("Attempt to prove tx...");
let prove_req = RuskRequest::new("prove_execute", utx.circuit.clone());

let prove_req = RuskRequest::new("prove_execute", proof.to_vec());
let proof = prover
.call(2, "rusk", &prove_req)
.await
.map_err(|e| ExecutionCoreError::PhoenixCircuit(e.to_string()))?;

let proof =
prover.call(2, "rusk", &prove_req).await.map_err(|e| {
ExecutionCoreError::PhoenixCircuit(e.to_string())
})?;
let tx = PhoenixTransaction::from_unproven(utx, proof);

utx.set_proof(proof);
status("Proving sucesss!");

status("Proving sucesss!");
}
Ok(tx.into())
}

/// Requests that a node propagates a given transaction.
pub async fn propagate(
&self,
tx: Transaction,
) -> Result<Transaction, Error> {
let status = self.status;

let tx_bytes = tx.to_var_bytes();

Expand Down
73 changes: 38 additions & 35 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ use serde::Serialize;
use std::fmt::Debug;
use std::fs;
use std::path::{Path, PathBuf};
use wallet_core::transaction::{
moonlight_deployment, moonlight_stake_reward, phoenix_deployment,
};

use wallet_core::{
phoenix_balance,
Expand All @@ -32,9 +29,12 @@ use wallet_core::{
derive_phoenix_vk,
},
transaction::{
moonlight, moonlight_stake, moonlight_to_phoenix, moonlight_unstake,
phoenix, phoenix_stake, phoenix_stake_reward, phoenix_to_moonlight,
phoenix_unstake,
moonlight, moonlight_deployment, moonlight_stake,
moonlight_stake_reward, moonlight_to_phoenix, moonlight_unstake,
unproven::{
phoenix, phoenix_deployment, phoenix_stake, phoenix_stake_reward,
phoenix_to_moonlight, phoenix_unstake,
},
},
BalanceInfo,
};
Expand All @@ -52,7 +52,7 @@ use zeroize::Zeroize;
use super::*;

use crate::{
clients::{Prover, State},
clients::State,
crypto::encrypt,
currency::Dusk,
dat::{
Expand Down Expand Up @@ -497,7 +497,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {

from_sk.zeroize();

state.prove_and_propagate(tx).await
state.propagate(tx).await
}

/// Executes a generic contract call, paying gas with phoenix notes
Expand Down Expand Up @@ -537,7 +537,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let root = state.fetch_root().await?;
let chain_id = state.fetch_chain_id().await?;

let tx = phoenix(
let utx = phoenix(
&mut rng,
&sender_sk,
sender.pk()?,
Expand All @@ -551,12 +551,12 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
gas.price,
chain_id,
Some(data),
&Prover,
)?;

sender_sk.zeroize();

state.prove_and_propagate(tx).await
let tx = state.prove_unproven(utx).await?;
state.propagate(tx).await
}

/// Transfers funds between Phoenix addresses
Expand Down Expand Up @@ -600,7 +600,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let root = state.fetch_root().await?;
let chain_id = state.fetch_chain_id().await?;

let tx = phoenix(
let utx = phoenix(
&mut rng,
&sender_sk,
change_pk,
Expand All @@ -614,12 +614,12 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
gas.price,
chain_id,
None::<ContractCall>,
&Prover,
)?;

sender_sk.zeroize();

state.prove_and_propagate(tx).await
let tx = state.prove_unproven(utx).await?;
state.propagate(tx).await
}

/// Transfer through Moonlight
Expand Down Expand Up @@ -668,7 +668,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {

from_sk.zeroize();

state.prove_and_propagate(tx).await
state.propagate(tx).await
}

/// Stakes Dusk using Phoenix notes
Expand Down Expand Up @@ -716,15 +716,16 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let root = state.fetch_root().await?;
let chain_id = state.fetch_chain_id().await?;

let stake = phoenix_stake(
let utx = phoenix_stake(
&mut rng, &sender_sk, &stake_sk, inputs, root, gas.limit,
gas.price, chain_id, amt, nonce, &Prover,
gas.price, chain_id, amt, nonce,
)?;

sender_sk.zeroize();
stake_sk.zeroize();

state.prove_and_propagate(stake).await
let tx = state.prove_unproven(utx).await?;
state.propagate(tx).await
}

/// Stake via Moonlight
Expand Down Expand Up @@ -771,7 +772,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {

stake_sk.zeroize();

state.prove_and_propagate(stake).await
state.propagate(stake).await
}

/// Obtains stake information for a given address
Expand Down Expand Up @@ -815,7 +816,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let root = state.fetch_root().await?;
let chain_id = state.fetch_chain_id().await?;

let unstake = phoenix_unstake(
let utx = phoenix_unstake(
&mut rng,
&sender_sk,
&stake_sk,
Expand All @@ -825,13 +826,13 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
gas.limit,
gas.price,
chain_id,
&Prover,
)?;

sender_sk.zeroize();
stake_sk.zeroize();

state.prove_and_propagate(unstake).await
let tx = state.prove_unproven(utx).await?;
state.propagate(tx).await
}

/// Unstakes Dusk through Moonlight
Expand Down Expand Up @@ -875,7 +876,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {

stake_sk.zeroize();

state.prove_and_propagate(unstake).await
state.propagate(unstake).await
}

/// Withdraw accumulated staking reward for a given address to Phoenix
Expand Down Expand Up @@ -907,7 +908,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
.map(|s| s.reward)
.unwrap_or(0);

let withdraw = phoenix_stake_reward(
let utx = phoenix_stake_reward(
&mut rng,
&sender_sk,
&stake_sk,
Expand All @@ -917,13 +918,13 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
gas.limit,
gas.price,
chain_id,
&Prover,
)?;

sender_sk.zeroize();
stake_sk.zeroize();

state.prove_and_propagate(withdraw).await
let tx = state.prove_unproven(utx).await?;
state.propagate(tx).await
}

/// Convert balance from Phoenix to Moonlight
Expand All @@ -947,15 +948,16 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let mut sender_sk = self.phoenix_secret_key(sender_index);
let mut stake_sk = self.bls_secret_key(sender_index);

let convert = phoenix_to_moonlight(
let utx = phoenix_to_moonlight(
&mut rng, &sender_sk, &stake_sk, inputs, root, amt, gas.limit,
gas.price, chain_id, &Prover,
gas.price, chain_id,
)?;

sender_sk.zeroize();
stake_sk.zeroize();

state.prove_and_propagate(convert).await
let tx = state.prove_unproven(utx).await?;
state.propagate(tx).await
}

/// Convert balance from Moonlight to Phoenix
Expand Down Expand Up @@ -985,7 +987,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
sender_sk.zeroize();
stake_sk.zeroize();

state.prove_and_propagate(convert).await
state.propagate(convert).await
}

/// Withdraw accumulated staking reward for a given address to Moonlight
Expand All @@ -1011,7 +1013,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {

sender_sk.zeroize();

state.prove_and_propagate(withdraw).await
state.propagate(withdraw).await
}

/// Deploy a contract using Moonlight
Expand All @@ -1037,7 +1039,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {

sender_sk.zeroize();

state.prove_and_propagate(deploy).await
state.propagate(deploy).await
}

/// Deploy a contract using Phoenix
Expand All @@ -1060,14 +1062,15 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let mut sender_sk = self.phoenix_secret_key(sender_index);
let apk = self.bls_public_key(sender_index);

let deploy = phoenix_deployment(
let utx = phoenix_deployment(
&mut rng, &sender_sk, inputs, root, bytes_code, &apk, init_args, 0,
gas.limit, gas.price, chain_id, &Prover,
gas.limit, gas.price, chain_id,
)?;

sender_sk.zeroize();

state.prove_and_propagate(deploy).await
let tx = state.prove_unproven(utx).await?;
state.propagate(tx).await
}

/// Returns BLS key-pair for provisioner nodes
Expand Down

0 comments on commit af6406f

Please sign in to comment.