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

Commit

Permalink
Merge pull request #165 from magicalne/fix_gas_price
Browse files Browse the repository at this point in the history
fix: gas price was hard-coded to 1
  • Loading branch information
Flouse authored Jun 19, 2022
2 parents ae65ef5 + d95b40f commit fef5a48
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 13 deletions.
24 changes: 13 additions & 11 deletions c/polyjuice.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ int load_account_script(gw_context_t* gw_ctx, uint32_t account_id,
input_data : [u8; input_size]
]
*/
int parse_args(struct evmc_message* msg, uint128_t* gas_price,
gw_context_t* ctx) {
int parse_args(struct evmc_message* msg, gw_context_t* ctx) {
gw_transaction_context_t *tx_ctx = &ctx->transaction_context;
debug_print_int("args_len", tx_ctx->args_len);
if (tx_ctx->args_len < (8 + 8 + 16 + 16 + 4)) {
Expand Down Expand Up @@ -176,9 +175,9 @@ int parse_args(struct evmc_message* msg, uint128_t* gas_price,
debug_print_int("[gas_limit]", gas_limit);

/* args[16..32] gas price */
memcpy(gas_price, args + offset, sizeof(uint128_t));
memcpy(&g_gas_price, args + offset, sizeof(uint128_t));
offset += 16;
debug_print_int("[gas_price]", (int64_t)(*gas_price));
debug_print_int("[gas_price]", (int64_t)(g_gas_price));

/* args[32..48] transfer value */
evmc_uint256be value{0};
Expand Down Expand Up @@ -310,9 +309,12 @@ int load_account_code(gw_context_t* gw_ctx, uint32_t account_id,
struct evmc_tx_context get_tx_context(struct evmc_host_context* context) {
struct evmc_tx_context ctx{0};
memcpy(ctx.tx_origin.bytes, g_tx_origin.bytes, 20);

/* gas price = 1 */
ctx.tx_gas_price.bytes[31] = 0x01;
evmc_uint256be gas_price = {0};
uint8_t* gas_price_ptr = (uint8_t*)(&g_gas_price);
for (int i = 0; i < 16; i++) {
gas_price.bytes[31 - i] = *(gas_price_ptr + i);
}
ctx.tx_gas_price = gas_price;

ctx.block_number = context->gw_ctx->block_info.number;
/*
Expand Down Expand Up @@ -625,7 +627,8 @@ struct evmc_result call(struct evmc_host_context* context,
}
res.status_code = EVMC_SUCCESS;
} else {
ret = handle_message(gw_ctx, context->from_id, context->to_id, &context->destination, msg, &res);
ret = handle_message(gw_ctx, context->from_id, context->to_id,
&context->destination, msg, &res);
if (is_fatal_error(ret)) {
/* stop as soon as possible */
context->error_code = ret;
Expand Down Expand Up @@ -1347,10 +1350,9 @@ int run_polyjuice() {
}

evmc_message msg;
uint128_t gas_price;
/* Parse message */
ckb_debug("BEGIN parse_message()");
ret = parse_args(&msg, &gas_price, &context);
ret = parse_args(&msg, &context);
ckb_debug("END parse_message()");
if (ret != 0) {
return ret;
Expand Down Expand Up @@ -1435,7 +1437,7 @@ int run_polyjuice() {
ckb_debug("gas not enough");
return clean_evmc_result_and_return(&res, -1);
}
uint256_t fee_u256 = calculate_fee(gas_price, gas_used);
uint256_t fee_u256 = calculate_fee(g_gas_price, gas_used);
gw_reg_addr_t sender_addr = new_reg_addr(msg.sender.bytes);
ret = sudt_pay_fee(&context, g_sudt_id, /* g_sudt_id must already exists */
sender_addr, fee_u256);
Expand Down
4 changes: 3 additions & 1 deletion c/polyjuice_globals.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef POLYJUICE_GLOBALS_H
#define POLYJUICE_GLOBALS_H

#define POLYJUICE_VERSION "v1.2.0"
#define POLYJUICE_VERSION "v1.3.0"

#define ETH_ADDRESS_LEN 20

Expand Down Expand Up @@ -38,6 +38,8 @@ static evmc_address g_tx_origin = {0};
static uint8_t g_script_code_hash[32] = {0};
static uint8_t g_script_hash_type = 0xff;

static uint128_t g_gas_price = 0xffffffffffffffffffffffffffffffffu;

/* Minimal gas of a normal transaction*/
#define MIN_TX_GAS 21000
/* Minimal gas of a transaction that creates a contract */
Expand Down
50 changes: 50 additions & 0 deletions polyjuice-tests/src/test_cases/evm-contracts/OpcodeWithMsg.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
pragma solidity ^0.8.4;

contract opcodeTxWithMsg {
opcodeTxWithMsg ptw;

event MsgTxEvent(uint256 idx, MsgData msgData, TxData txData);
struct MsgData {
bytes msgData;
bytes4 msgSig;
uint256 msgValue;
address msgSender;
}

struct TxData {
uint256 txGasPrice;
address txOrigin;
}
MsgData public msgData;
TxData public txData;

constructor() public payable {
updateMsgAndTxData();
}

function getCurrentMsgDataAndTxData()
public
payable
returns (MsgData memory, TxData memory)
{
return (
MsgData(msg.data, msg.sig, msg.value, msg.sender),
TxData(tx.gasprice, tx.origin)
);
}

function getCurrentGasPrice() public payable returns (uint256) {
(msgData, txData) = getCurrentMsgDataAndTxData();
return txData.txGasPrice;
}

function updateMsgAndTxData() public payable {
(msgData, txData) = getCurrentMsgDataAndTxData();
emit MsgTxEvent(1, msgData, txData);
}

function call_updateMsgAndTxData(address _addr) public payable {
ptw = opcodeTxWithMsg(_addr);
ptw.updateMsgAndTxData{value: msg.value}();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[],"stateMutability":"payable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"idx","type":"uint256"},{"components":[{"internalType":"bytes","name":"msgData","type":"bytes"},{"internalType":"bytes4","name":"msgSig","type":"bytes4"},{"internalType":"uint256","name":"msgValue","type":"uint256"},{"internalType":"address","name":"msgSender","type":"address"}],"indexed":false,"internalType":"struct opcodeTxWithMsg.MsgData","name":"msgData","type":"tuple"},{"components":[{"internalType":"uint256","name":"txGasPrice","type":"uint256"},{"internalType":"address","name":"txOrigin","type":"address"}],"indexed":false,"internalType":"struct opcodeTxWithMsg.TxData","name":"txData","type":"tuple"}],"name":"MsgTxEvent","type":"event"},{"inputs":[{"internalType":"address","name":"_addr","type":"address"}],"name":"call_updateMsgAndTxData","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"getCurrentGasPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"getCurrentMsgDataAndTxData","outputs":[{"components":[{"internalType":"bytes","name":"msgData","type":"bytes"},{"internalType":"bytes4","name":"msgSig","type":"bytes4"},{"internalType":"uint256","name":"msgValue","type":"uint256"},{"internalType":"address","name":"msgSender","type":"address"}],"internalType":"struct opcodeTxWithMsg.MsgData","name":"","type":"tuple"},{"components":[{"internalType":"uint256","name":"txGasPrice","type":"uint256"},{"internalType":"address","name":"txOrigin","type":"address"}],"internalType":"struct opcodeTxWithMsg.TxData","name":"","type":"tuple"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"msgData","outputs":[{"internalType":"bytes","name":"msgData","type":"bytes"},{"internalType":"bytes4","name":"msgSig","type":"bytes4"},{"internalType":"uint256","name":"msgValue","type":"uint256"},{"internalType":"address","name":"msgSender","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"txData","outputs":[{"internalType":"uint256","name":"txGasPrice","type":"uint256"},{"internalType":"address","name":"txOrigin","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"updateMsgAndTxData","outputs":[],"stateMutability":"payable","type":"function"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6080604052620000146200001a60201b60201c565b6200075a565b6200002a6200016660201b60201c565b6001600060056000849190506000820151816000015560208201518160010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050839190506000820151816000019080519060200190620000ab9291906200026c565b5060208201518160010160006101000a81548163ffffffff021916908360e01c02179055506040820151816002015560608201518160030160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550505050507f6e8501467508980a3e9b31d9adf765fe4a2e27eecb15d0893f92a156555a3ad460018060056040516200015c939291906200055b565b60405180910390a1565b62000170620002fd565b6200017a6200035a565b60405180608001604052806000368080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505081526020016000357fffffffff00000000000000000000000000000000000000000000000000000000167bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191681526020013481526020013373ffffffffffffffffffffffffffffffffffffffff1681525060405180604001604052803a81526020013273ffffffffffffffffffffffffffffffffffffffff16815250915091509091565b8280546200027a9062000681565b90600052602060002090601f0160209004810192826200029e5760008555620002ea565b82601f10620002b957805160ff1916838001178555620002ea565b82800160010185558215620002ea579182015b82811115620002e9578251825591602001919060010190620002cc565b5b509050620002f991906200038a565b5090565b60405180608001604052806060815260200160007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916815260200160008152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b604051806040016040528060008152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b5b80821115620003a55760008160009055506001016200038b565b5090565b620003b48162000603565b82525050565b620003c58162000617565b82525050565b60008154620003da8162000681565b620003e68186620005b4565b9450600182166000811462000404576001811462000417576200044e565b60ff19831686526020860193506200044e565b62000422856200059f565b60005b83811015620004465781548189015260018201915060208101905062000425565b808801955050505b50505092915050565b62000462816200066d565b82525050565b60006080830160008084018583036000870152620004878382620003cb565b925050600184015490506200049c81620006d5565b620004ab6020870182620003ba565b5060028401549050620004be81620006f3565b620004cd60408701826200054a565b5060038401549050620004e081620006b7565b620004ef6060870182620003a9565b50819250505092915050565b6040820160008083015490506200051281620006f3565b6200052160008601826200054a565b50600183015490506200053481620006b7565b620005436020860182620003a9565b5050505050565b620005558162000663565b82525050565b600060808201905062000572600083018662000457565b818103602083015262000586818562000468565b9050620005976040830184620004fb565b949350505050565b60008190508160005260206000209050919050565b600082825260208201905092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000620005f28262000740565b9050919050565b6000819050919050565b6000620006108262000643565b9050919050565b60007fffffffff0000000000000000000000000000000000000000000000000000000082169050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60006200067a8262000663565b9050919050565b600060028204905060018216806200069a57607f821691505b60208210811415620006b157620006b062000711565b5b50919050565b6000620006ce620006c8836200074d565b620005c5565b9050919050565b6000620006ec620006e6836200074d565b620005e5565b9050919050565b60006200070a62000704836200074d565b620005f9565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60008160e01b9050919050565b60008160001c9050919050565b610da7806200076a6000396000f3fe6080604052600436106100555760003560e01c806311af65641461005a578063120400e714610078578063bae1e4c5146100a4578063c4c2bfdc146100c0578063d46a58c5146100ee578063f1945487146100f8575b600080fd5b610062610117565b60405161006f9190610b11565b60405180910390f35b34801561008457600080fd5b5061008d610225565b60405161009b929190610b2c565b60405180910390f35b6100be60048036038101906100b99190610775565b610257565b005b3480156100cc57600080fd5b506100d561031b565b6040516100e59493929190610a57565b60405180910390f35b6100f66103ee565b005b61010061052e565b60405161010e929190610ae1565b60405180910390f35b600061012161052e565b6001600060056000849190506000820151816000015560208201518160010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050508391905060008201518160000190805190602001906101a0929190610630565b5060208201518160010160006101000a81548163ffffffff021916908360e01c02179055506040820151816002015560608201518160030160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050505050600560000154905090565b60058060000154908060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905082565b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663d46a58c5346040518263ffffffff1660e01b81526004016000604051808303818588803b1580156102ff57600080fd5b505af1158015610313573d6000803e3d6000fd5b505050505050565b600180600001805461032c90610c80565b80601f016020809104026020016040519081016040528092919081815260200182805461035890610c80565b80156103a55780601f1061037a576101008083540402835291602001916103a5565b820191906000526020600020905b81548152906001019060200180831161038857829003601f168201915b5050505050908060010160009054906101000a900460e01b908060020154908060030160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905084565b6103f661052e565b6001600060056000849190506000820151816000015560208201518160010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050839190506000820151816000019080519060200190610475929190610630565b5060208201518160010160006101000a81548163ffffffff021916908360e01c02179055506040820151816002015560608201518160030160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550505050507f6e8501467508980a3e9b31d9adf765fe4a2e27eecb15d0893f92a156555a3ad4600180600560405161052493929190610aa3565b60405180910390a1565b6105366106b6565b61053e610713565b60405180608001604052806000368080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505081526020016000357fffffffff00000000000000000000000000000000000000000000000000000000167bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191681526020013481526020013373ffffffffffffffffffffffffffffffffffffffff1681525060405180604001604052803a81526020013273ffffffffffffffffffffffffffffffffffffffff16815250915091509091565b82805461063c90610c80565b90600052602060002090601f01602090048101928261065e57600085556106a5565b82601f1061067757805160ff19168380011785556106a5565b828001600101855582156106a5579182015b828111156106a4578251825591602001919060010190610689565b5b5090506106b29190610743565b5090565b60405180608001604052806060815260200160007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916815260200160008152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b604051806040016040528060008152602001600073ffffffffffffffffffffffffffffffffffffffff1681525090565b5b8082111561075c576000816000905550600101610744565b5090565b60008135905061076f81610d5a565b92915050565b60006020828403121561078757600080fd5b600061079584828501610760565b91505092915050565b6107a781610bd3565b82525050565b6107b681610bd3565b82525050565b6107c581610be5565b82525050565b6107d481610be5565b82525050565b60006107e582610b6a565b6107ef8185610b75565b93506107ff818560208601610c4d565b61080881610d2f565b840191505092915050565b600061081e82610b6a565b6108288185610b86565b9350610838818560208601610c4d565b61084181610d2f565b840191505092915050565b6000815461085981610c80565b6108638186610b75565b9450600182166000811461087e5760018114610890576108c3565b60ff19831686526020860193506108c3565b61089985610b55565b60005b838110156108bb5781548189015260018201915060208101905061089c565b808801955050505b50505092915050565b6108d581610c3b565b82525050565b600060808301600083015184820360008601526108f882826107da565b915050602083015161090d60208601826107bc565b5060408301516109206040860182610a39565b506060830151610933606086018261079e565b508091505092915050565b6000608083016000808401858303600087015261095b838261084c565b9250506001840154905061096e81610ccc565b61097b60208701826107bc565b506002840154905061098c81610ce6565b6109996040870182610a39565b50600384015490506109aa81610cb2565b6109b7606087018261079e565b50819250505092915050565b6040820160008201516109d96000850182610a39565b5060208201516109ec602085018261079e565b50505050565b604082016000808301549050610a0781610ce6565b610a146000860182610a39565b5060018301549050610a2581610cb2565b610a32602086018261079e565b5050505050565b610a4281610c31565b82525050565b610a5181610c31565b82525050565b60006080820190508181036000830152610a718187610813565b9050610a8060208301866107cb565b610a8d6040830185610a48565b610a9a60608301846107ad565b95945050505050565b6000608082019050610ab860008301866108cc565b8181036020830152610aca818561093e565b9050610ad960408301846109f2565b949350505050565b60006060820190508181036000830152610afb81856108db565b9050610b0a60208301846109c3565b9392505050565b6000602082019050610b266000830184610a48565b92915050565b6000604082019050610b416000830185610a48565b610b4e60208301846107ad565b9392505050565b60008190508160005260206000209050919050565b600081519050919050565b600082825260208201905092915050565b600082825260208201905092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610bc282610d40565b9050919050565b6000819050919050565b6000610bde82610c11565b9050919050565b60007fffffffff0000000000000000000000000000000000000000000000000000000082169050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b6000610c4682610c31565b9050919050565b60005b83811015610c6b578082015181840152602081019050610c50565b83811115610c7a576000848401525b50505050565b60006002820490506001821680610c9857607f821691505b60208210811415610cac57610cab610d00565b5b50919050565b6000610cc5610cc083610d4d565b610b97565b9050919050565b6000610cdf610cda83610d4d565b610bb7565b9050919050565b6000610cf9610cf483610d4d565b610bc9565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000601f19601f8301169050919050565b60008160e01b9050919050565b60008160001c9050919050565b610d6381610bd3565b8114610d6e57600080fd5b5056fea2646970667358221220e7bf3b3c6e874d18629e8e0d98cfe9fbf8628a304d0548da29d9935c291d0c6464736f6c63430008040033
74 changes: 74 additions & 0 deletions polyjuice-tests/src/test_cases/gas_price.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use ckb_vm::Bytes;
use gw_common::state::State;
use gw_store::{chain_view::ChainView, traits::chain_store::ChainStore};
use gw_types::{
packed::RawL2Transaction,
prelude::{Builder, Entity, Pack},
};

use crate::helper::{
create_block_producer, deploy, new_block_info, setup, MockContractInfo, PolyjuiceArgsBuilder,
CREATOR_ACCOUNT_ID, L2TX_MAX_CYCLES,
};

const INIT_CODE: &str = include_str!("./evm-contracts/opcodeTxWithMsg.bin");
#[test]
fn gas_price_test() -> anyhow::Result<()> {
let (store, mut state, generator) = setup();
let block_producer = create_block_producer(&mut state);

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

// Deploy Contract
let _run_result = deploy(
&generator,
&store,
&mut state,
CREATOR_ACCOUNT_ID,
from_id,
INIT_CODE,
1284600,
0,
block_producer.clone(),
0,
);

let contract_account = MockContractInfo::create(&from_eth_address, 0);
let new_account_id = state
.get_account_id_by_script_hash(&contract_account.script_hash)?
.unwrap();

//call getCurrentGasPrice
let input = hex::decode("11af6564")?;
let block_info = new_block_info(block_producer, 1, 0);
let gas_price = 0x111;
let args = PolyjuiceArgsBuilder::default()
.gas_limit(228060)
.gas_price(gas_price)
.value(10000)
.input(&input)
.build();
let raw_tx = RawL2Transaction::new_builder()
.from_id(from_id.pack())
.to_id(new_account_id.pack())
.args(Bytes::from(args).pack())
.build();
let db = store.begin_transaction();
let tip_block_hash = db.get_tip_block_hash().unwrap();
let run_result = generator
.execute_transaction(
&ChainView::new(&db, tip_block_hash),
&state,
&block_info,
&raw_tx,
L2TX_MAX_CYCLES,
)
.expect("Call getCurrentGasPrice()");
let mut arr = [0u8; 16];
arr.copy_from_slice(&run_result.return_data[16..]);
let tx_gas_price = u128::from_be_bytes(arr);
assert_eq!(tx_gas_price as u128, gas_price);
Ok(())
}
Loading

0 comments on commit fef5a48

Please sign in to comment.