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

Commit

Permalink
feat!: change to u256 sudt amount
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroqn committed Apr 26, 2022
1 parent bc11148 commit e45d12f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
60 changes: 30 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,21 @@ 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);
uint8_t tmp_u256_be[32];
put_u128(fee, (uint8_t*)&tmp_u256_be);
uint256_t fee_u256 = {0};
ret = parse_u256(tmp_u256_be, &fee_u256);

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
11 changes: 10 additions & 1 deletion 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 @@ -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

0 comments on commit e45d12f

Please sign in to comment.