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

Commit

Permalink
BREAKING CHANGE: change ckb decimal to 18 (#146)
Browse files Browse the repository at this point in the history
* chore: bump godwoken-scripts and godwoken commit
* feat!: change to u256 sudt amount
* refactor!: change fee to u128
* fix: tests
* chore(Makefile): change fetch-gw-scripts to pr prebuilds
* chore(ci): godwoken prebuilds
  • Loading branch information
zeroqn authored Apr 29, 2022
1 parent 9f83077 commit 8688af8
Show file tree
Hide file tree
Showing 34 changed files with 183 additions and 183 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ jobs:
with:
# github.ref: The branch or tag ref that triggered the workflow run. For branches this is the format refs/heads/<branch_name>, and for tags it is refs/tags/<tag_name>.
polyjuice_ref: ${{ github.ref }}
kicker_ref: refs/pull/235/head # https://github.com/RetricSu/godwoken-kicker/pull/235
gw_prebuild_image_name: ghcr.io/zeroqn/godwoken-prebuilds
gw_prebuild_image_tag: v1.1-feat-change-ckb-decimal-to-18
kicker_ref: refs/pull/239/head # https://github.com/RetricSu/godwoken-kicker/pull/239
tests_ref: develop # https://github.com/nervosnetwork/godwoken-tests/commits/develop
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,11 @@ contract/sudt-erc20-proxy:

# fetch godwoken-scripts from godwoken-prebuilds image,
# including meta-contract and sudt-contract
PREBUILDS := ghcr.io/zeroqn/godwoken-prebuilds:v1.1-feat-change-ckb-decimal-to-18
fetch-gw-scripts:
mkdir -p build
docker run --rm -v `pwd`/build:/build-dir \
ghcr.io/nervosnetwork/godwoken-prebuilds:v1.1 \
$(PREBUILDS) \
cp -r /scripts/godwoken-scripts /build-dir \
&& echo "Copy godwoken-scripts"

Expand Down
8 changes: 4 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
const BINARIES: &[(&str, &str)] = &[
(
"generator",
"b1284ae3e3268611368b60c4e7f08b5086cd2d5c5f20c56c94051e689b658f7f",
"6f53704176f5cb3b74cfc72051d7db610b39481698b3624780402332bbe92b44",
),
(
"validator",
"9996e062253cd32ebf1b820c391b0103781ff943e66fc937e03cfc817759655f",
"0e224654c6853ca6d0d7d26d0893d507348f5f60f6b20d3e7a6c5fa3e63bd157",
),
(
"generator_log",
"235e1c7a126c5e8954a4008a57301f45ad60074ac71e0ccaa730391afb2fddf3",
"81596e5729f40301019c5fa0746f60b950ca57a9103f1ac30687724b7effeea5",
),
(
"validator_log",
"8b25482e9c3a7c5070fe356aac24c999b7b60dc142a78f9134f0c58d99b56df4",
"65549e96e013a21abe4fc5a44a9dcbf1020cb9b58e07e1fb6e2161a61bdd1101",
),
];

Expand Down
58 changes: 28 additions & 30 deletions c/polyjuice.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
#include <string.h>

#include "ckb_syscalls.h"
#include "godwoken.h"
#include "gw_eth_addr_reg.h"

#include <ethash/keccak.hpp>
#include <evmc/evmc.h>
#include <evmc/evmc.hpp>
#include <evmone/evmone.h>

#include "uint256.h"

/* https://stackoverflow.com/a/1545079 */
#pragma push_macro("errno")
#undef errno
#include "godwoken.h"
#include "gw_eth_addr_reg.h"
#include "gw_syscalls.h"
#include "sudt_utils.h"
#pragma pop_macro("errno")

#include "common.h"

#include "sudt_utils.h"
#include "polyjuice_errors.h"
#include "polyjuice_utils.h"

Expand All @@ -32,7 +33,6 @@
#endif
#include "contracts.h"


#define is_create(kind) ((kind) == EVMC_CREATE || (kind) == EVMC_CREATE2)
#define is_special_call(kind) \
((kind) == EVMC_CALLCODE || (kind) == EVMC_DELEGATECALL)
Expand Down Expand Up @@ -124,6 +124,7 @@ int load_account_script(gw_context_t* gw_ctx, uint32_t account_id,
return 0;
}

// TODO: change gas_limit, gas_price, value to u256
/**
Message = [
header : [u8; 8] 0xff, 0xff, 0xff, "POLY", call_kind
Expand Down Expand Up @@ -508,22 +509,22 @@ evmc_uint256be get_balance(struct evmc_host_context* context,

gw_reg_addr_t addr = new_reg_addr(address->bytes);

uint128_t value_u128 = 0;
uint256_t value = {0};
int ret = sudt_get_balance(context->gw_ctx,
g_sudt_id, /* g_sudt_id account must exists */
addr, &value_u128);
addr, &value);
if (ret != 0) {
ckb_debug("sudt_get_balance failed");
context->error_code = FATAL_POLYJUICE;
return balance;
}

uint8_t* value_ptr = (uint8_t*)(&value_u128);
for (int i = 0; i < 16; i++) {
uint8_t* value_ptr = (uint8_t*)(&value);
for (int i = 0; i < 32; i++) {
balance.bytes[31 - i] = *(value_ptr + i);
}
debug_print_data("address", address->bytes, 20);
debug_print_int("balance", value_u128);
debug_print_data("balance", (uint8_t*)&value, 32);
ckb_debug("END get_balance");
return balance;
}
Expand All @@ -533,7 +534,7 @@ void selfdestruct(struct evmc_host_context* context,
const evmc_address* beneficiary) {
gw_reg_addr_t from_addr = new_reg_addr(address->bytes);

uint128_t balance;
uint256_t balance;
int ret = sudt_get_balance(context->gw_ctx,
g_sudt_id, /* g_sudt_id account must exists */
from_addr, &balance);
Expand All @@ -543,7 +544,8 @@ void selfdestruct(struct evmc_host_context* context,
return;
}

if (balance > 0) {
uint256_t zero = {0};
if (uint256_cmp(balance, zero) == LARGER) {
gw_reg_addr_t to_addr = new_reg_addr(beneficiary->bytes);

ret = sudt_transfer(context->gw_ctx, g_sudt_id,
Expand Down Expand Up @@ -906,18 +908,14 @@ int create_new_account(gw_context_t* ctx,
int handle_transfer(gw_context_t* ctx,
const evmc_message* msg,
bool to_address_is_eoa) {
uint8_t value_u128_bytes[16];
for (int i = 0; i < 16; i++) {
if (msg->value.bytes[i] != 0) {
ckb_debug("[handle_transfer] transfer value can not larger than u128::max()");
return FATAL_POLYJUICE;
}
value_u128_bytes[i] = msg->value.bytes[31 - i];
uint256_t value;
uint8_t* value_ptr = (uint8_t*)&value;
for (int i = 0; i < 32; i++) {
value_ptr[i] = msg->value.bytes[31 - i];
}
uint128_t value_u128 = *(uint128_t*)value_u128_bytes;
debug_print_data("[handle_transfer] sender", msg->sender.bytes, 20);
debug_print_data("[handle_transfer] destination", msg->destination.bytes, 20);
debug_print_int("[handle_transfer] msg->value", value_u128);
debug_print_data("[handle_transfer] msg->value", (uint8_t*)&value, 32);

if (msg->kind == EVMC_CALL
&& memcmp(msg->sender.bytes, g_tx_origin.bytes, 20) == 0
Expand All @@ -929,13 +927,11 @@ int handle_transfer(gw_context_t* ctx,
gw_reg_addr_t from_addr = new_reg_addr(msg->sender.bytes);
gw_reg_addr_t to_addr = new_reg_addr(msg->destination.bytes);

if (value_u128 == 0) {
uint256_t zero = {0};
if (uint256_cmp(value, zero) == EQUAL) {
return 0;
}
int ret = sudt_transfer(ctx, g_sudt_id,
from_addr,
to_addr,
value_u128);
int ret = sudt_transfer(ctx, g_sudt_id, from_addr, to_addr, value);
if (ret != 0) {
ckb_debug("[handle_transfer] sudt_transfer failed");
return ret;
Expand Down Expand Up @@ -1368,17 +1364,19 @@ int run_polyjuice() {

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);
uint256_t fee_u256 = {0};
memcpy((uint8_t *)(&fee_u256), (uint8_t*)(&fee), 16);

ret = sudt_pay_fee(&context, g_sudt_id, /* g_sudt_id must already exists */
sender_addr, fee_u256);
if (ret != 0) {
debug_print_int("[run_polyjuice] pay fee to block_producer failed", ret);
return clean_evmc_result_and_return(&res, ret);
}

// call the SYS_PAY_FEE syscall to record the fee
// NOTICE: this function do not actually execute the transfer of assets
ret = sys_pay_fee(&context, sender_addr, g_sudt_id, fee);
ret = sys_pay_fee(&context, sender_addr, g_sudt_id, fee_u256);
if (ret != 0) {
debug_print_int("[run_polyjuice] Record fee payment failed", ret);
return clean_evmc_result_and_return(&res, ret);
Expand Down
13 changes: 11 additions & 2 deletions c/polyjuice_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ int build_script(const uint8_t code_hash[32], const uint8_t hash_type,
return 0;
}


/**
* @param script_hash should have been initialed as zero_hash = {0}
*
Expand Down Expand Up @@ -218,7 +217,7 @@ void rlp_encode_sender_and_nonce(const evmc_address *sender, uint32_t nonce,
data[0] = *data_len - 1 + RLP_LIST_OFFSET;
}

/* Parse uint32_t/uint128_t from big endian byte32 data */
/* Parse uint32_t/uint64_t/uint128_t/uint256_t from big endian byte32 data */
int parse_integer(const uint8_t data_be[32], uint8_t *value,
size_t value_size) {
if (value_size > 32) {
Expand Down Expand Up @@ -246,6 +245,9 @@ int parse_u64(const uint8_t data_be[32], uint64_t *value) {
int parse_u128(const uint8_t data_be[32], uint128_t *value) {
return parse_integer(data_be, (uint8_t *)value, sizeof(uint128_t));
}
int parse_u256(const uint8_t data_be[32], uint256_t *value) {
return parse_integer(data_be, (uint8_t *)value, sizeof(uint256_t));
}

/* serialize uint64_t to big endian byte32 */
void put_u64(uint64_t value, uint8_t *output) {
Expand All @@ -263,6 +265,13 @@ void put_u128(uint128_t value, uint8_t *output) {
}
}

void put_u256(uint256_t value, uint8_t *output) {
uint8_t *value_le = (uint8_t *)(&value);
for (size_t i = 0; i < 32; i++) {
*(output + 31 - i) = *(value_le + i);
}
}

/* If it is a fatal error, terminate the whole process.
* ====
* - gw_errors.h GW_FATAIL_xxx [50, 80)
Expand Down
17 changes: 9 additions & 8 deletions c/sudt_contracts.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ int balance_of_any_sudt(gw_context_t* ctx, const uint8_t* code_data,
evmc_address address = *((evmc_address*)(input_src + 32 + 12));

gw_reg_addr_t addr = new_reg_addr(address.bytes);
uint128_t balance;

uint256_t balance;
ret = sudt_get_balance(ctx, sudt_id, addr, &balance);
if (ret == GW_ERROR_NOT_FOUND) {
debug_print_int("[balance_of_any_sudt] sudt account not found", sudt_id);
Expand All @@ -73,7 +73,7 @@ int balance_of_any_sudt(gw_context_t* ctx, const uint8_t* code_data,
return ERROR_BALANCE_OF_ANY_SUDT;
}
}
put_u128(balance, *output);
put_u256(balance, *output);
return 0;
}

Expand Down Expand Up @@ -118,8 +118,8 @@ int total_supply_of_any_sudt(gw_context_t* ctx, const uint8_t* code_data,
*output_size = 32;
memset(*output, 0, 32);

uint8_t total_supply_le[32] = {0};
ret = sudt_get_total_supply(ctx, sudt_id, total_supply_le);
uint256_t total_supply_le = {0};
ret = sudt_get_total_supply(ctx, sudt_id, &total_supply_le);
if (ret == GW_ERROR_NOT_FOUND) {
debug_print_int("sudt account not found", sudt_id);
return 0;
Expand All @@ -132,8 +132,9 @@ int total_supply_of_any_sudt(gw_context_t* ctx, const uint8_t* code_data,
}
}

uint8_t* total_supply_le_bytes = (uint8_t*)&total_supply_le;
for (size_t i = 0; i < 32; i++) {
(*output)[31 - i] = total_supply_le[i];
(*output)[31 - i] = total_supply_le_bytes[i];
}
return 0;
}
Expand Down Expand Up @@ -198,12 +199,12 @@ int transfer_to_any_sudt(gw_context_t* ctx, const uint8_t* code_data,
}

uint32_t sudt_id = 0;
uint128_t amount = 0;
uint256_t amount = {0};
ret = parse_u32(input_src, &sudt_id);
if (ret != 0) {
return ERROR_TRANSFER_TO_ANY_SUDT;
}
ret = parse_u128(input_src + 96, &amount);
ret = parse_u256(input_src + 96, &amount);
if (ret != 0) {
return ERROR_TRANSFER_TO_ANY_SUDT;
}
Expand Down
2 changes: 1 addition & 1 deletion deps/godwoken-scripts
4 changes: 2 additions & 2 deletions devtools/ci/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ else
git clone --depth=1 https://github.com/nervosnetwork/godwoken.git $GODWOKEN_DIR
fi
cd $GODWOKEN_DIR
# checkout https://github.com/nervosnetwork/godwoken/pull/659/head
git fetch origin 0dd1500455ca81c23addadff05399925921aa352
# checkout https://github.com/nervosnetwork/godwoken/pull/675/merge
git fetch origin pull/675/merge
git checkout FETCH_HEAD
git submodule update --init --recursive --depth=1

Expand Down
Loading

0 comments on commit 8688af8

Please sign in to comment.