Skip to content

Commit

Permalink
Merge pull request #4135 from EthanYuan/v0.111.x
Browse files Browse the repository at this point in the history
backport #4073 #4122
  • Loading branch information
doitian authored Sep 7, 2023
2 parents cba96ac + 2b6f8da commit 9fb20c1
Show file tree
Hide file tree
Showing 56 changed files with 1,218 additions and 667 deletions.
18 changes: 16 additions & 2 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.111.0-rc11"
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.111.0-rc11"}

[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-hash/default", "ckb-fixed-hash", "ckb-error", "ckb-occupied-capacity", "numext-fixed-uint"]

[dependencies]
cfg-if = "1.0"
molecule = { version = "0.7.5", default-features = false }
ckb-hash = { path = "../hash", version = "= 0.111.0-rc11", default-features = false, optional = true }
ckb-fixed-hash = { path = "../fixed-hash", version = "= 0.111.0-rc11", optional = true }
ckb-error = { path = "../../error", version = "= 0.111.0-rc11", optional = true }
ckb-occupied-capacity = { path = "../occupied-capacity", version = "= 0.111.0-rc11", optional = true }
numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"], optional = true }
File renamed without changes.
File renamed without changes.
File renamed without changes.
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;
File renamed without changes.
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

0 comments on commit 9fb20c1

Please sign in to comment.