From b391bd10966d1c026778f115ca3373b2a93ed211 Mon Sep 17 00:00:00 2001 From: Flouse Date: Wed, 3 Nov 2021 17:58:32 +0800 Subject: [PATCH] refactor: move CONTRACT_ACCOUNT_SCRIPT_ARGS_SIZE to polyjuice_globals.h --- c/other_contracts.h | 7 +++---- c/polyjuice.h | 9 ++++----- c/polyjuice_globals.h | 19 ++++++++++++------- c/polyjuice_utils.h | 4 +--- c/sudt_contracts.h | 1 - 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/c/other_contracts.h b/c/other_contracts.h index 08b70572..25b47546 100644 --- a/c/other_contracts.h +++ b/c/other_contracts.h @@ -3,7 +3,6 @@ #define OTHER_CONTRACTS_H_ #include "polyjuice_utils.h" -#include "polyjuice_globals.h" /* Gas fee */ #define RECOVER_ACCOUNT_GAS 3600 /* more than ecrecover */ @@ -91,7 +90,7 @@ int eth_to_godwoken_addr_gas(const uint8_t* input_src, input[12..32] => ETH address output: - output[12..32] => godwoken short address + output[12..32] => short_gw_script_hash, a.k.a. godwoken short address */ int eth_to_godwoken_addr(gw_context_t* ctx, const uint8_t* code_data, @@ -111,13 +110,13 @@ int eth_to_godwoken_addr(gw_context_t* ctx, } } int ret; - uint8_t script_args[SCRIPT_ARGS_LEN]; + uint8_t script_args[CONTRACT_ACCOUNT_SCRIPT_ARGS_LEN]; memcpy(script_args, g_rollup_script_hash, 32); memcpy(script_args + 32, (uint8_t*)(&g_creator_account_id), 4); memcpy(script_args + 32 + 4, input_src + 12, 20); mol_seg_t new_script_seg; ret = build_script(g_script_code_hash, g_script_hash_type, script_args, - SCRIPT_ARGS_LEN, &new_script_seg); + CONTRACT_ACCOUNT_SCRIPT_ARGS_LEN, &new_script_seg); if (ret != 0) { return ret; } diff --git a/c/polyjuice.h b/c/polyjuice.h index 85371bb2..c797aac1 100644 --- a/c/polyjuice.h +++ b/c/polyjuice.h @@ -30,7 +30,6 @@ int printf(const char *format, ...) { #include "common.h" #include "sudt_utils.h" -#include "polyjuice_globals.h" #include "polyjuice_errors.h" #include "polyjuice_utils.h" @@ -274,7 +273,7 @@ int load_account_code(gw_context_t* gw_ctx, uint32_t account_id, mol_seg_t hash_type_seg = MolReader_Script_get_hash_type(&script_seg); mol_seg_t args_seg = MolReader_Script_get_args(&script_seg); mol_seg_t raw_args_seg = MolReader_Bytes_raw_bytes(&args_seg); - if (raw_args_seg.size != CONTRACT_ACCOUNT_SCRIPT_ARGS_SIZE) { + if (raw_args_seg.size != CONTRACT_ACCOUNT_SCRIPT_ARGS_LEN) { debug_print_int("[load_account_code] invalid account script", account_id); debug_print_int("[load_account_code] raw_args_seg.size", raw_args_seg.size); // This is an EoA or other kind of account @@ -811,7 +810,7 @@ int load_globals(gw_context_t* ctx, uint32_t to_id, evmc_call_kind call_kind) { /* polyjuice creator account */ g_creator_account_id = to_id; creator_raw_args_seg = raw_args_seg; - } else if (raw_args_seg.size == CONTRACT_ACCOUNT_SCRIPT_ARGS_SIZE) { + } else if (raw_args_seg.size == CONTRACT_ACCOUNT_SCRIPT_ARGS_LEN) { /* read creator account id and do some checking */ memcpy(&g_creator_account_id, raw_args_seg.ptr + 32, sizeof(uint32_t)); int ret = load_account_script(ctx, @@ -858,7 +857,7 @@ int create_new_account(gw_context_t* ctx, } int ret = 0; - uint8_t script_args[SCRIPT_ARGS_LEN]; + uint8_t script_args[CONTRACT_ACCOUNT_SCRIPT_ARGS_LEN]; uint8_t data[128] = {0}; uint32_t data_len = 0; if (msg->kind == EVMC_CREATE) { @@ -913,7 +912,7 @@ int create_new_account(gw_context_t* ctx, mol_seg_t new_script_seg; uint32_t new_account_id; ret = build_script(g_script_code_hash, g_script_hash_type, script_args, - SCRIPT_ARGS_LEN, &new_script_seg); + CONTRACT_ACCOUNT_SCRIPT_ARGS_LEN, &new_script_seg); if (ret != 0) { return ret; } diff --git a/c/polyjuice_globals.h b/c/polyjuice_globals.h index c059f4ad..12882206 100644 --- a/c/polyjuice_globals.h +++ b/c/polyjuice_globals.h @@ -1,24 +1,29 @@ #ifndef POLYJUICE_GLOBALS_H #define POLYJUICE_GLOBALS_H -#define POLYJUICE_VERSION "v0.8.9" +#define POLYJUICE_VERSION "v0.9.0" #define POLYJUICE_SHORT_ADDR_LEN 20 -/* 32 + 4 + 20 */ -#define SCRIPT_ARGS_LEN 56 + +/** Polyjuice contract account (normal/create2) script args size */ +#define CONTRACT_ACCOUNT_SCRIPT_ARGS_LEN 56 /* 32 + 4 + 20 */ static uint8_t g_rollup_script_hash[32] = {0}; static uint32_t g_sudt_id = UINT32_MAX; static bool g_is_using_native_eth_address = false; /** - * Receipt.contractAddress - * The contract address created, if the transaction was a contract creation, - * otherwise null + * Receipt.contractAddress is the created contract, + * if the transaction was a contract creation, otherwise null */ -static uint32_t g_created_id = UINT32_MAX; static uint8_t g_created_address[20] = {0}; +static uint32_t g_created_id = UINT32_MAX; +/** + * creator_account, known as root account + * see also: https://github.com/nervosnetwork/godwoken/blob/5735d8f/docs/life_of_a_polyjuice_transaction.md#root-account--deployment + */ static uint32_t g_creator_account_id = UINT32_MAX; + static evmc_address g_tx_origin = {0}; static uint8_t g_script_code_hash[32] = {0}; diff --git a/c/polyjuice_utils.h b/c/polyjuice_utils.h index f2bd02d5..07b44caa 100644 --- a/c/polyjuice_utils.h +++ b/c/polyjuice_utils.h @@ -7,6 +7,7 @@ #include #include "ckb_syscalls.h" +#include "polyjuice_globals.h" #include "polyjuice_errors.h" #define ETH_ADDRESS_LEN 20 @@ -50,9 +51,6 @@ void debug_print_int(const char* prefix, int64_t ret) { #define memset(dest, c, n) _smt_fast_memset(dest, c, n) -/* polyjuice contract account (normal/create2) script args size*/ -static const uint32_t CONTRACT_ACCOUNT_SCRIPT_ARGS_SIZE = 32 + 4 + 20; - int build_script(const uint8_t code_hash[32], const uint8_t hash_type, const uint8_t* args, const uint32_t args_len, mol_seg_t* script_seg) { diff --git a/c/sudt_contracts.h b/c/sudt_contracts.h index 05e0a1b7..aedd449f 100644 --- a/c/sudt_contracts.h +++ b/c/sudt_contracts.h @@ -3,7 +3,6 @@ #define SUDT_CONTRACTS_H_ #include "polyjuice_utils.h" -#include "polyjuice_globals.h" #define BALANCE_OF_ANY_SUDT_GAS 150 #define TRANSFER_TO_ANY_SUDT_GAS 300