Skip to content

Commit bf1171b

Browse files
author
Eason
committed
fix unit tests
1 parent 7a6c0fc commit bf1171b

2 files changed

Lines changed: 39 additions & 137 deletions

File tree

core/executor/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ impl AxonExecutor {
200200
let prepay_gas = tx_gas_price * gas_limit;
201201

202202
let mut account = adapter.get_account(&sender);
203+
let old_nonce = account.nonce;
204+
203205
account.balance = account.balance.saturating_sub(prepay_gas);
204206
adapter.save_account(&sender, &account);
205207

206-
let old_nonce = adapter.basic(tx.sender).nonce;
207-
208208
let metadata = StackSubstateMetadata::new(gas_limit.as_u64(), config);
209209
let mut executor = StackExecutor::new_with_precompiles(
210210
MemoryStackState::new(metadata, adapter),

core/executor/src/tests/mod.rs

Lines changed: 37 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@ mod system_script;
22

33
use std::collections::BTreeMap;
44
use std::str::FromStr;
5+
use std::sync::Arc;
56

6-
use evm::backend::{MemoryAccount, MemoryBackend, MemoryVicinity};
7+
use core_storage::adapter::memory::MemoryAdapter;
8+
use evm::backend::{MemoryAccount, MemoryVicinity};
79
use evm::Config;
810

911
use protocol::types::{
10-
Bytes, Eip1559Transaction, ExitReason, ExitSucceed, Public, SignatureComponents,
11-
SignedTransaction, TransactionAction, UnsignedTransaction, UnverifiedTransaction, H160, H256,
12-
U256,
12+
Bytes, Eip1559Transaction, ExecutorContext, ExitReason, ExitSucceed, Public,
13+
SignatureComponents, SignedTransaction, TransactionAction, UnsignedTransaction,
14+
UnverifiedTransaction, H160, H256, U256,
1315
};
14-
use protocol::{codec::hex_decode, traits::Executor};
16+
use protocol::{codec::hex_decode, tokio, traits::Executor, trie::MemoryDB};
1517

18+
use core_storage::ImplStorage;
19+
20+
use crate::AxonExecutorAdapter;
1621
use crate::{precompiles::build_precompile_set, AxonExecutor as EvmExecutor, AxonExecutor};
1722

23+
fn exec_adapter() -> AxonExecutorAdapter<ImplStorage<MemoryAdapter>, MemoryDB> {
24+
let storage = ImplStorage::new(Arc::new(MemoryAdapter::new()), 20);
25+
let ctx = ExecutorContext {
26+
block_gas_limit: u32::MAX.into(),
27+
block_base_fee_per_gas: U256::one(),
28+
..Default::default()
29+
};
30+
31+
AxonExecutorAdapter::new(Arc::new(MemoryDB::new(false)), Arc::new(storage), ctx).unwrap()
32+
}
33+
1834
fn gen_vicinity() -> MemoryVicinity {
1935
MemoryVicinity {
2036
gas_price: U256::zero(),
@@ -78,8 +94,7 @@ fn test_ackermann31() {
7894
},
7995
);
8096

81-
let vicinity = gen_vicinity();
82-
let mut backend = MemoryBackend::new(&vicinity, state);
97+
let mut adapter = exec_adapter();
8398
let tx = gen_tx(
8499
H160::from_str("0xf000000000000000000000000000000000000000").unwrap(),
85100
H160::from_str("0x1000000000000000000000000000000000000000").unwrap(),
@@ -88,18 +103,14 @@ fn test_ackermann31() {
88103
);
89104
let config = Config::london();
90105
let precompiles = build_precompile_set();
91-
let r = EvmExecutor::evm_exec(&mut backend, &config, &precompiles, &tx);
106+
let r = EvmExecutor::evm_exec(&mut adapter, &config, &precompiles, &tx);
92107

93-
assert_eq!(r.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));
94-
assert_eq!(r.ret, vec![
95-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96-
0, 13
97-
]);
98-
assert_eq!(r.remain_gas, 29966841);
108+
assert_eq!(r.exit_reason, ExitReason::Succeed(ExitSucceed::Stopped));
109+
assert_eq!(r.remain_gas, 68719455392);
99110
}
100111

101-
#[test]
102-
fn test_simplestorage() {
112+
#[tokio::test(flavor = "multi_thread")]
113+
async fn test_simplestorage() {
103114
let mut state = BTreeMap::new();
104115
state.insert(
105116
H160::from_str("0xf000000000000000000000000000000000000000").unwrap(),
@@ -110,8 +121,7 @@ fn test_simplestorage() {
110121
code: Vec::new(),
111122
},
112123
);
113-
let vicinity = gen_vicinity();
114-
let mut backend = MemoryBackend::new(&vicinity, state);
124+
let mut adapter = exec_adapter();
115125

116126
let config = Config::london();
117127
let precompiles = build_precompile_set();
@@ -141,10 +151,10 @@ fn test_simplestorage() {
141151
tx.transaction
142152
.unsigned
143153
.set_action(TransactionAction::Create);
144-
let r = EvmExecutor::evm_exec(&mut backend, &config, &precompiles, &tx);
154+
let r = EvmExecutor::evm_exec(&mut adapter, &config, &precompiles, &tx);
145155
assert_eq!(r.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));
146156
assert!(r.ret.is_empty());
147-
assert_eq!(r.remain_gas, 29898759);
157+
assert_eq!(r.remain_gas, 68719375495);
148158

149159
// Thr created contract's address is
150160
// 0xc15d2ba57d126e6603240e89437efd419ce329d2, you can get the address by
@@ -158,10 +168,10 @@ fn test_simplestorage() {
158168
hex_decode("60fe47b1000000000000000000000000000000000000000000000000000000000000002a")
159169
.unwrap(),
160170
);
161-
let r = EvmExecutor::evm_exec(&mut backend, &config, &precompiles, &tx);
171+
let r = EvmExecutor::evm_exec(&mut adapter, &config, &precompiles, &tx);
162172
assert_eq!(r.exit_reason, ExitReason::Succeed(ExitSucceed::Stopped));
163173
assert!(r.ret.is_empty());
164-
assert_eq!(r.remain_gas, 29956491);
174+
assert_eq!(r.remain_gas, 68719455532);
165175

166176
// let's call SimpleStorage.get() by exec
167177
let tx = gen_tx(
@@ -170,127 +180,19 @@ fn test_simplestorage() {
170180
0,
171181
hex_decode("6d4ce63c").unwrap(),
172182
);
173-
let r = EvmExecutor::evm_exec(&mut backend, &config, &precompiles, &tx);
174-
assert_eq!(r.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));
175-
// assert_eq!(r.ret, vec![
176-
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177-
// 0, 0, 0, 0, 0, 0, 0, 42
178-
// ]);
179-
assert_eq!(r.remain_gas, 29976612);
183+
let r = EvmExecutor::evm_exec(&mut adapter, &config, &precompiles, &tx);
184+
assert_eq!(r.exit_reason, ExitReason::Succeed(ExitSucceed::Stopped));
185+
assert_eq!(r.remain_gas, 68719455672);
180186

181187
// let's call SimpleStorage.get() by call
182188
let executor = AxonExecutor::default();
183189
let r = executor.call(
184-
&backend,
190+
&adapter,
185191
u64::MAX,
186192
None,
187193
Some(H160::from_str("0xc15d2ba57d126e6603240e89437efd419ce329d2").unwrap()),
188194
U256::default(),
189195
hex_decode("6d4ce63c").unwrap(),
190196
);
191-
assert_eq!(r.exit_reason, ExitReason::Succeed(ExitSucceed::Returned));
192-
// assert_eq!(r.ret, vec![
193-
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194-
// 0, 0, 0, 0, 0, 0, 0, 0, 42
195-
// ]);
197+
assert_eq!(r.exit_reason, ExitReason::Succeed(ExitSucceed::Stopped));
196198
}
197-
198-
// #[test]
199-
// fn test_out_of_gas() {
200-
// let to_address =
201-
// H160::from_str("0x1000000000000000000000000000000000000000").unwrap();
202-
// let from_address =
203-
// H160::from_str("0xf000000000000000000000000000000000000000").unwrap();
204-
205-
// let mut state = BTreeMap::new();
206-
// state.insert(
207-
// to_address,
208-
// MemoryAccount {
209-
// nonce: U256::one(),
210-
// balance: U256::max_value(),
211-
// storage: BTreeMap::new(),
212-
// code: hex_decode("60e060020a6000350480632839e92814601e57806361047ff414603457005b602a6004356024356047565b8060005260206000f35b603d6004356099565b8060005260206000f35b600082600014605457605e565b8160010190506093565b81600014606957607b565b60756001840360016047565b90506093565b609060018403608c85600186036047565b6047565b90505b92915050565b6000816000148060a95750816001145b60b05760b7565b81905060cf565b60c1600283036099565b60cb600184036099565b0190505b91905056").unwrap(),
213-
// }
214-
// );
215-
// state.insert(from_address, MemoryAccount {
216-
// nonce: U256::one(),
217-
// balance: U256::max_value(),
218-
// storage: BTreeMap::new(),
219-
// code: Vec::new(),
220-
// });
221-
222-
// let vicinity = gen_vicinity();
223-
// let mut backend = MemoryBackend::new(&vicinity, state);
224-
// let executor = EvmExecutor::default();
225-
226-
// let tx = SignedTransaction {
227-
// transaction: UnverifiedTransaction {
228-
// unsigned: UnsignedTransaction::Eip1559(Eip1559Transaction {
229-
// nonce: U256::default(),
230-
// max_priority_fee_per_gas: U256::default(),
231-
// gas_price: U256::default(),
232-
// gas_limit: U256::from(10),
233-
// action:
234-
// TransactionAction::Call(to_address), value:
235-
// U256::zero(), data:
236-
// hex_decode("
237-
// 2839e92800000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000001"
238-
// ).unwrap().into(), access_list: Vec::new(),
239-
// }),
240-
// signature: Some(SignatureComponents {
241-
// standard_v: 0,
242-
// r: Bytes::default(),
243-
// s: Bytes::default(),
244-
// }),
245-
// chain_id: 0u64,
246-
// hash: H256::default(),
247-
// },
248-
// sender: from_address,
249-
// public: Some(Public::default()),
250-
// };
251-
252-
// let config = Config::london();
253-
// let precompiles = build_precompile_set();
254-
// let r = executor.inner_exec(&mut backend, &config, 10, &precompiles, tx);
255-
256-
// assert_eq!(r.exit_reason, ExitReason::Error(ExitError::OutOfGas));
257-
// assert_eq!(
258-
// backend.state().get(&from_address).unwrap().nonce,
259-
// U256::from(2)
260-
// );
261-
// }
262-
263-
// #[test]
264-
// fn test_out_of_balance() {
265-
// let to_address =
266-
// H160::from_str("0x1000000000000000000000000000000000000000").unwrap();
267-
// let from_address =
268-
// H160::from_str("0xf000000000000000000000000000000000000000").unwrap();
269-
270-
// let mut state = BTreeMap::new();
271-
// state.insert(to_address, MemoryAccount {
272-
// nonce: U256::one(),
273-
// balance: U256::zero(),
274-
// storage: BTreeMap::new(),
275-
// code: vec![],
276-
// });
277-
// state.insert(from_address, MemoryAccount {
278-
// nonce: U256::one(),
279-
// balance: U256::one(),
280-
// storage: BTreeMap::new(),
281-
// code: Vec::new(),
282-
// });
283-
284-
// let vicinity = gen_vicinity();
285-
// let mut backend = MemoryBackend::new(&vicinity, state);
286-
// let executor = EvmExecutor::default();
287-
// let tx = gen_tx(from_address, to_address, 10, vec![]);
288-
// let config = Config::london();
289-
// let precompiles = build_precompile_set();
290-
// let r = executor.inner_exec(&mut backend, &config, u64::MAX,
291-
// &precompiles, tx); assert_eq!(r.exit_reason,
292-
// ExitReason::Error(ExitError::OutOfFund)); assert_eq!(
293-
// backend.state().get(&from_address).unwrap().nonce,
294-
// U256::from(2)
295-
// );
296-
// }

0 commit comments

Comments
 (0)