diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b179c27dd..3003caf78 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -31,10 +31,13 @@ jobs: - name: Test TOML serialization run: cargo run --bin godwoken -- generate-example-config -o test.toml + # FIXME: change to nervosnetwork/godwoken-tests after merge pr 675 integration-test: - uses: nervosnetwork/godwoken-tests/.github/workflows/reusable-integration-test-v1.yml@develop + uses: zeroqn/godwoken-tests/.github/workflows/reusable-integration-test-v1.yml@13e3482b6c00dc29891db8e624769762d19968b3 with: # github.ref: The branch or tag ref that triggered the workflow run. For branches this is the format refs/heads/, and for tags it is refs/tags/. godwoken_ref: ${{ github.ref }} - kicker_ref: refs/pull/235/head # https://github.com/RetricSu/godwoken-kicker/pull/235 - tests_ref: develop # https://github.com/nervosnetwork/godwoken-tests/commits/develop + gw_prebuild_image_name: ghcr.io/zeroqn/godwoken-prebuilds + gw_prebuild_image_tag: v1.1-feat-change-ckb-decimal-to-18 # For godwoken-scripts and godwoken-polyjuice binaries + kicker_ref: refs/pull/239/head # https://github.com/RetricSu/godwoken-kicker/pull/239 + tests_ref: refs/pull/105/head # https://github.com/nervosnetwork/godwoken-tests/pull/105 diff --git a/CHANGELOG.md b/CHANGELOG.md index 90e425185..54cc353b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,10 +18,12 @@ In the new version, compatibility improvements for Godwoken include: compatibility, remove the web3-provider plugin. - Support native ETH address in API and EVM, remove the Godwoken address concept. - Support Ethereum signature format and EIP-712. User can view the transaction before signing, instead of signing a random 32 bytes message. [#561](https://github.com/nervosnetwork/godwoken/pull/561) -- Fix `totalSupply` interface for sUDT ERC-20 proxy contract [#560](https://github.com/nervosnetwork/godwoken/pull/560) -- Support interactive with eth address that are not yet registered to Godwoken. +- Fix the `totalSupply` interface of sUDT ERC-20 proxy contract [#560](https://github.com/nervosnetwork/godwoken/pull/560) +- Support interactive with eth address that hasn't been registered to Godwoken. +- Unify layer 2 fungible token represatation as uint256. +- Change layer 2 ckb decimal from 8 to 18, improve compatibility between metamask and native ckb. [#675](https://github.com/nervosnetwork/godwoken/pull/675) -Developers can use Godwoken v1 the same way they use other ethereum-compatible chains, requiring only switching the network to Godwoken. With v1, the polyjuice-provider web3 plugin was removed. +Developers can use Godwoken v1 the same way they use other ethereum-compatible chains, requiring only switching the network to Godwoken. The polyjuice-provider web3 plugin was removed in Godwoken v1. ### Other improvements diff --git a/Cargo.lock b/Cargo.lock index b99489cbd..1c18d799a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1963,7 +1963,6 @@ dependencies = [ "gw-hash", "gw-types", "merkle-cbt", - "primitive-types", "sparse-merkle-tree", "thiserror", ] @@ -2385,6 +2384,7 @@ dependencies = [ "ckb-types", "gw-hash", "molecule", + "primitive-types", "sparse-merkle-tree", ] diff --git a/crates/benches/benches/benchmarks/fee_queue.rs b/crates/benches/benches/benchmarks/fee_queue.rs index b2b56e789..09e495317 100644 --- a/crates/benches/benches/benchmarks/fee_queue.rs +++ b/crates/benches/benches/benchmarks/fee_queue.rs @@ -45,7 +45,7 @@ fn bench_add_full(b: &mut Bencher) { .raw(RawL2Transaction::new_builder().nonce(i.pack()).build()) .build(), ), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -66,7 +66,7 @@ fn bench_add_full(b: &mut Bencher) { ) .build(), ), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -102,7 +102,7 @@ fn bench_add_fetch_20(b: &mut Bencher) { .raw(RawL2Transaction::new_builder().nonce(i.pack()).build()) .build(), ), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -125,7 +125,7 @@ fn bench_add_fetch_20(b: &mut Bencher) { ) .build(), ), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), diff --git a/crates/benches/benches/benchmarks/smt.rs b/crates/benches/benches/benchmarks/smt.rs index 9de11bec9..1fef4e448 100644 --- a/crates/benches/benches/benchmarks/smt.rs +++ b/crates/benches/benches/benchmarks/smt.rs @@ -35,6 +35,7 @@ use gw_types::{ SubmitTransactions, }, prelude::*, + U256, }; use pprof::criterion::{Output, PProfProfiler}; @@ -224,11 +225,11 @@ impl BenchExecutionEnvironment { .set( SUDTTransfer::new_builder() .to_address(Bytes::from(to_address.to_bytes()).pack()) - .amount(1.pack()) + .amount(U256::one().pack()) .fee( Fee::new_builder() .registry_id(ETH_REGISTRY_ACCOUNT_ID.pack()) - .amount(1.pack()) + .amount(1u128.pack()) .build(), ) .build(), @@ -287,7 +288,7 @@ impl BenchExecutionEnvironment { .mapping_registry_address_to_script_hash(addr.clone(), account_script_hash) .unwrap(); state - .mint_sudt(CKB_SUDT_ACCOUNT_ID, &addr, CKB_BALANCE) + .mint_sudt(CKB_SUDT_ACCOUNT_ID, &addr, CKB_BALANCE.into()) .unwrap(); Account { id: account_id } diff --git a/crates/benches/benches/benchmarks/sudt.rs b/crates/benches/benches/benchmarks/sudt.rs index 127bf6ed5..cd29a6cc1 100644 --- a/crates/benches/benches/benchmarks/sudt.rs +++ b/crates/benches/benches/benchmarks/sudt.rs @@ -16,6 +16,7 @@ use gw_types::{ packed::{AllowedTypeHash, BlockInfo, Fee}, packed::{RawL2Transaction, RollupConfig, SUDTArgs, SUDTTransfer, Script}, prelude::*, + U256, }; const DUMMY_SUDT_VALIDATOR_SCRIPT_TYPE_HASH: [u8; 32] = [3u8; 32]; @@ -133,7 +134,7 @@ pub fn bench(c: &mut Criterion) { ) .build(); - let init_a_balance: u128 = 10000; + let init_a_balance = U256::from(10000u128); // init accounts let _meta = tree @@ -184,7 +185,7 @@ pub fn bench(c: &mut Criterion) { let b_script_hash = tree.get_script_hash(b_id).expect("get script hash"); tree.mapping_registry_address_to_script_hash(a_addr.clone(), a_script_hash) .unwrap(); - tree.mapping_registry_address_to_script_hash(b_addr.clone(), b_script_hash) + tree.mapping_registry_address_to_script_hash(b_addr, b_script_hash) .unwrap(); let block_producer_script = { @@ -219,8 +220,8 @@ pub fn bench(c: &mut Criterion) { }, |(mut tree, rollup_config, sudt_id, a_id, b_script_hash, block_info)| { // transfer from A to B - let value = 4000u128; - let fee = 42u64; + let value: U256 = 4000u128.into(); + let fee = 42u128; let b_addr = tree .get_registry_address_by_script_hash(ETH_REGISTRY_ACCOUNT_ID, &b_script_hash) .expect("get script hash") diff --git a/crates/block-producer/src/withdrawal.rs b/crates/block-producer/src/withdrawal.rs index 3250fff2d..e699ad65c 100644 --- a/crates/block-producer/src/withdrawal.rs +++ b/crates/block-producer/src/withdrawal.rs @@ -341,7 +341,7 @@ mod test { .build(); let withdrawal = { - let fee = 50u64; + let fee = 50u128; let raw = RawWithdrawalRequest::new_builder() .nonce(1u32.pack()) .capacity((500 * 10u64.pow(8)).pack()) diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml index 84fe21cf1..0b99a636a 100644 --- a/crates/common/Cargo.toml +++ b/crates/common/Cargo.toml @@ -12,7 +12,6 @@ sparse-merkle-tree = { version = "0.5.2-rc1", default-features = false } merkle-cbt = { version = "0.3.0", default-features = false } gw-hash = { path = "../hash" } thiserror = { version = "1.0", optional = true } -primitive-types = { version = "0.10", default-features = false } gw-types = { path = "../types", default-features = false } [features] diff --git a/crates/common/src/ckb_decimal.rs b/crates/common/src/ckb_decimal.rs new file mode 100644 index 000000000..80083934e --- /dev/null +++ b/crates/common/src/ckb_decimal.rs @@ -0,0 +1,32 @@ +use gw_types::U256; + +// NOTE: u64::MAX is about 10^18, U256::MAX is about 10^77. Multiple 10 pow +// `CKB_DECIMAL_POW_EXP` should not overflow. +pub const CKB_DECIMAL_POW_EXP: u32 = 10; +pub const CKB_DECIMAL_POWER_TEN: u64 = 10u64.pow(CKB_DECIMAL_POW_EXP); + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub struct CKBCapacity(U256); + +impl CKBCapacity { + pub fn from_layer1(amount: u64) -> Self { + CKBCapacity(U256::from(amount) * CKB_DECIMAL_POWER_TEN) + } + + pub fn from_layer2(amount: U256) -> Self { + CKBCapacity(amount) + } + + pub fn to_layer1(&self) -> Option { + let truncated = self.0 / CKB_DECIMAL_POWER_TEN; + if truncated.bits() > u64::BITS as usize { + None + } else { + Some(truncated.as_u64()) + } + } + + pub fn to_layer2(&self) -> U256 { + self.0 + } +} diff --git a/crates/common/src/h256_ext.rs b/crates/common/src/h256_ext.rs index c425a6329..2285cd8bf 100644 --- a/crates/common/src/h256_ext.rs +++ b/crates/common/src/h256_ext.rs @@ -1,6 +1,6 @@ // type aliases -pub use primitive_types::U256; +use gw_types::U256; pub use sparse_merkle_tree::H256; pub trait H256Ext { diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index ef2f922e5..b4baefcf7 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -1,6 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] pub mod builtins; +pub mod ckb_decimal; pub mod error; pub mod h256_ext; pub mod merkle_utils; @@ -14,7 +15,7 @@ pub mod test_traits; // re-exports pub use gw_hash::blake2b; -pub use h256_ext::{H256, U256}; +pub use h256_ext::H256; pub use sparse_merkle_tree; /// constants diff --git a/crates/common/src/state.rs b/crates/common/src/state.rs index 35a49f9cd..82ce0b0b7 100644 --- a/crates/common/src/state.rs +++ b/crates/common/src/state.rs @@ -16,9 +16,11 @@ // // Thus, the first 5 bytes keeps uniqueness for different type of keys. +use gw_types::U256; + use crate::builtins::ETH_REGISTRY_ACCOUNT_ID; use crate::error::Error; -use crate::h256_ext::{H256Ext, H256, U256}; +use crate::h256_ext::{H256Ext, H256}; use crate::registry_address::RegistryAddress; use crate::vec::Vec; use crate::{blake2b::new_blake2b, merkle_utils::calculate_state_checkpoint}; @@ -200,16 +202,16 @@ pub trait State { Ok(Some(id)) } - fn get_sudt_balance(&self, sudt_id: u32, address: &RegistryAddress) -> Result { + fn get_sudt_balance(&self, sudt_id: u32, address: &RegistryAddress) -> Result { // get balance let sudt_key = build_sudt_key(SUDT_KEY_FLAG_BALANCE, address); let balance = self.get_value(sudt_id, &sudt_key)?; - Ok(balance.to_u128()) + Ok(balance.to_u256()) } fn get_sudt_total_supply(&self, sudt_id: u32) -> Result { - let total_supoly = self.get_value(sudt_id, &SUDT_TOTAL_SUPPLY_KEY)?; - Ok(total_supoly.to_u256()) + let total_supply = self.get_value(sudt_id, &SUDT_TOTAL_SUPPLY_KEY)?; + Ok(total_supply.to_u256()) } fn store_data_hash(&mut self, data_hash: H256) -> Result<(), Error> { @@ -229,20 +231,20 @@ pub trait State { &mut self, sudt_id: u32, address: &RegistryAddress, - amount: u128, + amount: U256, ) -> Result<(), Error> { let sudt_key = build_sudt_key(SUDT_KEY_FLAG_BALANCE, address); let raw_key = build_account_key(sudt_id, &sudt_key); // calculate balance - let mut balance = self.get_raw(&raw_key)?.to_u128(); + let mut balance = self.get_raw(&raw_key)?.to_u256(); balance = balance.checked_add(amount).ok_or(Error::AmountOverflow)?; - self.update_raw(raw_key, H256::from_u128(balance))?; + self.update_raw(raw_key, H256::from_u256(balance))?; // update total supply let raw_key = build_account_key(sudt_id, &SUDT_TOTAL_SUPPLY_KEY); let mut total_supply = self.get_raw(&raw_key)?.to_u256(); total_supply = total_supply - .checked_add(U256::from(amount)) + .checked_add(amount) .ok_or(Error::AmountOverflow)?; self.update_raw(raw_key, H256::from_u256(total_supply))?; @@ -254,20 +256,20 @@ pub trait State { &mut self, sudt_id: u32, address: &RegistryAddress, - amount: u128, + amount: U256, ) -> Result<(), Error> { let sudt_key = build_sudt_key(SUDT_KEY_FLAG_BALANCE, address); let raw_key = build_account_key(sudt_id, &sudt_key); // calculate balance - let mut balance = self.get_raw(&raw_key)?.to_u128(); + let mut balance = self.get_raw(&raw_key)?.to_u256(); balance = balance.checked_sub(amount).ok_or(Error::AmountOverflow)?; - self.update_raw(raw_key, H256::from_u128(balance))?; + self.update_raw(raw_key, H256::from_u256(balance))?; // update total supply let raw_key = build_account_key(sudt_id, &SUDT_TOTAL_SUPPLY_KEY); let mut total_supply = self.get_raw(&raw_key)?.to_u256(); total_supply = total_supply - .checked_sub(U256::from(amount)) + .checked_sub(amount) .ok_or(Error::AmountOverflow)?; self.update_raw(raw_key, H256::from_u256(total_supply))?; diff --git a/crates/common/src/test_traits.rs b/crates/common/src/test_traits.rs index d3dbca0df..a490c876d 100644 --- a/crates/common/src/test_traits.rs +++ b/crates/common/src/test_traits.rs @@ -21,7 +21,7 @@ pub trait StateTest: State { self.update_value(address.registry_id, &key, value.into())?; // address -> script let key = build_registry_address_to_script_hash_key(&address); - self.update_value(address.registry_id, &key, script_hash.into())?; + self.update_value(address.registry_id, &key, script_hash)?; Ok(()) } } diff --git a/crates/generator/src/account_lock_manage/eip712/types.rs b/crates/generator/src/account_lock_manage/eip712/types.rs index 074c129e8..8a1770b9d 100644 --- a/crates/generator/src/account_lock_manage/eip712/types.rs +++ b/crates/generator/src/account_lock_manage/eip712/types.rs @@ -196,7 +196,7 @@ pub struct Withdrawal { nonce: u32, chain_id: u64, // withdrawal fee, paid to block producer - fee: u64, + fee: u128, // layer1 lock to withdraw after challenge period layer1_owner_lock: Script, // CKB amount @@ -217,6 +217,7 @@ impl EIP712Encode for Withdrawal { fn encode_data(&self, buf: &mut Vec) { use ethabi::Token; + buf.extend(ethabi::encode(&[Token::Uint( self.address.hash_struct().into(), )])); @@ -482,7 +483,7 @@ mod tests { }, nonce: 1, chain_id: 1, - fee: 1000, + fee: 1000u64.into(), layer1_owner_lock: Script { code_hash: hex::decode( "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", @@ -514,12 +515,12 @@ mod tests { salt: None, }; let message = withdrawal.eip712_message(domain_seperator.hash_struct()); - let signature: [u8; 65] = hex::decode("edcae58907b6e218b1bc4513afeefb30aad433bcd4a9c937fa53a24bb9abc12a6bc4bfa29ef3f1ee0e58464b1f97ad6bcebd434dbbc2c29539b02539843675681b").unwrap().try_into().unwrap(); + let signature: [u8; 65] = hex::decode("22cae59f1bfaf58f423d1a414cbcaefd45a89dd54c9142fccbb2473c74f4741b45f77f1f3680b8c0b6362957c8d79f96a683a859ccbf22a6cfc1ebc311b936d301").unwrap().try_into().unwrap(); let pubkey_hash = Secp256k1Eth::default() .recover(message.into(), &signature) .unwrap(); assert_eq!( - "e8ae579256c3b84efb76bbb69cb6bcbef1375f00".to_string(), + "cc3e7fb0176a0e22a7f675306ceeb61d26eb0dc4".to_string(), hex::encode(pubkey_hash) ); } diff --git a/crates/generator/src/dummy_state.rs b/crates/generator/src/dummy_state.rs index a90c83752..06a8cd429 100644 --- a/crates/generator/src/dummy_state.rs +++ b/crates/generator/src/dummy_state.rs @@ -63,6 +63,7 @@ mod tests { use gw_types::{ packed::Script, prelude::{Builder, Entity, Pack}, + U256, }; use crate::{traits::StateExt, Error}; @@ -171,21 +172,45 @@ mod tests { // mint sudt let user_a = RegistryAddress::new(0, vec![1u8; 20]); let user_b = RegistryAddress::new(0, vec![2u8; 20]); - tree.mint_sudt(sudt_id, &user_a, 100).unwrap(); - assert_eq!(tree.get_sudt_total_supply(sudt_id).unwrap(), 100.into()); - tree.mint_sudt(sudt_id, &user_a, 230).unwrap(); - assert_eq!(tree.get_sudt_total_supply(sudt_id).unwrap(), 330.into()); - tree.mint_sudt(sudt_id, &user_b, 155).unwrap(); - assert_eq!(tree.get_sudt_total_supply(sudt_id).unwrap(), 485.into()); + tree.mint_sudt(sudt_id, &user_a, U256::from(100u64)) + .unwrap(); + assert_eq!( + tree.get_sudt_total_supply(sudt_id).unwrap(), + U256::from(100) + ); + tree.mint_sudt(sudt_id, &user_a, U256::from(230u64)) + .unwrap(); + assert_eq!( + tree.get_sudt_total_supply(sudt_id).unwrap(), + U256::from(330) + ); + tree.mint_sudt(sudt_id, &user_b, U256::from(155u64)) + .unwrap(); + assert_eq!( + tree.get_sudt_total_supply(sudt_id).unwrap(), + U256::from(485) + ); // burn sudt - tree.burn_sudt(sudt_id, &user_a, 85).unwrap(); - assert_eq!(tree.get_sudt_total_supply(sudt_id).unwrap(), 400.into()); + tree.burn_sudt(sudt_id, &user_a, U256::from(85u64)).unwrap(); + assert_eq!( + tree.get_sudt_total_supply(sudt_id).unwrap(), + U256::from(400) + ); // overdraft - let err = tree.burn_sudt(sudt_id, &user_b, 200).unwrap_err(); + let err = tree + .burn_sudt(sudt_id, &user_b, U256::from(200u64)) + .unwrap_err(); assert_eq!(err, gw_common::error::Error::AmountOverflow); - assert_eq!(tree.get_sudt_total_supply(sudt_id).unwrap(), 400.into()); - tree.burn_sudt(sudt_id, &user_b, 100).unwrap(); - assert_eq!(tree.get_sudt_total_supply(sudt_id).unwrap(), 300.into()); + assert_eq!( + tree.get_sudt_total_supply(sudt_id).unwrap(), + U256::from(400) + ); + tree.burn_sudt(sudt_id, &user_b, U256::from(100u64)) + .unwrap(); + assert_eq!( + tree.get_sudt_total_supply(sudt_id).unwrap(), + U256::from(300) + ); } #[test] diff --git a/crates/generator/src/syscalls/mod.rs b/crates/generator/src/syscalls/mod.rs index c5702878a..d7d84f570 100644 --- a/crates/generator/src/syscalls/mod.rs +++ b/crates/generator/src/syscalls/mod.rs @@ -68,6 +68,7 @@ pub(crate) struct L2Syscalls<'a, S, C> { pub(crate) result: &'a mut RunResult, } +#[allow(dead_code)] fn load_data_u128(machine: &mut Mac, addr: u64) -> Result { let mut data = [0u8; 16]; for (i, c) in data.iter_mut().enumerate() { @@ -472,7 +473,7 @@ impl<'a, S: State, C: ChainView, Mac: SupportMachine> Syscalls for L2Syscal let sudt_id = machine.registers()[A2].to_u8(); let amount = { let amount_addr = machine.registers()[A3].to_u64(); - load_data_u128(machine, amount_addr)? + load_data_h256(machine, amount_addr)?.to_u256() }; // TODO record fee payment in the generator context diff --git a/crates/generator/src/traits.rs b/crates/generator/src/traits.rs index d79daedc8..49571377b 100644 --- a/crates/generator/src/traits.rs +++ b/crates/generator/src/traits.rs @@ -1,10 +1,12 @@ use crate::error::{AccountError, DepositError, Error, WithdrawalError}; use crate::sudt::build_l2_sudt_script; +use gw_common::ckb_decimal::{CKBCapacity, CKB_DECIMAL_POW_EXP}; use gw_common::registry::context::RegistryContext; use gw_common::registry_address::RegistryAddress; use gw_common::{builtins::CKB_SUDT_ACCOUNT_ID, state::State, CKB_SUDT_SCRIPT_ARGS, H256}; use gw_traits::CodeStore; use gw_types::offchain::RollupContext; +use gw_types::U256; use gw_types::{ bytes::Bytes, core::ScriptHashType, @@ -47,7 +49,7 @@ pub trait StateExt { payer: &RegistryAddress, block_producer: &RegistryAddress, sudt_id: u32, - amount: u128, + amount: U256, ) -> Result<(), Error>; fn apply_withdrawal_requests( @@ -112,7 +114,7 @@ impl StateExt for S { payer: &RegistryAddress, block_producer: &RegistryAddress, sudt_id: u32, - amount: u128, + amount: U256, ) -> Result<(), Error> { log::debug!( "account: 0x{} pay fee to block_producer: 0x{}, sudt_id: {}, amount: {}", @@ -136,6 +138,8 @@ impl StateExt for S { let account_script_hash: H256 = request.script().hash().into(); // mint CKB let capacity: u64 = request.capacity().unpack(); + log::debug!("[generator] deposit capacity {}", capacity); + // NOTE: the address length `20` is a hard-coded value, we may re-visit here to extend more address format let address = match self.get_account_id_by_script_hash(&account_script_hash)? { Some(_id) => { @@ -171,10 +175,13 @@ impl StateExt for S { addr } }; - self.mint_sudt(CKB_SUDT_ACCOUNT_ID, &address, capacity.into())?; + // Align CKB to 18 decimals + let ckb_amount = CKBCapacity::from_layer1(capacity).to_layer2(); + self.mint_sudt(CKB_SUDT_ACCOUNT_ID, &address, ckb_amount)?; log::debug!( - "[generator] mint {} shannons CKB to account {}", + "[generator] mint {} shannons * 10^{} CKB to account {}", capacity, + CKB_DECIMAL_POW_EXP, hex::encode(account_script_hash.as_slice()), ); let sudt_script_hash = request.sudt_script_hash().unpack(); @@ -195,7 +202,7 @@ impl StateExt for S { return Err(AccountError::InvalidSUDTOperation.into()); } // mint SUDT - self.mint_sudt(sudt_id, &address, amount)?; + self.mint_sudt(sudt_id, &address, amount.into())?; log::debug!( "[generator] mint {} amount sUDT {} to account {}", amount, @@ -231,22 +238,26 @@ impl StateExt for S { let capacity: u64 = raw.capacity().unpack(); // pay fee to block producer { - let fee: u64 = raw.fee().unpack(); + let fee: U256 = raw.fee().unpack().into(); self.pay_fee( &withdrawal_address, block_producer_address, CKB_SUDT_ACCOUNT_ID, - fee.into(), + fee, )?; } // burn CKB - self.burn_sudt(CKB_SUDT_ACCOUNT_ID, &withdrawal_address, capacity.into())?; + self.burn_sudt( + CKB_SUDT_ACCOUNT_ID, + &withdrawal_address, + CKBCapacity::from_layer1(capacity).to_layer2(), + )?; let sudt_id = self .get_account_id_by_script_hash(&l2_sudt_script_hash.into())? .ok_or(AccountError::UnknownSUDT)?; if sudt_id != CKB_SUDT_ACCOUNT_ID { // burn sudt - self.burn_sudt(sudt_id, &withdrawal_address, amount)?; + self.burn_sudt(sudt_id, &withdrawal_address, amount.into())?; } else if amount != 0 { return Err(WithdrawalError::WithdrawFakedCKB.into()); } diff --git a/crates/generator/src/typed_transaction/types.rs b/crates/generator/src/typed_transaction/types.rs index 1cc47fe6a..11a536dec 100644 --- a/crates/generator/src/typed_transaction/types.rs +++ b/crates/generator/src/typed_transaction/types.rs @@ -6,6 +6,7 @@ use gw_types::{ core::AllowedContractType, packed::{ETHAddrRegArgsReader, L2Transaction, MetaContractArgsReader, SUDTArgsReader}, prelude::*, + U256, }; use crate::error::{AccountError, TransactionValidateError}; @@ -35,7 +36,7 @@ impl TypedTransaction { /// Got expect cost of the tx, (transfer value + fee). /// returns none if tx has no cost, it may happend when we call readonly interface of some Godwoken builtin contracts. - pub fn cost(&self) -> Option { + pub fn cost(&self) -> Option { match self { Self::EthAddrReg(tx) => tx.cost(), Self::Meta(tx) => tx.cost(), @@ -48,7 +49,7 @@ impl TypedTransaction { pub struct EthAddrRegTx(L2Transaction); impl EthAddrRegTx { - pub fn cost(&self) -> Option { + pub fn cost(&self) -> Option { use gw_types::packed::ETHAddrRegArgsUnionReader::*; let args: Bytes = self.0.raw().args().unpack(); @@ -56,8 +57,8 @@ impl EthAddrRegTx { match args.to_enum() { EthToGw(_) | GwToEth(_) => None, - SetMapping(args) => Some(args.fee().amount().unpack()), - BatchSetMapping(args) => Some(args.fee().amount().unpack()), + SetMapping(args) => Some(args.fee().amount().unpack().into()), + BatchSetMapping(args) => Some(args.fee().amount().unpack().into()), } } } @@ -65,14 +66,14 @@ impl EthAddrRegTx { pub struct MetaTx(L2Transaction); impl MetaTx { - pub fn cost(&self) -> Option { + pub fn cost(&self) -> Option { use gw_types::packed::MetaContractArgsUnionReader::*; let args: Bytes = self.0.raw().args().unpack(); let args = MetaContractArgsReader::from_slice(&args).ok()?; match args.to_enum() { - CreateAccount(args) => Some(args.fee().amount().unpack()), + CreateAccount(args) => Some(args.fee().amount().unpack().into()), } } } @@ -80,7 +81,7 @@ impl MetaTx { pub struct SimpleUDTTx(L2Transaction); impl SimpleUDTTx { - pub fn cost(&self) -> Option { + pub fn cost(&self) -> Option { use gw_types::packed::SUDTArgsUnionReader::*; let args: Bytes = self.0.raw().args().unpack(); @@ -94,11 +95,10 @@ impl SimpleUDTTx { if to_id == CKB_SUDT_ACCOUNT_ID { // CKB transfer cost: transfer CKB value + fee let value = args.amount().unpack(); - let cost = value.checked_add(fee.into())?; - cost.try_into().ok() + value.checked_add(fee.into()) } else { // Simple UDT transfer cost: fee - Some(fee) + Some(fee.into()) } } } @@ -107,7 +107,7 @@ impl SimpleUDTTx { pub struct PolyjuiceTx(L2Transaction); impl PolyjuiceTx { - pub fn cost(&self) -> Option { + pub fn cost(&self) -> Option { let args: Bytes = self.0.raw().args().unpack(); if args.len() < 52 { log::error!( diff --git a/crates/generator/src/utils.rs b/crates/generator/src/utils.rs index 565e6de52..d029025af 100644 --- a/crates/generator/src/utils.rs +++ b/crates/generator/src/utils.rs @@ -108,7 +108,7 @@ mod test { // ## Fulfill withdrawal request let req = { - let fee = 50u64; + let fee = 50u128; let raw = RawWithdrawalRequest::new_builder() .nonce(1u32.pack()) .capacity((500 * 10u64.pow(8)).pack()) diff --git a/crates/generator/src/verification/withdrawal.rs b/crates/generator/src/verification/withdrawal.rs index bd4d95bdb..87d50c21c 100644 --- a/crates/generator/src/verification/withdrawal.rs +++ b/crates/generator/src/verification/withdrawal.rs @@ -1,9 +1,12 @@ -use gw_common::{builtins::CKB_SUDT_ACCOUNT_ID, h256_ext::H256Ext, state::State, H256}; +use gw_common::{ + builtins::CKB_SUDT_ACCOUNT_ID, ckb_decimal::CKBCapacity, h256_ext::H256Ext, state::State, H256, +}; use gw_traits::CodeStore; use gw_types::{ offchain::RollupContext, packed::{Script, WithdrawalRequestExtra}, prelude::*, + U256, }; use tracing::instrument; @@ -57,7 +60,7 @@ impl<'a, S: State + CodeStore> WithdrawalVerifier<'a, S> { let sudt_script_hash: H256 = raw.sudt_script_hash().unpack(); let amount: u128 = raw.amount().unpack(); let capacity: u64 = raw.capacity().unpack(); - let fee: u64 = raw.fee().unpack(); + let fee = raw.fee().unpack(); let registry_address = self .state .get_registry_address_by_script_hash(raw.registry_id().unpack(), &account_script_hash)? @@ -93,7 +96,9 @@ impl<'a, S: State + CodeStore> WithdrawalVerifier<'a, S> { let ckb_balance = self .state .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, ®istry_address)?; - let required_ckb_capacity: u128 = capacity.saturating_add(fee).into(); + let required_ckb_capacity = CKBCapacity::from_layer1(capacity) + .to_layer2() + .saturating_add(fee.into()); if required_ckb_capacity > ckb_balance { return Err(WithdrawalError::Overdraft.into()); } @@ -111,7 +116,7 @@ impl<'a, S: State + CodeStore> WithdrawalVerifier<'a, S> { return Err(WithdrawalError::NonPositiveSUDTAmount.into()); } let balance = self.state.get_sudt_balance(sudt_id, ®istry_address)?; - if amount > balance { + if U256::from(amount) > balance { return Err(WithdrawalError::Overdraft.into()); } } else if amount != 0 { diff --git a/crates/jsonrpc-types/src/godwoken.rs b/crates/jsonrpc-types/src/godwoken.rs index ca0941889..fedbe26de 100644 --- a/crates/jsonrpc-types/src/godwoken.rs +++ b/crates/jsonrpc-types/src/godwoken.rs @@ -833,7 +833,7 @@ pub struct RawWithdrawalRequest { // layer1 lock to withdraw after challenge period pub owner_lock_hash: H256, pub chain_id: Uint64, - pub fee: Uint64, + pub fee: Uint128, } impl From for packed::RawWithdrawalRequest { @@ -858,7 +858,7 @@ impl From for packed::RawWithdrawalRequest { .registry_id(registry_id.value().pack()) .owner_lock_hash(owner_lock_hash.pack()) .chain_id(chain_id.value().pack()) - .fee(fee.value().pack()) + .fee(u128::from(fee).pack()) .build() } } @@ -868,7 +868,7 @@ impl From for RawWithdrawalRequest { let nonce: u32 = raw_withdrawal_request.nonce().unpack(); let capacity: u64 = raw_withdrawal_request.capacity().unpack(); let amount: u128 = raw_withdrawal_request.amount().unpack(); - let fee: u64 = raw_withdrawal_request.fee().unpack(); + let fee: u128 = raw_withdrawal_request.fee().unpack(); let chain_id: u64 = raw_withdrawal_request.chain_id().unpack(); let registry_id: u32 = raw_withdrawal_request.registry_id().unpack(); Self { diff --git a/crates/mem-pool/src/fee/queue.rs b/crates/mem-pool/src/fee/queue.rs index 393fa4fde..48cff8054 100644 --- a/crates/mem-pool/src/fee/queue.rs +++ b/crates/mem-pool/src/fee/queue.rs @@ -196,7 +196,7 @@ mod tests { let entry1 = FeeEntry { item: FeeItem::Tx(Default::default()), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -204,7 +204,7 @@ mod tests { let entry2 = FeeEntry { item: FeeItem::Tx(Default::default()), - fee: 101 * 1000, + fee: (101 * 1000u64).into(), cycles_limit: 1000, sender: 3, order: queue.len(), @@ -212,7 +212,7 @@ mod tests { let entry3 = FeeEntry { item: FeeItem::Tx(Default::default()), - fee: 100 * 1001, + fee: (100 * 1001u64).into(), cycles_limit: 1001, sender: 4, order: queue.len(), @@ -220,7 +220,7 @@ mod tests { let entry4 = FeeEntry { item: FeeItem::Withdrawal(Default::default()), - fee: 101 * 1001, + fee: (101 * 1001u64).into(), cycles_limit: 1001, sender: 5, order: queue.len(), @@ -278,7 +278,7 @@ mod tests { let entry1 = FeeEntry { item: FeeItem::Tx(Default::default()), - fee: 10 * 1000, + fee: (10 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -288,7 +288,7 @@ mod tests { let entry2 = FeeEntry { item: FeeItem::Tx(Default::default()), - fee: 1000, + fee: 1000u64.into(), cycles_limit: 100, sender: 3, order: queue.len(), @@ -298,7 +298,7 @@ mod tests { let entry3 = FeeEntry { item: FeeItem::Tx(Default::default()), - fee: 1000, + fee: 1000u64.into(), cycles_limit: 500, sender: 4, order: queue.len(), @@ -308,7 +308,7 @@ mod tests { let entry4 = FeeEntry { item: FeeItem::Withdrawal(Default::default()), - fee: 101 * 1000, + fee: (101 * 1000u64).into(), cycles_limit: 1001, sender: 5, order: queue.len(), @@ -356,7 +356,7 @@ mod tests { .raw(RawL2Transaction::new_builder().nonce(1u32.pack()).build()) .build(), ), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -368,7 +368,7 @@ mod tests { .raw(RawL2Transaction::new_builder().nonce(0u32.pack()).build()) .build(), ), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -415,7 +415,7 @@ mod tests { .raw(RawL2Transaction::new_builder().nonce(0u32.pack()).build()) .build(), ), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -427,7 +427,7 @@ mod tests { .raw(RawL2Transaction::new_builder().nonce(0u32.pack()).build()) .build(), ), - fee: 101 * 1000, + fee: (101 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -444,7 +444,7 @@ mod tests { { let items = queue.fetch(&tree, 3).expect("fetch"); assert_eq!(items.len(), 1); - assert_eq!(items[0].fee, 101 * 1000); + assert_eq!(items[0].fee, (101 * 1000u64).into()); // try fetch remain items let items = queue.fetch(&tree, 1).expect("fetch"); assert_eq!(items.len(), 0); @@ -478,7 +478,7 @@ mod tests { .raw(RawL2Transaction::new_builder().nonce(i.pack()).build()) .build(), ), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), @@ -500,7 +500,7 @@ mod tests { ) .build(), ), - fee: 100 * 1000, + fee: (100 * 1000u64).into(), cycles_limit: 1000, sender: 2, order: queue.len(), diff --git a/crates/mem-pool/src/fee/types.rs b/crates/mem-pool/src/fee/types.rs index c7cb84610..088cf2861 100644 --- a/crates/mem-pool/src/fee/types.rs +++ b/crates/mem-pool/src/fee/types.rs @@ -76,7 +76,7 @@ pub struct FeeEntry { /// sender pub sender: u32, /// fee - pub fee: u64, + pub fee: u128, /// estimate cycles limit pub cycles_limit: u64, } @@ -91,9 +91,10 @@ impl Ord for FeeEntry { fn cmp(&self, other: &Self) -> Ordering { // A / B > C / D => A * D > C * B // higher fee rate is priority - let ord = (self.fee as u128) + let ord = self + .fee .saturating_mul(other.cycles_limit.into()) - .cmp(&(other.fee as u128).saturating_mul(self.cycles_limit.into())); + .cmp(&other.fee.saturating_mul(self.cycles_limit.into())); if ord != Ordering::Equal { return ord; } @@ -152,7 +153,7 @@ impl FeeEntry { } struct L2Fee { - fee: u64, + fee: u128, cycles_limit: u64, } @@ -178,7 +179,7 @@ fn parse_l2tx_fee_rate( match backend_type { BackendType::Meta => { let meta_args = MetaContractArgs::from_slice(raw_l2tx_args.as_ref())?; - let fee: u64 = match meta_args.to_enum() { + let fee = match meta_args.to_enum() { MetaContractArgsUnion::CreateAccount(args) => args.fee().amount().unpack(), }; let cycles_limit: u64 = fee_config.meta_cycles_limit; @@ -187,7 +188,7 @@ fn parse_l2tx_fee_rate( } BackendType::EthAddrReg => { let eth_addr_reg_args = ETHAddrRegArgs::from_slice(raw_l2tx_args.as_ref())?; - let fee: u64 = match eth_addr_reg_args.to_enum() { + let fee = match eth_addr_reg_args.to_enum() { ETHAddrRegArgsUnion::EthToGw(_) | ETHAddrRegArgsUnion::GwToEth(_) => 0, ETHAddrRegArgsUnion::SetMapping(args) => args.fee().amount().unpack(), ETHAddrRegArgsUnion::BatchSetMapping(args) => args.fee().amount().unpack(), @@ -199,7 +200,7 @@ fn parse_l2tx_fee_rate( } BackendType::Sudt => { let sudt_args = SUDTArgs::from_slice(raw_l2tx_args.as_ref())?; - let fee: u64 = match sudt_args.to_enum() { + let fee = match sudt_args.to_enum() { SUDTArgsUnion::SUDTQuery(_) => { // SUDTQuery fee rate is 0 0 @@ -221,7 +222,7 @@ fn parse_l2tx_fee_rate( let gas_limit = u64::from_le_bytes(poly_args[8..16].try_into()?); let gas_price = u128::from_le_bytes(poly_args[16..32].try_into()?); Ok(L2Fee { - fee: gas_price.saturating_mul(gas_limit.into()).try_into()?, + fee: gas_price.saturating_mul(gas_limit.into()), cycles_limit: gas_limit, }) } diff --git a/crates/mem-pool/src/pool.rs b/crates/mem-pool/src/pool.rs index 2fd9b1f02..71bc267bb 100644 --- a/crates/mem-pool/src/pool.rs +++ b/crates/mem-pool/src/pool.rs @@ -10,7 +10,8 @@ use anyhow::{anyhow, Result}; use gw_common::{ - builtins::CKB_SUDT_ACCOUNT_ID, registry_address::RegistryAddress, state::State, H256, + builtins::CKB_SUDT_ACCOUNT_ID, ckb_decimal::CKBCapacity, registry_address::RegistryAddress, + state::State, H256, }; use gw_config::{MemPoolConfig, NodeMode}; use gw_dynamic_config::manager::DynamicConfigManager; @@ -690,7 +691,9 @@ impl MemPool { let address = state .get_registry_address_by_script_hash(registry_id, &script_hash)? .expect("must exist"); - let capacity = state.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)?; + let capacity = CKBCapacity::from_layer2( + state.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &address)?, + ); let deprecated_withdrawals = list.remove_lower_nonce_withdrawals(nonce, capacity); for withdrawal in deprecated_withdrawals { let withdrawal_hash: H256 = withdrawal.hash().into(); diff --git a/crates/mem-pool/src/types.rs b/crates/mem-pool/src/types.rs index 5e90abdbc..e2ecfa84d 100644 --- a/crates/mem-pool/src/types.rs +++ b/crates/mem-pool/src/types.rs @@ -1,3 +1,4 @@ +use gw_common::ckb_decimal::CKBCapacity; use gw_types::{ packed::{L2Transaction, WithdrawalRequestExtra}, prelude::*, @@ -33,7 +34,7 @@ impl EntryList { pub fn remove_lower_nonce_withdrawals( &mut self, nonce: u32, - capacity: u128, + capacity: CKBCapacity, ) -> Vec { let mut removed = Vec::default(); @@ -49,7 +50,8 @@ impl EntryList { // remove lower balance withdrawals if let Some(withdrawal) = self.withdrawals.get(0) { let withdrawal_capacity: u64 = withdrawal.raw().capacity().unpack(); - if (withdrawal_capacity as u128) > capacity { + let capacity = capacity.to_layer1().unwrap(); + if withdrawal_capacity > capacity { // TODO instead of remove all withdrawals, put them into future queue removed.extend_from_slice(&self.withdrawals); self.withdrawals.clear(); diff --git a/crates/mem-pool/src/withdrawal.rs b/crates/mem-pool/src/withdrawal.rs index 578f76aae..e683689b1 100644 --- a/crates/mem-pool/src/withdrawal.rs +++ b/crates/mem-pool/src/withdrawal.rs @@ -334,7 +334,7 @@ mod test { .build(); let req = { - let fee = 50u64; + let fee = 50u128; let raw = RawWithdrawalRequest::new_builder() .nonce(1u32.pack()) .capacity((500 * 10u64.pow(8)).pack()) diff --git a/crates/rpc-server/src/registry.rs b/crates/rpc-server/src/registry.rs index 6eef50173..6ccac5feb 100644 --- a/crates/rpc-server/src/registry.rs +++ b/crates/rpc-server/src/registry.rs @@ -13,7 +13,7 @@ use gw_generator::{ }; use gw_jsonrpc_types::{ blockchain::Script, - ckb_jsonrpc_types::{JsonBytes, Uint128, Uint32}, + ckb_jsonrpc_types::{JsonBytes, Uint32}, godwoken::{ BackendInfo, BackendType, EoaScript, EoaScriptType, ErrorTxReceipt, GlobalState, GwScript, GwScriptType, L2BlockCommittedInfo, L2BlockStatus, L2BlockView, L2BlockWithStatus, @@ -42,6 +42,7 @@ use gw_traits::CodeStore; use gw_types::{ packed::{self, BlockInfo, Byte32, L2Transaction, RollupConfig, WithdrawalRequestExtra}, prelude::*, + U256, }; use gw_version::Version; use jsonrpc_v2::{Data, Error as RpcError, MapRouter, Params, Server, Server as JsonrpcServer}; @@ -1149,7 +1150,7 @@ async fn get_balance( Params(params): Params, store: Data, mem_pool_state: Data>, -) -> Result { +) -> Result { let (serialized_address, sudt_id, block_number) = match params { GetBalanceParams::Tip(p) => (p.0, p.1, None), GetBalanceParams::Number(p) => p, @@ -1170,7 +1171,7 @@ async fn get_balance( tree.get_sudt_balance(sudt_id.into(), &address)? } }; - Ok(balance.into()) + Ok(balance) } // account_id, key, block_number diff --git a/crates/tests/src/tests/chain.rs b/crates/tests/src/tests/chain.rs index 37235ae1b..80366d2e2 100644 --- a/crates/tests/src/tests/chain.rs +++ b/crates/tests/src/tests/chain.rs @@ -10,6 +10,7 @@ use gw_chain::chain::{ }; use gw_common::{ builtins::CKB_SUDT_ACCOUNT_ID, + ckb_decimal::CKBCapacity, h256_ext::H256Ext, merkle_utils::{calculate_ckb_merkle_root, ckb_merkle_leaf_hash}, smt::Blake2bHasher, @@ -167,8 +168,8 @@ async fn test_produce_blocks() { .unwrap(); let balance_a = tree.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &a_addr).unwrap(); let balance_b = tree.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &b_addr).unwrap(); - assert_eq!(balance_a, 690 * CKB as u128); - assert_eq!(balance_b, 500 * CKB as u128); + assert_eq!(balance_a, CKBCapacity::from_layer1(690 * CKB).to_layer2()); + assert_eq!(balance_b, CKBCapacity::from_layer1(500 * CKB).to_layer2()); } drop(chain); @@ -535,7 +536,10 @@ async fn test_layer1_revert() { let alice_balance = tree .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &alice_addr) .unwrap(); - assert_eq!(alice_balance, 400 * CKB as u128); + assert_eq!( + alice_balance, + CKBCapacity::from_layer1(400 * CKB).to_layer2() + ); let bob_id_opt = tree .get_account_id_by_script_hash(&bob_script.hash().into()) @@ -585,7 +589,10 @@ async fn test_layer1_revert() { let alice_balance = tree .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &alice_addr) .unwrap(); - assert_eq!(alice_balance, 400 * CKB as u128); + assert_eq!( + alice_balance, + CKBCapacity::from_layer1(400 * CKB).to_layer2() + ); let bob_script_hash: H256 = bob_script.hash().into(); let bob_id = tree @@ -604,7 +611,7 @@ async fn test_layer1_revert() { let bob_balance = tree .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &bob_addr) .unwrap(); - assert_eq!(bob_balance, 500 * CKB as u128); + assert_eq!(bob_balance, CKBCapacity::from_layer1(500 * CKB).to_layer2()); } } @@ -714,8 +721,8 @@ async fn test_sync_blocks() { .unwrap(); let balance_a = tree.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &a_addr).unwrap(); let balance_b = tree.get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &b_addr).unwrap(); - assert_eq!(balance_a, 800 * CKB as u128); - assert_eq!(balance_b, 500 * CKB as u128); + assert_eq!(balance_a, CKBCapacity::from_layer1(800 * CKB).to_layer2()); + assert_eq!(balance_b, CKBCapacity::from_layer1(500 * CKB).to_layer2()); } drop(chain2); diff --git a/crates/tests/src/tests/deposit_withdrawal.rs b/crates/tests/src/tests/deposit_withdrawal.rs index 8992e7f54..49f8105d1 100644 --- a/crates/tests/src/tests/deposit_withdrawal.rs +++ b/crates/tests/src/tests/deposit_withdrawal.rs @@ -9,8 +9,9 @@ use anyhow::Result; use gw_chain::chain::Chain; use gw_common::{ builtins::{CKB_SUDT_ACCOUNT_ID, ETH_REGISTRY_ACCOUNT_ID}, + ckb_decimal::CKBCapacity, state::State, - H256, U256, + H256, }; use gw_generator::{ error::{DepositError, WithdrawalError}, @@ -25,6 +26,7 @@ use gw_types::{ WithdrawalRequestExtra, }, prelude::*, + U256, }; use std::{collections::HashSet, iter::FromIterator}; @@ -173,9 +175,12 @@ async fn test_deposit_and_withdrawal() { let ckb_balance = tree .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &user_addr) .unwrap(); - assert_eq!(ckb_balance, capacity as u128); + assert_eq!(ckb_balance, CKBCapacity::from_layer1(capacity).to_layer2()); let ckb_total_supply = tree.get_sudt_total_supply(CKB_SUDT_ACCOUNT_ID).unwrap(); - assert_eq!(ckb_total_supply, U256::from(capacity)); + assert_eq!( + ckb_total_supply, + CKBCapacity::from_layer1(capacity).to_layer2() + ); ( user_id, user_script_hash, @@ -207,11 +212,11 @@ async fn test_deposit_and_withdrawal() { state .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &user_addr) .unwrap(), - capacity as u128 + CKBCapacity::from_layer1(capacity).to_layer2() ); assert_eq!( - state.get_sudt_total_supply(CKB_SUDT_ACCOUNT_ID).unwrap(), - U256::from(capacity) + state.get_sudt_total_supply(CKB_SUDT_ACCOUNT_ID,).unwrap(), + CKBCapacity::from_layer1(capacity).to_layer2() ) } // withdrawal @@ -232,11 +237,14 @@ async fn test_deposit_and_withdrawal() { let ckb_balance2 = tree .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &user_addr) .unwrap(); - assert_eq!(ckb_balance, ckb_balance2 + withdraw_capacity as u128); + assert_eq!( + ckb_balance, + ckb_balance2 + CKBCapacity::from_layer1(withdraw_capacity).to_layer2() + ); let ckb_total_supply2 = tree.get_sudt_total_supply(CKB_SUDT_ACCOUNT_ID).unwrap(); assert_eq!( ckb_total_supply, - ckb_total_supply2 + U256::from(withdraw_capacity) + ckb_total_supply2 + CKBCapacity::from_layer1(withdraw_capacity).to_layer2() ); let nonce = tree.get_nonce(user_id).unwrap(); assert_eq!(nonce, 1); @@ -260,8 +268,8 @@ async fn test_deposit_and_withdrawal() { ckb_balance2 ); assert_eq!( - state.get_sudt_total_supply(CKB_SUDT_ACCOUNT_ID).unwrap(), - U256::from(ckb_balance2) + state.get_sudt_total_supply(CKB_SUDT_ACCOUNT_ID,).unwrap(), + ckb_balance2 ); assert_eq!(state.get_nonce(user_id).unwrap(), nonce); } @@ -363,7 +371,7 @@ async fn test_deposit_u128_overflow() { let ckb_balance = tree .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &alice_addr) .unwrap(); - assert_eq!(ckb_balance, capacity as u128); + assert_eq!(ckb_balance, CKBCapacity::from_layer1(capacity).to_layer2()); let bob_id = tree .get_account_id_by_script_hash(&bob_script_hash) @@ -374,10 +382,13 @@ async fn test_deposit_u128_overflow() { let ckb_balance = tree .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &bob_addr) .unwrap(); - assert_eq!(ckb_balance, capacity as u128); + assert_eq!(ckb_balance, CKBCapacity::from_layer1(capacity).to_layer2()); let ckb_total_supply = tree.get_sudt_total_supply(CKB_SUDT_ACCOUNT_ID).unwrap(); - assert_eq!(ckb_total_supply, U256::from(capacity * 2)); + assert_eq!( + ckb_total_supply, + CKBCapacity::from_layer1(capacity).to_layer2() * 2u8 + ); let l2_sudt_script_hash = build_l2_sudt_script(chain.generator().rollup_context(), &sudt_script_hash).hash(); @@ -387,10 +398,10 @@ async fn test_deposit_u128_overflow() { .expect("sudt exists"); let alice_sudt_balance = tree.get_sudt_balance(sudt_id, &alice_addr).unwrap(); - assert_eq!(alice_sudt_balance, u128::MAX); + assert_eq!(alice_sudt_balance, U256::from(u128::MAX)); let bob_sudt_balance = tree.get_sudt_balance(sudt_id, &bob_addr).unwrap(); - assert_eq!(bob_sudt_balance, u128::MAX); + assert_eq!(bob_sudt_balance, U256::from(u128::MAX)); let sudt_total_supply = tree.get_sudt_total_supply(sudt_id).unwrap(); assert_eq!( @@ -463,7 +474,7 @@ async fn test_overdraft() { state .get_sudt_balance(CKB_SUDT_ACCOUNT_ID, &user_addr) .unwrap(), - capacity as u128 + CKBCapacity::from_layer1(capacity).to_layer2() ); } } diff --git a/crates/tests/src/tests/restore_mem_block.rs b/crates/tests/src/tests/restore_mem_block.rs index ae731c931..6d22784ac 100644 --- a/crates/tests/src/tests/restore_mem_block.rs +++ b/crates/tests/src/tests/restore_mem_block.rs @@ -30,6 +30,7 @@ use gw_types::packed::{ WithdrawalRequest, WithdrawalRequestExtra, }; use gw_types::prelude::Pack; +use gw_types::U256; const CKB: u64 = 100000000; @@ -141,7 +142,7 @@ async fn test_restore_mem_block() { let to_addr = RegistryAddress::new(ETH_REGISTRY_ACCOUNT_ID, to_script.hash()[0..20].to_vec()); let transfer = SUDTTransfer::new_builder() - .amount((DEPOSIT_CAPACITY as u128 / 2).pack()) + .amount(U256::from(DEPOSIT_CAPACITY as u128 / 2).pack()) .to_address(Bytes::from(to_addr.to_bytes()).pack()) .fee( Fee::new_builder() diff --git a/crates/tools/src/create_creator_account.rs b/crates/tools/src/create_creator_account.rs index 1a64feed1..22d04868f 100644 --- a/crates/tools/src/create_creator_account.rs +++ b/crates/tools/src/create_creator_account.rs @@ -26,7 +26,7 @@ pub async fn create_creator_account( config_path: &Path, scripts_deployment_path: &Path, ) -> Result<()> { - let fee: u64 = fee_amount.parse().expect("fee format error"); + let fee: u128 = fee_amount.parse().expect("fee format error"); let scripts_deployment_content = std::fs::read_to_string(scripts_deployment_path)?; let scripts_deployment: ScriptsDeploymentResult = diff --git a/crates/tools/src/deposit_ckb.rs b/crates/tools/src/deposit_ckb.rs index e2b349d6d..9709aeab8 100644 --- a/crates/tools/src/deposit_ckb.rs +++ b/crates/tools/src/deposit_ckb.rs @@ -13,6 +13,7 @@ use ckb_types::{ }; use gw_common::builtins::{CKB_SUDT_ACCOUNT_ID, ETH_REGISTRY_ACCOUNT_ID}; use gw_types::packed::{CellOutput, CustodianLockArgs}; +use gw_types::U256; use gw_types::{ bytes::Bytes as GwBytes, packed::{Byte32, DepositLockArgs, Script}, @@ -21,7 +22,6 @@ use gw_types::{ use std::path::Path; use std::str::FromStr; use std::time::{Duration, Instant}; -use std::u128; #[allow(clippy::too_many_arguments)] pub async fn deposit_ckb( @@ -158,7 +158,7 @@ fn privkey_to_lock_hash(privkey: &H256) -> Result { async fn wait_for_balance_change( godwoken_rpc_client: &mut GodwokenRpcClient, from_script_hash: &H256, - init_balance: u128, + init_balance: U256, timeout_secs: u64, ) -> Result<()> { let retry_timeout = Duration::from_secs(timeout_secs); @@ -189,7 +189,7 @@ async fn wait_for_balance_change( async fn get_balance_by_script_hash( godwoken_rpc_client: &mut GodwokenRpcClient, script_hash: &H256, -) -> Result { +) -> Result { let addr = godwoken_rpc_client .get_registry_address_by_script_hash(script_hash) .await; @@ -201,7 +201,7 @@ async fn get_balance_by_script_hash( } Err(e) => { log::warn!("failed to get_registry_address_by_script_hash, {}", e); - 0 + U256::zero() } }; Ok(balance) diff --git a/crates/tools/src/godwoken_rpc.rs b/crates/tools/src/godwoken_rpc.rs index eefc764dc..4b2fc610b 100644 --- a/crates/tools/src/godwoken_rpc.rs +++ b/crates/tools/src/godwoken_rpc.rs @@ -3,16 +3,17 @@ use ckb_jsonrpc_types::Script; use ckb_types::H256; use gw_common::{builtins::ETH_REGISTRY_ACCOUNT_ID, registry_address::RegistryAddress}; use gw_jsonrpc_types::{ - ckb_jsonrpc_types::{JsonBytes, Uint128, Uint32}, + ckb_jsonrpc_types::{JsonBytes, Uint32}, debugger::{DumpChallengeTarget, ReprMockTransaction}, godwoken::{RunResult, TxReceipt}, }; +use gw_types::U256; use std::{ sync::{ atomic::{AtomicU64, Ordering}, Arc, }, - u128, u32, + u32, }; type AccountID = Uint32; @@ -52,12 +53,12 @@ impl GodwokenRpcClient { .map(|opt| opt.map(Into::into)) } - pub async fn get_balance(&self, addr: &RegistryAddress, sudt_id: u32) -> Result { + pub async fn get_balance(&self, addr: &RegistryAddress, sudt_id: u32) -> Result { let params = serde_json::to_value(( JsonBytes::from_vec(addr.to_bytes()), AccountID::from(sudt_id), ))?; - self.rpc::("get_balance", params) + self.rpc::("get_balance", params) .await .map(Into::into) } diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index f404e3d15..13f5792dd 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -1222,7 +1222,7 @@ async fn main() -> Result<()> { let mut rpc_client = GodwokenRpcClient::new(godwoken_rpc_url); let config = read_config(config_path)?; let pk = read_privkey(privkey_path)?; - let fee: u64 = fee.parse().expect("fee format error"); + let fee: u128 = fee.parse().expect("fee format error"); let account_id = match sudt::account::create_sudt_account( &mut rpc_client, diff --git a/crates/tools/src/report_accounts.rs b/crates/tools/src/report_accounts.rs index 99f6d7080..253037357 100644 --- a/crates/tools/src/report_accounts.rs +++ b/crates/tools/src/report_accounts.rs @@ -1,6 +1,7 @@ use anyhow::Result; use ckb_jsonrpc_types::Serialize; use gw_common::builtins::CKB_SUDT_ACCOUNT_ID; +use gw_types::U256; use std::path::Path; use tokio::task::JoinHandle; @@ -10,7 +11,7 @@ use crate::godwoken_rpc::GodwokenRpcClient; struct Account { pub id: u32, pub code_hash: ckb_fixed_hash::H256, - pub ckb: u128, + pub ckb: U256, pub nonce: u32, } diff --git a/crates/tools/src/sudt/account.rs b/crates/tools/src/sudt/account.rs index 52ff150af..b34542ef4 100644 --- a/crates/tools/src/sudt/account.rs +++ b/crates/tools/src/sudt/account.rs @@ -65,7 +65,7 @@ pub async fn create_sudt_account( rpc_client: &mut GodwokenRpcClient, pk: &H256, sudt_type_hash: H256, - fee: u64, + fee: u128, config: &Config, deployment: &ScriptsDeploymentResult, registry_id: u32, diff --git a/crates/tools/src/sudt/transfer.rs b/crates/tools/src/sudt/transfer.rs index 5a37868a6..fe61e62f7 100644 --- a/crates/tools/src/sudt/transfer.rs +++ b/crates/tools/src/sudt/transfer.rs @@ -11,8 +11,8 @@ use gw_common::builtins::ETH_REGISTRY_ACCOUNT_ID; use gw_common::registry_address::RegistryAddress; use gw_types::packed::{Fee, L2Transaction, RawL2Transaction, SUDTArgs, SUDTTransfer}; use gw_types::prelude::Pack as GwPack; +use gw_types::U256; use std::path::Path; -use std::u128; #[allow(clippy::too_many_arguments)] pub async fn transfer( @@ -26,8 +26,8 @@ pub async fn transfer( config_path: &Path, scripts_deployment_path: &Path, ) -> Result<()> { - let amount: u128 = amount.parse().expect("sUDT amount format error"); - let fee: u64 = fee.parse().expect("fee format error"); + let amount: U256 = amount.parse().expect("sUDT amount format error"); + let fee: u128 = fee.parse().expect("fee format error"); let scripts_deployment_content = std::fs::read_to_string(scripts_deployment_path)?; let scripts_deployment: ScriptsDeploymentResult = diff --git a/crates/tools/src/withdraw.rs b/crates/tools/src/withdraw.rs index 173127316..94f6bbc76 100644 --- a/crates/tools/src/withdraw.rs +++ b/crates/tools/src/withdraw.rs @@ -11,6 +11,7 @@ use ckb_types::{prelude::Builder as CKBBuilder, prelude::Entity as CKBEntity}; use gw_common::registry_address::RegistryAddress; use gw_types::core::ScriptHashType; use gw_types::packed::{CellOutput, WithdrawalLockArgs, WithdrawalRequestExtra}; +use gw_types::U256; use gw_types::{ packed::{Byte32, RawWithdrawalRequest, WithdrawalRequest}, prelude::Pack as GwPack, @@ -37,7 +38,7 @@ pub async fn withdraw( let capacity = parse_capacity(capacity)?; let amount: u128 = amount.parse().expect("sUDT amount format error"); let chain_id: u64 = chain_id.parse().expect("chain_id format error"); - let fee = parse_capacity(fee)?; + let fee: u128 = fee.parse()?; let scripts_deployment_content = fs::read_to_string(scripts_deployment_path)?; let scripts_deployment: ScriptsDeploymentResult = @@ -132,7 +133,7 @@ fn create_raw_withdrawal_request( nonce: u32, capacity: u64, amount: u128, - fee: u64, + fee: u128, chain_id: u64, sudt_script_hash: &H256, account_script_hash: &H256, @@ -180,7 +181,7 @@ fn generate_withdrawal_message_to_sign( async fn wait_for_balance_change( godwoken_rpc_client: &mut GodwokenRpcClient, addr: &RegistryAddress, - init_balance: u128, + init_balance: U256, timeout_secs: u64, ) -> Result<()> { let retry_timeout = Duration::from_secs(timeout_secs); diff --git a/crates/types/Cargo.toml b/crates/types/Cargo.toml index fa1da7bf5..194807787 100644 --- a/crates/types/Cargo.toml +++ b/crates/types/Cargo.toml @@ -17,3 +17,4 @@ gw-hash = { path = "../hash", default-features = false } ckb-fixed-hash = { version = "0.100.0", optional = true } sparse-merkle-tree = { version = "0.5.2-rc1", default-features = false } ckb-types = { version = "0.100.0", default-features = false, optional = true } +primitive-types = { version = "0.10", default-features = false, features = [ "impl-serde", "impl-rlp" ] } diff --git a/crates/types/schemas/godwoken.mol b/crates/types/schemas/godwoken.mol index 152b9a161..30394affc 100644 --- a/crates/types/schemas/godwoken.mol +++ b/crates/types/schemas/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: Uint128, } vector WithdrawalRequestVec ; @@ -237,7 +237,7 @@ struct Fee { // registry id registry_id: Uint32, // amount in CKB - amount: Uint64, + amount: Uint128, } table CreateAccount { @@ -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, } diff --git a/crates/types/src/conversion/godwoken.rs b/crates/types/src/conversion/godwoken.rs index e57bd3511..5889fd857 100644 --- a/crates/types/src/conversion/godwoken.rs +++ b/crates/types/src/conversion/godwoken.rs @@ -1,5 +1,7 @@ use core::convert::TryInto; +use primitive_types::U256; + use crate::{core::H256, packed, prelude::*, vec::Vec}; impl Pack for (H256, H256) { @@ -33,6 +35,23 @@ impl<'r> Unpack<[u8; 20]> for packed::Byte20Reader<'r> { } impl_conversion_for_entity_unpack!([u8; 20], Byte20); +impl Pack for U256 { + fn pack(&self) -> packed::Uint256 { + let mut buf = [0u8; 32]; + self.to_little_endian(&mut buf); + packed::Uint256::new_unchecked(buf.to_vec().into()) + } +} + +impl<'r> Unpack for packed::Uint256Reader<'r> { + // Inline so that the panic branch can be optimized out. + #[inline] + fn unpack(&self) -> U256 { + U256::from_little_endian(self.as_slice()) + } +} +impl_conversion_for_entity_unpack!(U256, Uint256); + impl_conversion_for_vector!(u32, Uint32Vec, Uint32VecReader); impl_conversion_for_vector!((H256, H256), KVPairVec, KVPairVecReader); impl_conversion_for_packed_iterator_pack!(KVPair, KVPairVec); diff --git a/crates/types/src/generated/godwoken.rs b/crates/types/src/generated/godwoken.rs index 8ddb17058..af35c8181 100644 --- a/crates/types/src/generated/godwoken.rs +++ b/crates/types/src/generated/godwoken.rs @@ -5568,14 +5568,15 @@ impl ::core::default::Default for RawWithdrawalRequest { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, ]; RawWithdrawalRequest::new_unchecked(v.into()) } } impl RawWithdrawalRequest { - pub const TOTAL_SIZE: usize = 144; - pub const FIELD_SIZES: [usize; 9] = [4, 8, 8, 16, 32, 32, 4, 32, 8]; + pub const TOTAL_SIZE: usize = 152; + pub const FIELD_SIZES: [usize; 9] = [4, 8, 8, 16, 32, 32, 4, 32, 16]; pub const FIELD_COUNT: usize = 9; pub fn nonce(&self) -> Uint32 { Uint32::new_unchecked(self.0.slice(0..4)) @@ -5601,8 +5602,8 @@ impl RawWithdrawalRequest { pub fn owner_lock_hash(&self) -> Byte32 { Byte32::new_unchecked(self.0.slice(104..136)) } - pub fn fee(&self) -> Uint64 { - Uint64::new_unchecked(self.0.slice(136..144)) + pub fn fee(&self) -> Uint128 { + Uint128::new_unchecked(self.0.slice(136..152)) } pub fn as_reader<'r>(&'r self) -> RawWithdrawalRequestReader<'r> { RawWithdrawalRequestReader::new_unchecked(self.as_slice()) @@ -5679,8 +5680,8 @@ impl<'r> ::core::fmt::Display for RawWithdrawalRequestReader<'r> { } } impl<'r> RawWithdrawalRequestReader<'r> { - pub const TOTAL_SIZE: usize = 144; - pub const FIELD_SIZES: [usize; 9] = [4, 8, 8, 16, 32, 32, 4, 32, 8]; + pub const TOTAL_SIZE: usize = 152; + pub const FIELD_SIZES: [usize; 9] = [4, 8, 8, 16, 32, 32, 4, 32, 16]; pub const FIELD_COUNT: usize = 9; pub fn nonce(&self) -> Uint32Reader<'r> { Uint32Reader::new_unchecked(&self.as_slice()[0..4]) @@ -5706,8 +5707,8 @@ impl<'r> RawWithdrawalRequestReader<'r> { pub fn owner_lock_hash(&self) -> Byte32Reader<'r> { Byte32Reader::new_unchecked(&self.as_slice()[104..136]) } - pub fn fee(&self) -> Uint64Reader<'r> { - Uint64Reader::new_unchecked(&self.as_slice()[136..144]) + pub fn fee(&self) -> Uint128Reader<'r> { + Uint128Reader::new_unchecked(&self.as_slice()[136..152]) } } impl<'r> molecule::prelude::Reader<'r> for RawWithdrawalRequestReader<'r> { @@ -5741,11 +5742,11 @@ pub struct RawWithdrawalRequestBuilder { pub(crate) account_script_hash: Byte32, pub(crate) registry_id: Uint32, pub(crate) owner_lock_hash: Byte32, - pub(crate) fee: Uint64, + pub(crate) fee: Uint128, } impl RawWithdrawalRequestBuilder { - pub const TOTAL_SIZE: usize = 144; - pub const FIELD_SIZES: [usize; 9] = [4, 8, 8, 16, 32, 32, 4, 32, 8]; + pub const TOTAL_SIZE: usize = 152; + pub const FIELD_SIZES: [usize; 9] = [4, 8, 8, 16, 32, 32, 4, 32, 16]; pub const FIELD_COUNT: usize = 9; pub fn nonce(mut self, v: Uint32) -> Self { self.nonce = v; @@ -5779,7 +5780,7 @@ impl RawWithdrawalRequestBuilder { self.owner_lock_hash = v; self } - pub fn fee(mut self, v: Uint64) -> Self { + pub fn fee(mut self, v: Uint128) -> Self { self.fee = v; self } @@ -6181,12 +6182,12 @@ impl ::core::fmt::Display for WithdrawalRequest { impl ::core::default::Default for WithdrawalRequest { fn default() -> Self { let v: Vec = vec![ - 160, 0, 0, 0, 12, 0, 0, 0, 156, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 168, 0, 0, 0, 12, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; WithdrawalRequest::new_unchecked(v.into()) } @@ -9028,9 +9029,10 @@ impl ::core::fmt::Display for MetaContractArgs { impl ::core::default::Default for MetaContractArgs { fn default() -> Self { let v: Vec = vec![ - 0, 0, 0, 0, 77, 0, 0, 0, 12, 0, 0, 0, 65, 0, 0, 0, 53, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, + 0, 0, 0, 0, 85, 0, 0, 0, 12, 0, 0, 0, 65, 0, 0, 0, 53, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, ]; MetaContractArgs::new_unchecked(v.into()) } @@ -9295,19 +9297,19 @@ impl ::core::fmt::Display for Fee { } impl ::core::default::Default for Fee { fn default() -> Self { - let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; Fee::new_unchecked(v.into()) } } impl Fee { - pub const TOTAL_SIZE: usize = 12; - pub const FIELD_SIZES: [usize; 2] = [4, 8]; + pub const TOTAL_SIZE: usize = 20; + pub const FIELD_SIZES: [usize; 2] = [4, 16]; pub const FIELD_COUNT: usize = 2; pub fn registry_id(&self) -> Uint32 { Uint32::new_unchecked(self.0.slice(0..4)) } - pub fn amount(&self) -> Uint64 { - Uint64::new_unchecked(self.0.slice(4..12)) + pub fn amount(&self) -> Uint128 { + Uint128::new_unchecked(self.0.slice(4..20)) } pub fn as_reader<'r>(&'r self) -> FeeReader<'r> { FeeReader::new_unchecked(self.as_slice()) @@ -9365,14 +9367,14 @@ impl<'r> ::core::fmt::Display for FeeReader<'r> { } } impl<'r> FeeReader<'r> { - pub const TOTAL_SIZE: usize = 12; - pub const FIELD_SIZES: [usize; 2] = [4, 8]; + pub const TOTAL_SIZE: usize = 20; + pub const FIELD_SIZES: [usize; 2] = [4, 16]; pub const FIELD_COUNT: usize = 2; pub fn registry_id(&self) -> Uint32Reader<'r> { Uint32Reader::new_unchecked(&self.as_slice()[0..4]) } - pub fn amount(&self) -> Uint64Reader<'r> { - Uint64Reader::new_unchecked(&self.as_slice()[4..12]) + pub fn amount(&self) -> Uint128Reader<'r> { + Uint128Reader::new_unchecked(&self.as_slice()[4..20]) } } impl<'r> molecule::prelude::Reader<'r> for FeeReader<'r> { @@ -9399,17 +9401,17 @@ impl<'r> molecule::prelude::Reader<'r> for FeeReader<'r> { #[derive(Debug, Default)] pub struct FeeBuilder { pub(crate) registry_id: Uint32, - pub(crate) amount: Uint64, + pub(crate) amount: Uint128, } impl FeeBuilder { - pub const TOTAL_SIZE: usize = 12; - pub const FIELD_SIZES: [usize; 2] = [4, 8]; + pub const TOTAL_SIZE: usize = 20; + pub const FIELD_SIZES: [usize; 2] = [4, 16]; pub const FIELD_COUNT: usize = 2; pub fn registry_id(mut self, v: Uint32) -> Self { self.registry_id = v; self } - pub fn amount(mut self, v: Uint64) -> Self { + pub fn amount(mut self, v: Uint128) -> Self { self.amount = v; self } @@ -9463,9 +9465,9 @@ impl ::core::fmt::Display for CreateAccount { impl ::core::default::Default for CreateAccount { fn default() -> Self { let v: Vec = vec![ - 77, 0, 0, 0, 12, 0, 0, 0, 65, 0, 0, 0, 53, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, + 85, 0, 0, 0, 12, 0, 0, 0, 65, 0, 0, 0, 53, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; CreateAccount::new_unchecked(v.into()) } @@ -10257,8 +10259,9 @@ impl ::core::fmt::Display for SUDTTransfer { impl ::core::default::Default for SUDTTransfer { fn default() -> Self { let v: Vec = vec![ - 48, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 72, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; SUDTTransfer::new_unchecked(v.into()) } @@ -10287,11 +10290,11 @@ impl SUDTTransfer { let end = molecule::unpack_number(&slice[8..]) as usize; Bytes::new_unchecked(self.0.slice(start..end)) } - pub fn amount(&self) -> Uint128 { + pub fn amount(&self) -> Uint256 { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[8..]) as usize; let end = molecule::unpack_number(&slice[12..]) as usize; - Uint128::new_unchecked(self.0.slice(start..end)) + Uint256::new_unchecked(self.0.slice(start..end)) } pub fn fee(&self) -> Fee { let slice = self.as_slice(); @@ -10388,11 +10391,11 @@ impl<'r> SUDTTransferReader<'r> { let end = molecule::unpack_number(&slice[8..]) as usize; BytesReader::new_unchecked(&self.as_slice()[start..end]) } - pub fn amount(&self) -> Uint128Reader<'r> { + pub fn amount(&self) -> Uint256Reader<'r> { let slice = self.as_slice(); let start = molecule::unpack_number(&slice[8..]) as usize; let end = molecule::unpack_number(&slice[12..]) as usize; - Uint128Reader::new_unchecked(&self.as_slice()[start..end]) + Uint256Reader::new_unchecked(&self.as_slice()[start..end]) } pub fn fee(&self) -> FeeReader<'r> { let slice = self.as_slice(); @@ -10455,7 +10458,7 @@ impl<'r> molecule::prelude::Reader<'r> for SUDTTransferReader<'r> { return ve!(Self, OffsetsNotMatch); } BytesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?; - Uint128Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?; + Uint256Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?; FeeReader::verify(&slice[offsets[2]..offsets[3]], compatible)?; Ok(()) } @@ -10463,7 +10466,7 @@ impl<'r> molecule::prelude::Reader<'r> for SUDTTransferReader<'r> { #[derive(Debug, Default)] pub struct SUDTTransferBuilder { pub(crate) to_address: Bytes, - pub(crate) amount: Uint128, + pub(crate) amount: Uint256, pub(crate) fee: Fee, } impl SUDTTransferBuilder { @@ -10472,7 +10475,7 @@ impl SUDTTransferBuilder { self.to_address = v; self } - pub fn amount(mut self, v: Uint128) -> Self { + pub fn amount(mut self, v: Uint256) -> Self { self.amount = v; self } @@ -13263,8 +13266,8 @@ impl ::core::fmt::Display for CCWithdrawalWitness { impl ::core::default::Default for CCWithdrawalWitness { fn default() -> Self { let v: Vec = vec![ - 130, 2, 0, 0, 36, 0, 0, 0, 88, 1, 0, 0, 248, 1, 0, 0, 45, 2, 0, 0, 98, 2, 0, 0, 118, 2, - 0, 0, 122, 2, 0, 0, 126, 2, 0, 0, 52, 1, 0, 0, 44, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, + 138, 2, 0, 0, 36, 0, 0, 0, 88, 1, 0, 0, 0, 2, 0, 0, 53, 2, 0, 0, 106, 2, 0, 0, 126, 2, + 0, 0, 130, 2, 0, 0, 134, 2, 0, 0, 52, 1, 0, 0, 44, 0, 0, 0, 52, 0, 0, 0, 56, 0, 0, 0, 88, 0, 0, 0, 120, 0, 0, 0, 128, 0, 0, 0, 164, 0, 0, 0, 200, 0, 0, 0, 204, 0, 0, 0, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -13275,17 +13278,17 @@ impl ::core::default::Default for CCWithdrawalWitness { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 160, 0, 0, 0, 12, 0, 0, 0, 156, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 168, 0, 0, 0, 12, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, 16, - 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, 16, 0, 0, 0, - 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 53, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, + 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 12, + 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; CCWithdrawalWitness::new_unchecked(v.into()) } @@ -16236,20 +16239,20 @@ impl ::core::default::Default for SetMapping { fn default() -> Self { let v: Vec = vec![ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; SetMapping::new_unchecked(v.into()) } } impl SetMapping { - pub const TOTAL_SIZE: usize = 44; - pub const FIELD_SIZES: [usize; 2] = [32, 12]; + pub const TOTAL_SIZE: usize = 52; + pub const FIELD_SIZES: [usize; 2] = [32, 20]; pub const FIELD_COUNT: usize = 2; pub fn gw_script_hash(&self) -> Byte32 { Byte32::new_unchecked(self.0.slice(0..32)) } pub fn fee(&self) -> Fee { - Fee::new_unchecked(self.0.slice(32..44)) + Fee::new_unchecked(self.0.slice(32..52)) } pub fn as_reader<'r>(&'r self) -> SetMappingReader<'r> { SetMappingReader::new_unchecked(self.as_slice()) @@ -16307,14 +16310,14 @@ impl<'r> ::core::fmt::Display for SetMappingReader<'r> { } } impl<'r> SetMappingReader<'r> { - pub const TOTAL_SIZE: usize = 44; - pub const FIELD_SIZES: [usize; 2] = [32, 12]; + pub const TOTAL_SIZE: usize = 52; + pub const FIELD_SIZES: [usize; 2] = [32, 20]; pub const FIELD_COUNT: usize = 2; pub fn gw_script_hash(&self) -> Byte32Reader<'r> { Byte32Reader::new_unchecked(&self.as_slice()[0..32]) } pub fn fee(&self) -> FeeReader<'r> { - FeeReader::new_unchecked(&self.as_slice()[32..44]) + FeeReader::new_unchecked(&self.as_slice()[32..52]) } } impl<'r> molecule::prelude::Reader<'r> for SetMappingReader<'r> { @@ -16344,8 +16347,8 @@ pub struct SetMappingBuilder { pub(crate) fee: Fee, } impl SetMappingBuilder { - pub const TOTAL_SIZE: usize = 44; - pub const FIELD_SIZES: [usize; 2] = [32, 12]; + pub const TOTAL_SIZE: usize = 52; + pub const FIELD_SIZES: [usize; 2] = [32, 20]; pub const FIELD_COUNT: usize = 2; pub fn gw_script_hash(mut self, v: Byte32) -> Self { self.gw_script_hash = v; @@ -16405,7 +16408,8 @@ impl ::core::fmt::Display for BatchSetMapping { impl ::core::default::Default for BatchSetMapping { fn default() -> Self { let v: Vec = vec![ - 28, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 36, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, ]; BatchSetMapping::new_unchecked(v.into()) } diff --git a/crates/types/src/generated/store.rs b/crates/types/src/generated/store.rs index 4a5eeb9cc..9bb095dfc 100644 --- a/crates/types/src/generated/store.rs +++ b/crates/types/src/generated/store.rs @@ -3410,14 +3410,15 @@ impl ::core::fmt::Display for WithdrawalRequestExtra { impl ::core::default::Default for WithdrawalRequestExtra { fn default() -> Self { let v: Vec = vec![ - 225, 0, 0, 0, 12, 0, 0, 0, 172, 0, 0, 0, 160, 0, 0, 0, 12, 0, 0, 0, 156, 0, 0, 0, 0, 0, + 233, 0, 0, 0, 12, 0, 0, 0, 180, 0, 0, 0, 168, 0, 0, 0, 12, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 53, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, ]; WithdrawalRequestExtra::new_unchecked(v.into()) } diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index c82bfebb7..54272c49d 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -12,6 +12,7 @@ mod std_traits; pub use generated::packed; pub use molecule::bytes; +pub use primitive_types::U256; cfg_if::cfg_if! { if #[cfg(feature = "std")] { diff --git a/crates/web3-indexer/src/indexer.rs b/crates/web3-indexer/src/indexer.rs index e532cc270..28783039b 100644 --- a/crates/web3-indexer/src/indexer.rs +++ b/crates/web3-indexer/src/indexer.rs @@ -17,18 +17,22 @@ use gw_common::state::State; use gw_common::{builtins::CKB_SUDT_ACCOUNT_ID, registry_address::RegistryAddress}; use gw_store::{state::state_db::StateContext, traits::chain_store::ChainStore, Store}; use gw_traits::CodeStore; -use gw_types::packed::{ - L2Block, RollupAction, RollupActionReader, RollupActionUnion, Transaction, WitnessArgs, -}; use gw_types::{ bytes::Bytes, packed::{SUDTArgs, SUDTArgsUnion, Script}, prelude::*, }; +use gw_types::{ + packed::{ + L2Block, RollupAction, RollupActionReader, RollupActionUnion, Transaction, WitnessArgs, + }, + U256, +}; use rust_decimal::{prelude::ToPrimitive, Decimal}; use sqlx::types::chrono::{DateTime, NaiveDateTime, Utc}; use sqlx::PgPool; +// TODO: Remove web3 indexer const MILLIS_PER_SEC: u64 = 1_000; pub struct Web3Indexer { pool: PgPool, @@ -146,8 +150,8 @@ impl Web3Indexer { .bind(hex(web3_block.hash.as_slice())?) .bind(hex(web3_block.parent_hash.as_slice())?) .bind(hex(&web3_block.logs_bloom)?) - .bind(Decimal::from(web3_block.gas_limit)) - .bind(Decimal::from(web3_block.gas_used)) + .bind(format!("0x{:x}", web3_block.gas_limit)) + .bind(format!("0x{:x}", web3_block.gas_used)) .bind(web3_block.timestamp) .bind(hex(&web3_block.miner)?) .bind(Decimal::from(web3_block.size)) @@ -174,16 +178,16 @@ impl Web3Indexer { .bind(web3_tx.transaction_index) .bind(hex(&web3_tx.from_address)?) .bind(web3_to_address_hex) - .bind(Decimal::from(web3_tx.value)) + .bind(format!("0x{:x}", web3_tx.value)) .bind(Decimal::from(web3_tx.nonce)) - .bind(Decimal::from(web3_tx.gas_limit)) + .bind(format!("0x{:x}", web3_tx.gas_limit)) .bind(Decimal::from(web3_tx.gas_price)) .bind(hex(&web3_tx.data)?) .bind(Decimal::from(web3_tx.v)) .bind(hex(&web3_tx.r)?) .bind(hex(&web3_tx.s)?) - .bind(Decimal::from(web3_tx.cumulative_gas_used)) - .bind(Decimal::from(web3_tx.gas_used)) + .bind(format!("0x{:x}", web3_tx.cumulative_gas_used)) + .bind(format!("0x{:x}", web3_tx.gas_used)) .bind(hex(&web3_tx.logs_bloom)?) .bind(web3_contract_address_hex) .bind(web3_tx.status) @@ -245,7 +249,7 @@ impl Web3Indexer { ) -> Result> { let block_number = l2_block.raw().number().unpack(); let block_hash: gw_common::H256 = blake2b_256(l2_block.raw().as_slice()).into(); - let mut cumulative_gas_used = 0; + let mut cumulative_gas_used = 0u128; let l2_transactions = l2_block.transactions(); let mut web3_tx_with_logs_vec: Vec = vec![]; let mut tx_index = 0u32; @@ -376,7 +380,7 @@ impl Web3Indexer { tx_index, from_address, to_address, - polyjuice_args.value, + polyjuice_args.value.into(), nonce, polyjuice_args.gas_limit.into(), polyjuice_args.gas_price, @@ -454,13 +458,13 @@ impl Web3Indexer { let mut to_address = [0u8; 20]; to_address.copy_from_slice(to_address_data.as_ref()); - let amount: u128 = sudt_transfer.amount().unpack(); - let fee: u64 = sudt_transfer.fee().amount().unpack(); + let amount: U256 = sudt_transfer.amount().unpack(); + let fee = sudt_transfer.fee().amount().unpack(); let value = amount; // Represent SUDTTransfer fee in web3 style, set gas_price as 1 temporary. let gas_price = 1; - let gas_limit = fee.into(); + let gas_limit = fee; cumulative_gas_used += gas_limit; let nonce: u32 = l2_transaction.raw().nonce().unpack(); @@ -510,8 +514,8 @@ impl Web3Indexer { let block_number = l2_block.raw().number().unpack(); let block_hash: gw_common::H256 = l2_block.hash().into(); let parent_hash: gw_common::H256 = l2_block.raw().parent_block_hash().unpack(); - let mut gas_limit = 0; - let mut gas_used = 0; + let mut gas_limit = 0u128; + let mut gas_used = 0u128; for web3_tx_with_logs in web3_tx_with_logs_vec { gas_limit += web3_tx_with_logs.tx.gas_limit; gas_used += web3_tx_with_logs.tx.gas_used; diff --git a/crates/web3-indexer/src/types.rs b/crates/web3-indexer/src/types.rs index 5a115ad7d..6d9365fb2 100644 --- a/crates/web3-indexer/src/types.rs +++ b/crates/web3-indexer/src/types.rs @@ -1,4 +1,5 @@ use gw_common::H256; +use gw_types::U256; use sha3::{Digest, Keccak256}; use sqlx::types::chrono::{DateTime, Utc}; @@ -26,7 +27,7 @@ pub struct Transaction { pub transaction_index: u32, pub from_address: Address, pub to_address: Option
, - pub value: u128, + pub value: U256, pub nonce: u32, pub gas_limit: u128, pub gas_price: u128, @@ -51,7 +52,7 @@ impl Transaction { transaction_index: u32, from_address: Address, to_address: Option
, - value: u128, + value: U256, nonce: u32, gas_limit: u128, gas_price: u128, diff --git a/devtools/fetch-binaries.sh b/devtools/fetch-binaries.sh index 548cf792c..27cbd0f25 100755 --- a/devtools/fetch-binaries.sh +++ b/devtools/fetch-binaries.sh @@ -1,6 +1,6 @@ #!/bin/bash dir_name='.tmp/binaries' -image='ghcr.io/jjyr/godwoken-prebuilds:refactor-registry-address-202204061147' +image='ghcr.io/zeroqn/godwoken-prebuilds:v1.1-feat-change-ckb-decimal-to-18' docker pull $image [ -d $dir_name ] && rm -rf $dir_name && echo "Delete old dir"