Skip to content

Commit

Permalink
Merge pull request #3274 from dusk-network/mocello/3273_vm_session
Browse files Browse the repository at this point in the history
vm: Fix review comments
  • Loading branch information
moCello authored Dec 22, 2024
2 parents d21148b + 27a067e commit 31d4dd2
Show file tree
Hide file tree
Showing 27 changed files with 436 additions and 495 deletions.
5 changes: 3 additions & 2 deletions contracts/stake/benches/get_provisioners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const NUM_STAKES: usize = 1000;
const OWNER: [u8; 32] = [0; 32];
const POINT_LIMIT: u64 = 0x100000000;
const TEST_STAKE: u64 = 500_000_000_000_000;
const CHAIN_ID: u8 = 42;

fn config() -> Criterion {
Criterion::default().sample_size(SAMPLE_SIZE)
Expand All @@ -41,7 +42,7 @@ fn instantiate(vm: &VM) -> Session {
"../../../target/dusk/wasm32-unknown-unknown/release/stake_contract.wasm"
);

let mut session = abi::new_genesis_session(vm);
let mut session = vm.genesis_session(CHAIN_ID);

session
.deploy(
Expand All @@ -63,7 +64,7 @@ fn instantiate(vm: &VM) -> Session {

let base = session.commit().expect("Committing should succeed");

abi::new_session(vm, base, 1)
vm.session(base, CHAIN_ID, 1)
.expect("Instantiating new session should succeed")
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/stake/tests/common/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn instantiate<Rng: RngCore + CryptoRng>(
pk: &PhoenixPublicKey,
genesis_value: u64,
) -> Session {
let mut session = dusk_vm::new_genesis_session(vm, CHAIN_ID);
let mut session = vm.genesis_session(CHAIN_ID);

// deploy transfer-contract
let transfer_bytecode = include_bytes!(
Expand Down Expand Up @@ -81,6 +81,6 @@ pub fn instantiate<Rng: RngCore + CryptoRng>(
// sets the block height for all subsequent operations to 1
let base = session.commit().expect("Committing should succeed");

dusk_vm::new_session(vm, base, CHAIN_ID, 1)
vm.session(base, CHAIN_ID, 1)
.expect("Instantiating new session should succeed")
}
41 changes: 1 addition & 40 deletions contracts/stake/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use std::sync::mpsc;

use dusk_core::abi::ContractError;
use dusk_core::transfer::data::TransactionData;
use dusk_core::transfer::phoenix::{
Note, NoteLeaf, NoteOpening, NoteTreeItem, PublicKey as PhoenixPublicKey,
Expand All @@ -15,7 +14,7 @@ use dusk_core::transfer::phoenix::{
};
use dusk_core::transfer::{Transaction, TRANSFER_CONTRACT};
use dusk_core::{BlsScalar, LUX};
use dusk_vm::{CallReceipt, Error as VMError, Session};
use dusk_vm::{Error as VMError, Session};
use rand::rngs::StdRng;
use rusk_prover::LocalProver;

Expand Down Expand Up @@ -98,44 +97,6 @@ pub fn filter_notes_owned_by<I: IntoIterator<Item = Note>>(
.collect()
}

/// Executes a transaction, returning the call receipt
pub fn execute(
session: &mut Session,
tx: impl Into<Transaction>,
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, VMError> {
let tx = tx.into();

// Spend the inputs and execute the call. If this errors the transaction is
// unspendable.
let mut receipt = session.call::<_, Result<Vec<u8>, ContractError>>(
TRANSFER_CONTRACT,
"spend_and_execute",
&tx,
tx.gas_limit(),
)?;

// Ensure all gas is consumed if there's an error in the contract call
if receipt.data.is_err() {
receipt.gas_spent = receipt.gas_limit;
}

// Refund the appropriate amount to the transaction. This call is guaranteed
// to never error. If it does, then a programming error has occurred. As
// such, the call to `Result::expect` is warranted.
let refund_receipt = session
.call::<_, ()>(
TRANSFER_CONTRACT,
"refund",
&receipt.gas_spent,
u64::MAX,
)
.expect("Refunding must succeed");

receipt.events.extend(refund_receipt.events);

Ok(receipt)
}

/// Generate a TxCircuit given the sender secret-key, receiver public-key, the
/// input note positions in the transaction tree and the new output-notes.
pub fn create_transaction<const I: usize>(
Expand Down
39 changes: 18 additions & 21 deletions contracts/stake/tests/partial_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ use dusk_core::signatures::bls::{
};
use dusk_core::stake::{Reward, RewardReason, EPOCH, STAKE_CONTRACT};
use dusk_core::transfer::TRANSFER_CONTRACT;
use dusk_vm::{
new_genesis_session, new_session, ContractData, Error as VMError, Session,
VM,
};
use dusk_vm::{execute, ContractData, Error as VMError, Session, VM};
use rand::rngs::StdRng;
use rand::SeedableRng;
use wallet_core::transaction::{
moonlight_stake, moonlight_stake_reward, moonlight_unstake,
};

pub mod common;

use crate::common::assert::*;
use crate::common::init::CHAIN_ID;
use crate::common::utils::*;
Expand Down Expand Up @@ -63,7 +59,7 @@ fn stake() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;

// verify 1st stake transaction
let gas_spent_1 = receipt.gas_spent;
Expand Down Expand Up @@ -91,7 +87,7 @@ fn stake() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;

// verify 2nd stake transaction
let gas_spent_2 = receipt.gas_spent;
Expand All @@ -109,7 +105,7 @@ fn stake() -> Result<(), VMError> {
// in order to test the locking some of the stake during a top-up, we need
// to start a new session at a block-height on which the stake is eligible
let base = session.commit()?;
let mut session = new_session(&vm, base, CHAIN_ID, 2 * EPOCH)?;
let mut session = vm.session(base, CHAIN_ID, 2 * EPOCH)?;

// execute 3rd stake transaction
let stake_3 = STAKE_VALUE - stake_1 - stake_2;
Expand All @@ -125,7 +121,7 @@ fn stake() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;

// verify 3rd stake transaction
let gas_spent_3 = receipt.gas_spent;
Expand Down Expand Up @@ -160,7 +156,7 @@ fn stake() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
assert!(execute(&mut session, tx,).is_err());
assert!(execute(&mut session, &tx, 0, 0, 0).is_err());

Ok(())
}
Expand Down Expand Up @@ -194,7 +190,7 @@ fn unstake() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;
let mut moonlight_balance = GENESIS_VALUE - STAKE_VALUE - receipt.gas_spent;
assert_moonlight(&mut session, &moonlight_pk, moonlight_balance, nonce);

Expand All @@ -216,7 +212,7 @@ fn unstake() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;

// verify 1st unstake transaction
let gas_spent_1 = receipt.gas_spent;
Expand All @@ -233,7 +229,7 @@ fn unstake() -> Result<(), VMError> {

// re-stake the unstaked value after the stake has become eligible
let base = session.commit()?;
let mut session = new_session(&vm, base, CHAIN_ID, 2 * EPOCH)?;
let mut session = vm.session(base, CHAIN_ID, 2 * EPOCH)?;
nonce += 1;
let tx = moonlight_stake(
&moonlight_sk,
Expand All @@ -246,7 +242,7 @@ fn unstake() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;
total_stake = STAKE_VALUE;
let mut locked = unstake_1 / 10;
assert_stake(&mut session, &stake_pk, total_stake, locked, 0);
Expand All @@ -272,7 +268,7 @@ fn unstake() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;

// verify 2nd unstake transaction
let gas_spent_2 = receipt.gas_spent;
Expand Down Expand Up @@ -310,7 +306,7 @@ fn unstake() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;

// verify 3rd unstake transaction
let gas_spent_3 = receipt.gas_spent;
Expand Down Expand Up @@ -353,7 +349,7 @@ fn withdraw_reward() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;
let mut moonlight_balance = GENESIS_VALUE - STAKE_VALUE - receipt.gas_spent;
assert_moonlight(&mut session, &moonlight_pk, moonlight_balance, nonce);
// add a reward to the staked key
Expand All @@ -378,7 +374,7 @@ fn withdraw_reward() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;

// verify 1st reward withdrawal
let gas_spent_1 = receipt.gas_spent;
Expand Down Expand Up @@ -413,7 +409,7 @@ fn withdraw_reward() -> Result<(), VMError> {
CHAIN_ID,
)
.expect("tx creation should pass");
let receipt = execute(&mut session, tx)?;
let receipt = execute(&mut session, &tx, 0, 0, 0)?;

// verify 1st reward withdrawal
let gas_spent_2 = receipt.gas_spent;
Expand Down Expand Up @@ -458,7 +454,7 @@ fn add_reward(
/// genesis-value.
fn instantiate(vm: &mut VM, moonlight_pk: &BlsPublicKey) -> Session {
// create a new session using an ephemeral vm
let mut session = new_genesis_session(vm, CHAIN_ID);
let mut session = vm.genesis_session(CHAIN_ID);

// deploy transfer-contract
const OWNER: [u8; 32] = [0; 32];
Expand Down Expand Up @@ -502,7 +498,8 @@ fn instantiate(vm: &mut VM, moonlight_pk: &BlsPublicKey) -> Session {
// sets the block height for all subsequent operations to 1
let base = session.commit().expect("Committing should succeed");

let mut session = new_session(vm, base, CHAIN_ID, 1)
let mut session = vm
.session(base, CHAIN_ID, 1)
.expect("Instantiating new session should succeed");

// check that the moonlight account is initialized as expected
Expand Down
23 changes: 12 additions & 11 deletions contracts/stake/tests/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

pub mod common;

use dusk_core::signatures::bls::{
PublicKey as BlsPublicKey, SecretKey as BlsSecretKey,
};
Expand All @@ -21,11 +19,12 @@ use dusk_core::transfer::withdraw::{
Withdraw, WithdrawReceiver, WithdrawReplayToken,
};
use dusk_core::{dusk, JubJubScalar};
use dusk_vm::{new_session, VM};
use dusk_vm::{execute, VM};
use ff::Field;
use rand::rngs::StdRng;
use rand::SeedableRng;

pub mod common;
use crate::common::assert::{
assert_reward_event, assert_stake, assert_stake_event,
};
Expand Down Expand Up @@ -86,8 +85,8 @@ fn stake_withdraw_unstake() {
contract_call,
);

let receipt =
execute(&mut session, tx).expect("Executing TX should succeed");
let receipt = execute(&mut session, &tx, 0, 0, 0)
.expect("Executing TX should succeed");

let gas_spent = receipt.gas_spent;
receipt.data.expect("Executed TX should not error");
Expand Down Expand Up @@ -178,11 +177,12 @@ fn stake_withdraw_unstake() {
// set different block height so that the new notes are easily located and
// filtered
let base = session.commit().expect("Committing should succeed");
let mut session = new_session(vm, base, CHAIN_ID, 2)
let mut session = vm
.session(base, CHAIN_ID, 2)
.expect("Instantiating new session should succeed");

let receipt =
execute(&mut session, tx).expect("Executing TX should succeed");
let receipt = execute(&mut session, &tx, 0, 0, 0)
.expect("Executing TX should succeed");

let gas_spent = receipt.gas_spent;
receipt.data.expect("Executed TX should not error");
Expand Down Expand Up @@ -274,11 +274,12 @@ fn stake_withdraw_unstake() {
// filtered
// sets the block height for all subsequent operations to 1
let base = session.commit().expect("Committing should succeed");
let mut session = new_session(vm, base, CHAIN_ID, 3)
let mut session = vm
.session(base, CHAIN_ID, 3)
.expect("Instantiating new session should succeed");

let receipt =
execute(&mut session, tx).expect("Executing TX should succeed");
let receipt = execute(&mut session, &tx, 0, 0, 0)
.expect("Executing TX should succeed");
update_root(&mut session).expect("Updating the root should succeed");

let gas_spent = receipt.gas_spent;
Expand Down
40 changes: 3 additions & 37 deletions contracts/transfer/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use std::sync::mpsc;

use dusk_core::abi::{ContractError, ContractId};
use dusk_core::abi::ContractId;
use dusk_core::signatures::bls::PublicKey as AccountPublicKey;
use dusk_core::transfer::moonlight::AccountData;
use dusk_core::transfer::phoenix::{Note, NoteLeaf, ViewKey as PhoenixViewKey};
use dusk_core::transfer::{Transaction, TRANSFER_CONTRACT};
use dusk_core::transfer::TRANSFER_CONTRACT;
use dusk_core::BlsScalar;
use dusk_vm::{CallReceipt, Error as VMError, Session};
use dusk_vm::{Error as VMError, Session};

const GAS_LIMIT: u64 = 0x10_000_000;

Expand All @@ -31,40 +31,6 @@ pub fn chain_id(session: &mut Session) -> Result<u8, VMError> {
.map(|r| r.data)
}

/// Executes a transaction.
/// Returns result containing gas spent.
pub fn execute(
session: &mut Session,
tx: impl Into<Transaction>,
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, VMError> {
let tx = tx.into();

let mut receipt = session.call::<_, Result<Vec<u8>, ContractError>>(
TRANSFER_CONTRACT,
"spend_and_execute",
&tx,
tx.gas_limit(),
)?;

// Ensure all gas is consumed if there's an error in the contract call
if receipt.data.is_err() {
receipt.gas_spent = receipt.gas_limit;
}

let refund_receipt = session
.call::<_, ()>(
TRANSFER_CONTRACT,
"refund",
&receipt.gas_spent,
u64::MAX,
)
.expect("Refunding must succeed");

receipt.events.extend(refund_receipt.events);

Ok(receipt)
}

// moonlight helper functions

pub fn account(
Expand Down
Loading

0 comments on commit 31d4dd2

Please sign in to comment.