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

Commit

Permalink
refactor: move CONTRACT_ACCOUNT_SCRIPT_ARGS_SIZE to polyjuice_globals.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Flouse committed Nov 3, 2021
1 parent 7b7b44e commit b391bd1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
7 changes: 3 additions & 4 deletions c/other_contracts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions c/polyjuice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
19 changes: 12 additions & 7 deletions c/polyjuice_globals.h
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
4 changes: 1 addition & 3 deletions c/polyjuice_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <evmc/evmc.h>
#include "ckb_syscalls.h"
#include "polyjuice_globals.h"
#include "polyjuice_errors.h"

#define ETH_ADDRESS_LEN 20
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion c/sudt_contracts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b391bd1

Please sign in to comment.