Skip to content

Commit

Permalink
rusk: 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 3d3f83b commit b4690a4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
8 changes: 4 additions & 4 deletions rusk/src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use dusk_core::{
signatures::bls::PublicKey as BlsPublicKey, transfer::phoenix::CoreError,
BlsScalar, Error as ExecErr,
};
use dusk_vm::PiecrustError;
use dusk_vm::Error as VMError;

#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -44,7 +44,7 @@ pub enum Error {
/// Originating from Phoenix.
Phoenix(CoreError),
/// Piecrust VM internal Errors
Vm(PiecrustError),
Vm(VMError),
/// IO Errors
Io(io::Error),
/// Failed to produce proper state
Expand All @@ -70,8 +70,8 @@ impl From<Box<dyn std::error::Error>> for Error {
}
}

impl From<PiecrustError> for Error {
fn from(err: PiecrustError) -> Self {
impl From<VMError> for Error {
fn from(err: VMError) -> Self {
Error::Vm(err)
}
}
Expand Down
26 changes: 8 additions & 18 deletions rusk/src/lib/node/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use dusk_core::transfer::{
TRANSFER_CONTRACT,
};
use dusk_core::{BlsScalar, Dusk};
use dusk_vm::{CallReceipt, PiecrustError, Session};
use dusk_vm::{new_session, CallReceipt, Error as VMError, Session, VM};
use node::vm::bytecode_charge;
use node_data::events::contract::{ContractEvent, ContractTxEvent};
use node_data::ledger::{Hash, Slash, SpentTransaction, Transaction};
Expand Down Expand Up @@ -85,7 +85,7 @@ impl Rusk {
let mut base_commit = [0u8; 32];
base_commit.copy_from_slice(&base_commit_bytes);

let vm = Arc::new(dusk_vm::new_vm(dir)?);
let vm = Arc::new(VM::new(dir)?);

let tip = Arc::new(RwLock::new(RuskTip {
current: base_commit,
Expand Down Expand Up @@ -219,9 +219,7 @@ impl Rusk {
err,
});
}
Err(PiecrustError::Panic(val))
if val == PANIC_NONCE_NOT_READY =>
{
Err(VMError::Panic(val)) if val == PANIC_NONCE_NOT_READY => {
// If the transaction panic due to a not yet valid nonce,
// we should not discard the transactions since it can be
// included in future.
Expand Down Expand Up @@ -537,12 +535,8 @@ impl Rusk {
tip.current
});

let session = dusk_vm::new_session(
&self.vm,
commit,
self.chain_id,
block_height,
)?;
let session =
new_session(&self.vm, commit, self.chain_id, block_height)?;

Ok(session)
}
Expand Down Expand Up @@ -769,7 +763,7 @@ fn execute(
gas_per_deploy_byte: u64,
min_deploy_points: u64,
min_deployment_gas_price: u64,
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, PiecrustError> {
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, VMError> {
// Transaction will be discarded if it is a deployment transaction
// with gas limit smaller than deploy charge.
if let Some(deploy) = tx.deploy() {
Expand All @@ -779,14 +773,10 @@ fn execute(
min_deploy_points,
);
if tx.gas_price() < min_deployment_gas_price {
return Err(PiecrustError::Panic(
"gas price too low to deploy".into(),
));
return Err(VMError::Panic("gas price too low to deploy".into()));
}
if tx.gas_limit() < deploy_charge {
return Err(PiecrustError::Panic(
"not enough gas to deploy".into(),
));
return Err(VMError::Panic("not enough gas to deploy".into()));
}
}

Expand Down
5 changes: 3 additions & 2 deletions rusk/src/lib/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use dusk_core::transfer::{
moonlight::Transaction as MoonlightTransaction,
phoenix::Transaction as PhoenixTransaction,
};
use dusk_vm::host_queries;
use rusk_profile::Circuit as CircuitProfile;

use std::sync::LazyLock;
Expand Down Expand Up @@ -48,7 +49,7 @@ pub fn verify_proof(tx: &PhoenixTransaction) -> Result<bool> {

// Maybe we want to handle internal serialization error too,
// currently they map to `false`.
Ok(dusk_vm::verify_plonk(
Ok(host_queries::verify_plonk(
vd.to_vec(),
tx.proof().to_vec(),
tx.public_inputs(),
Expand All @@ -57,7 +58,7 @@ pub fn verify_proof(tx: &PhoenixTransaction) -> Result<bool> {

/// Verifies the signature of the incoming transaction.
pub fn verify_signature(tx: &MoonlightTransaction) -> Result<bool> {
Ok(dusk_vm::verify_bls(
Ok(host_queries::verify_bls(
tx.signature_message(),
*tx.sender(),
*tx.signature(),
Expand Down
16 changes: 8 additions & 8 deletions rusk/tests/services/contract_deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use dusk_core::abi::ContractId;
use dusk_core::transfer::data::{
ContractBytecode, ContractDeploy, TransactionData,
};
use dusk_vm::{ContractData, PiecrustError};
use dusk_vm::{new_session, ContractData, Error as VMError, VM};
use rand::prelude::*;
use rand::rngs::StdRng;
use rusk::gen_id::gen_contract_id;
Expand Down Expand Up @@ -230,9 +230,9 @@ impl Fixture {

pub fn assert_bob_contract_is_not_deployed(&self) {
let commit = self.rusk.state_root();
let vm = dusk_vm::new_vm(self.path.as_path())
.expect("VM creation should succeed");
let mut session = dusk_vm::new_session(&vm, commit, CHAIN_ID, 0)
let vm =
VM::new(self.path.as_path()).expect("VM creation should succeed");
let mut session = new_session(&vm, commit, CHAIN_ID, 0)
.expect("Session creation should succeed");
let result = session.call::<_, u64>(
self.contract_id,
Expand All @@ -241,16 +241,16 @@ impl Fixture {
u64::MAX,
);
match result.err() {
Some(PiecrustError::ContractDoesNotExist(_)) => (),
Some(VMError::ContractDoesNotExist(_)) => (),
_ => assert!(false),
}
}

pub fn assert_bob_contract_is_deployed(&self) {
let commit = self.rusk.state_root();
let vm = dusk_vm::new_vm(self.path.as_path())
.expect("VM creation should succeed");
let mut session = dusk_vm::new_session(&vm, commit, CHAIN_ID, 0)
let vm =
VM::new(self.path.as_path()).expect("VM creation should succeed");
let mut session = new_session(&vm, commit, CHAIN_ID, 0)
.expect("Session creation should succeed");
let result = session.call::<_, u64>(
self.contract_id,
Expand Down
14 changes: 7 additions & 7 deletions rusk/tests/services/owner_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use dusk_core::signatures::bls::{
PublicKey as BlsPublicKey, SecretKey as BlsSecretKey,
Signature as BlsSignature,
};
use dusk_vm::{CallReceipt, ContractData, Session};
use dusk_vm::{new_session, CallReceipt, ContractData, Session, VM};
use rusk::gen_id::gen_contract_id;
use rusk::{Error, Result, Rusk};
use rusk_recovery_tools::state;
Expand Down Expand Up @@ -144,9 +144,9 @@ impl Fixture {
pub fn assert_bob_contract_is_deployed(&self) {
const BOB_ECHO_VALUE: u64 = 775;
let commit = self.rusk.state_root();
let vm = dusk_vm::new_vm(self.path.as_path())
.expect("VM creation should succeed");
let mut session = dusk_vm::new_session(&vm, commit, CHAIN_ID, 0)
let vm =
VM::new(self.path.as_path()).expect("VM creation should succeed");
let mut session = new_session(&vm, commit, CHAIN_ID, 0)
.expect("Session creation should succeed");
let result = session.call::<_, u64>(
self.contract_id,
Expand All @@ -168,10 +168,10 @@ impl Fixture {

pub fn set_session(&mut self) {
let commit = self.rusk.state_root();
let vm = dusk_vm::new_vm(self.path.as_path())
.expect("VM creation should succeed");
let vm =
VM::new(self.path.as_path()).expect("VM creation should succeed");
self.session = Some(
dusk_vm::new_session(&vm, commit, CHAIN_ID, 0)
new_session(&vm, commit, CHAIN_ID, 0)
.expect("Session creation should succeed"),
);
}
Expand Down
2 changes: 1 addition & 1 deletion rusk/tests/services/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn wallet_transfer(
info!("Tx: {}", hex::encode(tx.to_var_bytes()));

let tx_hash_input_bytes = tx.to_hash_input_bytes();
let tx_id = dusk_vm::hash(tx_hash_input_bytes);
let tx_id = dusk_vm::host_queries::hash(tx_hash_input_bytes);

info!("Tx ID: {}", hex::encode(tx_id.to_bytes()));
let txs: Vec<SpentTransaction> = generator_procedure(
Expand Down

0 comments on commit b4690a4

Please sign in to comment.