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

BREAKING CHANGE: change ckb decimal to 18 #118

Merged
merged 16 commits into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
381 changes: 350 additions & 31 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ VALIDATOR_FLAGS := -DGW_VALIDATOR
LDFLAGS := -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections
MOLC := moleculec
MOLC_VERSION := 0.7.2
PROTOCOL_VERSION := b4db76522b7982f650cc5a6053d5f3957b7cbfd0
PROTOCOL_VERSION := 8d32a46d4abc36dcf1d9dccd3ac5f69d050c246e
PROTOCOL_SCHEMA_URL := https://raw.githubusercontent.com/nervosnetwork/godwoken/${PROTOCOL_VERSION}/crates/types/schemas

# docker pull nervos/ckb-riscv-gnu-toolchain:gnu-bionic-20191012
Expand Down Expand Up @@ -51,12 +51,12 @@ debug-all: all
$(SECP256K1_HELPER):
cd deps/ckb-production-scripts && git submodule init && git submodule update -r && make all-via-docker

build/meta-contract-generator: contracts/meta_contract.c gw_def.h generator_utils.h
build/meta-contract-generator: contracts/meta_contract.c sudt_utils.h gw_def.h generator_utils.h
$(CC) $(CFLAGS) $(GENERATOR_FLAGS) $(LDFLAGS) -o $@ $<
$(OBJCOPY) --only-keep-debug $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

build/meta-contract-validator: contracts/meta_contract.c gw_def.h validator_utils.h
build/meta-contract-validator: contracts/meta_contract.c sudt_utils.h gw_def.h validator_utils.h
$(CC) $(CFLAGS) $(VALIDATOR_FLAGS) $(LDFLAGS) -o $@ $<
$(OBJCOPY) --only-keep-debug $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@
Expand Down
20 changes: 12 additions & 8 deletions c/contracts/eth_addr_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/

#include "gw_eth_addr_reg.h"
#include "gw_syscalls.h"
#include "sudt_utils.h"

/* MSG_TYPE */
Expand All @@ -22,7 +21,7 @@
#define MSG_SET_MAPPING 2
#define MSG_BATCH_SET_MAPPING 3

int handle_fee(gw_context_t *ctx, uint32_t registry_id, uint64_t fee) {
int handle_fee(gw_context_t *ctx, uint32_t registry_id, uint256_t amount) {
if (ctx == NULL) {
return GW_FATAL_INVALID_CONTEXT;
}
Expand All @@ -41,10 +40,7 @@ int handle_fee(gw_context_t *ctx, uint32_t registry_id, uint64_t fee) {
return ret;
}

/* pay fee */
uint32_t sudt_id = CKB_SUDT_ACCOUNT_ID;
uint128_t fee_amount = fee;
return sudt_pay_fee(ctx, sudt_id, payer_addr, fee_amount);
return sudt_pay_fee(ctx, CKB_SUDT_ACCOUNT_ID, payer_addr, amount);
}

int main() {
Expand Down Expand Up @@ -110,8 +106,12 @@ int main() {
mol_seg_t fee_seg = MolReader_SetMapping_get_fee(&msg.seg);
mol_seg_t amount_seg = MolReader_Fee_get_amount(&fee_seg);
mol_seg_t reg_id_seg = MolReader_Fee_get_registry_id(&fee_seg);
uint64_t fee_amount = *(uint64_t *)amount_seg.ptr;
uint32_t reg_id = *(uint32_t *)reg_id_seg.ptr;

uint256_t fee_amount = {0};
_gw_fast_memcpy((uint8_t *)(&fee_amount), (uint8_t *)amount_seg.ptr,
sizeof(uint128_t));

ret = handle_fee(&ctx, reg_id, fee_amount);
if (ret != 0) {
return ret;
Expand Down Expand Up @@ -139,8 +139,12 @@ int main() {
mol_seg_t fee_seg = MolReader_BatchSetMapping_get_fee(&msg.seg);
mol_seg_t amount_seg = MolReader_Fee_get_amount(&fee_seg);
mol_seg_t reg_id_seg = MolReader_Fee_get_registry_id(&fee_seg);
uint64_t fee_amount = *(uint64_t *)amount_seg.ptr;
uint32_t reg_id = *(uint32_t *)reg_id_seg.ptr;

uint256_t fee_amount = {0};
_gw_fast_memcpy((uint8_t *)(&fee_amount), (uint8_t *)amount_seg.ptr,
sizeof(uint128_t));

ret = handle_fee(&ctx, reg_id, fee_amount);
if (ret != 0) {
return ret;
Expand Down
16 changes: 8 additions & 8 deletions c/contracts/meta_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@

/* MSG_TYPE */
#define MSG_CREATE_ACCOUNT 0
/* Constants */
#define CKB_SUDT_ACCOUNT_ID 1

int handle_fee(gw_context_t *ctx, uint32_t registry_id, uint64_t fee) {
int handle_fee(gw_context_t *ctx, uint32_t registry_id, uint256_t amount) {
if (ctx == NULL) {
return GW_FATAL_INVALID_CONTEXT;
}
Expand All @@ -37,9 +35,7 @@ int handle_fee(gw_context_t *ctx, uint32_t registry_id, uint64_t fee) {
}

/* pay fee */
uint32_t sudt_id = CKB_SUDT_ACCOUNT_ID;
uint128_t fee_amount = fee;
ret = sudt_pay_fee(ctx, sudt_id, payer_addr, fee_amount);
ret = sudt_pay_fee(ctx, CKB_SUDT_ACCOUNT_ID, payer_addr, amount);
if (ret != 0) {
ckb_debug("failed to pay fee");
return ret;
Expand Down Expand Up @@ -76,9 +72,13 @@ int main() {
mol_seg_t fee_seg = MolReader_CreateAccount_get_fee(&msg.seg);
mol_seg_t amount_seg = MolReader_Fee_get_amount(&fee_seg);
mol_seg_t reg_id_seg = MolReader_Fee_get_registry_id(&fee_seg);
uint64_t fee_amount = *(uint64_t *)amount_seg.ptr;

uint256_t fee_amount = {0};
_gw_fast_memcpy((uint8_t *)(&fee_amount), (uint8_t *)amount_seg.ptr,
sizeof(uint128_t));

uint32_t reg_id = *(uint32_t *)reg_id_seg.ptr;
int ret = handle_fee(&ctx, reg_id, fee_amount);
ret = handle_fee(&ctx, reg_id, fee_amount);
if (ret != 0) {
ckb_debug("failed to handle fee");
return ret;
Expand Down
18 changes: 11 additions & 7 deletions c/contracts/sudt.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ int main() {
if (ret != 0) {
return ret;
}
uint128_t balance = 0;
uint256_t balance = {0};
ret = sudt_get_balance(&ctx, sudt_id, addr, &balance);
if (ret != 0) {
return ret;
}
ret = ctx.sys_set_program_return_data(&ctx, (uint8_t *)&balance,
sizeof(uint128_t));
sizeof(uint256_t));
if (ret != 0) {
return ret;
}
Expand All @@ -84,9 +84,12 @@ int main() {
mol_seg_t fee_seg = MolReader_SUDTTransfer_get_fee(&msg.seg);
mol_seg_t fee_amount_seg = MolReader_Fee_get_amount(&fee_seg);
mol_seg_t fee_reg_seg = MolReader_Fee_get_registry_id(&fee_seg);
uint32_t reg_id = *(uint32_t *)fee_reg_seg.ptr;
uint64_t fee_amount = *(uint64_t *)fee_amount_seg.ptr;

uint256_t fee_amount = {0};
_gw_fast_memcpy((uint8_t *)(&fee_amount), (uint8_t *)fee_amount_seg.ptr,
sizeof(uint128_t));

uint32_t reg_id = *(uint32_t *)fee_reg_seg.ptr;
uint32_t from_id = ctx.transaction_context.from_id;
uint8_t from_script_hash[32] = {0};
ret =
Expand All @@ -108,10 +111,11 @@ int main() {
return ret;
}

uint128_t amount = *(uint128_t *)amount_seg.ptr;
uint256_t amount = {0};
_gw_fast_memcpy((uint8_t *)(&amount), (uint8_t *)amount_seg.ptr, 32);

/* pay fee */
uint32_t ckb_sudt_id = CKB_SUDT_ACCOUNT_ID;
ret = sudt_pay_fee(&ctx, ckb_sudt_id, from_addr, fee_amount);
ret = sudt_pay_fee(&ctx, CKB_SUDT_ACCOUNT_ID, from_addr, fee_amount);
if (ret != 0) {
printf("pay fee failed");
return ret;
Expand Down
6 changes: 3 additions & 3 deletions c/examples/sudt_total_supply.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ int main() {
}
sudt_id = *(uint32_t *)ctx.transaction_context.args;

uint8_t total_supply[32];
ret = sudt_get_total_supply(&ctx, sudt_id, total_supply);
uint256_t total_supply = {0};
ret = sudt_get_total_supply(&ctx, sudt_id, &total_supply);
if (ret != 0) {
return ret;
}

ctx.sys_set_program_return_data(&ctx, total_supply, 32);
ctx.sys_set_program_return_data(&ctx, (uint8_t *)&total_supply, 32);
return gw_finalize(&ctx);
}
5 changes: 3 additions & 2 deletions c/generator_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "ckb_syscalls.h"
#include "gw_def.h"
#include "uint256.h"

/* syscalls */
/* Syscall account store / load / create */
Expand Down Expand Up @@ -359,7 +360,7 @@ int sys_log(gw_context_t *ctx, uint32_t account_id, uint8_t service_flag,
}

int sys_pay_fee(gw_context_t *ctx, gw_reg_addr_t addr, uint32_t sudt_id,
uint128_t amount) {
uint256_t amount) {
if (ctx == NULL) {
return GW_FATAL_INVALID_CONTEXT;
}
Expand All @@ -377,7 +378,7 @@ int sys_pay_fee(gw_context_t *ctx, gw_reg_addr_t addr, uint32_t sudt_id,
}
_gw_cpy_addr(buf, addr);

return syscall(GW_SYS_PAY_FEE, buf, len, sudt_id, &amount, 0, 0);
return syscall(GW_SYS_PAY_FEE, buf, len, sudt_id, (uint8_t *)&amount, 0, 0);
}

int _sys_load_rollup_config(uint8_t *addr, uint64_t *len) {
Expand Down
6 changes: 3 additions & 3 deletions c/godwoken.mol
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct RawWithdrawalRequest {
// layer1 lock to withdraw after challenge period
owner_lock_hash: Byte32,
// withdrawal fee, paid to block producer
fee: Uint64,
fee: Uint128,
}

vector WithdrawalRequestVec <WithdrawalRequest>;
Expand Down Expand Up @@ -237,7 +237,7 @@ struct Fee {
// registry id
registry_id: Uint32,
// amount in CKB
amount: Uint64,
amount: Uint128,
}

table CreateAccount {
Expand All @@ -260,7 +260,7 @@ table SUDTQuery {
table SUDTTransfer {
// Godwoken registry address: (registry_id (4 bytes) | address len(4 bytes) | address)
to_address: Bytes,
amount: Uint128,
amount: Uint256,
// paid fee(ckb)
fee: Fee,
}
Expand Down
3 changes: 2 additions & 1 deletion c/gw_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "gw_registry_addr.h"
#include "stddef.h"
#include "uint256.h"

typedef unsigned __int128 uint128_t;

Expand Down Expand Up @@ -253,7 +254,7 @@ typedef int (*gw_log_fn)(struct gw_context_t *ctx, uint32_t account_id,
* @return The status code, 0 is success
*/
typedef int (*gw_pay_fee_fn)(struct gw_context_t *ctx, gw_reg_addr_t payer_addr,
uint32_t sudt_id, uint128_t amount);
uint32_t sudt_id, uint256_t amount);

/**
* Get registry address by script_hash
Expand Down
Loading