Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
fix: subtract IntrinsicGas before handle_message
Browse files Browse the repository at this point in the history
  • Loading branch information
Flouse committed Jun 7, 2022
1 parent 2c6528c commit 159378d
Show file tree
Hide file tree
Showing 19 changed files with 55 additions and 55 deletions.
20 changes: 10 additions & 10 deletions c/polyjuice.h
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ int handle_message(gw_context_t* ctx,
ckb_debug("[handle_message] Don't run evm and return empty data");
res->output_data = NULL;
res->output_size = 0;
res->gas_left = msg.gas - MIN_TRANSACTION_GAS;
res->gas_left = msg.gas;
res->status_code = EVMC_SUCCESS;
}

Expand Down Expand Up @@ -1353,8 +1353,7 @@ int run_polyjuice() {
return ret;
}

//check gas limit
if (msg.gas < MIN_TRANSACTION_GAS) {
if (msg.gas < MIN_TRANSACTION_GAS) { // check gas_limit >= MIN_TRANSACTION_GAS
debug_print_int("Min gas limit is 21000. Insufficient gas limit", msg.gas);
return ERROR_INSUFFICIENT_GAS_LIMIT;
}
Expand All @@ -1374,9 +1373,13 @@ int run_polyjuice() {
uint8_t evm_memory[MAX_EVM_MEMORY_SIZE];
init_evm_memory(evm_memory, MAX_EVM_MEMORY_SIZE);

/* init EVM execution result */
struct evmc_result res;
memset(&res, 0, sizeof(evmc_result));
res.status_code = EVMC_FAILURE; // Generic execution failure
res.status_code = EVMC_FAILURE; // Generic execution failure
debug_print_int("[run_polyjuice] initial gas limit", msg.gas);
int64_t initial_gas = msg.gas;
msg.gas -= MIN_TRANSACTION_GAS; // subtract IntrinsicGas

int ret_handle_message = handle_message(&context, UINT32_MAX, UINT32_MAX, NULL, &msg, &res);
// debug_print evmc_result.output_data if the execution failed
Expand All @@ -1387,13 +1390,10 @@ int run_polyjuice() {
res.output_size > 100 ? 100 : res.output_size);
}

debug_print_int("gas limit", msg.gas);
debug_print_int("gas left", res.gas_left);
debug_print_int("[run_polyjuice] gas left", res.gas_left);
uint64_t gas_used =
(uint64_t)(res.gas_left <= 0 ? msg.gas : msg.gas - res.gas_left);
if (gas_used < MIN_TRANSACTION_GAS) {
gas_used = MIN_TRANSACTION_GAS;
}
(uint64_t)(res.gas_left <= 0 ? initial_gas : initial_gas - res.gas_left);
debug_print_int("[run_polyjuice] gas_used", gas_used);

/* emit POLYJUICE_SYSTEM log to Godwoken */
ret = emit_evm_result_log(&context, gas_used, res.status_code);
Expand Down
2 changes: 1 addition & 1 deletion polyjuice-tests/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ pub fn simple_storage_get(
let block_info = new_block_info(addr, block_number, block_number);
let input = hex::decode("6d4ce63c").unwrap();
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(30000)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
4 changes: 2 additions & 2 deletions polyjuice-tests/src/test_cases/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn test_get_contract_code() {
let block_info = new_block_info(block_producer_id.clone(), block_number, block_number);
let input = hex::decode("c59083f5").expect("createMemoryArray function");
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(30233)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -80,7 +80,7 @@ fn test_get_contract_code() {
let block_info = new_block_info(block_producer_id, block_number, block_number);
let input = hex::decode("ea879634").expect("getCode function");
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(25375)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
6 changes: 3 additions & 3 deletions polyjuice-tests/src/test_cases/address_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn create_address_collision_overwrite() -> Result<()> {
CREATOR_ACCOUNT_ID,
from_id,
INIT_CODE,
122000,
132018,
0,
block_producer_id,
1,
Expand Down Expand Up @@ -91,7 +91,7 @@ fn create_address_collision_duplicate() {
CREATOR_ACCOUNT_ID,
eoa_id,
SS_CODE,
122000,
130000,
0,
block_producer_id.clone(),
1,
Expand All @@ -106,7 +106,7 @@ fn create_address_collision_duplicate() {
CREATOR_ACCOUNT_ID,
from_id,
INIT_CODE,
122000,
130000,
0,
block_producer_id,
1,
Expand Down
10 changes: 5 additions & 5 deletions polyjuice-tests/src/test_cases/contract_call_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn test_contract_call_contract() {
CREATOR_ACCOUNT_ID,
from_id,
SS_INIT_CODE,
122000,
132000,
0,
block_producer.clone(),
block_number,
Expand All @@ -58,7 +58,7 @@ fn test_contract_call_contract() {
CREATOR_ACCOUNT_ID,
from_id,
input.as_str(),
122000,
132000,
0,
block_producer.clone(),
block_number,
Expand Down Expand Up @@ -97,7 +97,7 @@ fn test_contract_call_contract() {
hex::decode("28cc7b2500000000000000000000000000000000000000000000000000000000000000de")
.unwrap(); // 0xde = 222
let args = PolyjuiceArgsBuilder::default()
.gas_limit(51000)
.gas_limit(71000)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -187,7 +187,7 @@ fn test_contract_call_non_exists_contract() {
hex::decode("56c94e70000000000000000000000000ffffffffffffffffffffffffffffffffffffffff")
.unwrap();
let args = PolyjuiceArgsBuilder::default()
.gas_limit(51000)
.gas_limit(73000)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -227,7 +227,7 @@ fn test_contract_call_non_exists_contract() {
let input = hex::decode(format!("56c94e70{}", hex::encode(eoa_ethabi_addr))).unwrap();
println!("{}", hex::encode(&input));
let args = PolyjuiceArgsBuilder::default()
.gas_limit(51000)
.gas_limit(73000)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
4 changes: 2 additions & 2 deletions polyjuice-tests/src/test_cases/contract_create_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn test_contract_create_contract() {
CREATOR_ACCOUNT_ID,
from_id,
INIT_CODE,
122000,
132018,
0,
block_producer_id.clone(),
1,
Expand Down Expand Up @@ -81,7 +81,7 @@ fn test_contract_create_contract() {
let block_info = new_block_info(block_producer_id, 2, 0);
let input = hex::decode("6d4ce63c").unwrap();
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(22030)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
6 changes: 3 additions & 3 deletions polyjuice-tests/src/test_cases/delegatecall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn test_delegatecall() {

let from_eth_address = [1u8; 20];
let (from_id, _from_script_hash) =
helper::create_eth_eoa_account(&mut state, &from_eth_address, 280000u64.into());
helper::create_eth_eoa_account(&mut state, &from_eth_address, 300000u64.into());

// Deploy SimpleStorage
let mut block_number = 1;
Expand All @@ -33,7 +33,7 @@ fn test_delegatecall() {
CREATOR_ACCOUNT_ID,
from_id,
SS_INIT_CODE,
122000,
132000,
0,
block_producer.clone(),
block_number,
Expand All @@ -53,7 +53,7 @@ fn test_delegatecall() {
CREATOR_ACCOUNT_ID,
from_id,
INIT_CODE,
122000,
132000,
0,
block_producer.clone(),
block_number,
Expand Down
2 changes: 1 addition & 1 deletion polyjuice-tests/src/test_cases/ecrecover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn test_ecrecover() {
signed_hash,
)).unwrap();
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(37487)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
6 changes: 3 additions & 3 deletions polyjuice-tests/src/test_cases/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn test_erc20() {

let from_eth_address3 = [3u8; 20];
let (from_id3, _from_script_hash3) =
helper::create_eth_eoa_account(&mut state, &from_eth_address3, 80000u64.into());
helper::create_eth_eoa_account(&mut state, &from_eth_address3, 100000u64.into());

// Deploy ERC20
let run_result = deploy(
Expand All @@ -38,7 +38,7 @@ fn test_erc20() {
CREATOR_ACCOUNT_ID,
from_id1,
INIT_CODE,
122000,
132000,
0,
block_producer_id.clone(),
1,
Expand Down Expand Up @@ -136,7 +136,7 @@ fn test_erc20() {
println!(">> [input]: {}", args_str);
let input = hex::decode(args_str).unwrap();
let args = PolyjuiceArgsBuilder::default()
.gas_limit(80000)
.gas_limit(100000)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
14 changes: 7 additions & 7 deletions polyjuice-tests/src/test_cases/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn test_error_handling() {
helper::CREATOR_ACCOUNT_ID,
from_id,
CONTRACT_CODE,
80000,
96978,
0,
block_producer.to_owned(),
4,
Expand All @@ -51,7 +51,7 @@ fn test_error_handling() {
let block_info = helper::new_block_info(block_producer.to_owned(), block_number, block_number);
let input = hex::decode("2b813bc0").expect("testAssert() method ID");
let args = helper::PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(30000)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -80,7 +80,7 @@ fn test_error_handling() {
let block_info = helper::new_block_info(block_producer.to_owned(), block_number, block_number);
let input = hex::decode("67478cc9").expect("mockAssertPayable() method ID");
let args = helper::PolyjuiceArgsBuilder::default()
.gas_limit(21319)
.gas_limit(30001)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -119,7 +119,7 @@ fn test_error_handling() {
hex::decode("8a8a9b640000000000000000000000000000000000000000000000000000000000000001")
.expect("decode the function selector and arg of testOverflowError(1)");
let args = helper::PolyjuiceArgsBuilder::default()
.gas_limit(21319)
.gas_limit(30002)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -154,7 +154,7 @@ fn test_error_handling() {
hex::decode("b8bd717f0000000000000000000000000000000000000000000000000000000000000009")
.expect("decode the function selector and arg of testRequire(9)");
let args = helper::PolyjuiceArgsBuilder::default()
.gas_limit(21986)
.gas_limit(30003)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -191,7 +191,7 @@ fn test_error_handling() {
hex::decode("209877670000000000000000000000000000000000000000000000000000000000000008")
.expect("decode the function selector and arg of testRevert(8)");
let args = helper::PolyjuiceArgsBuilder::default()
.gas_limit(21921)
.gas_limit(30004)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -226,7 +226,7 @@ fn test_error_handling() {
hex::decode("5729f42c000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000137465737420726576657274206d65737361676500000000000000000000000000")
.expect("decode the function selector and arg of testRevertMsg('test revert message')");
let args = helper::PolyjuiceArgsBuilder::default()
.gas_limit(23264)
.gas_limit(30005)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
4 changes: 2 additions & 2 deletions polyjuice-tests/src/test_cases/fallback_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn test_fallback_function() {
let input = hex::decode(INIT_CODE).unwrap();
let args = PolyjuiceArgsBuilder::default()
.do_create(true)
.gas_limit(22000)
.gas_limit(41144)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -71,7 +71,7 @@ fn test_fallback_function() {
let block_info = new_block_info(block_producer, 2, 0);
let input = hex::decode("3333").unwrap();
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(51144)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
4 changes: 2 additions & 2 deletions polyjuice-tests/src/test_cases/get_block_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn test_get_block_info() {
let input = hex::decode(INIT_CODE).unwrap();
let args = PolyjuiceArgsBuilder::default()
.do_create(true)
.gas_limit(160000)
.gas_limit(144749)
.gas_price(1)
.value(0)
.input(&input)
Expand Down Expand Up @@ -121,7 +121,7 @@ fn test_get_block_info() {
let block_info = new_block_info(block_producer.clone(), block_number + 1, timestamp + 1);
let input = hex::decode(fn_sighash).unwrap();
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(31000)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
2 changes: 1 addition & 1 deletion polyjuice-tests/src/test_cases/get_chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn test_get_chain_id() {
let block_info = new_block_info(block_producer, 3, 0);
let input = hex::decode("6d4ce63c").unwrap();
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(31000)
.gas_price(1)
.value(0)
.input(&input)
Expand Down
6 changes: 3 additions & 3 deletions polyjuice-tests/src/test_cases/parse_log_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn test_parse_log_event() {
CREATOR_ACCOUNT_ID,
from_id,
INIT_CODE,
50000,
22814,
deploy_value,
block_producer.clone(),
block_number,
Expand Down Expand Up @@ -142,7 +142,7 @@ fn test_parse_log_event() {
assert_eq!(&the_from_addr, &address);
// The block producer id is `0`
assert_eq!(&the_to_addr, &block_producer);
assert_eq!(amount, U256::from(21000u64));
assert_eq!(amount, U256::from(22814u64));
} else {
panic!("unexpected polyjuice log");
}
Expand All @@ -155,7 +155,7 @@ fn test_parse_log_event() {
let input = hex::decode("51973ec9").unwrap();
let call_value = 0xac;
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(22839)
.gas_price(1)
.value(call_value)
.input(&input)
Expand Down
2 changes: 1 addition & 1 deletion polyjuice-tests/src/test_cases/receive_ether.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn receive_ether_test() -> anyhow::Result<()> {
//call receive()
let block_info = new_block_info(block_producer, 1, 0);
let args = PolyjuiceArgsBuilder::default()
.gas_limit(21000)
.gas_limit(22806)
.gas_price(1)
.value(1000)
.input(&[])
Expand Down
Loading

0 comments on commit 159378d

Please sign in to comment.