From 7ac316e02c59dcfa7f652704249a32e10742c757 Mon Sep 17 00:00:00 2001 From: "zero.qn" Date: Sun, 24 Apr 2022 18:07:42 +0800 Subject: [PATCH 01/16] feat!(contracts): change ckb decimal to 18 --- c/Makefile | 2 +- contracts/Cargo.lock | 34 ++++++++++++++++--- .../src/verifications/eip712/types.rs | 4 +-- contracts/gw-utils/Cargo.toml | 4 +-- .../src/verifications/submit_block.rs | 18 ++++------ 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/c/Makefile b/c/Makefile index 3553bca3..4554c79d 100644 --- a/c/Makefile +++ b/c/Makefile @@ -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 := 1090567d2c54384f5456a0363e5574e0af1b7268 PROTOCOL_SCHEMA_URL := https://raw.githubusercontent.com/nervosnetwork/godwoken/${PROTOCOL_VERSION}/crates/types/schemas # docker pull nervos/ckb-riscv-gnu-toolchain:gnu-bionic-20191012 diff --git a/contracts/Cargo.lock b/contracts/Cargo.lock index b9c231d4..3c562ebe 100644 --- a/contracts/Cargo.lock +++ b/contracts/Cargo.lock @@ -280,20 +280,19 @@ dependencies = [ [[package]] name = "gw-common" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "cfg-if 0.1.10", "gw-hash", "gw-types", "merkle-cbt", - "primitive-types", "sparse-merkle-tree", ] [[package]] name = "gw-hash" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "blake2b-ref", ] @@ -309,11 +308,12 @@ dependencies = [ [[package]] name = "gw-types" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "cfg-if 0.1.10", "gw-hash", "molecule 0.7.2", + "primitive-types", "sparse-merkle-tree", ] @@ -332,6 +332,24 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "impl-rlp" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f28220f89297a075ddc7245cd538076ee98b01f2a9c23a53a4f1105d5a322808" +dependencies = [ + "rlp", +] + +[[package]] +name = "impl-serde" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4551f042f3438e64dbd6226b20527fc84a6e1fe65688b58746a2f53623f25f5c" +dependencies = [ + "serde", +] + [[package]] name = "keccak" version = "0.1.0" @@ -384,6 +402,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05e4722c697a58a99d5d06a08c30821d7c082a4632198de1eaa5a6c22ef42373" dependencies = [ "fixed-hash", + "impl-rlp", + "impl-serde", "uint", ] @@ -410,6 +430,12 @@ dependencies = [ "cc", ] +[[package]] +name = "serde" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" + [[package]] name = "sha3" version = "0.9.1" diff --git a/contracts/challenge-lock/src/verifications/eip712/types.rs b/contracts/challenge-lock/src/verifications/eip712/types.rs index 0b82630c..2cbf12f4 100644 --- a/contracts/challenge-lock/src/verifications/eip712/types.rs +++ b/contracts/challenge-lock/src/verifications/eip712/types.rs @@ -7,7 +7,7 @@ use alloc::{ use gw_utils::{ ckb_std::debug, error::Error, - gw_types::{core::ScriptHashType, packed::RawWithdrawalRequest, prelude::Unpack}, + gw_types::{core::ScriptHashType, packed::RawWithdrawalRequest, prelude::Unpack, U256}, }; use sha3::{Digest, Keccak256}; @@ -199,7 +199,7 @@ pub struct Withdrawal { nonce: u32, chain_id: u64, // withdrawal fee, paid to block producer - fee: u64, + fee: U256, // layer1 lock to withdraw after challenge period layer1_owner_lock: Script, // CKB amount diff --git a/contracts/gw-utils/Cargo.toml b/contracts/gw-utils/Cargo.toml index 66943a20..b11e1878 100644 --- a/contracts/gw-utils/Cargo.toml +++ b/contracts/gw-utils/Cargo.toml @@ -8,5 +8,5 @@ edition = "2018" [dependencies] ckb-std = "0.9.0" -gw-types = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0", default-features = false } -gw-common = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0", default-features = false } +gw-types = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268", default-features = false } +gw-common = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268", default-features = false } diff --git a/contracts/state-validator/src/verifications/submit_block.rs b/contracts/state-validator/src/verifications/submit_block.rs index 1a520a31..afdc3291 100644 --- a/contracts/state-validator/src/verifications/submit_block.rs +++ b/contracts/state-validator/src/verifications/submit_block.rs @@ -15,8 +15,8 @@ use gw_utils::gw_types::packed::{L2BlockReader, WithdrawalRequestReader}; // https://nervosnetwork.github.io/ckb-std/riscv64imac-unknown-none-elf/doc/ckb_std/index.html use crate::ckb_std::{ckb_constants::Source, debug}; use gw_state::kv_state::KVState; -use gw_utils::gw_common; -use gw_utils::gw_types; +use gw_utils::gw_common::{self, ckb_decimal}; +use gw_utils::gw_types::{self, U256}; use super::check_status; use crate::types::BlockContext; @@ -261,7 +261,7 @@ fn check_layer2_deposit( }; // mint CKB - kv_state.mint_sudt(CKB_SUDT_ACCOUNT_ID, &address, request.value.capacity.into())?; + kv_state.mint_ckb(&address, ckb_decimal::to_18(request.value.capacity))?; if request.value.sudt_script_hash.as_slice() == CKB_SUDT_SCRIPT_ARGS { if request.value.amount != 0 { // SUDT amount must equals to zero if sudt script hash is equals to CKB_SUDT_SCRIPT_ARGS @@ -299,10 +299,10 @@ fn check_layer2_withdrawal( kv_state: &mut KVState, payer_address: &RegistryAddress, block_producer_address: &RegistryAddress, - amount: u64, + amount: U256, ) -> Result<(), Error> { - kv_state.burn_sudt(CKB_SUDT_ACCOUNT_ID, payer_address, amount.into())?; - kv_state.mint_sudt(CKB_SUDT_ACCOUNT_ID, block_producer_address, amount.into())?; + kv_state.burn_ckb(payer_address, amount)?; + kv_state.mint_ckb(block_producer_address, amount)?; Ok(()) } @@ -335,11 +335,7 @@ fn check_layer2_withdrawal( pay_fee(kv_state, &address, &block_producer_address, fee)?; } // burn CKB - kv_state.burn_sudt( - CKB_SUDT_ACCOUNT_ID, - &address, - raw.capacity().unpack() as u128, - )?; + kv_state.burn_ckb(&address, ckb_decimal::to_18(raw.capacity().unpack()))?; // find Simple UDT account let sudt_id = kv_state .get_account_id_by_script_hash(&l2_sudt_script_hash.into())? From a30c1840559c43699167c7bdff9e41ad01f81d7e Mon Sep 17 00:00:00 2001 From: "zero.qn" Date: Mon, 25 Apr 2022 13:18:31 +0800 Subject: [PATCH 02/16] feat(c contracts): change ckb fee amount to uint256 --- c/Makefile | 12 +-- c/contracts/eth_addr_reg.c | 30 +++++-- c/contracts/meta_contract.c | 22 +++-- c/contracts/sudt.c | 15 ++-- c/generator_utils.h | 5 +- c/godwoken.mol | 4 +- c/gw_def.h | 3 +- c/gw_sudt_ckb_utils.h | 163 ++++++++++++++++++++++++++++++++++++ c/sudt_utils.h | 45 +++++----- c/uint256.h | 100 ++++++++++++++++++++++ c/validator_utils.h | 3 +- 11 files changed, 342 insertions(+), 60 deletions(-) create mode 100644 c/gw_sudt_ckb_utils.h create mode 100644 c/uint256.h diff --git a/c/Makefile b/c/Makefile index 4554c79d..49c2067c 100644 --- a/c/Makefile +++ b/c/Makefile @@ -51,32 +51,32 @@ 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 gw_sudt_ckb_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 gw_sudt_ckb_utils.h gw_def.h validator_utils.h $(CC) $(CFLAGS) $(VALIDATOR_FLAGS) $(LDFLAGS) -o $@ $< $(OBJCOPY) --only-keep-debug $@ $@.debug $(OBJCOPY) --strip-debug --strip-all $@ -build/sudt-generator: contracts/sudt.c sudt_utils.h gw_def.h generator_utils.h +build/sudt-generator: contracts/sudt.c gw_sudt_ckb_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/sudt-validator: contracts/sudt.c sudt_utils.h gw_def.h validator_utils.h +build/sudt-validator: contracts/sudt.c gw_sudt_ckb_utils.h gw_def.h validator_utils.h $(CC) $(CFLAGS) $(VALIDATOR_FLAGS) $(LDFLAGS) -o $@ $< $(OBJCOPY) --only-keep-debug $@ $@.debug $(OBJCOPY) --strip-debug --strip-all $@ -build/eth-addr-reg-generator: contracts/eth_addr_reg.c sudt_utils.h gw_def.h generator_utils.h +build/eth-addr-reg-generator: contracts/eth_addr_reg.c gw_sudt_ckb_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/eth-addr-reg-validator: contracts/eth_addr_reg.c sudt_utils.h gw_def.h validator_utils.h +build/eth-addr-reg-validator: contracts/eth_addr_reg.c gw_sudt_ckb_utils.h gw_def.h validator_utils.h $(CC) $(CFLAGS) $(VALIDATOR_FLAGS) $(LDFLAGS) -o $@ $< $(OBJCOPY) --only-keep-debug $@ $@.debug $(OBJCOPY) --strip-debug --strip-all $@ diff --git a/c/contracts/eth_addr_reg.c b/c/contracts/eth_addr_reg.c index 918c329d..410c4818 100644 --- a/c/contracts/eth_addr_reg.c +++ b/c/contracts/eth_addr_reg.c @@ -13,8 +13,7 @@ */ #include "gw_eth_addr_reg.h" -#include "gw_syscalls.h" -#include "sudt_utils.h" +#include "gw_sudt_ckb_utils.h" /* MSG_TYPE */ #define MSG_QUERY_GW_BY_ETH 0 @@ -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; } @@ -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 ckb_pay_fee(ctx, payer_addr, amount); } int main() { @@ -110,8 +106,16 @@ 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}; + + ret = uint256_from_little_endian(amount_seg.ptr, amount_seg.size, + &fee_amount); + if (ret != 0) { + ckb_debug("failed to fetch uint256 fee amount"); + return ret; + } + ret = handle_fee(&ctx, reg_id, fee_amount); if (ret != 0) { return ret; @@ -139,8 +143,16 @@ 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}; + + ret = uint256_from_little_endian(amount_seg.ptr, amount_seg.size, + &fee_amount); + if (ret != 0) { + ckb_debug("failed to fetch uint256 fee amount"); + return ret; + } + ret = handle_fee(&ctx, reg_id, fee_amount); if (ret != 0) { return ret; diff --git a/c/contracts/meta_contract.c b/c/contracts/meta_contract.c index d56b5dca..9688dd51 100644 --- a/c/contracts/meta_contract.c +++ b/c/contracts/meta_contract.c @@ -7,15 +7,13 @@ */ #include "ckb_syscalls.h" +#include "gw_sudt_ckb_utils.h" #include "gw_syscalls.h" -#include "sudt_utils.h" /* 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; } @@ -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 = ckb_pay_fee(ctx, payer_addr, amount); if (ret != 0) { ckb_debug("failed to pay fee"); return ret; @@ -76,9 +72,17 @@ 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}; + + int ret = uint256_from_little_endian(amount_seg.ptr, amount_seg.size, + &fee_amount); + if (ret != 0) { + ckb_debug("failed to fetch uint256 fee amount"); + return ret; + } + 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; diff --git a/c/contracts/sudt.c b/c/contracts/sudt.c index bda54c81..73a547c6 100644 --- a/c/contracts/sudt.c +++ b/c/contracts/sudt.c @@ -28,9 +28,9 @@ */ #include "ckb_syscalls.h" +#include "gw_sudt_ckb_utils.h" #include "gw_syscalls.h" #include "stdio.h" -#include "sudt_utils.h" /* MSG_TYPE */ #define MSG_QUERY 0 @@ -84,9 +84,15 @@ 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}; + ret = uint256_from_little_endian(fee_amount_seg.ptr, fee_amount_seg.size, + &fee_amount); + if (ret != 0) { + ckb_debug("failed to fetch uint256 fee amount"); + return ret; + } + 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 = @@ -110,8 +116,7 @@ int main() { uint128_t amount = *(uint128_t *)amount_seg.ptr; /* pay fee */ - uint32_t ckb_sudt_id = CKB_SUDT_ACCOUNT_ID; - ret = sudt_pay_fee(&ctx, ckb_sudt_id, from_addr, fee_amount); + ret = ckb_pay_fee(&ctx, from_addr, fee_amount); if (ret != 0) { printf("pay fee failed"); return ret; diff --git a/c/generator_utils.h b/c/generator_utils.h index 4ed26b9c..d6cad429 100644 --- a/c/generator_utils.h +++ b/c/generator_utils.h @@ -13,6 +13,7 @@ #include "ckb_syscalls.h" #include "gw_def.h" +#include "uint256.h" /* syscalls */ /* Syscall account store / load / create */ @@ -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; } @@ -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) { diff --git a/c/godwoken.mol b/c/godwoken.mol index 152b9a16..e7f0a396 100644 --- a/c/godwoken.mol +++ b/c/godwoken.mol @@ -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: Uint256, } vector WithdrawalRequestVec ; @@ -237,7 +237,7 @@ struct Fee { // registry id registry_id: Uint32, // amount in CKB - amount: Uint64, + amount: Uint256, } table CreateAccount { diff --git a/c/gw_def.h b/c/gw_def.h index 5be40169..ebc9ee75 100644 --- a/c/gw_def.h +++ b/c/gw_def.h @@ -3,6 +3,7 @@ #include "gw_registry_addr.h" #include "stddef.h" +#include "uint256.h" typedef unsigned __int128 uint128_t; @@ -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 diff --git a/c/gw_sudt_ckb_utils.h b/c/gw_sudt_ckb_utils.h new file mode 100644 index 00000000..b24b3b0c --- /dev/null +++ b/c/gw_sudt_ckb_utils.h @@ -0,0 +1,163 @@ +#include "godwoken.h" +#include "gw_def.h" +#include "gw_registry_addr.h" +#include "gw_syscalls.h" +#include "sudt_utils.h" +#include "uint256.h" + +/* format: + * from_addr | to_addr | amount(32 bytes) + */ +int _ckb_emit_log(gw_context_t *ctx, gw_reg_addr_t from_addr, + gw_reg_addr_t to_addr, const uint256_t amount, + uint8_t service_flag) { +#ifdef GW_VALIDATOR + uint32_t data_size = 0; + uint8_t *data = NULL; +#else + uint8_t data[256] = {0}; + /* from_addr + to_addr + amount(32 bytes) */ + uint32_t data_size = + GW_REG_ADDR_SIZE(from_addr) + GW_REG_ADDR_SIZE(to_addr) + 32; + if (data_size > 256) { + printf("_ckb_emit_log: data is large than buffer"); + return GW_FATAL_BUFFER_OVERFLOW; + } + _gw_cpy_addr(data, from_addr); + _gw_cpy_addr(data + GW_REG_ADDR_SIZE(from_addr), to_addr); + memcpy(data + GW_REG_ADDR_SIZE(from_addr) + GW_REG_ADDR_SIZE(to_addr), + (uint8_t *)(&amount), 32); +#endif + return ctx->sys_log(ctx, CKB_SUDT_ACCOUNT_ID, service_flag, data_size, data); +} + +int _ckb_get_balance(gw_context_t *ctx, gw_reg_addr_t address, + uint256_t *balance) { + uint8_t key[64] = {0}; + uint32_t key_len = 64; + int ret = _sudt_build_key(SUDT_KEY_FLAG_BALANCE, address, key, &key_len); + if (ret != 0) { + return ret; + } + uint8_t value[32] = {0}; + ret = ctx->sys_load(ctx, CKB_SUDT_ACCOUNT_ID, key, key_len, value); + if (ret != 0) { + return ret; + } + uint256_from_little_endian((uint8_t *)&value, 32, balance); + return 0; +} + +int _ckb_set_balance(gw_context_t *ctx, gw_reg_addr_t address, + uint256_t balance) { + uint8_t key[64] = {0}; + uint32_t key_len = 64; + int ret = _sudt_build_key(SUDT_KEY_FLAG_BALANCE, address, key, &key_len); + if (ret != 0) { + return ret; + } + + uint8_t value[32] = {0}; + uint256_to_little_endian(balance, (uint8_t *)&value, 32); + ret = ctx->sys_store(ctx, CKB_SUDT_ACCOUNT_ID, key, key_len, value); + return ret; +} + +int ckb_get_balance(gw_context_t *ctx, gw_reg_addr_t addr, uint256_t *balance) { + return _ckb_get_balance(ctx, addr, balance); +} + +int _ckb_get_total_supply(gw_context_t *ctx, uint8_t total_supply[32]) { + return ctx->sys_load(ctx, CKB_SUDT_ACCOUNT_ID, SUDT_TOTAL_SUPPLY_KEY, 32, + total_supply); +} + +int ckb_get_total_supply(gw_context_t *ctx, uint8_t total_supply[32]) { + return _ckb_get_total_supply(ctx, total_supply); +} + +int _ckb_transfer(gw_context_t *ctx, gw_reg_addr_t from_addr, + gw_reg_addr_t to_addr, const uint256_t amount, + uint8_t service_flag) { + int ret; + + /* check from account */ + uint256_t from_balance = {0}; + ret = _ckb_get_balance(ctx, from_addr, &from_balance); + if (ret != 0) { + printf("transfer: can't get sender's balance"); + return ret; + } + if (uint256_cmp(from_balance, amount) == SMALLER) { + printf("transfer: insufficient balance"); + return GW_SUDT_ERROR_INSUFFICIENT_BALANCE; + } + + if (_gw_cmp_addr(from_addr, to_addr) == 0) { + printf("transfer: [warning] transfer to self"); + } + + uint256_t new_from_balance = {0}; + uint256_underflow_sub(from_balance, amount, &new_from_balance); + + /* update sender balance */ + ret = _ckb_set_balance(ctx, from_addr, new_from_balance); + if (ret != 0) { + printf("transfer: update sender's balance failed"); + return ret; + } + + /* check to account */ + uint256_t to_balance = {0}; + ret = _ckb_get_balance(ctx, to_addr, &to_balance); + if (ret != 0) { + printf("transfer: can't get receiver's balance"); + return ret; + } + + uint256_t new_to_balance = {0}; + int overflow = uint256_overflow_add(to_balance, amount, &new_to_balance); + if (overflow) { + printf("transfer: balance overflow"); + return GW_SUDT_ERROR_AMOUNT_OVERFLOW; + } + + /* update receiver balance */ + ret = _ckb_set_balance(ctx, to_addr, new_to_balance); + if (ret != 0) { + printf("transfer: update receiver's balance failed"); + return ret; + } + + /* emit log */ + ret = _ckb_emit_log(ctx, from_addr, to_addr, amount, service_flag); + if (ret != 0) { + printf("transfer: emit log failed"); + } + return ret; +} + +int ckb_transfer(gw_context_t *ctx, gw_reg_addr_t from_addr, + gw_reg_addr_t to_addr, const uint256_t amount) { + return _ckb_transfer(ctx, from_addr, to_addr, amount, GW_LOG_SUDT_TRANSFER); +} + +/* Pay fee */ +int ckb_pay_fee(gw_context_t *ctx, gw_reg_addr_t from_addr, + const uint256_t amount) { + int ret = _ckb_transfer(ctx, from_addr, ctx->block_info.block_producer, + amount, GW_LOG_SUDT_PAY_FEE); + if (ret != 0) { + printf("pay fee transfer failed"); + return ret; + } + + /* call syscall, we use this action to emit event to runtime, this function + do + * not actually pay the fee */ + ret = ctx->sys_pay_fee(ctx, from_addr, CKB_SUDT_ACCOUNT_ID, amount); + if (ret != 0) { + printf("sys pay fee failed"); + } + return ret; +} diff --git a/c/sudt_utils.h b/c/sudt_utils.h index 94523632..3c4ed8fc 100644 --- a/c/sudt_utils.h +++ b/c/sudt_utils.h @@ -35,12 +35,6 @@ #include "gw_def.h" #include "gw_registry_addr.h" #include "overflow_add.h" -#include "stdio.h" - -/* Prepare withdrawal fields */ -#define WITHDRAWAL_LOCK_HASH 1 -#define WITHDRAWAL_AMOUNT 2 -#define WITHDRAWAL_BLOCK_NUMBER 3 #define CKB_SUDT_ACCOUNT_ID 1 #define SUDT_KEY_FLAG_BALANCE 1 @@ -222,22 +216,23 @@ int sudt_transfer(gw_context_t *ctx, const uint32_t sudt_id, } /* Pay fee */ -int sudt_pay_fee(gw_context_t *ctx, const uint32_t sudt_id, - gw_reg_addr_t from_addr, const uint128_t amount) { - /* transfer SUDT */ - int ret = - _sudt_transfer(ctx, sudt_id, from_addr, ctx->block_info.block_producer, - amount, GW_LOG_SUDT_PAY_FEE); - if (ret != 0) { - printf("pay fee transfer failed"); - return ret; - } - - /* call syscall, we use this action to emit event to runtime, this function do - * not actually pay the fee */ - ret = ctx->sys_pay_fee(ctx, from_addr, sudt_id, amount); - if (ret != 0) { - printf("sys pay fee failed"); - } - return ret; -} +// int sudt_pay_fee(gw_context_t *ctx, const uint32_t sudt_id, +// gw_reg_addr_t from_addr, const uint128_t amount) { +// /* transfer SUDT */ +// int ret = +// _sudt_transfer(ctx, sudt_id, from_addr, ctx->block_info.block_producer, +// amount, GW_LOG_SUDT_PAY_FEE); +// if (ret != 0) { +// printf("pay fee transfer failed"); +// return ret; +// } +// +// /* call syscall, we use this action to emit event to runtime, this function +// do +// * not actually pay the fee */ +// ret = ctx->sys_pay_fee(ctx, from_addr, sudt_id, amount); +// if (ret != 0) { +// printf("sys pay fee failed"); +// } +// return ret; +// } diff --git a/c/uint256.h b/c/uint256.h new file mode 100644 index 00000000..56cdad7d --- /dev/null +++ b/c/uint256.h @@ -0,0 +1,100 @@ +#ifndef GW_UINT256_H_ +#define GW_UINT256_H_ + +#define MAX_UINT32 ((uint64_t)0xFFFFFFFF) + +typedef struct uint256_t { + uint32_t array[8]; +} uint256_t; + +int uint256_from_little_endian(uint8_t* bytes, int size, uint256_t* num) { + if (size < 32) { + return 1; + } + + for (int i = 0; i < 8; ++i) { + num->array[i] = *(uint32_t*)&bytes[i * 4]; + } + + return 0; +} + +int uint256_to_little_endian(const uint256_t num, uint8_t* bytes, int size) { + if (size < 32) { + return 1; + } + + for (int i = 0; i < 8; ++i) { + *(uint32_t*)&bytes[i * 4] = num.array[i]; + } + + return 0; +} + +void uint256_zero(uint256_t* num) { + for (int i = 0; i < 8; ++i) { + num->array[i] = 0; + } +} + +void uint256_one(uint256_t* num) { + uint256_zero(num); + num->array[0] = 1; +} + +void uint256_max(uint256_t* num) { + for (int i = 0; i < 8; ++i) { + num->array[i] = (uint32_t)0xFFFFFFFF; + } +} + +int uint256_overflow_add(const uint256_t a, const uint256_t b, uint256_t* sum) { + uint64_t tmp; + + int carry = 0; + uint256_zero(sum); + + for (int i = 0; i < 8; ++i) { + tmp = (uint64_t)a.array[i] + b.array[i] + carry; + carry = (tmp > MAX_UINT32); + sum->array[i] = (tmp & MAX_UINT32); + } + + return carry; +} + +int uint256_underflow_sub(const uint256_t a, const uint256_t b, + uint256_t* rem) { + uint64_t res; + uint64_t tmp1; + uint64_t tmp2; + + int borrow = 0; + uint256_zero(rem); + + for (int i = 0; i < 8; ++i) { + tmp1 = (uint64_t)a.array[i] + (MAX_UINT32 + 1); + tmp2 = (uint64_t)b.array[i] + borrow; + res = (tmp1 - tmp2); + rem->array[i] = (uint32_t)(res & MAX_UINT32); + borrow = (res <= MAX_UINT32); + } + + return borrow; +} + +enum { SMALLER = -1, EQUAL = 0, LARGER = 1 }; + +int uint256_cmp(const uint256_t a, const uint256_t b) { + for (int i = 7; i >= 0; --i) { + if (a.array[i] > b.array[i]) { + return LARGER; + } else if (a.array[i] < b.array[i]) { + return SMALLER; + } + } + + return EQUAL; +} + +#endif diff --git a/c/validator_utils.h b/c/validator_utils.h index 017ae22d..db919537 100644 --- a/c/validator_utils.h +++ b/c/validator_utils.h @@ -6,6 +6,7 @@ #include "ckb_smt.h" #include "ckb_syscalls.h" #include "gw_def.h" +#include "uint256.h" #define SCRIPT_HASH_TYPE_DATA 0 #define SCRIPT_HASH_TYPE_TYPE 1 @@ -657,7 +658,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 payer_addr, uint32_t sudt_id, - uint128_t amount) { + uint256_t amount) { if (ctx == NULL) { return GW_FATAL_INVALID_CONTEXT; } From 1efd33d51a8578e3fb6ec93cb804dfa992de9334 Mon Sep 17 00:00:00 2001 From: "zero.qn" Date: Mon, 25 Apr 2022 13:33:59 +0800 Subject: [PATCH 03/16] fix(tests): change fee to U256 --- Cargo.lock | 381 ++++++++++++++++-- tests/Cargo.toml | 22 +- tests/src/script_tests/l2_scripts/examples.rs | 9 +- .../script_tests/l2_scripts/meta_contract.rs | 7 +- tests/src/script_tests/l2_scripts/mod.rs | 26 +- tests/src/script_tests/l2_scripts/sudt.rs | 53 +-- .../cancel_challenge/tx_execution.rs | 3 +- .../cancel_challenge/tx_signature.rs | 3 +- .../script_tests/state_validator/revert.rs | 3 +- 9 files changed, 421 insertions(+), 86 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ebdee0b3..40ba1f81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aead" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b613b8e1e3cf911a086f53f03bf286f52fd7a7258e4fa606f0ef220d39d8877" +dependencies = [ + "generic-array 0.14.5", +] + [[package]] name = "ahash" version = "0.7.6" @@ -281,6 +290,15 @@ dependencies = [ "generic-array 0.14.5", ] +[[package]] +name = "block-buffer" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324" +dependencies = [ + "generic-array 0.14.5", +] + [[package]] name = "block-padding" version = "0.1.5" @@ -302,6 +320,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "476e9cd489f9e121e02ffa6014a8ef220ecb15c05ed23fc34cca13925dc283fb" +[[package]] +name = "bs58" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" + [[package]] name = "bstr" version = "0.2.17" @@ -402,6 +426,31 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chacha20" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01b72a433d0cf2aef113ba70f62634c56fddb0f244e6377185c56a7cadbd8f91" +dependencies = [ + "cfg-if 1.0.0", + "cipher", + "cpufeatures", + "zeroize", +] + +[[package]] +name = "chacha20poly1305" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b84ed6d1d5f7aa9bdde921a5090e0ca4d934d250ea3b402a5fab3a994e28a2a" +dependencies = [ + "aead", + "chacha20", + "cipher", + "poly1305", + "zeroize", +] + [[package]] name = "chrono" version = "0.4.19" @@ -415,6 +464,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "cipher" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7" +dependencies = [ + "generic-array 0.14.5", +] + [[package]] name = "ckb-chain-spec" version = "0.100.0" @@ -940,6 +998,16 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "crypto-common" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8" +dependencies = [ + "generic-array 0.14.5", + "typenum", +] + [[package]] name = "crypto-mac" version = "0.11.1" @@ -956,6 +1024,19 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" +[[package]] +name = "curve25519-dalek" +version = "3.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90f9d052967f590a76e62eb387bd0bbb1b000182c3cefe5364db6b7211651bc0" +dependencies = [ + "byteorder", + "digest 0.9.0", + "rand_core 0.5.1", + "subtle", + "zeroize", +] + [[package]] name = "debugid" version = "0.7.3" @@ -997,6 +1078,17 @@ dependencies = [ "generic-array 0.14.5", ] +[[package]] +name = "digest" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506" +dependencies = [ + "block-buffer 0.10.2", + "crypto-common", + "subtle", +] + [[package]] name = "dirs" version = "4.0.0" @@ -1244,7 +1336,7 @@ dependencies = [ "futures-core", "futures-sink", "pin-project", - "spin", + "spin 0.9.2", ] [[package]] @@ -1448,8 +1540,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" dependencies = [ "cfg-if 1.0.0", + "js-sys", "libc", "wasi 0.9.0+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -1519,7 +1613,7 @@ dependencies = [ [[package]] name = "gw-block-producer" version = "1.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "async-channel", @@ -1542,6 +1636,7 @@ dependencies = [ "gw-generator", "gw-jsonrpc-types", "gw-mem-pool", + "gw-p2p-network", "gw-rpc-client", "gw-rpc-server", "gw-rpc-ws-server", @@ -1563,6 +1658,7 @@ dependencies = [ "serde", "serde_json", "sqlx", + "tentacle", "thiserror", "tikv-jemallocator", "tokio", @@ -1576,7 +1672,7 @@ dependencies = [ [[package]] name = "gw-chain" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "ckb-fixed-hash", @@ -1602,7 +1698,7 @@ dependencies = [ [[package]] name = "gw-challenge" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "arc-swap", @@ -1634,7 +1730,7 @@ dependencies = [ [[package]] name = "gw-ckb-hardfork" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "arc-swap", "ckb-types", @@ -1645,13 +1741,12 @@ dependencies = [ [[package]] name = "gw-common" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "cfg-if 0.1.10", "gw-hash", "gw-types", "merkle-cbt", - "primitive-types", "sparse-merkle-tree", "thiserror", ] @@ -1659,7 +1754,7 @@ dependencies = [ [[package]] name = "gw-config" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "ckb-fixed-hash", "gw-jsonrpc-types", @@ -1670,7 +1765,7 @@ dependencies = [ [[package]] name = "gw-db" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "ckb-rocksdb", "gw-config", @@ -1684,7 +1779,7 @@ dependencies = [ [[package]] name = "gw-dynamic-config" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "arc-swap", @@ -1700,7 +1795,7 @@ dependencies = [ [[package]] name = "gw-generator" version = "0.2.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "arc-swap", @@ -1729,7 +1824,7 @@ dependencies = [ [[package]] name = "gw-hash" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "blake2b-ref", ] @@ -1737,7 +1832,7 @@ dependencies = [ [[package]] name = "gw-jsonrpc-types" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "ckb-fixed-hash", @@ -1751,7 +1846,7 @@ dependencies = [ [[package]] name = "gw-mem-pool" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "async-trait", @@ -1761,6 +1856,7 @@ dependencies = [ "gw-config", "gw-dynamic-config", "gw-generator", + "gw-p2p-network", "gw-rpc-client", "gw-rpc-ws-server", "gw-store", @@ -1770,14 +1866,30 @@ dependencies = [ "hex", "log", "rdkafka", + "tentacle", "tokio", "tracing", ] +[[package]] +name = "gw-p2p-network" +version = "0.1.0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +dependencies = [ + "anyhow", + "async-trait", + "gw-config", + "gw-types", + "log", + "socket2", + "tentacle", + "tokio", +] + [[package]] name = "gw-rpc-client" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "arc-swap", @@ -1811,7 +1923,7 @@ dependencies = [ [[package]] name = "gw-rpc-server" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "async-channel", @@ -1859,7 +1971,7 @@ dependencies = [ [[package]] name = "gw-rpc-ws-server" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "async-channel", @@ -1884,7 +1996,7 @@ dependencies = [ [[package]] name = "gw-store" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "arc-swap", @@ -1933,7 +2045,7 @@ dependencies = [ "secp256k1 0.20.3", "sha3", "tempfile", - "tentacle-multiaddr", + "tentacle-multiaddr 0.2.0", "thiserror", "tokio", ] @@ -1941,7 +2053,7 @@ dependencies = [ [[package]] name = "gw-traits" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "gw-common", "gw-db", @@ -1951,7 +2063,7 @@ dependencies = [ [[package]] name = "gw-tx-filter" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "gw-common", "gw-config", @@ -1965,20 +2077,21 @@ dependencies = [ [[package]] name = "gw-types" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "cfg-if 0.1.10", "ckb-fixed-hash", "ckb-types", "gw-hash", "molecule 0.7.2", + "primitive-types", "sparse-merkle-tree", ] [[package]] name = "gw-utils" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "ckb-crypto", @@ -1994,7 +2107,7 @@ dependencies = [ [[package]] name = "gw-version" version = "1.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", ] @@ -2002,7 +2115,7 @@ dependencies = [ [[package]] name = "gw-web3-indexer" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=b4db76522b7982f650cc5a6053d5f3957b7cbfd0#b4db76522b7982f650cc5a6053d5f3957b7cbfd0" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" dependencies = [ "anyhow", "ckb-hash", @@ -2122,6 +2235,15 @@ dependencies = [ "digest 0.9.0", ] +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.3", +] + [[package]] name = "hostname" version = "0.3.1" @@ -2813,6 +2935,12 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +[[package]] +name = "nohash-hasher" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" + [[package]] name = "nom" version = "7.1.1" @@ -3284,6 +3412,17 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" +[[package]] +name = "poly1305" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "048aeb476be11a4b6ca432ca569e375810de9294ae78f4774e78ea98a9246ede" +dependencies = [ + "cpufeatures", + "opaque-debug 0.3.0", + "universal-hash", +] + [[package]] name = "pprof" version = "0.6.2" @@ -3780,6 +3919,21 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "ring" +version = "0.16.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin 0.5.2", + "untrusted", + "web-sys", + "winapi 0.3.9", +] + [[package]] name = "rlp" version = "0.5.1" @@ -4129,6 +4283,17 @@ dependencies = [ "opaque-debug 0.3.0", ] +[[package]] +name = "sha2" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55deaec60f81eefe3cce0dc50bda92d6d8e88f2a27df7c5033b42afeb1ed2676" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.10.3", +] + [[package]] name = "sha3" version = "0.9.1" @@ -4202,6 +4367,12 @@ dependencies = [ "cfg-if 0.1.10", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "spin" version = "0.9.2" @@ -4257,7 +4428,7 @@ dependencies = [ "futures-util", "hashlink", "hex", - "hmac", + "hmac 0.11.0", "indexmap", "itoa 1.0.1", "libc", @@ -4274,7 +4445,7 @@ dependencies = [ "serde", "serde_json", "sha-1 0.9.8", - "sha2", + "sha2 0.9.9", "smallvec", "sqlformat", "sqlx-rt", @@ -4297,7 +4468,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "sha2", + "sha2 0.9.9", "sqlx-core", "sqlx-rt", "syn", @@ -4390,6 +4561,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + [[package]] name = "tap" version = "1.0.1" @@ -4410,17 +4593,85 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "tentacle" +version = "0.4.0-beta.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863528f8370d37ef73e403c59699e0383871f50b464a96262b7320303313255e" +dependencies = [ + "async-trait", + "bytes 1.1.0", + "futures", + "js-sys", + "libc", + "log", + "molecule 0.7.2", + "nohash-hasher", + "once_cell", + "rand 0.8.5", + "socket2", + "tentacle-multiaddr 0.3.2", + "tentacle-secio", + "thiserror", + "tokio", + "tokio-util 0.7.1", + "tokio-yamux", + "wasm-bindgen", + "wasm-bindgen-futures", + "winapi 0.3.9", +] + [[package]] name = "tentacle-multiaddr" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed6476818af8d86ba63c7f961c417c70906700166dd8fdef8b9ac7f47c629f3f" dependencies = [ - "bs58", + "bs58 0.3.1", "bytes 0.5.6", "serde", - "sha2", - "unsigned-varint", + "sha2 0.9.9", + "unsigned-varint 0.3.3", +] + +[[package]] +name = "tentacle-multiaddr" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faafcea0d508fc5296ca846ca1f2fcb60d740f53b02e771b407c06ae13e23c90" +dependencies = [ + "bs58 0.4.0", + "bytes 1.1.0", + "serde", + "sha2 0.10.2", + "unsigned-varint 0.7.1", +] + +[[package]] +name = "tentacle-secio" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a25404f0ad099d7ac35c4873e854a1f9772014b1c42379780f7b9f55fa464913" +dependencies = [ + "bs58 0.4.0", + "bytes 1.1.0", + "chacha20poly1305", + "futures", + "hmac 0.12.1", + "log", + "molecule 0.7.2", + "openssl", + "openssl-sys", + "rand 0.7.3", + "rand 0.8.5", + "rand_core 0.5.1", + "ring", + "secp256k1 0.20.3", + "sha2 0.10.2", + "tokio", + "tokio-util 0.7.1", + "unsigned-varint 0.7.1", + "x25519-dalek", ] [[package]] @@ -4650,6 +4901,20 @@ dependencies = [ "tracing", ] +[[package]] +name = "tokio-yamux" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "129b802888b976c5b14aefc5f11d563ed25eaceb74a9e6e03d1d1de50d994cc7" +dependencies = [ + "bytes 1.1.0", + "futures", + "log", + "nohash-hasher", + "tokio", + "tokio-util 0.7.1", +] + [[package]] name = "toml" version = "0.5.8" @@ -4902,12 +5167,34 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" +[[package]] +name = "universal-hash" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f214e8f697e925001e66ec2c6e37a4ef93f0f78c2eed7814394e10c62025b05" +dependencies = [ + "generic-array 0.14.5", + "subtle", +] + [[package]] name = "unsigned-varint" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f67332660eb59a6f1eb24ff1220c9e8d01738a8503c6002e30bcfe4bd9f2b4a9" +[[package]] +name = "unsigned-varint" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d86a8dc7f45e4c1b0d30e43038c38f274e77af056aa5f74b93c2cf9eb3c1c836" + +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + [[package]] name = "url" version = "2.2.2" @@ -5201,3 +5488,35 @@ name = "wyz" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" + +[[package]] +name = "x25519-dalek" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2392b6b94a576b4e2bf3c5b2757d63f10ada8020a2e4d08ac849ebcf6ea8e077" +dependencies = [ + "curve25519-dalek", + "rand_core 0.5.1", + "zeroize", +] + +[[package]] +name = "zeroize" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4756f7db3f7b5574938c3eb1c117038b8e07f95ee6718c0efad4ac21508f1efd" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f8f187641dad4f680d25c4bfc4225b418165984179f26ca76ec4fb6441d3a17" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] diff --git a/tests/Cargo.toml b/tests/Cargo.toml index f30b8ee7..90f2a794 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -7,17 +7,17 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -gw-types = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-common = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-config = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-db = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-store = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-traits = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-generator = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-chain = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-mem-pool = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-block-producer = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } -gw-ckb-hardfork = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "b4db76522b7982f650cc5a6053d5f3957b7cbfd0" } +gw-types = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-common = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-config = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-db = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-store = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-traits = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-generator = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-chain = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-mem-pool = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-block-producer = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } +gw-ckb-hardfork = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "1090567d2c54384f5456a0363e5574e0af1b7268" } parking_lot = "0.11" anyhow = "1.0" blake2b-rs = "0.2" diff --git a/tests/src/script_tests/l2_scripts/examples.rs b/tests/src/script_tests/l2_scripts/examples.rs index 5fc70e2d..4fb7d51e 100644 --- a/tests/src/script_tests/l2_scripts/examples.rs +++ b/tests/src/script_tests/l2_scripts/examples.rs @@ -8,7 +8,7 @@ use super::{ }; use gw_common::{ builtins::ETH_REGISTRY_ACCOUNT_ID, h256_ext::H256Ext, registry_address::RegistryAddress, - state::State, H256, U256, + state::State, H256, }; use gw_config::BackendType; use gw_generator::{ @@ -27,6 +27,7 @@ use gw_types::{ core::ScriptHashType, packed::{RawL2Transaction, RollupConfig, Script}, prelude::*, + U256, }; #[test] @@ -345,11 +346,13 @@ fn test_example_account_operation() { let registry_id = ETH_REGISTRY_ACCOUNT_ID; let from_addr = RegistryAddress::new(registry_id, vec![0x33u8; 20]); let to_addr = RegistryAddress::new(registry_id, vec![0x44u8; 20]); - let amount: u128 = 101; + let amount: U256 = 101u64.into(); + let mut buf = [0u8; 32]; + amount.to_little_endian(&mut buf); let mut data = Vec::default(); data.extend(from_addr.to_bytes()); data.extend(to_addr.to_bytes()); - data.extend(&amount.to_le_bytes()[..]); + data.extend(&buf); let args = AccountOp::Log { service_flag: GW_LOG_SUDT_TRANSFER, account_id, diff --git a/tests/src/script_tests/l2_scripts/meta_contract.rs b/tests/src/script_tests/l2_scripts/meta_contract.rs index dbfef5d7..eaf5b7d4 100644 --- a/tests/src/script_tests/l2_scripts/meta_contract.rs +++ b/tests/src/script_tests/l2_scripts/meta_contract.rs @@ -12,6 +12,7 @@ use gw_generator::{ syscalls::error_codes::{GW_ERROR_DUPLICATED_SCRIPT_HASH, GW_SUDT_ERROR_INSUFFICIENT_BALANCE}, traits::StateExt, }; +use gw_types::U256; use gw_types::{ core::ScriptHashType, packed::{AllowedTypeHash, CreateAccount, Fee, MetaContractArgs, RollupConfig, Script}, @@ -55,7 +56,7 @@ fn test_meta_contract() { .script(contract_script.clone()) .fee( Fee::new_builder() - .amount(1000u64.pack()) + .amount(U256::from(1000u64).pack()) .registry_id(ctx.eth_registry_id.pack()) .build(), ) @@ -136,7 +137,7 @@ fn test_duplicated_script_hash() { .script(contract_script.clone()) .fee( Fee::new_builder() - .amount(1000u64.pack()) + .amount(U256::from(1000u64).pack()) .registry_id(a_address.registry_id.pack()) .build(), ) @@ -191,7 +192,7 @@ fn test_insufficient_balance_to_pay_fee() { .script(contract_script.clone()) .fee( Fee::new_builder() - .amount(1000u64.pack()) + .amount(U256::from(1000u64).pack()) .registry_id(ctx.eth_registry_id.pack()) .build(), ) diff --git a/tests/src/script_tests/l2_scripts/mod.rs b/tests/src/script_tests/l2_scripts/mod.rs index c6f96d9d..9504a5b7 100644 --- a/tests/src/script_tests/l2_scripts/mod.rs +++ b/tests/src/script_tests/l2_scripts/mod.rs @@ -8,6 +8,7 @@ use gw_generator::{account_lock_manage::AccountLockManage, Generator}; use gw_generator::{error::TransactionError, traits::StateExt}; use gw_traits::{ChainView, CodeStore}; use gw_types::offchain::RollupContext; +use gw_types::U256; use gw_types::{ bytes::Bytes, offchain::RunResult, @@ -143,7 +144,7 @@ pub struct SudtLog { sudt_id: u32, from_addr: RegistryAddress, to_addr: RegistryAddress, - amount: u128, + amount: U256, log_type: SudtLogType, } @@ -154,7 +155,7 @@ impl SudtLog { let raw_data = item.data().raw_data(); let data: &[u8] = raw_data.as_ref(); let log_type = SudtLogType::from_u8(service_flag)?; - if data.len() > (1 + 32 + 32 + 16) { + if data.len() > (1 + 32 + 32 + 32) { return Err(format!("Invalid data length: {}", data.len())); } let from_addr = { @@ -173,9 +174,16 @@ impl SudtLog { ) }; - let mut u128_bytes = [0u8; 16]; - u128_bytes.copy_from_slice(&data[data.len() - 16..]); - let amount = u128::from_le_bytes(u128_bytes); + let amount: U256 = if data.len() > (1 + 32 + 32 + 16) { + let mut u256_bytes = [0u8; 32]; + u256_bytes.copy_from_slice(&data[data.len() - 32..]); + U256::from_little_endian(&u256_bytes) + } else { + let mut u128_bytes = [0u8; 16]; + u128_bytes.copy_from_slice(&data[data.len() - 16..]); + u128::from_le_bytes(u128_bytes).into() + }; + Ok(SudtLog { sudt_id, from_addr, @@ -190,24 +198,24 @@ pub fn check_transfer_logs( logs: &[LogItem], sudt_id: u32, block_producer_addr: &RegistryAddress, - fee: u64, + fee: impl Into, from_addr: &RegistryAddress, to_addr: &RegistryAddress, - amount: u128, + amount: impl Into, ) { // pay fee log let sudt_fee_log = SudtLog::from_log_item(&logs[0]).unwrap(); assert_eq!(sudt_fee_log.sudt_id, CKB_SUDT_ACCOUNT_ID); assert_eq!(&sudt_fee_log.from_addr, from_addr,); assert_eq!(&sudt_fee_log.to_addr, block_producer_addr); - assert_eq!(sudt_fee_log.amount, fee as u128); + assert_eq!(sudt_fee_log.amount, fee.into()); assert_eq!(sudt_fee_log.log_type, SudtLogType::PayFee); // transfer to `to_id` let sudt_transfer_log = SudtLog::from_log_item(&logs[1]).unwrap(); assert_eq!(sudt_transfer_log.sudt_id, sudt_id); assert_eq!(&sudt_transfer_log.from_addr, from_addr); assert_eq!(&sudt_transfer_log.to_addr, to_addr); - assert_eq!(sudt_transfer_log.amount, amount); + assert_eq!(sudt_transfer_log.amount, amount.into()); assert_eq!(sudt_transfer_log.log_type, SudtLogType::Transfer); } diff --git a/tests/src/script_tests/l2_scripts/sudt.rs b/tests/src/script_tests/l2_scripts/sudt.rs index d588744c..49311400 100644 --- a/tests/src/script_tests/l2_scripts/sudt.rs +++ b/tests/src/script_tests/l2_scripts/sudt.rs @@ -12,6 +12,7 @@ use gw_generator::syscalls::error_codes::{ use gw_generator::{error::TransactionError, traits::StateExt}; use gw_traits::CodeStore; use gw_types::packed::{BlockInfo, Fee}; +use gw_types::U256; use gw_types::{ core::ScriptHashType, packed::{RollupConfig, SUDTArgs, SUDTQuery, SUDTTransfer, Script}, @@ -99,9 +100,9 @@ fn test_sudt() { .expect("init balance"); // init ckb for a to pay fee - let init_ckb = 100; + let init_ckb: U256 = 100u64.into(); ctx.state - .mint_sudt(CKB_SUDT_ACCOUNT_ID, &a_address, init_ckb) + .mint_ckb(&a_address, init_ckb) .expect("init balance"); // check balance of A, B @@ -130,7 +131,7 @@ fn test_sudt() { // transfer from A to B { let value = 4000u128; - let fee = 42u64; + let fee: U256 = 42u64.into(); let sender_nonce = ctx.state.get_nonce(a_id).unwrap(); let args = SUDTArgs::new_builder() .set( @@ -178,7 +179,7 @@ fn test_sudt() { a_id, sudt_id, &a_address, - init_a_balance - value as u128, + init_a_balance - value, ); // check sender's ckb @@ -189,7 +190,7 @@ fn test_sudt() { a_id, CKB_SUDT_ACCOUNT_ID, &a_address, - init_ckb - fee as u128, + init_ckb - fee, ); // check receiver's sudt @@ -233,7 +234,7 @@ fn test_sudt() { a_id, CKB_SUDT_ACCOUNT_ID, &block_producer, - fee as u128, + fee, ); } } @@ -419,7 +420,7 @@ fn test_transfer_to_non_exist_account() { #[test] fn test_transfer_to_self() { let init_a_balance: u128 = 10000; - let init_ckb: u128 = 100; + let init_ckb: U256 = 100u64.into(); let rollup_config = RollupConfig::new_builder() .l2_sudt_validator_script_type_hash(DUMMY_SUDT_VALIDATOR_SCRIPT_TYPE_HASH.pack()) @@ -476,7 +477,7 @@ fn test_transfer_to_self() { .get_script_hash(block_producer_id) .expect("get script hash"); let block_producer = ctx.create_eth_address(block_producer_script_hash.into(), [42u8; 20]); - let block_producer_balance = 0; + let block_producer_balance = 0u128; let block_info = new_block_info(&block_producer, 10, 0); // init balance for a @@ -485,13 +486,13 @@ fn test_transfer_to_self() { .expect("init balance"); ctx.state - .mint_sudt(CKB_SUDT_ACCOUNT_ID, &a_address, init_ckb) + .mint_ckb(&a_address, init_ckb) .expect("init balance"); // transfer from A to A, zero value { let value: u128 = 0; - let fee: u64 = 0; + let fee: U256 = 0u64.into(); let sender_nonce = ctx.state.get_nonce(a_id).unwrap(); let args = SUDTArgs::new_builder() .set( @@ -549,7 +550,7 @@ fn test_transfer_to_self() { } // transfer from A to A, normal value - let fee: u64 = 20; + let fee: U256 = 20u64.into(); { let value: u128 = 1000; let args = SUDTArgs::new_builder() @@ -594,7 +595,7 @@ fn test_transfer_to_self() { a_id, sudt_id, &a_address, - init_a_balance as u128, + init_a_balance, ); // sender's ckb balance @@ -605,7 +606,7 @@ fn test_transfer_to_self() { a_id, CKB_SUDT_ACCOUNT_ID, &a_address, - init_ckb - fee as u128, + init_ckb - fee, ); // block producer's balance @@ -616,7 +617,7 @@ fn test_transfer_to_self() { a_id, sudt_id, &block_producer, - block_producer_balance as u128, + block_producer_balance, ); // block producer's balance @@ -627,7 +628,7 @@ fn test_transfer_to_self() { a_id, CKB_SUDT_ACCOUNT_ID, &block_producer, - fee as u128, + fee, ); } @@ -669,7 +670,7 @@ fn test_transfer_to_self() { a_id, sudt_id, &a_address, - init_a_balance as u128, + init_a_balance, ); // sender's ckb @@ -680,7 +681,7 @@ fn test_transfer_to_self() { a_id, CKB_SUDT_ACCOUNT_ID, &a_address, - init_ckb - fee as u128, + init_ckb - fee, ); // block producer ckb check_balance( @@ -690,7 +691,7 @@ fn test_transfer_to_self() { a_id, CKB_SUDT_ACCOUNT_ID, &block_producer, - fee as u128, + fee, ); } } @@ -698,7 +699,7 @@ fn test_transfer_to_self() { #[test] fn test_transfer_to_self_overflow() { let init_a_balance: u128 = u128::MAX - 1; - let init_ckb: u128 = 100; + let init_ckb: U256 = 100u64.into(); let rollup_config = RollupConfig::new_builder() .l2_sudt_validator_script_type_hash(DUMMY_SUDT_VALIDATOR_SCRIPT_TYPE_HASH.pack()) @@ -763,13 +764,13 @@ fn test_transfer_to_self_overflow() { .mint_sudt(sudt_id, &a_address, init_a_balance) .expect("init balance"); ctx.state - .mint_sudt(CKB_SUDT_ACCOUNT_ID, &a_address, init_ckb) + .mint_ckb(&a_address, init_ckb) .expect("init balance"); // transfer from A to A, zero value { let value: u128 = 0; - let fee: u64 = 0; + let fee: U256 = 0u64.into(); let args = SUDTArgs::new_builder() .set( SUDTTransfer::new_builder() @@ -798,7 +799,7 @@ fn test_transfer_to_self_overflow() { &run_result.logs, sudt_id, &block_producer, - fee.into(), + fee, &a_address, &a_address, value, @@ -849,7 +850,7 @@ fn test_transfer_to_self_overflow() { // transfer from A to A, 1 value { let value: u128 = 1; - let fee: u64 = 0; + let fee: U256 = 0u64.into(); let args = SUDTArgs::new_builder() .set( SUDTTransfer::new_builder() @@ -878,7 +879,7 @@ fn test_transfer_to_self_overflow() { &run_result.logs, sudt_id, &block_producer, - fee.into(), + fee, &a_address, &a_address, value, @@ -1215,7 +1216,7 @@ fn check_balance( sender_id: u32, sudt_id: u32, address: &RegistryAddress, - expected_balance: u128, + expected_balance: impl Into, ) { // check balance let args = SUDTArgs::new_builder() @@ -1239,5 +1240,5 @@ fn check_balance( buf.copy_from_slice(&return_data); u128::from_le_bytes(buf) }; - assert_eq!(balance, expected_balance); + assert_eq!(U256::from(balance), expected_balance.into()); } diff --git a/tests/src/script_tests/state_validator/cancel_challenge/tx_execution.rs b/tests/src/script_tests/state_validator/cancel_challenge/tx_execution.rs index 1662ccbc..404592ec 100644 --- a/tests/src/script_tests/state_validator/cancel_challenge/tx_execution.rs +++ b/tests/src/script_tests/state_validator/cancel_challenge/tx_execution.rs @@ -30,6 +30,7 @@ use gw_types::packed::AllowedTypeHash; use gw_types::packed::CCTransactionWitness; use gw_types::packed::Fee; use gw_types::prelude::*; +use gw_types::U256; use gw_types::{ bytes::Bytes, core::{ChallengeTargetType, ScriptHashType, Status}, @@ -153,7 +154,7 @@ async fn test_cancel_tx_execute() { .amount(Pack::pack(&transfer_capacity)) .fee( Fee::new_builder() - .amount(Pack::pack(&fee_capacity)) + .amount(Pack::pack(&U256::from(fee_capacity))) .registry_id(Pack::pack(&receiver_address.registry_id)) .build(), ) diff --git a/tests/src/script_tests/state_validator/cancel_challenge/tx_signature.rs b/tests/src/script_tests/state_validator/cancel_challenge/tx_signature.rs index c3599894..d36730c6 100644 --- a/tests/src/script_tests/state_validator/cancel_challenge/tx_signature.rs +++ b/tests/src/script_tests/state_validator/cancel_challenge/tx_signature.rs @@ -36,6 +36,7 @@ use gw_types::packed::AllowedTypeHash; use gw_types::packed::CCTransactionSignatureWitness; use gw_types::packed::Fee; use gw_types::prelude::*; +use gw_types::U256; use gw_types::{ bytes::Bytes, core::{ChallengeTargetType, ScriptHashType, Status}, @@ -167,7 +168,7 @@ async fn test_cancel_tx_signature() { .amount(Pack::pack(&transfer_capacity)) .fee( Fee::new_builder() - .amount(Pack::pack(&fee_capacity)) + .amount(Pack::pack(&U256::from(fee_capacity))) .registry_id(Pack::pack(&receiver_address.registry_id)) .build(), ) diff --git a/tests/src/script_tests/state_validator/revert.rs b/tests/src/script_tests/state_validator/revert.rs index 3ea50adf..3ba49ae6 100644 --- a/tests/src/script_tests/state_validator/revert.rs +++ b/tests/src/script_tests/state_validator/revert.rs @@ -24,6 +24,7 @@ use gw_store::state::state_db::StateContext; use gw_store::traits::chain_store::ChainStore; use gw_types::core::AllowedEoaType; use gw_types::packed::{AllowedTypeHash, Fee}; +use gw_types::U256; use gw_types::{ bytes::Bytes, core::{ChallengeTargetType, ScriptHashType, Status}, @@ -148,7 +149,7 @@ async fn test_revert() { .amount(Pack::pack(&150_00000000u128)) .fee( Fee::new_builder() - .amount(Pack::pack(&1_00000000u64)) + .amount(Pack::pack(&U256::from(1_00000000u64))) .registry_id(Pack::pack(ð_registry_id)) .build(), ) From abefe62b45b7aa5b1a5874cf217f23e34b257ede Mon Sep 17 00:00:00 2001 From: "zero.qn" Date: Mon, 25 Apr 2022 15:55:03 +0800 Subject: [PATCH 04/16] feat(gw_sudt_ckb_utils): expose u256_sudt_* api --- c/gw_sudt_ckb_utils.h | 117 ++++++++++++++++++++++++++++++++---------- 1 file changed, 90 insertions(+), 27 deletions(-) diff --git a/c/gw_sudt_ckb_utils.h b/c/gw_sudt_ckb_utils.h index b24b3b0c..a2e6c26a 100644 --- a/c/gw_sudt_ckb_utils.h +++ b/c/gw_sudt_ckb_utils.h @@ -8,9 +8,9 @@ /* format: * from_addr | to_addr | amount(32 bytes) */ -int _ckb_emit_log(gw_context_t *ctx, gw_reg_addr_t from_addr, - gw_reg_addr_t to_addr, const uint256_t amount, - uint8_t service_flag) { +int _u256_sudt_emit_log(gw_context_t *ctx, const uint32_t sudt_id, + gw_reg_addr_t from_addr, gw_reg_addr_t to_addr, + const uint256_t amount, uint8_t service_flag) { #ifdef GW_VALIDATOR uint32_t data_size = 0; uint8_t *data = NULL; @@ -20,7 +20,7 @@ int _ckb_emit_log(gw_context_t *ctx, gw_reg_addr_t from_addr, uint32_t data_size = GW_REG_ADDR_SIZE(from_addr) + GW_REG_ADDR_SIZE(to_addr) + 32; if (data_size > 256) { - printf("_ckb_emit_log: data is large than buffer"); + printf("_u256_sudt_emit_log: data is large than buffer"); return GW_FATAL_BUFFER_OVERFLOW; } _gw_cpy_addr(data, from_addr); @@ -28,11 +28,11 @@ int _ckb_emit_log(gw_context_t *ctx, gw_reg_addr_t from_addr, memcpy(data + GW_REG_ADDR_SIZE(from_addr) + GW_REG_ADDR_SIZE(to_addr), (uint8_t *)(&amount), 32); #endif - return ctx->sys_log(ctx, CKB_SUDT_ACCOUNT_ID, service_flag, data_size, data); + return ctx->sys_log(ctx, sudt_id, service_flag, data_size, data); } -int _ckb_get_balance(gw_context_t *ctx, gw_reg_addr_t address, - uint256_t *balance) { +int _u256_sudt_get_balance(gw_context_t *ctx, const uint32_t sudt_id, + gw_reg_addr_t address, uint256_t *balance) { uint8_t key[64] = {0}; uint32_t key_len = 64; int ret = _sudt_build_key(SUDT_KEY_FLAG_BALANCE, address, key, &key_len); @@ -40,7 +40,7 @@ int _ckb_get_balance(gw_context_t *ctx, gw_reg_addr_t address, return ret; } uint8_t value[32] = {0}; - ret = ctx->sys_load(ctx, CKB_SUDT_ACCOUNT_ID, key, key_len, value); + ret = ctx->sys_load(ctx, sudt_id, key, key_len, value); if (ret != 0) { return ret; } @@ -48,8 +48,8 @@ int _ckb_get_balance(gw_context_t *ctx, gw_reg_addr_t address, return 0; } -int _ckb_set_balance(gw_context_t *ctx, gw_reg_addr_t address, - uint256_t balance) { +int _u256_sudt_set_balance(gw_context_t *ctx, const uint32_t sudt_id, + gw_reg_addr_t address, uint256_t balance) { uint8_t key[64] = {0}; uint32_t key_len = 64; int ret = _sudt_build_key(SUDT_KEY_FLAG_BALANCE, address, key, &key_len); @@ -63,27 +63,51 @@ int _ckb_set_balance(gw_context_t *ctx, gw_reg_addr_t address, return ret; } +int u256_sudt_get_balance(gw_context_t *ctx, const uint32_t sudt_id, + gw_reg_addr_t addr, uint256_t *balance) { + int ret = gw_verify_sudt_account(ctx, sudt_id); + if (ret != 0) { + return ret; + } + return _u256_sudt_get_balance(ctx, sudt_id, addr, balance); +} + int ckb_get_balance(gw_context_t *ctx, gw_reg_addr_t addr, uint256_t *balance) { - return _ckb_get_balance(ctx, addr, balance); + return _u256_sudt_get_balance(ctx, CKB_SUDT_ACCOUNT_ID, addr, balance); } -int _ckb_get_total_supply(gw_context_t *ctx, uint8_t total_supply[32]) { - return ctx->sys_load(ctx, CKB_SUDT_ACCOUNT_ID, SUDT_TOTAL_SUPPLY_KEY, 32, - total_supply); +int _u256_sudt_get_total_supply(gw_context_t *ctx, const uint32_t sudt_id, + uint256_t *total_supply) { + uint8_t value[32] = {0}; + int ret = ctx->sys_load(ctx, sudt_id, SUDT_TOTAL_SUPPLY_KEY, 32, value); + if (ret != 0) { + return ret; + } + uint256_from_little_endian((uint8_t *)&value, 32, total_supply); + return 0; } -int ckb_get_total_supply(gw_context_t *ctx, uint8_t total_supply[32]) { - return _ckb_get_total_supply(ctx, total_supply); +int u256_sudt_get_total_supply(gw_context_t *ctx, const uint32_t sudt_id, + uint256_t *total_supply) { + int ret = gw_verify_sudt_account(ctx, sudt_id); + if (ret != 0) { + return ret; + } + return _u256_sudt_get_total_supply(ctx, sudt_id, total_supply); +} + +int ckb_get_total_supply(gw_context_t *ctx, uint256_t *total_supply) { + return _u256_sudt_get_total_supply(ctx, CKB_SUDT_ACCOUNT_ID, total_supply); } -int _ckb_transfer(gw_context_t *ctx, gw_reg_addr_t from_addr, - gw_reg_addr_t to_addr, const uint256_t amount, - uint8_t service_flag) { +int _u256_sudt_transfer(gw_context_t *ctx, const uint32_t sudt_id, + gw_reg_addr_t from_addr, gw_reg_addr_t to_addr, + const uint256_t amount, uint8_t service_flag) { int ret; /* check from account */ uint256_t from_balance = {0}; - ret = _ckb_get_balance(ctx, from_addr, &from_balance); + ret = _u256_sudt_get_balance(ctx, sudt_id, from_addr, &from_balance); if (ret != 0) { printf("transfer: can't get sender's balance"); return ret; @@ -101,7 +125,7 @@ int _ckb_transfer(gw_context_t *ctx, gw_reg_addr_t from_addr, uint256_underflow_sub(from_balance, amount, &new_from_balance); /* update sender balance */ - ret = _ckb_set_balance(ctx, from_addr, new_from_balance); + ret = _u256_sudt_set_balance(ctx, sudt_id, from_addr, new_from_balance); if (ret != 0) { printf("transfer: update sender's balance failed"); return ret; @@ -109,7 +133,7 @@ int _ckb_transfer(gw_context_t *ctx, gw_reg_addr_t from_addr, /* check to account */ uint256_t to_balance = {0}; - ret = _ckb_get_balance(ctx, to_addr, &to_balance); + ret = _u256_sudt_get_balance(ctx, sudt_id, to_addr, &to_balance); if (ret != 0) { printf("transfer: can't get receiver's balance"); return ret; @@ -123,30 +147,69 @@ int _ckb_transfer(gw_context_t *ctx, gw_reg_addr_t from_addr, } /* update receiver balance */ - ret = _ckb_set_balance(ctx, to_addr, new_to_balance); + ret = _u256_sudt_set_balance(ctx, sudt_id, to_addr, new_to_balance); if (ret != 0) { printf("transfer: update receiver's balance failed"); return ret; } /* emit log */ - ret = _ckb_emit_log(ctx, from_addr, to_addr, amount, service_flag); + ret = _u256_sudt_emit_log(ctx, sudt_id, from_addr, to_addr, amount, + service_flag); if (ret != 0) { printf("transfer: emit log failed"); } return ret; } +int u256_sudt_transfer(gw_context_t *ctx, const uint32_t sudt_id, + gw_reg_addr_t from_addr, gw_reg_addr_t to_addr, + const uint256_t amount) { + int ret = gw_verify_sudt_account(ctx, sudt_id); + if (ret != 0) { + return ret; + } + return _u256_sudt_transfer(ctx, sudt_id, from_addr, to_addr, amount, + GW_LOG_SUDT_TRANSFER); +} + int ckb_transfer(gw_context_t *ctx, gw_reg_addr_t from_addr, gw_reg_addr_t to_addr, const uint256_t amount) { - return _ckb_transfer(ctx, from_addr, to_addr, amount, GW_LOG_SUDT_TRANSFER); + return _u256_sudt_transfer(ctx, CKB_SUDT_ACCOUNT_ID, from_addr, to_addr, + amount, GW_LOG_SUDT_TRANSFER); +} + +int u256_sudt_pay_fee(gw_context_t *ctx, const uint32_t sudt_id, + gw_reg_addr_t from_addr, const uint256_t amount) { + int ret = gw_verify_sudt_account(ctx, sudt_id); + if (ret != 0) { + return ret; + } + + ret = _u256_sudt_transfer(ctx, sudt_id, from_addr, + ctx->block_info.block_producer, amount, + GW_LOG_SUDT_PAY_FEE); + if (ret != 0) { + printf("pay fee transfer failed"); + return ret; + } + + /* call syscall, we use this action to emit event to runtime, this function + do + * not actually pay the fee */ + ret = ctx->sys_pay_fee(ctx, from_addr, sudt_id, amount); + if (ret != 0) { + printf("sys pay fee failed"); + } + return ret; } /* Pay fee */ int ckb_pay_fee(gw_context_t *ctx, gw_reg_addr_t from_addr, const uint256_t amount) { - int ret = _ckb_transfer(ctx, from_addr, ctx->block_info.block_producer, - amount, GW_LOG_SUDT_PAY_FEE); + int ret = _u256_sudt_transfer(ctx, CKB_SUDT_ACCOUNT_ID, from_addr, + ctx->block_info.block_producer, amount, + GW_LOG_SUDT_PAY_FEE); if (ret != 0) { printf("pay fee transfer failed"); return ret; From 76e40cf3b4d749121e88dc792baa327ecf475f66 Mon Sep 17 00:00:00 2001 From: "zero.qn" Date: Mon, 25 Apr 2022 16:46:10 +0800 Subject: [PATCH 05/16] chore: update godwoken commit, change all sudt to u256 --- Cargo.lock | 46 +- c/Makefile | 2 +- c/godwoken.mol | 405 ------------------ contracts/Cargo.lock | 6 +- contracts/gw-utils/Cargo.toml | 4 +- .../src/verifications/submit_block.rs | 8 +- tests/Cargo.toml | 22 +- tests/src/script_tests/l2_scripts/sudt.rs | 6 +- 8 files changed, 47 insertions(+), 452 deletions(-) delete mode 100644 c/godwoken.mol diff --git a/Cargo.lock b/Cargo.lock index 40ba1f81..47875534 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1613,7 +1613,7 @@ dependencies = [ [[package]] name = "gw-block-producer" version = "1.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "async-channel", @@ -1672,7 +1672,7 @@ dependencies = [ [[package]] name = "gw-chain" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "ckb-fixed-hash", @@ -1698,7 +1698,7 @@ dependencies = [ [[package]] name = "gw-challenge" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "arc-swap", @@ -1730,7 +1730,7 @@ dependencies = [ [[package]] name = "gw-ckb-hardfork" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "arc-swap", "ckb-types", @@ -1741,7 +1741,7 @@ dependencies = [ [[package]] name = "gw-common" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "cfg-if 0.1.10", "gw-hash", @@ -1754,7 +1754,7 @@ dependencies = [ [[package]] name = "gw-config" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "ckb-fixed-hash", "gw-jsonrpc-types", @@ -1765,7 +1765,7 @@ dependencies = [ [[package]] name = "gw-db" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "ckb-rocksdb", "gw-config", @@ -1779,7 +1779,7 @@ dependencies = [ [[package]] name = "gw-dynamic-config" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "arc-swap", @@ -1795,7 +1795,7 @@ dependencies = [ [[package]] name = "gw-generator" version = "0.2.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "arc-swap", @@ -1824,7 +1824,7 @@ dependencies = [ [[package]] name = "gw-hash" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "blake2b-ref", ] @@ -1832,7 +1832,7 @@ dependencies = [ [[package]] name = "gw-jsonrpc-types" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "ckb-fixed-hash", @@ -1846,7 +1846,7 @@ dependencies = [ [[package]] name = "gw-mem-pool" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "async-trait", @@ -1874,7 +1874,7 @@ dependencies = [ [[package]] name = "gw-p2p-network" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "async-trait", @@ -1889,7 +1889,7 @@ dependencies = [ [[package]] name = "gw-rpc-client" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "arc-swap", @@ -1923,7 +1923,7 @@ dependencies = [ [[package]] name = "gw-rpc-server" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "async-channel", @@ -1971,7 +1971,7 @@ dependencies = [ [[package]] name = "gw-rpc-ws-server" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "async-channel", @@ -1996,7 +1996,7 @@ dependencies = [ [[package]] name = "gw-store" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "arc-swap", @@ -2053,7 +2053,7 @@ dependencies = [ [[package]] name = "gw-traits" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "gw-common", "gw-db", @@ -2063,7 +2063,7 @@ dependencies = [ [[package]] name = "gw-tx-filter" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "gw-common", "gw-config", @@ -2077,7 +2077,7 @@ dependencies = [ [[package]] name = "gw-types" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "cfg-if 0.1.10", "ckb-fixed-hash", @@ -2091,7 +2091,7 @@ dependencies = [ [[package]] name = "gw-utils" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "ckb-crypto", @@ -2107,7 +2107,7 @@ dependencies = [ [[package]] name = "gw-version" version = "1.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", ] @@ -2115,7 +2115,7 @@ dependencies = [ [[package]] name = "gw-web3-indexer" version = "0.1.0" -source = "git+https://github.com/nervosnetwork/godwoken.git?rev=1090567d2c54384f5456a0363e5574e0af1b7268#1090567d2c54384f5456a0363e5574e0af1b7268" +source = "git+https://github.com/nervosnetwork/godwoken.git?rev=ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8#ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8" dependencies = [ "anyhow", "ckb-hash", diff --git a/c/Makefile b/c/Makefile index 49c2067c..efe42761 100644 --- a/c/Makefile +++ b/c/Makefile @@ -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 := 1090567d2c54384f5456a0363e5574e0af1b7268 +PROTOCOL_VERSION := ed9d3ab716ea5d5a8f22f5ea7e6c90e23b6a49a8 PROTOCOL_SCHEMA_URL := https://raw.githubusercontent.com/nervosnetwork/godwoken/${PROTOCOL_VERSION}/crates/types/schemas # docker pull nervos/ckb-riscv-gnu-toolchain:gnu-bionic-20191012 diff --git a/c/godwoken.mol b/c/godwoken.mol deleted file mode 100644 index e7f0a396..00000000 --- a/c/godwoken.mol +++ /dev/null @@ -1,405 +0,0 @@ -import blockchain; - -vector Uint32Vec ; - -struct BlockMerkleState { - merkle_root: Byte32, - count: Uint64, -} - -struct AccountMerkleState { - merkle_root: Byte32, - count: Uint32, -} - -struct GlobalStateV0 { - rollup_config_hash: Byte32, - account: AccountMerkleState, - block: BlockMerkleState, - reverted_block_root: Byte32, - tip_block_hash: Byte32, - last_finalized_block_number: Uint64, - // 0: running, 1: halting - status: byte, -} - -struct GlobalState { - rollup_config_hash: Byte32, - account: AccountMerkleState, - block: BlockMerkleState, - reverted_block_root: Byte32, - tip_block_hash: Byte32, - tip_block_timestamp: Uint64, - last_finalized_block_number: Uint64, - // 0: running, 1: halting - status: byte, - version: byte, -} - -struct AllowedTypeHash { - type_: byte, - hash: Byte32, -} - -vector AllowedTypeHashVec ; - -// allowed eoa: -// 0: unknown, 1: eth, 2: tron -// -// allowed contract: -// 0: unknown, 1: meta, 2: sudt, 3: polyjuice, 4: eth addr reg -table RollupConfig { - l1_sudt_script_type_hash: Byte32, - custodian_script_type_hash: Byte32, - deposit_script_type_hash: Byte32, - withdrawal_script_type_hash: Byte32, - challenge_script_type_hash: Byte32, - stake_script_type_hash: Byte32, - l2_sudt_validator_script_type_hash: Byte32, - burn_lock_hash: Byte32, - required_staking_capacity: Uint64, - challenge_maturity_blocks: Uint64, - finality_blocks: Uint64, - reward_burn_rate: byte, // * reward_burn_rate / 100 - chain_id: Uint64, // chain id - allowed_eoa_type_hashes: AllowedTypeHashVec, // list of script code_hash allowed an EOA(external owned account) to use - allowed_contract_type_hashes: AllowedTypeHashVec, // list of script code_hash allowed a contract account to use -} - -table RawL2Transaction { - // chain id - chain_id: Uint64, - from_id: Uint32, - to_id: Uint32, - nonce: Uint32, - args: Bytes, -} - -table L2Transaction { - raw: RawL2Transaction, - signature: Bytes, -} - -vector L2TransactionVec ; - -struct SubmitTransactions { - tx_witness_root: Byte32, - tx_count: Uint32, - // hash(account_root | account_count) before apply all txs - prev_state_checkpoint: Byte32, -} - -struct SubmitWithdrawals { - withdrawal_witness_root: Byte32, - withdrawal_count: Uint32, -} - -table RawL2Block { - number: Uint64, - // In registry address format: registry_id (4 bytes) | address len (4 bytes) | address (n bytes) - block_producer: Bytes, - parent_block_hash: Byte32, - stake_cell_owner_lock_hash: Byte32, - timestamp: Uint64, - prev_account: AccountMerkleState, - post_account: AccountMerkleState, - // hash(account_root | account_count) of each withdrawals & transactions - state_checkpoint_list: Byte32Vec, - submit_withdrawals: SubmitWithdrawals, - submit_transactions: SubmitTransactions, -} - -vector RawL2BlockVec ; - -table L2Block { - raw: RawL2Block, - kv_state: KVPairVec, - kv_state_proof: Bytes, - transactions: L2TransactionVec, - block_proof: Bytes, - withdrawals: WithdrawalRequestVec, -} - -table DepositRequest { - // CKB amount - capacity: Uint64, - // SUDT amount - amount: Uint128, - sudt_script_hash: Byte32, - script: Script, - // Deposit to a Godwoken registry - registry_id: Uint32, -} - -vector DepositRequestVec ; - -struct RawWithdrawalRequest { - nonce: Uint32, - // chain id - chain_id: Uint64, - // CKB amount - capacity: Uint64, - // SUDT amount - amount: Uint128, - sudt_script_hash: Byte32, - // layer2 account_script_hash - account_script_hash: Byte32, - // withdrawal registry ID - registry_id: Uint32, - // layer1 lock to withdraw after challenge period - owner_lock_hash: Byte32, - // withdrawal fee, paid to block producer - fee: Uint256, -} - -vector WithdrawalRequestVec ; - -table WithdrawalRequest { - raw: RawWithdrawalRequest, - signature: Bytes, -} - -// --- contract execution --- - -struct KVPair { k: Byte32, v: Byte32, } -vector KVPairVec ; - -table BlockInfo { - block_producer: Bytes, - number: Uint64, - timestamp: Uint64, -} - -// --- end of contract execution --- - -// --- deposit lock --- -// a rollup_type_hash exists before this args, to make args friendly to prefix search -table DepositLockArgs { - // layer1 lock hash - owner_lock_hash: Byte32, - layer2_lock: Script, - cancel_timeout: Uint64, - registry_id: Uint32, -} -// --- end of deposit lock --- - -// --- custodian lock --- -// a rollup_type_hash exists before this args, to make args friendly to prefix search -table CustodianLockArgs { - deposit_block_hash: Byte32, - deposit_block_number: Uint64, - // used for revert this cell to deposit request cell - // after finalize, this lock is meaningless - deposit_lock_args: DepositLockArgs, -} - -struct UnlockCustodianViaRevertWitness { - deposit_lock_hash: Byte32, -} - -// --- end of custodian lock --- - -// --- withdrawal lock --- -// a rollup_type_hash exists before this args, to make args friendly to prefix search -struct WithdrawalLockArgs { - withdrawal_block_hash: Byte32, - withdrawal_block_number: Uint64, - account_script_hash: Byte32, - // layer1 lock to withdraw after challenge period - owner_lock_hash: Byte32, -} - -union UnlockWithdrawalWitness { - UnlockWithdrawalViaFinalize, - UnlockWithdrawalViaRevert, -} -table UnlockWithdrawalViaFinalize { -} -struct UnlockWithdrawalViaRevert { - custodian_lock_hash: Byte32, -} -// --- end of withdrawal lock --- - -// --- stake lock --- -// a rollup_type_hash exists before this args, to make args friendly to prefix search -struct StakeLockArgs { - owner_lock_hash: Byte32, - stake_block_number: Uint64, -} -// --- end of stake lock --- - -// --- builtin Meta contract --- -union MetaContractArgs { - CreateAccount, -} - -struct Fee { - // registry id - registry_id: Uint32, - // amount in CKB - amount: Uint256, -} - -table CreateAccount { - script: Script, - fee: Fee, -} -// --- end of Meta contract - -// --- layer2 SUDT --- -union SUDTArgs { - SUDTQuery, - SUDTTransfer, -} - -table SUDTQuery { - // Godwoken registry address: (registry_id (4 bytes) | address len(4 bytes) | address) - address: Bytes, -} - -table SUDTTransfer { - // Godwoken registry address: (registry_id (4 bytes) | address len(4 bytes) | address) - to_address: Bytes, - amount: Uint128, - // paid fee(ckb) - fee: Fee, -} -// --- end of layer2 SUDT --- - -// --- challenge --- - -struct ChallengeTarget { - block_hash: Byte32, - // index of the challenge target - target_index: Uint32, - // 0: tx execution, 1: tx signature, 2: withdrawal signature - target_type: byte, -} - -// a rollup_type_hash exists before this args, to make args friendly to prefix search -table ChallengeLockArgs { - target: ChallengeTarget, - // layer1 lock to receive the rewards - rewards_receiver_lock: Script, -} - -// witness to prove the validity of challenge -table ChallengeWitness { - raw_l2block: RawL2Block, - block_proof: Bytes, // block proof -} - -vector ScriptVec