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

feat: execute call implemented #92

Merged
merged 5 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ description = "MoveVM support pallet"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
anyhow = { version = "1.0", default-features = false }
bcs = { git = "https://github.com/eigerco/bcs.git", default-features = false, branch = "master" }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive",] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
serde = { version = "1.0", default-features = false, features = ["derive"] }
sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v1.0.0' }

# MoveVM dependencies
Expand Down
19 changes: 17 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod benchmarking;

mod storage;

pub mod transaction;

pub mod weights;
pub use weights::*;

Expand All @@ -30,6 +32,7 @@ pub mod pallet {
use move_vm_backend::Mvm;
use move_vm_types::gas::UnmeteredGasMeter;
use sp_std::{default::Default, vec::Vec};
use transaction::Transaction;

use super::*;
use crate::storage::MoveVmStorage;
Expand Down Expand Up @@ -80,13 +83,25 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::execute())]
pub fn execute(
origin: OriginFor<T>,
_bytecode: Vec<u8>,
transaction_bc: Vec<u8>,
_gas_limit: u64,
) -> DispatchResult {
// Allow only signed calls.
let who = ensure_signed(origin)?;

// TODO: Execute bytecode
let storage = Self::move_vm_storage();
let vm = Mvm::new(storage).map_err(|_err| Error::<T>::ExecuteFailed)?;

let transaction = Transaction::try_from(transaction_bc.as_slice())
.map_err(|_| Error::<T>::ExecuteFailed)?;

vm.execute_script(
transaction.script_bc.as_slice(),
transaction.type_args,
transaction.args.iter().map(|x| x.as_slice()).collect(),
&mut UnmeteredGasMeter, // TODO(asmie): gas handling
)
.map_err(|_err| Error::<T>::ExecuteFailed)?;

// Emit an event.
Self::deposit_event(Event::ExecuteCalled { who });
Expand Down
26 changes: 26 additions & 0 deletions src/transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use core::convert::TryFrom;

//TODO: either leave it as is when moving to move-vm-backend (then remove this comment) or
// change it to crate error type
use anyhow::Error;
35359595 marked this conversation as resolved.
Show resolved Hide resolved
use frame_support::dispatch::Vec;
use move_core_types::language_storage::TypeTag;
use serde::{Deserialize, Serialize};

/// Transaction representation used in execute call.
#[derive(Serialize, Deserialize, Debug)]
pub struct Transaction {
/// Script bytecode.
pub script_bc: Vec<u8>,
/// Script args.
pub args: Vec<Vec<u8>>,
/// Script type arguments.
pub type_args: Vec<TypeTag>,
}

impl TryFrom<&[u8]> for Transaction {
type Error = Error;
fn try_from(blob: &[u8]) -> Result<Self, Self::Error> {
bcs::from_bytes(blob).map_err(Error::msg)
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/assets/move/scripts/Generics.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
script {
fun generic_1<T: copy + drop>(x: T) {
let _y = x;
}
}
17 changes: 17 additions & 0 deletions tests/assets/move/scripts/Loops.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
script {
fun empty_loop() {
let iterations: u64 = 10;

while (iterations > 0) {
iterations = iterations - 1;
}
}
}

script {
fun empty_loop_param(iterations: u64) {
while (iterations > 0) {
iterations = iterations - 1;
}
}
}
94 changes: 90 additions & 4 deletions tests/execute.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,98 @@
mod mock;
use frame_support::assert_ok;
use mock::*;
use move_core_types::language_storage::TypeTag;
use pallet_move::transaction::Transaction;

#[test]
#[ignore = "to be implemented"]
/// Test execution of a script with correct parameters.
fn execute_script_correct() {
/// Test execution of a script.
fn execute_script_empty() {
new_test_ext().execute_with(|| {
assert_eq!(1, 0);
let module =
include_bytes!("assets/move/build/move/bytecode_scripts/empty_scr.mv").to_vec();

let type_args: Vec<TypeTag> = vec![];
let params: Vec<&[u8]> = vec![];

let transaction = Transaction {
script_bc: module.clone(),
type_args,
args: params.iter().map(|x| x.to_vec()).collect(),
};

let transaction_bc = bcs::to_bytes(&transaction).unwrap();

let res = MoveModule::execute(RuntimeOrigin::signed(0xFECA000000000000), transaction_bc, 0);

assert_ok!(res);

let module =
include_bytes!("assets/move/build/move/bytecode_scripts/empty_loop.mv").to_vec();

let type_args: Vec<TypeTag> = vec![];
let params: Vec<&[u8]> = vec![];

let transaction = Transaction {
script_bc: module.clone(),
type_args,
args: params.iter().map(|x| x.to_vec()).collect(),
};

let transaction_bc = bcs::to_bytes(&transaction).unwrap();

let res = MoveModule::execute(RuntimeOrigin::signed(0xFECA000000000000), transaction_bc, 0);

assert_ok!(res);
});
}

#[test]
/// Test execution of a script with parametrized function.
fn execute_script_params() {
new_test_ext().execute_with(|| {
let module =
include_bytes!("assets/move/build/move/bytecode_scripts/empty_loop_param.mv").to_vec();

let iter_count = bcs::to_bytes(&10u64).unwrap();
let type_args: Vec<TypeTag> = vec![];
let params: Vec<&[u8]> = vec![&iter_count];

let transaction = Transaction {
script_bc: module.clone(),
type_args,
args: params.iter().map(|x| x.to_vec()).collect(),
};

let transaction_bc = bcs::to_bytes(&transaction).unwrap();

let res = MoveModule::execute(RuntimeOrigin::signed(0xFECA000000000000), transaction_bc, 0);

assert_ok!(res);
});
}

#[test]
/// Test execution of a script with generic function.
fn execute_script_generic() {
new_test_ext().execute_with(|| {
let module =
include_bytes!("assets/move/build/move/bytecode_scripts/generic_1.mv").to_vec();

let param = bcs::to_bytes(&100u64).unwrap();
let type_args: Vec<TypeTag> = vec![TypeTag::U64];
let params: Vec<&[u8]> = vec![&param];

let transaction = Transaction {
script_bc: module.clone(),
type_args,
args: params.iter().map(|x| x.to_vec()).collect(),
};

let transaction_bc = bcs::to_bytes(&transaction).unwrap();

let res = MoveModule::execute(RuntimeOrigin::signed(0xFECA000000000000), transaction_bc, 0);

assert_ok!(res);
});
}

Expand Down
Loading