Skip to content
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions executor/wasm/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ use casper_execution_engine::engine_state::{EngineConfig, ExecutionEngineV1};
use casper_executor_wasm_interface::{
executor::{
ExecuteRequest, ExecuteRequestBuilder, ExecuteWithProviderError, ExecuteWithProviderResult,
Executor,
},
install::{
InstallContractError, InstallContractRequest, InstallContractRequestBuilder,
InstallContractResult, InstallContractWithProviderResult,
InstallContractWithProviderResult,
},
};
use casper_storage::{
Expand Down
162 changes: 162 additions & 0 deletions executor/wasm/tests/collections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use std::{env, path::PathBuf, sync::Arc};

use casper_execution_engine::engine_state::{EngineConfig, ExecutionEngineV1};
use casper_executor_wasm::{
testing::{
base_execute_builder, base_install_request_builder, make_address_generator,
make_global_state_with_genesis, read_wasm, run_create_contract, run_wasm_session,
},
ExecutorConfigBuilder, ExecutorKind, ExecutorV2,
};

use casper_executor_wasm::{chainspec_config, chainspec_config::ChainspecConfig};
use casper_executor_wasm_interface::executor::ExecutionKind;
use casper_storage::global_state::state::CommitProvider;
use once_cell::sync::Lazy;

/// Symlink to chainspec.
pub static CHAINSPEC_SYMLINK: Lazy<PathBuf> = Lazy::new(|| {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../resources/local/")
.join(chainspec_config::CHAINSPEC_NAME)
});

#[test]
fn should_run_test_suite() {
let chainspec_config = ChainspecConfig::from_chainspec_path(&*CHAINSPEC_SYMLINK)
.expect("must get chainspec config");
let storage_costs = chainspec_config.storage_costs;
let mint_costs = chainspec_config.system_costs_config.mint_costs().clone();
let auction_costs = chainspec_config.system_costs_config.auction_costs().clone();
let v1_config = EngineConfig::from(chainspec_config.clone());
let execution_engine_v1 = ExecutionEngineV1::new(v1_config);
let wasm_v2_config = *chainspec_config.wasm_config.v2();
let memory_limit = wasm_v2_config.max_memory();
let message_limits = chainspec_config.wasm_config.messages_limits();
let executor_config = ExecutorConfigBuilder::default()
.with_memory_limit(memory_limit)
.with_executor_kind(ExecutorKind::Compiled)
.with_wasm_config(wasm_v2_config)
.with_storage_costs(storage_costs)
.with_mint_costs(mint_costs)
.with_auction_costs(auction_costs)
.with_baseline_motes_amount(chainspec_config.core_config.baseline_motes_amount)
.with_message_limits(message_limits)
.build()
.expect("Should build");
let mut executor = ExecutorV2::new(executor_config, execution_engine_v1);

let (global_state, mut state_root_hash, _tempdir) = make_global_state_with_genesis();

let address_generator = make_address_generator();

let vm2_collections_test = read_wasm("vm2_collections_test.wasm");

let install_request = base_install_request_builder(&chainspec_config)
.with_wasm_bytes(vm2_collections_test.wasm)
.with_bundle_data(vm2_collections_test.meta.expect("should have bundle data"))
.with_shared_address_generator(Arc::clone(&address_generator))
.with_transferred_value(0)
.with_entry_point("new".to_string())
.build()
.expect("should build");

let create_result = run_create_contract(
&mut executor,
&global_state,
state_root_hash,
install_request,
);

let contract_address = *create_result.smart_contract_addr();
state_root_hash = global_state
.commit_effects(state_root_hash, create_result.effects().clone())
.expect("Should commit");

let run_method = base_execute_builder(&chainspec_config)
.with_shared_address_generator(Arc::clone(&address_generator))
.with_transferred_value(0)
.with_execution_kind(ExecutionKind::Stored {
address: contract_address,
entry_point: "assertions".to_owned(),
})
.build()
.expect("should build");
let res = run_wasm_session(&mut executor, &global_state, state_root_hash, run_method);
assert!(res.is_ok());
if let Ok(res) = res {
assert!(res.host_error.is_none());
}
}

#[test]
fn inserting_into_non_existing_vec_index_fails() {
let chainspec_config = ChainspecConfig::from_chainspec_path(&*CHAINSPEC_SYMLINK)
.expect("must get chainspec config");
let storage_costs = chainspec_config.storage_costs;
let mint_costs = chainspec_config.system_costs_config.mint_costs().clone();
let auction_costs = chainspec_config.system_costs_config.auction_costs().clone();
let v1_config = EngineConfig::from(chainspec_config.clone());
let execution_engine_v1 = ExecutionEngineV1::new(v1_config);
let wasm_v2_config = *chainspec_config.wasm_config.v2();
let memory_limit = wasm_v2_config.max_memory();
let message_limits = chainspec_config.wasm_config.messages_limits();
let executor_config = ExecutorConfigBuilder::default()
.with_memory_limit(memory_limit)
.with_executor_kind(ExecutorKind::Compiled)
.with_wasm_config(wasm_v2_config)
.with_storage_costs(storage_costs)
.with_mint_costs(mint_costs)
.with_auction_costs(auction_costs)
.with_baseline_motes_amount(chainspec_config.core_config.baseline_motes_amount)
.with_message_limits(message_limits)
.build()
.expect("Should build");
let mut executor = ExecutorV2::new(executor_config, execution_engine_v1);

let (global_state, mut state_root_hash, _tempdir) = make_global_state_with_genesis();

let address_generator = make_address_generator();

let vm2_collections_test = read_wasm("vm2_collections_test.wasm");

let install_request = base_install_request_builder(&chainspec_config)
.with_wasm_bytes(vm2_collections_test.wasm)
.with_bundle_data(vm2_collections_test.meta.expect("should have bundle data"))
.with_shared_address_generator(Arc::clone(&address_generator))
.with_transferred_value(0)
.with_entry_point("new".to_string())
.build()
.expect("should build");

let create_result = run_create_contract(
&mut executor,
&global_state,
state_root_hash,
install_request,
);

let contract_address = *create_result.smart_contract_addr();
state_root_hash = global_state
.commit_effects(state_root_hash, create_result.effects().clone())
.expect("Should commit");

let run_method = base_execute_builder(&chainspec_config)
.with_shared_address_generator(Arc::clone(&address_generator))
.with_transferred_value(0)
.with_execution_kind(ExecutionKind::Stored {
address: contract_address,
entry_point: "test_remove_invalid_index_prepare".to_owned(),
})
.build()
.expect("should build");
let res = run_wasm_session(&mut executor, &global_state, state_root_hash, run_method);
assert!(res.is_ok());

if let Ok(res) = res {
assert!(matches!(
res.host_error,
Some(casper_executor_wasm_common::error::CallError::NotCallable)
));
}
}
2 changes: 2 additions & 0 deletions executor/wasm_host/src/host/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ pub(crate) fn host_env_balance<S: GlobalStateReader + 'static>(
))
}

/// Please note that the returning EnvInfo structure
/// cannot hold any data that can change in course of one wasm execution.
pub(crate) fn host_env_info<S: GlobalStateReader + 'static>(
caller: &mut impl Caller<Context = Context<S>>,
) -> VMResult<(Option<Bytes>, u32)> {
Expand Down
66 changes: 25 additions & 41 deletions smart_contracts/contracts/vm2/vm2-cep18/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,15 @@ impl Burnable for TokenContract {}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use super::*;

use casper_contract_sdk::{
abi::collector::ABI_ITEMS,
casper::{
self,
native::{
current_environment, dispatch_with, with_current_environment, Environment,
DEFAULT_ADDRESS,
},
native::{with_env, EnvironmentMock, DEFAULT_ADDRESS},
Entity,
},
ContractHandle, ToCallData,
Expand All @@ -99,10 +98,10 @@ mod tests {
const BOB: Entity = Entity::Account([2; 32]);

#[test]
#[ignore = "TODO this should be a real e2e test wiring real storage and ffi"]
fn it_works() {
let stub = Environment::new(Default::default(), DEFAULT_ADDRESS);

let result = casper::native::dispatch_with(stub, || {
let env = Arc::new(EnvironmentMock::new());
with_env(env.clone(), || {
let mut contract = TokenContract::new("Foo Token".to_string());

assert_eq!(contract.require_any_role(&[ADMIN_ROLE]), Ok(()));
Expand All @@ -127,14 +126,13 @@ mod tests {
);
assert_eq!(contract.transfer(ALICE, U256::from(10_000u64)), Ok(()));
});
assert!(matches!(result, Ok(())));
}

#[test]
#[ignore = "TODO this should be a real e2e test wiring real storage and ffi"]
fn e2e() {
// let db = casper::native::Container::default();
// let env = Environment::new(db.clone(), DEFAULT_ADDRESS);
let result = casper::native::dispatch(move || {
let env = Arc::new(EnvironmentMock::new());
with_env(env.clone(), || {
assert_eq!(casper::get_caller(), DEFAULT_ADDRESS);

let constructor = TokenContractRef::new("Foo Token".to_string());
Expand All @@ -150,15 +148,8 @@ mod tests {
)
.expect("Should create");

let new_env = with_current_environment(|env| env);
let new_env = new_env.smart_contract(Entity::Contract(create_result.contract_address));
dispatch_with(new_env, || {
// This is the caller of the contract
casper::read_contract_state::<TokenContract>().unwrap();
})
.unwrap();

// assert_eq!(casper::get_caller(), DEFAULT_ADDRESS);
let new_env = Arc::new(EnvironmentMock::new());
casper::read_contract_state::<TokenContract>().unwrap();

let cep18_handle =
ContractHandle::<TokenContractRef>::from_address(create_result.contract_address);
Expand Down Expand Up @@ -231,25 +222,20 @@ mod tests {
);
assert_eq!(casper::get_caller(), DEFAULT_ADDRESS);

let alice_env = current_environment().session(ALICE);

casper::native::dispatch_with(alice_env, || {
assert_eq!(casper::get_caller(), ALICE);
assert_eq!(
cep18_handle
.call(|cep18| cep18.my_balance())
.expect("Should call"),
U256::from(1000u64)
);
assert_eq!(
cep18_handle
.build_call()
.call(|cep18| cep18.transfer(BOB, U256::from(1u64)))
.expect("Should call"),
Ok(())
);
})
.expect("Success");
assert_eq!(casper::get_caller(), ALICE);
assert_eq!(
cep18_handle
.call(|cep18| cep18.my_balance())
.expect("Should call"),
U256::from(1000u64)
);
assert_eq!(
cep18_handle
.build_call()
.call(|cep18| cep18.transfer(BOB, U256::from(1u64)))
.expect("Should call"),
Ok(())
);

let bob_balance = cep18_handle
.build_call()
Expand All @@ -263,8 +249,6 @@ mod tests {
.expect("Should call");
assert_eq!(alice_balance, U256::from(999u64));
});

assert!(matches!(result, Ok(())));
}

#[test]
Expand Down
15 changes: 15 additions & 0 deletions smart_contracts/contracts/vm2/vm2-collections-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Test contract proving functionality of vm2 collections
[package]
name = "vm2-collections-test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
const-fnv1a-hash = "1.1.0"
casper-executor-wasm-common = { path = "../../../../executor/wasm_common" }
borsh = { version = "1.5", features = ["derive"] }
casper-contract-sdk = { path = "../../../vm2/sdk", features = ["testing"] }
7 changes: 7 additions & 0 deletions smart_contracts/contracts/vm2/vm2-collections-test/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
// Check if target arch is wasm32 and set link flags accordingly
if std::env::var("TARGET").unwrap() == "wasm32-unknown-unknown" {
println!("cargo:rustc-link-arg=--import-memory");
println!("cargo:rustc-link-arg=--export-table");
}
}
Loading