Skip to content

Commit

Permalink
stake-contract: Add Dusk's VM
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Dec 20, 2024
1 parent 7e2a026 commit 3d3f83b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
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,7 +11,7 @@ use dusk_core::{
transfer::TRANSFER_CONTRACT,
BlsPublicKey, BlsSecretKey,
};
use dusk_vm::{ContractData, PiecrustError, Session, VM};
use dusk_vm::{ContractData, Error as VMError, Session, VM, VM};
use rand::rngs::StdRng;
use rand::{CryptoRng, RngCore, SeedableRng};
use std::sync::mpsc;
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
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,7 +15,7 @@ use dusk_core::transfer::phoenix::{
};
use dusk_core::transfer::{Transaction, TRANSFER_CONTRACT};
use dusk_core::{BlsScalar, LUX};
use dusk_vm::{CallReceipt, PiecrustError, Session};
use dusk_vm::{CallReceipt, Error as VMError, Session};
use rand::rngs::StdRng;
use rusk_prover::LocalProver;

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 dusk_vm::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 dusk_vm::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 dusk_vm::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,7 +10,10 @@ use dusk_core::signatures::bls::{
};
use dusk_core::stake::{Reward, RewardReason, EPOCH, STAKE_CONTRACT};
use dusk_core::transfer::TRANSFER_CONTRACT;
use dusk_vm::{ContractData, PiecrustError, Session, VM};
use dusk_vm::{
new_genesis_session, new_session, ContractData, Error as VMError, Session,
VM,
};
use rand::rngs::StdRng;
use rand::SeedableRng;
use wallet_core::transaction::{
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 dusk_vm::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 = dusk_vm::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 dusk_vm::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 = dusk_vm::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 dusk_vm::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 = dusk_vm::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 = dusk_vm::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
8 changes: 4 additions & 4 deletions contracts/stake/tests/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use dusk_core::transfer::withdraw::{
Withdraw, WithdrawReceiver, WithdrawReplayToken,
};
use dusk_core::{dusk, JubJubScalar};
use dusk_vm::{new_session, VM};
use ff::Field;
use rand::rngs::StdRng;
use rand::SeedableRng;
Expand All @@ -41,8 +42,7 @@ fn stake_withdraw_unstake() {

let rng = &mut StdRng::seed_from_u64(0xfeeb);

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

let phoenix_sender_sk = PhoenixSecretKey::random(rng);
let phoenix_sender_vk = PhoenixViewKey::from(&phoenix_sender_sk);
Expand Down Expand Up @@ -178,7 +178,7 @@ 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 = dusk_vm::new_session(vm, base, CHAIN_ID, 2)
let mut session = new_session(vm, base, CHAIN_ID, 2)
.expect("Instantiating new session should succeed");

let receipt =
Expand Down Expand Up @@ -274,7 +274,7 @@ 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 = dusk_vm::new_session(vm, base, CHAIN_ID, 3)
let mut session = new_session(vm, base, CHAIN_ID, 3)
.expect("Instantiating new session should succeed");

let receipt =
Expand Down

0 comments on commit 3d3f83b

Please sign in to comment.