-
Notifications
You must be signed in to change notification settings - Fork 582
/
tests.rs
140 lines (128 loc) · 3.81 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
mod contract_preload;
mod error_cases;
mod invalid_contracts;
mod rs_contract;
mod ts_contract;
use near_primitives::contract::ContractCode;
use crate::{run_vm, VMKind};
use near_primitives::runtime::fees::RuntimeFeesConfig;
use near_primitives::types::CompiledContractCache;
use near_primitives::version::ProtocolVersion;
use near_vm_errors::VMError;
use near_vm_logic::mocks::mock_external::MockedExternal;
use near_vm_logic::{GasCounterMode, VMConfig, VMContext, VMOutcome};
const CURRENT_ACCOUNT_ID: &str = "alice";
const SIGNER_ACCOUNT_ID: &str = "bob";
const SIGNER_ACCOUNT_PK: [u8; 3] = [0, 1, 2];
const PREDECESSOR_ACCOUNT_ID: &str = "carol";
const LATEST_PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion::MAX;
fn with_vm_variants(runner: fn(VMKind) -> ()) {
// #[cfg(feature = "wasmer0_vm")]
runner(VMKind::Wasmer0);
println!("========");
#[cfg(feature = "wasmtime_vm")]
runner(VMKind::Wasmtime);
//
// #[cfg(feature = "wasmer2_vm")]
// runner(VMKind::Wasmer2);
}
fn create_context(input: Vec<u8>) -> VMContext {
VMContext {
current_account_id: CURRENT_ACCOUNT_ID.parse().unwrap(),
signer_account_id: SIGNER_ACCOUNT_ID.parse().unwrap(),
signer_account_pk: Vec::from(&SIGNER_ACCOUNT_PK[..]),
predecessor_account_id: PREDECESSOR_ACCOUNT_ID.parse().unwrap(),
input,
block_index: 10,
block_timestamp: 42,
epoch_height: 1,
account_balance: 2u128,
account_locked_balance: 0,
storage_usage: 12,
attached_deposit: 2u128,
prepaid_gas: 10_u64.pow(14),
random_seed: vec![0, 1, 2],
view_config: None,
output_data_receivers: vec![],
}
}
fn make_simple_contract_call_with_gas_vm(
code: &[u8],
method_name: &str,
prepaid_gas: u64,
vm_kind: VMKind,
gas_counter_mode: GasCounterMode,
) -> (Option<VMOutcome>, Option<VMError>) {
let mut fake_external = MockedExternal::new();
let mut context = create_context(vec![]);
context.prepaid_gas = prepaid_gas;
let config = VMConfig::default();
let fees = RuntimeFeesConfig::default();
let promise_results = vec![];
let code = ContractCode::new(code.to_vec(), None);
run_vm(
&code,
method_name,
&mut fake_external,
context,
&config,
&fees,
&promise_results,
vm_kind,
LATEST_PROTOCOL_VERSION,
None,
gas_counter_mode,
)
}
fn make_simple_contract_call_vm(
code: &[u8],
method_name: &str,
vm_kind: VMKind,
) -> (Option<VMOutcome>, Option<VMError>) {
let with_old_counter = make_simple_contract_call_with_gas_vm(
code,
method_name,
10u64.pow(14),
vm_kind,
GasCounterMode::HostFunction,
);
println!("-------");
let with_new_counter = make_simple_contract_call_with_gas_vm(
code,
method_name,
10u64.pow(14),
vm_kind,
GasCounterMode::Wasm,
);
assert_eq!(with_old_counter, with_new_counter);
with_new_counter
}
fn make_cached_contract_call_vm(
cache: &mut dyn CompiledContractCache,
code: &[u8],
method_name: &str,
prepaid_gas: u64,
vm_kind: VMKind,
gas_counter_mode: GasCounterMode,
) -> (Option<VMOutcome>, Option<VMError>) {
let mut fake_external = MockedExternal::new();
let mut context = create_context(vec![]);
let config = VMConfig::default();
let fees = RuntimeFeesConfig::default();
let promise_results = vec![];
context.prepaid_gas = prepaid_gas;
let code = ContractCode::new(code.to_vec(), None);
run_vm(
&code,
method_name,
&mut fake_external,
context.clone(),
&config,
&fees,
&promise_results,
vm_kind,
LATEST_PROTOCOL_VERSION,
Some(cache),
gas_counter_mode,
)
}