Skip to content

Commit

Permalink
transfer-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 cb788b7 commit 7e2a026
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
16 changes: 8 additions & 8 deletions contracts/transfer/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ 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::BlsScalar;
use dusk_vm::{CallReceipt, PiecrustError, Session};
use dusk_vm::{CallReceipt, Error as VMError, Session};

const GAS_LIMIT: u64 = 0x10_000_000;

pub fn contract_balance(
session: &mut Session,
contract: ContractId,
) -> Result<u64, PiecrustError> {
) -> Result<u64, VMError> {
session
.call(TRANSFER_CONTRACT, "contract_balance", &contract, 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 @@ -36,7 +36,7 @@ pub fn chain_id(session: &mut Session) -> Result<u8, PiecrustError> {
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();

let mut receipt = session.call::<_, Result<Vec<u8>, ContractError>>(
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn execute(
pub fn account(
session: &mut Session,
pk: &AccountPublicKey,
) -> Result<AccountData, PiecrustError> {
) -> Result<AccountData, VMError> {
session
.call(TRANSFER_CONTRACT, "account", pk, GAS_LIMIT)
.map(|r| r.data)
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn owned_notes_value<'a, I: IntoIterator<Item = &'a Note>>(
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 @@ -123,7 +123,7 @@ pub fn leaves_from_height(
.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)
Expand All @@ -142,7 +142,7 @@ pub fn filter_notes_owned_by<I: IntoIterator<Item = Note>>(
pub fn existing_nullifiers(
session: &mut Session,
nullifiers: &Vec<BlsScalar>,
) -> Result<Vec<BlsScalar>, PiecrustError> {
) -> Result<Vec<BlsScalar>, VMError> {
session
.call(
TRANSFER_CONTRACT,
Expand Down
9 changes: 4 additions & 5 deletions contracts/transfer/tests/moonlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use dusk_core::transfer::{
ContractToAccount, ContractToContract, TRANSFER_CONTRACT,
};
use dusk_core::{dusk, JubJubScalar, LUX};
use dusk_vm::{ContractData, Session};
use dusk_vm::{new_genesis_session, new_session, ContractData, Session, VM};

const MOONLIGHT_GENESIS_VALUE: u64 = dusk(1_000.0);
const MOONLIGHT_GENESIS_NONCE: u64 = 0;
Expand Down Expand Up @@ -68,10 +68,9 @@ fn instantiate(moonlight_pk: &AccountPublicKey) -> Session {
"../../../target/dusk/wasm32-unknown-unknown/release/bob.wasm"
);

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 mut session = dusk_vm::new_genesis_session(vm, CHAIN_ID);
let mut session = new_genesis_session(vm, CHAIN_ID);

session
.deploy(
Expand Down Expand Up @@ -126,7 +125,7 @@ fn instantiate(moonlight_pk: &AccountPublicKey) -> Session {
// operations to 1
let base = session.commit().expect("Committing should succeed");
// start a new session from that base-commit
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 genesis state
Expand Down
20 changes: 11 additions & 9 deletions contracts/transfer/tests/phoenix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ use dusk_core::transfer::{
ContractToAccount, ContractToContract, TRANSFER_CONTRACT,
};
use dusk_core::{BlsScalar, JubJubScalar, LUX};
use dusk_vm::{ContractData, PiecrustError, Session};
use dusk_vm::{
new_genesis_session, new_session, ContractData, Error as VMError, Session,
VM,
};
use ff::Field;
use rand::rngs::StdRng;
use rand::{CryptoRng, RngCore, SeedableRng};
Expand Down Expand Up @@ -79,10 +82,9 @@ fn instantiate<const N: u8>(
"../../../target/dusk/wasm32-unknown-unknown/release/bob.wasm"
);

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 mut session = dusk_vm::new_genesis_session(vm, CHAIN_ID);
let mut session = new_genesis_session(vm, CHAIN_ID);

session
.deploy(
Expand Down Expand Up @@ -158,7 +160,7 @@ fn instantiate<const N: u8>(
// operations to 1
let base = session.commit().expect("Committing should succeed");
// start a new session from that base-commit
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 genesis state is correct:
Expand Down Expand Up @@ -1513,7 +1515,7 @@ fn contract_to_account() {
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 @@ -1530,13 +1532,13 @@ fn leaves_from_pos(
.collect())
}

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

fn root(session: &mut Session) -> Result<BlsScalar, PiecrustError> {
fn root(session: &mut Session) -> Result<BlsScalar, VMError> {
session
.call(TRANSFER_CONTRACT, "root", &(), GAS_LIMIT)
.map(|r| r.data)
Expand All @@ -1545,7 +1547,7 @@ fn root(session: &mut Session) -> Result<BlsScalar, PiecrustError> {
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)
Expand Down

0 comments on commit 7e2a026

Please sign in to comment.