|
| 1 | +use std::{env, path::PathBuf, sync::Arc}; |
| 2 | + |
| 3 | +use casper_execution_engine::engine_state::{EngineConfig, ExecutionEngineV1}; |
| 4 | +use casper_executor_wasm::{ |
| 5 | + testing::{ |
| 6 | + base_execute_builder, base_install_request_builder, make_address_generator, |
| 7 | + make_global_state_with_genesis, read_wasm, run_create_contract, run_wasm_session, |
| 8 | + }, |
| 9 | + ExecutorConfigBuilder, ExecutorKind, ExecutorV2, |
| 10 | +}; |
| 11 | + |
| 12 | +use casper_executor_wasm::{chainspec_config, chainspec_config::ChainspecConfig}; |
| 13 | +use casper_executor_wasm_interface::executor::ExecutionKind; |
| 14 | +use casper_storage::global_state::state::CommitProvider; |
| 15 | +use once_cell::sync::Lazy; |
| 16 | + |
| 17 | +/// Symlink to chainspec. |
| 18 | +pub static CHAINSPEC_SYMLINK: Lazy<PathBuf> = Lazy::new(|| { |
| 19 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 20 | + .join("../../resources/local/") |
| 21 | + .join(chainspec_config::CHAINSPEC_NAME) |
| 22 | +}); |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn should_run_test_suite() { |
| 26 | + let chainspec_config = ChainspecConfig::from_chainspec_path(&*CHAINSPEC_SYMLINK) |
| 27 | + .expect("must get chainspec config"); |
| 28 | + let storage_costs = chainspec_config.storage_costs; |
| 29 | + let mint_costs = chainspec_config.system_costs_config.mint_costs().clone(); |
| 30 | + let auction_costs = chainspec_config.system_costs_config.auction_costs().clone(); |
| 31 | + let v1_config = EngineConfig::from(chainspec_config.clone()); |
| 32 | + let execution_engine_v1 = ExecutionEngineV1::new(v1_config); |
| 33 | + let wasm_v2_config = *chainspec_config.wasm_config.v2(); |
| 34 | + let memory_limit = wasm_v2_config.max_memory(); |
| 35 | + let message_limits = chainspec_config.wasm_config.messages_limits(); |
| 36 | + let executor_config = ExecutorConfigBuilder::default() |
| 37 | + .with_memory_limit(memory_limit) |
| 38 | + .with_executor_kind(ExecutorKind::Compiled) |
| 39 | + .with_wasm_config(wasm_v2_config) |
| 40 | + .with_storage_costs(storage_costs) |
| 41 | + .with_mint_costs(mint_costs) |
| 42 | + .with_auction_costs(auction_costs) |
| 43 | + .with_baseline_motes_amount(chainspec_config.core_config.baseline_motes_amount) |
| 44 | + .with_message_limits(message_limits) |
| 45 | + .build() |
| 46 | + .expect("Should build"); |
| 47 | + let mut executor = ExecutorV2::new(executor_config, execution_engine_v1); |
| 48 | + |
| 49 | + let (global_state, mut state_root_hash, _tempdir) = make_global_state_with_genesis(); |
| 50 | + |
| 51 | + let address_generator = make_address_generator(); |
| 52 | + |
| 53 | + let vm2_collections_test = read_wasm("vm2_collections_test.wasm"); |
| 54 | + |
| 55 | + let install_request = base_install_request_builder(&chainspec_config) |
| 56 | + .with_wasm_bytes(vm2_collections_test.wasm) |
| 57 | + .with_bundle_data(vm2_collections_test.meta.expect("should have bundle data")) |
| 58 | + .with_shared_address_generator(Arc::clone(&address_generator)) |
| 59 | + .with_transferred_value(0) |
| 60 | + .with_entry_point("new".to_string()) |
| 61 | + .build() |
| 62 | + .expect("should build"); |
| 63 | + |
| 64 | + let create_result = run_create_contract( |
| 65 | + &mut executor, |
| 66 | + &global_state, |
| 67 | + state_root_hash, |
| 68 | + install_request, |
| 69 | + ); |
| 70 | + |
| 71 | + let contract_address = *create_result.smart_contract_addr(); |
| 72 | + state_root_hash = global_state |
| 73 | + .commit_effects(state_root_hash, create_result.effects().clone()) |
| 74 | + .expect("Should commit"); |
| 75 | + |
| 76 | + let run_method = base_execute_builder(&chainspec_config) |
| 77 | + .with_shared_address_generator(Arc::clone(&address_generator)) |
| 78 | + .with_transferred_value(0) |
| 79 | + .with_execution_kind(ExecutionKind::Stored { |
| 80 | + address: contract_address, |
| 81 | + entry_point: "assertions".to_owned(), |
| 82 | + }) |
| 83 | + .build() |
| 84 | + .expect("should build"); |
| 85 | + let res = run_wasm_session(&mut executor, &global_state, state_root_hash, run_method); |
| 86 | + assert!(res.is_ok()); |
| 87 | + if let Ok(res) = res { |
| 88 | + assert!(res.host_error.is_none()); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[test] |
| 93 | +fn inserting_into_non_existing_vec_index_fails() { |
| 94 | + let chainspec_config = ChainspecConfig::from_chainspec_path(&*CHAINSPEC_SYMLINK) |
| 95 | + .expect("must get chainspec config"); |
| 96 | + let storage_costs = chainspec_config.storage_costs; |
| 97 | + let mint_costs = chainspec_config.system_costs_config.mint_costs().clone(); |
| 98 | + let auction_costs = chainspec_config.system_costs_config.auction_costs().clone(); |
| 99 | + let v1_config = EngineConfig::from(chainspec_config.clone()); |
| 100 | + let execution_engine_v1 = ExecutionEngineV1::new(v1_config); |
| 101 | + let wasm_v2_config = *chainspec_config.wasm_config.v2(); |
| 102 | + let memory_limit = wasm_v2_config.max_memory(); |
| 103 | + let message_limits = chainspec_config.wasm_config.messages_limits(); |
| 104 | + let executor_config = ExecutorConfigBuilder::default() |
| 105 | + .with_memory_limit(memory_limit) |
| 106 | + .with_executor_kind(ExecutorKind::Compiled) |
| 107 | + .with_wasm_config(wasm_v2_config) |
| 108 | + .with_storage_costs(storage_costs) |
| 109 | + .with_mint_costs(mint_costs) |
| 110 | + .with_auction_costs(auction_costs) |
| 111 | + .with_baseline_motes_amount(chainspec_config.core_config.baseline_motes_amount) |
| 112 | + .with_message_limits(message_limits) |
| 113 | + .build() |
| 114 | + .expect("Should build"); |
| 115 | + let mut executor = ExecutorV2::new(executor_config, execution_engine_v1); |
| 116 | + |
| 117 | + let (global_state, mut state_root_hash, _tempdir) = make_global_state_with_genesis(); |
| 118 | + |
| 119 | + let address_generator = make_address_generator(); |
| 120 | + |
| 121 | + let vm2_collections_test = read_wasm("vm2_collections_test.wasm"); |
| 122 | + |
| 123 | + let install_request = base_install_request_builder(&chainspec_config) |
| 124 | + .with_wasm_bytes(vm2_collections_test.wasm) |
| 125 | + .with_bundle_data(vm2_collections_test.meta.expect("should have bundle data")) |
| 126 | + .with_shared_address_generator(Arc::clone(&address_generator)) |
| 127 | + .with_transferred_value(0) |
| 128 | + .with_entry_point("new".to_string()) |
| 129 | + .build() |
| 130 | + .expect("should build"); |
| 131 | + |
| 132 | + let create_result = run_create_contract( |
| 133 | + &mut executor, |
| 134 | + &global_state, |
| 135 | + state_root_hash, |
| 136 | + install_request, |
| 137 | + ); |
| 138 | + |
| 139 | + let contract_address = *create_result.smart_contract_addr(); |
| 140 | + state_root_hash = global_state |
| 141 | + .commit_effects(state_root_hash, create_result.effects().clone()) |
| 142 | + .expect("Should commit"); |
| 143 | + |
| 144 | + let run_method = base_execute_builder(&chainspec_config) |
| 145 | + .with_shared_address_generator(Arc::clone(&address_generator)) |
| 146 | + .with_transferred_value(0) |
| 147 | + .with_execution_kind(ExecutionKind::Stored { |
| 148 | + address: contract_address, |
| 149 | + entry_point: "test_remove_invalid_index_prepare".to_owned(), |
| 150 | + }) |
| 151 | + .build() |
| 152 | + .expect("should build"); |
| 153 | + let res = run_wasm_session(&mut executor, &global_state, state_root_hash, run_method); |
| 154 | + assert!(res.is_ok()); |
| 155 | + |
| 156 | + if let Ok(res) = res { |
| 157 | + assert!(matches!( |
| 158 | + res.host_error, |
| 159 | + Some(casper_executor_wasm_common::error::CallError::NotCallable) |
| 160 | + )); |
| 161 | + } |
| 162 | +} |
0 commit comments