Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split ckb-gen-types from ckb-types #4073

Merged
merged 24 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
adae0b0
create crate ckb-gen-types
EthanYuan Jul 12, 2023
d11afe6
move generated extensions and protocols.
EthanYuan Jul 16, 2023
02cad4e
move conversion module from ckb-types to ckb-gen-types.
EthanYuan Jul 16, 2023
724349e
Adjusting the folder
EthanYuan Jul 17, 2023
db07e5f
remove ckb-types generated protocols.
EthanYuan Jul 17, 2023
9df73d1
finish conversion/prelude for ckb-gen-types.
EthanYuan Jul 17, 2023
43e346c
finish extension for ckb-gen-types.
EthanYuan Jul 18, 2023
826e0ff
rename core to base in gen-types.
EthanYuan Jul 19, 2023
f81b20c
finish "std" features in ckb-gen-types.
EthanYuan Jul 19, 2023
1fbc627
finish ckb-types.
EthanYuan Jul 19, 2023
20b8fd9
Re-export ckb-gen-types in ckb-types.
EthanYuan Jul 20, 2023
be396a8
impl features of ckb-gen-types.
EthanYuan Jul 21, 2023
7dfb135
fix integration test.
EthanYuan Jul 21, 2023
5feee78
keep DepType in ckb-types.
EthanYuan Jul 24, 2023
2339d68
keep ExtraHashView in ckb-types
EthanYuan Jul 24, 2023
19fb090
Simplified core mod.
EthanYuan Jul 24, 2023
96433c8
fix make clippy
EthanYuan Jul 24, 2023
378835b
remove the hash mod in ckb-gen-types.
EthanYuan Jul 25, 2023
fce8454
Replace std trait with rust core trait, which can be used for both st…
EthanYuan Jul 26, 2023
b922314
Simplify code.
EthanYuan Jul 27, 2023
1763a79
impl the PartialOrd and Ord traits for the main types used in transac…
EthanYuan Jul 27, 2023
2022674
fix: rename file blockchain.rs
EthanYuan Aug 1, 2023
b2e0f46
fix PHONY "gen" in Makefile.
EthanYuan Aug 17, 2023
b773215
Merge pull request #2 from EthanYuan/ci-test
EthanYuan Aug 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ members = [
"util/constant",
"error",
"util/multisig",
"util/gen-types",
"util/types",
"util/jsonrpc-types",
"freezer",
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ check-dirty-hashes-toml: gen-hashes

##@ Generates Files
.PHONY: gen
GEN_MOL_IN_DIR := util/types/schemas
GEN_MOL_OUT_DIR := util/types/src/generated
GEN_MOL_IN_DIR := util/gen-types/schemas
GEN_MOL_OUT_DIR := util/gen-types/src/generated
GEN_MOL_FILES := ${GEN_MOL_OUT_DIR}/blockchain.rs ${GEN_MOL_OUT_DIR}/extensions.rs ${GEN_MOL_OUT_DIR}/protocols.rs
gen: check-moleculec-version ${GEN_MOL_FILES} # Generate Protocol Files

Expand Down
2 changes: 1 addition & 1 deletion sync/src/relayer/block_transactions_verifier.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Status, StatusCode};
use ckb_types::{core, packed};
use ckb_types::{core, packed, prelude::*};

pub struct BlockTransactionsVerifier {}

Expand Down
2 changes: 1 addition & 1 deletion test/src/specs/sync/sync_and_mine.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Node, Spec};
use ckb_types::packed;
use ckb_types::{packed, prelude::*};
use std::thread::sleep;
use std::time::{Duration, Instant};

Expand Down
32 changes: 32 additions & 0 deletions util/gen-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "ckb-gen-types"
version = "0.112.0-pre"
authors = ["Nervos Core Dev <[email protected]>"]
edition = "2021"
license = "MIT"
description = "Provides the generated types for CKB."
homepage = "https://github.com/nervosnetwork/ckb"
repository = "https://github.com/nervosnetwork/ckb"

[dev-dependencies]
ckb-hash = {path = "../hash", version = "= 0.112.0-pre"}

[features]
default = ["std"]
# Enable the `calc-hash` extension for CKB contract development in `no-std` env
calc-hash = ["ckb-hash/ckb-contract"]
# Enable the `check-data` extension for CKB contract development in `no-std` env
check-data = []
# Enable the `serialized-size` extension for CKB contract development in `no-std` env
serialized-size = ["calc-hash"]
# Enable all in `std` env
std = ["molecule/std", "ckb-fixed-hash", "ckb-error", "ckb-occupied-capacity", "numext-fixed-uint"]

[dependencies]
cfg-if = "1.0"
ckb-hash = { path = "../hash", version = "= 0.112.0-pre" }
molecule = { version = "0.7.5", default-features = false }
ckb-fixed-hash = { path = "../fixed-hash", version = "= 0.112.0-pre", optional = true }
ckb-error = { path = "../../error", version = "= 0.112.0-pre", optional = true }
ckb-occupied-capacity = { path = "../occupied-capacity", version = "= 0.112.0-pre", optional = true }
numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"], optional = true }
54 changes: 54 additions & 0 deletions util/gen-types/src/conversion/blockchain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#[cfg(feature = "std")]
mod std_env;

use crate::{borrow::ToOwned, bytes::Bytes, generated::packed, prelude::*, vec::Vec};

impl Pack<packed::Byte32> for [u8; 32] {
fn pack(&self) -> packed::Byte32 {
packed::Byte32::from_slice(&self[..]).expect("impossible: fail to pack [u8; 32]")
}
}

impl Pack<packed::ProposalShortId> for [u8; 10] {
fn pack(&self) -> packed::ProposalShortId {
packed::ProposalShortId::from_slice(&self[..])
.expect("impossible: fail to pack to ProposalShortId")
}
}

impl Pack<packed::Bytes> for Bytes {
fn pack(&self) -> packed::Bytes {
let len = (self.len() as u32).to_le_bytes();
let mut v = Vec::with_capacity(4 + self.len());
v.extend_from_slice(&len[..]);
v.extend_from_slice(&self[..]);
packed::Bytes::new_unchecked(v.into())
}
}

impl<'r> Unpack<Bytes> for packed::BytesReader<'r> {
fn unpack(&self) -> Bytes {
Bytes::from(self.raw_data().to_owned())
}
}

impl Unpack<Bytes> for packed::Bytes {
fn unpack(&self) -> Bytes {
self.raw_data()
}
}

impl_conversion_for_vector!(Bytes, BytesVec, BytesVecReader);
impl_conversion_for_packed_optional_pack!(Byte32, Byte32Opt);
impl_conversion_for_packed_optional_pack!(CellOutput, CellOutputOpt);
impl_conversion_for_packed_optional_pack!(Script, ScriptOpt);
impl_conversion_for_packed_iterator_pack!(ProposalShortId, ProposalShortIdVec);
impl_conversion_for_packed_iterator_pack!(Bytes, BytesVec);
impl_conversion_for_packed_iterator_pack!(Transaction, TransactionVec);
impl_conversion_for_packed_iterator_pack!(OutPoint, OutPointVec);
impl_conversion_for_packed_iterator_pack!(CellDep, CellDepVec);
impl_conversion_for_packed_iterator_pack!(CellOutput, CellOutputVec);
impl_conversion_for_packed_iterator_pack!(CellInput, CellInputVec);
impl_conversion_for_packed_iterator_pack!(UncleBlock, UncleBlockVec);
impl_conversion_for_packed_iterator_pack!(Header, HeaderVec);
impl_conversion_for_packed_iterator_pack!(Byte32, Byte32Vec);
47 changes: 47 additions & 0 deletions util/gen-types/src/conversion/blockchain/std_env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use ckb_fixed_hash::H256;
use ckb_occupied_capacity::Capacity;
use numext_fixed_uint::U256;

use crate::{packed, prelude::*};

impl Pack<packed::Uint64> for Capacity {
fn pack(&self) -> packed::Uint64 {
self.as_u64().pack()
}
}

impl<'r> Unpack<Capacity> for packed::Uint64Reader<'r> {
fn unpack(&self) -> Capacity {
Capacity::shannons(self.unpack())
}
}
impl_conversion_for_entity_unpack!(Capacity, Uint64);

impl Pack<packed::Uint256> for U256 {
fn pack(&self) -> packed::Uint256 {
packed::Uint256::from_slice(&self.to_le_bytes()[..]).expect("impossible: fail to pack U256")
}
}

impl<'r> Unpack<U256> for packed::Uint256Reader<'r> {
fn unpack(&self) -> U256 {
U256::from_little_endian(self.as_slice()).expect("internal error: fail to unpack U256")
}
}
impl_conversion_for_entity_unpack!(U256, Uint256);

impl Pack<packed::Byte32> for H256 {
fn pack(&self) -> packed::Byte32 {
packed::Byte32::from_slice(self.as_bytes()).expect("impossible: fail to pack H256")
}
}

impl<'r> Unpack<H256> for packed::Byte32Reader<'r> {
fn unpack(&self) -> H256 {
H256::from_slice(self.as_slice()).expect("internal error: fail to unpack H256")
}
}
impl_conversion_for_entity_unpack!(H256, Byte32);

impl_conversion_for_option!(H256, Byte32Opt, Byte32OptReader);
impl_conversion_for_vector!(Capacity, Uint64Vec, Uint64VecReader);
6 changes: 6 additions & 0 deletions util/gen-types/src/conversion/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[macro_use]
mod utilities;

mod blockchain;
mod network;
mod primitive;
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::{bytes::Bytes, packed, prelude::*};
#[cfg(not(feature = "std"))]
use alloc::{borrow::ToOwned, str, string::String};
#[cfg(feature = "std")]
use std::str;

use crate::{bytes::Bytes, generated::packed, prelude::*, vec, vec::Vec};

impl Pack<packed::Bool> for bool {
fn pack(&self) -> packed::Bool {
Expand Down Expand Up @@ -146,8 +151,8 @@ impl Pack<packed::Bytes> for str {

impl<'r> packed::BytesReader<'r> {
/// Converts self to a string slice.
pub fn as_utf8(&self) -> Result<&str, ::std::str::Utf8Error> {
::std::str::from_utf8(self.raw_data())
pub fn as_utf8(&self) -> Result<&str, str::Utf8Error> {
str::from_utf8(self.raw_data())
}

/// Converts self to a string slice without checking that the string contains valid UTF-8.
Expand All @@ -158,7 +163,7 @@ impl<'r> packed::BytesReader<'r> {
/// it are valid UTF-8. If this constraint is violated, undefined behavior
/// results, as the rest of Rust assumes that [`&str`]s are valid UTF-8.
pub unsafe fn as_utf8_unchecked(&self) -> &str {
::std::str::from_utf8_unchecked(self.raw_data())
str::from_utf8_unchecked(self.raw_data())
}

/// Checks whether self is contains valid UTF-8 binary data.
Expand Down
98 changes: 98 additions & 0 deletions util/gen-types/src/conversion/utilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
macro_rules! impl_conversion_for_entity_unpack {
($original:ty, $entity:ident) => {
impl Unpack<$original> for packed::$entity {
fn unpack(&self) -> $original {
self.as_reader().unpack()
}
}
};
}

macro_rules! impl_conversion_for_option_pack {
($original:ty, $entity:ident) => {
impl Pack<packed::$entity> for Option<$original> {
fn pack(&self) -> packed::$entity {
if let Some(ref inner) = self {
packed::$entity::new_unchecked(inner.pack().as_bytes())
} else {
packed::$entity::default()
}
}
}
};
}

macro_rules! impl_conversion_for_option_unpack {
($original:ty, $entity:ident, $reader:ident) => {
impl<'r> Unpack<Option<$original>> for packed::$reader<'r> {
fn unpack(&self) -> Option<$original> {
self.to_opt().map(|x| x.unpack())
}
}
impl_conversion_for_entity_unpack!(Option<$original>, $entity);
};
}

macro_rules! impl_conversion_for_option {
($original:ty, $entity:ident, $reader:ident) => {
impl_conversion_for_option_pack!($original, $entity);
impl_conversion_for_option_unpack!($original, $entity, $reader);
};
}

macro_rules! impl_conversion_for_vector_pack {
($original:ty, $entity:ident) => {
impl Pack<packed::$entity> for [$original] {
fn pack(&self) -> packed::$entity {
packed::$entity::new_builder()
.set(self.iter().map(|v| v.pack()).collect())
.build()
}
}
};
}

macro_rules! impl_conversion_for_vector_unpack {
($original:ty, $entity:ident, $reader:ident) => {
impl<'r> Unpack<Vec<$original>> for packed::$reader<'r> {
fn unpack(&self) -> Vec<$original> {
self.iter().map(|x| x.unpack()).collect()
}
}
impl_conversion_for_entity_unpack!(Vec<$original>, $entity);
};
}

macro_rules! impl_conversion_for_vector {
($original:ty, $entity:ident, $reader:ident) => {
impl_conversion_for_vector_pack!($original, $entity);
impl_conversion_for_vector_unpack!($original, $entity, $reader);
};
}

macro_rules! impl_conversion_for_packed_optional_pack {
($original:ident, $entity:ident) => {
impl Pack<packed::$entity> for Option<packed::$original> {
fn pack(&self) -> packed::$entity {
if let Some(ref inner) = self {
packed::$entity::new_unchecked(inner.as_bytes())
} else {
packed::$entity::default()
}
}
}
};
}

macro_rules! impl_conversion_for_packed_iterator_pack {
($item:ident, $vec:ident) => {
impl<T> PackVec<packed::$vec, packed::$item> for T
where
T: IntoIterator<Item = packed::$item>,
{
fn pack(self) -> packed::$vec {
packed::$vec::new_builder().extend(self).build()
}
}
};
}
Loading
Loading