Skip to content

Commit f8b0ece

Browse files
committed
tests(js): add test op_gascost
Signed-off-by: jsvisa <[email protected]>
1 parent 3dd960e commit f8b0ece

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

tests/it/geth_js.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,101 @@ fn test_geth_jstracer_revert() {
7070
// reverted operation
7171
assert!(result["error"].as_bool().unwrap());
7272
}
73+
74+
#[test]
75+
fn test_geth_jstracer_op_gascost() {
76+
/*
77+
pragma solidity ^0.8.13;
78+
79+
contract Foo {
80+
event Log(address indexed addr, uint256 value);
81+
82+
function foo() external {
83+
emit Log(msg.sender, 0);
84+
}
85+
86+
function bar() external {
87+
emit Log(msg.sender, 0);
88+
require(false, "barbarbar");
89+
}
90+
}
91+
*/
92+
93+
let code = hex!("608060405261023e806100115f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c8063c298557814610038578063febb0f7e14610042575b5f80fd5b61004061004c565b005b61004a61009c565b005b3373ffffffffffffffffffffffffffffffffffffffff167ff950957d2407bed19dc99b718b46b4ce6090c05589006dfb86fd22c34865b23e5f6040516100929190610177565b60405180910390a2565b3373ffffffffffffffffffffffffffffffffffffffff167ff950957d2407bed19dc99b718b46b4ce6090c05589006dfb86fd22c34865b23e5f6040516100e29190610177565b60405180910390a25f61012a576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610121906101ea565b60405180910390fd5b565b5f819050919050565b5f819050919050565b5f819050919050565b5f61016161015c6101578461012c565b61013e565b610135565b9050919050565b61017181610147565b82525050565b5f60208201905061018a5f830184610168565b92915050565b5f82825260208201905092915050565b7f62617262617262617200000000000000000000000000000000000000000000005f82015250565b5f6101d4600983610190565b91506101df826101a0565b602082019050919050565b5f6020820190508181035f830152610201816101c8565b905091905056fea2646970667358221220e058dc2c4bd629d62405850cc8e08e6bfad0eea187260784445dfe8f3ee0bea564736f6c634300081a0033");
94+
95+
let (addr, mut evm) = deploy_contract(code.into(), Address::ZERO, SpecId::CANCUN);
96+
97+
let code = r#"
98+
{
99+
data: [],
100+
memoryInstructions: { "MSTORE": "W", "MSTORE8": "B", "MLOAD": "R" },
101+
fault: function (_) {},
102+
step: function (log) {
103+
let op = log.op.toString();
104+
let instructions = this.memoryInstructions;
105+
if (Object.keys(instructions).includes(op)) {
106+
this.data.push({
107+
op: instructions[op],
108+
depth: log.getDepth(),
109+
offset: log.stack.peek(0),
110+
gasCost: log.getCost(),
111+
memorySize: log.memory.length(),
112+
});
113+
}
114+
},
115+
result: function (ctx, _) { return { error: !!ctx.error, data: this.data }; }
116+
}
117+
"#;
118+
119+
// test with normal operation
120+
let env = evm.env_with_tx(TxEnv {
121+
transact_to: TransactTo::Call(addr),
122+
data: hex!("c2985578").into(), // call foo
123+
..Default::default()
124+
});
125+
let mut insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap();
126+
let (res, _) = inspect(&mut evm.db, env.clone(), &mut insp).unwrap();
127+
assert!(res.result.is_success());
128+
129+
let result = insp.json_result(res, &env, &evm.db).unwrap();
130+
131+
assert!(!result["error"].as_bool().unwrap());
132+
assert_eq!(
133+
result["data"],
134+
serde_json::json!([
135+
{ "op": "W", "depth": 1, "offset": "64", "gasCost": 12, "memorySize": 0 },
136+
{ "op": "R", "depth": 1, "offset": "64", "gasCost": 3, "memorySize": 96 },
137+
{ "op": "W", "depth": 1, "offset": "128", "gasCost": 9, "memorySize": 96 },
138+
{ "op": "R", "depth": 1, "offset": "64", "gasCost": 3, "memorySize": 160 }
139+
])
140+
);
141+
142+
// test with reverted operation
143+
let env = evm.env_with_tx(TxEnv {
144+
transact_to: TransactTo::Call(addr),
145+
data: hex!("febb0f7e").into(), // call bar
146+
..Default::default()
147+
});
148+
let mut insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap();
149+
let (res, _) = inspect(&mut evm.db, env.clone(), &mut insp).unwrap();
150+
assert!(!res.result.is_success());
151+
152+
let result = insp.json_result(res, &env, &evm.db).unwrap();
153+
154+
assert!(result["error"].as_bool().unwrap());
155+
assert_eq!(
156+
result["data"],
157+
serde_json::json!([
158+
{ "op": "W", "depth": 1, "offset": "64", "gasCost": 12, "memorySize": 0 },
159+
{ "op": "R", "depth": 1, "offset": "64", "gasCost": 3, "memorySize": 96 },
160+
{ "op": "W", "depth": 1, "offset": "128", "gasCost": 9, "memorySize": 96 },
161+
{ "op": "R", "depth": 1, "offset": "64", "gasCost": 3, "memorySize": 160 },
162+
{ "op": "R", "depth": 1, "offset": "64", "gasCost": 3, "memorySize": 160 },
163+
{ "op": "W", "depth": 1, "offset": "128", "gasCost": 3, "memorySize": 160 },
164+
{ "op": "W", "depth": 1, "offset": "132", "gasCost": 6, "memorySize": 160 },
165+
{ "op": "W", "depth": 1, "offset": "164", "gasCost": 6, "memorySize": 192 },
166+
{ "op": "W", "depth": 1, "offset": "196", "gasCost": 6, "memorySize": 224 },
167+
{ "op": "R", "depth": 1, "offset": "64", "gasCost": 3, "memorySize": 256 }
168+
])
169+
);
170+
}

0 commit comments

Comments
 (0)