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

Commit

Permalink
chore: define g_gas_price in globals
Browse files Browse the repository at this point in the history
  • Loading branch information
magicalne committed Jun 15, 2022
1 parent cf4e164 commit d95b40f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
28 changes: 11 additions & 17 deletions c/polyjuice.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ int gw_increase_nonce(gw_context_t *ctx, uint32_t account_id, uint32_t *new_nonc
int handle_message(gw_context_t* ctx,
uint32_t parent_from_id,
uint32_t parent_to_id,
uint128_t gas_price,
evmc_address *parent_destination,
const evmc_message* msg, struct evmc_result* res);
typedef int (*stream_data_loader_fn)(gw_context_t* ctx, long data_id,
Expand All @@ -107,7 +106,6 @@ struct evmc_host_context {
// parent level destination
evmc_address destination;
int error_code;
uint128_t gas_price;
};

int load_account_script(gw_context_t* gw_ctx, uint32_t account_id,
Expand Down Expand Up @@ -141,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 @@ -178,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 @@ -313,7 +310,7 @@ 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);
evmc_uint256be gas_price = {0};
uint8_t* gas_price_ptr = (uint8_t*)(&context->gas_price);
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);
}
Expand Down Expand Up @@ -631,7 +628,7 @@ 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->gas_price, &context->destination, msg, &res);
&context->destination, msg, &res);
if (is_fatal_error(ret)) {
/* stop as soon as possible */
context->error_code = ret;
Expand Down Expand Up @@ -1008,12 +1005,11 @@ int execute_in_evmone(gw_context_t* ctx,
uint32_t to_id,
const uint8_t* code_data,
const size_t code_size,
struct evmc_result* res,
uint128_t gas_price) {
struct evmc_result* res) {
int ret = 0;
evmc_address sender = msg->sender;
evmc_address destination = msg->destination;
struct evmc_host_context context {ctx, code_data, code_size, msg->kind, from_id, to_id, sender, destination, 0, gas_price};
struct evmc_host_context context {ctx, code_data, code_size, msg->kind, from_id, to_id, sender, destination, 0};
struct evmc_vm* vm = evmc_create_evmone();
struct evmc_host_interface interface = {account_exists, get_storage, set_storage, get_balance,
get_code_size, get_code_hash, copy_code, selfdestruct,
Expand Down Expand Up @@ -1075,7 +1071,6 @@ int store_contract_code(gw_context_t* ctx,
int handle_message(gw_context_t* ctx,
uint32_t parent_from_id,
uint32_t parent_to_id,
uint128_t gas_price,
evmc_address *parent_destination,
const evmc_message* msg_origin, struct evmc_result* res) {
static const evmc_address zero_address{0};
Expand Down Expand Up @@ -1214,7 +1209,7 @@ int handle_message(gw_context_t* ctx,
debug_print_int("[handle_message] msg.kind", msg.kind);
/* NOTE: msg and res are updated */
if (to_address_exists && code_size > 0) {
ret = execute_in_evmone(ctx, &msg, parent_from_id, from_id, to_id, code_data, code_size, res, gas_price);
ret = execute_in_evmone(ctx, &msg, parent_from_id, from_id, to_id, code_data, code_size, res);
if (ret != 0) {
return ret;
}
Expand Down Expand Up @@ -1355,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 @@ -1398,7 +1392,7 @@ int run_polyjuice() {
int64_t initial_gas = msg.gas;
msg.gas -= min_gas; // subtract IntrinsicGas

int ret_handle_message = handle_message(&context, UINT32_MAX, UINT32_MAX, gas_price, NULL, &msg, &res);
int ret_handle_message = handle_message(&context, UINT32_MAX, UINT32_MAX, NULL, &msg, &res);
// debug_print evmc_result.output_data if the execution failed
if (res.status_code != 0) {
debug_print_int("evmc_result.output_size", res.output_size);
Expand Down Expand Up @@ -1443,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
2 changes: 2 additions & 0 deletions c/polyjuice_globals.h
Original file line number Diff line number Diff line change
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

0 comments on commit d95b40f

Please sign in to comment.