Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: Add dusk-vm #3237

Merged
merged 12 commits into from
Dec 22, 2024
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ members = [
"contracts/transfer",

"core",
"vm",
"wallet-core",

"rusk-prover",

"rusk-recovery",
"rusk-profile",
"rusk-abi",
"rusk",

"node-data",
Expand All @@ -34,9 +34,9 @@ resolver = "2"
# Workspace internal dependencies
dusk-consensus = { version = "0.1.1-rc.3", path = "./consensus/" }
dusk-core = { version = "0.1.0", path = "./core/" }
dusk-vm = { version = "0.1.0", path = "./vm/" }
node = { version = "0.1.0", path = "./node/" }
node-data = { version = "0.1.0", path = "./node-data/" }
rusk-abi = { version = "0.13.0-rc.0", path = "./rusk-abi/", default-features = false }
rusk-profile = { version = "0.6.0", path = "./rusk-profile/" }
rusk-prover = { version = "0.5.0", path = "./rusk-prover/" }
rusk-recovery = { version = "0.6.0", path = "./rusk-recovery/" }
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ help: ## Display this help screen
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

abi: ## Build the ABI
$(MAKE) -C ./rusk-abi all
$(MAKE) -C ./vm all

keys: ## Create the keys for the circuits
$(MAKE) -C ./rusk recovery-keys
Expand All @@ -22,7 +22,7 @@ contracts: ## Execute the test for all contracts
$(MAKE) -j1 -C ./contracts all

test: keys wasm ## Run the tests
$(MAKE) -C ./rusk-abi/ $@
$(MAKE) -C ./vm/ $@
$(MAKE) -C ./core/ $@
$(MAKE) state
$(MAKE) -j1 -C ./contracts $@
Expand All @@ -38,7 +38,7 @@ test: keys wasm ## Run the tests
clippy: ## Run clippy
$(MAKE) -C ./core/ $@
$(MAKE) -j1 -C ./contracts $@
$(MAKE) -C ./rusk-abi $@
$(MAKE) -C ./vm $@
$(MAKE) -C ./rusk-profile $@
$(MAKE) -C ./rusk-recovery $@
$(MAKE) -C ./rusk-prover/ $@
Expand All @@ -56,7 +56,7 @@ doc: ## Run doc gen
$(MAKE) -C ./node $@
$(MAKE) -C ./node-data $@
$(MAKE) -C ./rusk/ $@
$(MAKE) -C ./rusk-abi $@
$(MAKE) -C ./vm $@
$(MAKE) -C ./rusk-profile $@
$(MAKE) -C ./rusk-prover/ $@
$(MAKE) -C ./rusk-recovery $@
Expand Down
2 changes: 1 addition & 1 deletion contracts/stake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rkyv = { workspace = true, features = ["size_32"] }
dusk-core = { workspace = true, features = ["abi-dlmalloc"] }

[dev-dependencies]
rusk-abi = { workspace = true }
dusk-vm = { workspace = true }
dusk-core = { workspace = true, features = ["zk"] }
rusk-prover = { workspace = true }
wallet-core = { workspace = true }
Expand Down
11 changes: 5 additions & 6 deletions contracts/stake/benches/get_provisioners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use dusk_core::{
transfer::TRANSFER_CONTRACT,
BlsPublicKey, BlsSecretKey,
};
use dusk_vm::{ContractData, Error as VMError, Session, VM, VM};
use rand::rngs::StdRng;
use rand::{CryptoRng, RngCore, SeedableRng};
use rusk_abi::{ContractData, PiecrustError, Session, VM};
use std::sync::mpsc;

const SAMPLE_SIZE: usize = 10;
Expand All @@ -27,7 +27,7 @@ fn config() -> Criterion {
Criterion::default().sample_size(SAMPLE_SIZE)
}

fn update_root(session: &mut Session) -> Result<(), PiecrustError> {
fn update_root(session: &mut Session) -> Result<(), VMError> {
session
.call(TRANSFER_CONTRACT, "update_root", &(), POINT_LIMIT)
.map(|r| r.data)
Expand Down Expand Up @@ -69,7 +69,7 @@ fn instantiate(vm: &VM) -> Session {

fn do_get_provisioners(
session: &mut Session,
) -> Result<impl Iterator<Item = (BlsPublicKey, StakeData)>, PiecrustError> {
) -> Result<impl Iterator<Item = (BlsPublicKey, StakeData)>, VMError> {
let (sender, receiver) = mpsc::channel();
session.feeder_call::<_, ()>(
STAKE_CONTRACT,
Expand All @@ -87,7 +87,7 @@ fn do_get_provisioners(
fn do_insert_stake<Rng: RngCore + CryptoRng>(
rng: &mut Rng,
session: &mut Session,
) -> Result<(), PiecrustError> {
) -> Result<(), VMError> {
let stake_data = StakeData {
amount: Some((TEST_STAKE, 0)),
nonce: 1,
Expand All @@ -107,8 +107,7 @@ fn do_insert_stake<Rng: RngCore + CryptoRng>(
fn get_provisioners(c: &mut Criterion) {
let rng = &mut StdRng::seed_from_u64(0xfeeb);

let vm = &mut abi::new_ephemeral_vm()
.expect("Creating ephemeral VM should work");
let vm = &mut VM::ephemeral().expect("Creating ephemeral VM should work");

let mut session = instantiate(vm);

Expand Down
2 changes: 1 addition & 1 deletion contracts/stake/tests/common/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use dusk_core::stake::{
};
use dusk_core::transfer::moonlight::AccountData;
use dusk_core::transfer::TRANSFER_CONTRACT;
use dusk_vm::Session;
use rkyv::{check_archived_root, Deserialize, Infallible};
use rusk_abi::Session;

use super::utils::GAS_LIMIT;

Expand Down
6 changes: 3 additions & 3 deletions contracts/stake/tests/common/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use dusk_core::transfer::{
TRANSFER_CONTRACT,
};
use dusk_core::JubJubScalar;
use dusk_vm::{ContractData, Session, VM};
use ff::Field;
use rand::{CryptoRng, RngCore};
use rusk_abi::{ContractData, Session, VM};

use crate::common::utils::{update_root, GAS_LIMIT};

Expand All @@ -27,7 +27,7 @@ pub fn instantiate<Rng: RngCore + CryptoRng>(
pk: &PhoenixPublicKey,
genesis_value: u64,
) -> Session {
let mut session = rusk_abi::new_genesis_session(vm, CHAIN_ID);
let mut session = dusk_vm::new_genesis_session(vm, 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");

rusk_abi::new_session(vm, base, CHAIN_ID, 1)
dusk_vm::new_session(vm, base, CHAIN_ID, 1)
.expect("Instantiating new session should succeed")
}
16 changes: 8 additions & 8 deletions contracts/stake/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ 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 rand::rngs::StdRng;
use rusk_abi::{CallReceipt, PiecrustError, Session};
use rusk_prover::LocalProver;

pub const GAS_LIMIT: u64 = 0x100_000_000;
Expand All @@ -25,7 +25,7 @@ pub const GAS_PRICE: u64 = LUX;
pub fn leaves_from_height(
session: &mut Session,
height: u64,
) -> Result<Vec<NoteLeaf>, PiecrustError> {
) -> Result<Vec<NoteLeaf>, VMError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand All @@ -45,7 +45,7 @@ pub fn leaves_from_height(
pub fn leaves_from_pos(
session: &mut Session,
pos: u64,
) -> Result<Vec<NoteLeaf>, PiecrustError> {
) -> Result<Vec<NoteLeaf>, VMError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand All @@ -62,13 +62,13 @@ pub fn leaves_from_pos(
.collect())
}

pub fn update_root(session: &mut Session) -> Result<(), PiecrustError> {
pub fn update_root(session: &mut Session) -> Result<(), VMError> {
session
.call(TRANSFER_CONTRACT, "update_root", &(), GAS_LIMIT)
.map(|r| r.data)
}

pub fn root(session: &mut Session) -> Result<BlsScalar, PiecrustError> {
pub fn root(session: &mut Session) -> Result<BlsScalar, VMError> {
session
.call(TRANSFER_CONTRACT, "root", &(), GAS_LIMIT)
.map(|r| r.data)
Expand All @@ -77,13 +77,13 @@ pub fn root(session: &mut Session) -> Result<BlsScalar, PiecrustError> {
pub fn opening(
session: &mut Session,
pos: u64,
) -> Result<Option<NoteOpening>, PiecrustError> {
) -> Result<Option<NoteOpening>, VMError> {
session
.call(TRANSFER_CONTRACT, "opening", &pos, GAS_LIMIT)
.map(|r| r.data)
}

pub fn chain_id(session: &mut Session) -> Result<u8, PiecrustError> {
pub fn chain_id(session: &mut Session) -> Result<u8, VMError> {
session
.call(TRANSFER_CONTRACT, "chain_id", &(), GAS_LIMIT)
.map(|r| r.data)
Expand All @@ -102,7 +102,7 @@ pub fn filter_notes_owned_by<I: IntoIterator<Item = Note>>(
pub fn execute(
session: &mut Session,
tx: impl Into<Transaction>,
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, PiecrustError> {
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, VMError> {
let tx = tx.into();

// Spend the inputs and execute the call. If this errors the transaction is
Expand Down
12 changes: 5 additions & 7 deletions contracts/stake/tests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ use dusk_core::{
TRANSFER_CONTRACT,
},
};
use rusk_abi::PiecrustError;
use dusk_vm::{Error as VMError, VM};

use crate::common::assert::assert_reward_event;
use crate::common::init::instantiate;

const GENESIS_VALUE: u64 = dusk(1_000_000.0);

#[test]
fn reward_slash() -> Result<(), PiecrustError> {
fn reward_slash() -> Result<(), VMError> {
let rng = &mut StdRng::seed_from_u64(0xfeeb);

let vm = &mut rusk_abi::new_ephemeral_vm()
.expect("Creating ephemeral VM should work");
let vm = &mut VM::ephemeral().expect("Creating ephemeral VM should work");

let sk = PhoenixSecretKey::random(rng);
let pk = PhoenixPublicKey::from(&sk);
Expand Down Expand Up @@ -121,11 +120,10 @@ fn reward_slash() -> Result<(), PiecrustError> {
}

#[test]
fn stake_hard_slash() -> Result<(), PiecrustError> {
fn stake_hard_slash() -> Result<(), VMError> {
let rng = &mut StdRng::seed_from_u64(0xfeeb);

let vm = &mut rusk_abi::new_ephemeral_vm()
.expect("Creating ephemeral VM should work");
let vm = &mut VM::ephemeral().expect("Creating ephemeral VM should work");

let sk = PhoenixSecretKey::random(rng);
let pk = PhoenixPublicKey::from(&sk);
Expand Down
27 changes: 15 additions & 12 deletions contracts/stake/tests/partial_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ 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 rand::rngs::StdRng;
use rand::SeedableRng;
use rusk_abi::{ContractData, PiecrustError, Session, VM};
use wallet_core::transaction::{
moonlight_stake, moonlight_stake_reward, moonlight_unstake,
};
Expand All @@ -28,7 +31,7 @@ const STAKE_VALUE: u64 = GENESIS_VALUE / 2;
const GENESIS_NONCE: u64 = 0;

#[test]
fn stake() -> Result<(), PiecrustError> {
fn stake() -> Result<(), VMError> {
// ------
// instantiate the test

Expand All @@ -40,7 +43,7 @@ fn stake() -> Result<(), PiecrustError> {
let stake_sk = BlsSecretKey::random(rng);
let stake_pk = BlsPublicKey::from(&stake_sk);

let mut vm = &mut rusk_abi::new_ephemeral_vm()?;
let mut vm = &mut VM::ephemeral()?;
let mut session = instantiate(&mut vm, &moonlight_pk);

// ------
Expand Down Expand Up @@ -106,7 +109,7 @@ fn stake() -> Result<(), PiecrustError> {
// 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 = rusk_abi::new_session(&vm, base, CHAIN_ID, 2 * EPOCH)?;
let mut session = new_session(&vm, base, CHAIN_ID, 2 * EPOCH)?;

// execute 3rd stake transaction
let stake_3 = STAKE_VALUE - stake_1 - stake_2;
Expand Down Expand Up @@ -163,7 +166,7 @@ fn stake() -> Result<(), PiecrustError> {
}

#[test]
fn unstake() -> Result<(), PiecrustError> {
fn unstake() -> Result<(), VMError> {
// ------
// instantiate the test

Expand All @@ -175,7 +178,7 @@ fn unstake() -> Result<(), PiecrustError> {
let stake_sk = BlsSecretKey::random(rng);
let stake_pk = BlsPublicKey::from(&stake_sk);

let mut vm = &mut rusk_abi::new_ephemeral_vm()?;
let mut vm = &mut VM::ephemeral()?;
let mut session = instantiate(&mut vm, &moonlight_pk);

// initial stake
Expand Down Expand Up @@ -230,7 +233,7 @@ fn unstake() -> Result<(), PiecrustError> {

// re-stake the unstaked value after the stake has become eligible
let base = session.commit()?;
let mut session = rusk_abi::new_session(&vm, base, CHAIN_ID, 2 * EPOCH)?;
let mut session = new_session(&vm, base, CHAIN_ID, 2 * EPOCH)?;
nonce += 1;
let tx = moonlight_stake(
&moonlight_sk,
Expand Down Expand Up @@ -322,7 +325,7 @@ fn unstake() -> Result<(), PiecrustError> {
}

#[test]
fn withdraw_reward() -> Result<(), PiecrustError> {
fn withdraw_reward() -> Result<(), VMError> {
// ------
// instantiate the test

Expand All @@ -334,7 +337,7 @@ fn withdraw_reward() -> Result<(), PiecrustError> {
let stake_sk = BlsSecretKey::random(rng);
let stake_pk = BlsPublicKey::from(&stake_sk);

let mut vm = &mut rusk_abi::new_ephemeral_vm()?;
let mut vm = &mut VM::ephemeral()?;
let mut session = instantiate(&mut vm, &moonlight_pk);

// initial stake
Expand Down Expand Up @@ -435,7 +438,7 @@ fn add_reward(
session: &mut Session,
stake_pk: &BlsPublicKey,
reward: u64,
) -> Result<(), PiecrustError> {
) -> Result<(), VMError> {
let rewards = vec![Reward {
account: *stake_pk,
value: reward,
Expand All @@ -455,7 +458,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 = rusk_abi::new_genesis_session(vm, CHAIN_ID);
let mut session = new_genesis_session(vm, CHAIN_ID);

// deploy transfer-contract
const OWNER: [u8; 32] = [0; 32];
Expand Down Expand Up @@ -499,7 +502,7 @@ 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 = rusk_abi::new_session(vm, base, CHAIN_ID, 1)
let mut session = new_session(vm, base, CHAIN_ID, 1)
.expect("Instantiating new session should succeed");

// check that the moonlight account is initialized as expected
Expand Down
Loading
Loading