Skip to content

Commit

Permalink
feat: change builder funtion to generics
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Aug 14, 2024
1 parent fb844bf commit ecbd91f
Show file tree
Hide file tree
Showing 189 changed files with 4,407 additions and 3,306 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

10 changes: 5 additions & 5 deletions benches/benches/benchmarks/overall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ pub fn setup_chain(txs_size: usize) -> (Shared, ChainController) {
.map(|i| {
let data = Bytes::from(i.to_le_bytes().to_vec());
let output = CellOutput::new_builder()
.capacity(capacity_bytes!(50_000).into())
.capacity(capacity_bytes!(50_000))
.lock(secp_script.clone())
.build();
TransactionBuilder::default()
.input(CellInput::new(OutPoint::null(), 0))
.output(output.clone())
.output(output)
.output_data(data.clone().into())
.output_data(data.into())
.output_data(&data)
.output_data(&data)
.build()
})
.collect();

let genesis_block = BlockBuilder::default()
.compact_target(difficulty_to_compact(U256::from(1000u64)).into())
.compact_target(difficulty_to_compact(U256::from(1000u64)))
.dao(dao)
.transaction(tx)
.transactions(transactions)
Expand Down Expand Up @@ -207,7 +207,7 @@ fn bench(c: &mut Criterion) {
let raw_header = raw_block.header().raw();
let header = Header::new_builder()
.raw(raw_header)
.nonce(random::<u128>().into())
.nonce(random::<u128>())
.build();
let block = raw_block.as_builder().header(header).build().into_view();

Expand Down
13 changes: 5 additions & 8 deletions benches/benches/benchmarks/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,19 @@ const DEFAULT_CODE_HASH: H256 =

fn script() -> Script {
Script::new_builder()
.code_hash(DEFAULT_CODE_HASH.into())
.args(Bytes::from(PUBKEY_HASH.as_bytes()).into())
.hash_type(ScriptHashType::Type.into())
.code_hash(DEFAULT_CODE_HASH)
.args(Bytes::from(PUBKEY_HASH.as_bytes()))
.hash_type(ScriptHashType::Type)
.build()
}

fn cell_dep(genesis: &BlockView) -> CellDep {
let tx_hash = genesis.transaction(1).unwrap().hash();
let out_point = OutPoint::new_builder()
.tx_hash(tx_hash)
.index(0u32.into())
.build();
let out_point = OutPoint::new_builder().tx_hash(tx_hash).index(0u32).build();

CellDep::new_builder()
.out_point(out_point)
.dep_type(DepType::DepGroup.into())
.dep_type(DepType::DepGroup)
.build()
}

Expand Down
66 changes: 33 additions & 33 deletions benches/benches/benchmarks/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ pub fn new_always_success_chain(txs_size: usize, chains_num: usize) -> Chains {
.input(CellInput::new(OutPoint::null(), 0))
.output(
CellOutput::new_builder()
.capacity(capacity_bytes!(50_000).into())
.capacity(capacity_bytes!(50_000))
.lock(always_success_script.clone())
.build(),
)
.output_data(data.into())
.output_data(data)
.build()
})
.collect();

let genesis_block = BlockBuilder::default()
.dao(dao)
.compact_target(difficulty_to_compact(U256::from(1000u64)).into())
.compact_target(difficulty_to_compact(U256::from(1000u64)))
.transaction(tx)
.transactions(transactions)
.build();
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn create_always_success_tx() -> TransactionView {
.witness(script.clone().into_witness())
.input(CellInput::new(OutPoint::null(), 0))
.output(always_success_cell.clone())
.output_data(always_success_cell_data.into())
.output_data(always_success_cell_data)
.build()
}

Expand All @@ -110,11 +110,11 @@ pub fn create_always_success_cellbase(shared: &Shared, parent: &HeaderView) -> T
builder
.output(
CellOutput::new_builder()
.capacity(capacity.into())
.capacity(capacity)
.lock(always_success_script.clone())
.build(),
)
.output_data(Bytes::new().into())
.output_data(Bytes::new())
.build()
}
}
Expand Down Expand Up @@ -185,11 +185,11 @@ pub fn gen_always_success_block(
.transactions(transactions)
.proposals(proposals)
.parent_hash(p_block.hash())
.number(number.into())
.timestamp(timestamp.into())
.compact_target(epoch.compact_target().into())
.epoch(epoch.number_with_fraction(number).into())
.nonce(random::<u128>().into())
.number(number)
.timestamp(timestamp)
.compact_target(epoch.compact_target())
.epoch(epoch.number_with_fraction(number))
.nonce(random::<u128>())
.dao(dao)
.build();

Expand All @@ -208,7 +208,7 @@ lazy_static! {
let data: Bytes = raw_data.to_vec().into();

let cell = CellOutput::new_builder()
.capacity(Capacity::bytes(data.len()).unwrap().into())
.capacity(Capacity::bytes(data.len()).unwrap())
.build();
(cell, data)
};
Expand All @@ -219,13 +219,13 @@ lazy_static! {
let data: Bytes = raw_data.to_vec().into();

let cell = CellOutput::new_builder()
.capacity(Capacity::bytes(data.len()).unwrap().into())
.capacity(Capacity::bytes(data.len()).unwrap())
.build();

let script = Script::new_builder()
.code_hash(CellOutput::calc_data_hash(&data))
.args(Bytes::from(PUBKEY_HASH.as_bytes()).into())
.hash_type(ScriptHashType::Data.into())
.args(Bytes::from(PUBKEY_HASH.as_bytes()))
.hash_type(ScriptHashType::Data)
.build();

(cell, data, script)
Expand Down Expand Up @@ -263,21 +263,21 @@ pub fn new_secp_chain(txs_size: usize, chains_num: usize) -> Chains {
.map(|i| {
let data = Bytes::from(i.to_le_bytes().to_vec());
let output = CellOutput::new_builder()
.capacity(capacity_bytes!(50_000).into())
.capacity(capacity_bytes!(50_000))
.lock(secp_script.clone())
.build();
TransactionBuilder::default()
.input(CellInput::new(OutPoint::null(), 0))
.output(output.clone())
.output(output)
.output_data((&data).into())
.output_data(data.into())
.output_data(&data)
.output_data(&data)
.build()
})
.collect();

let genesis_block = BlockBuilder::default()
.compact_target(difficulty_to_compact(U256::from(1000u64)).into())
.compact_target(difficulty_to_compact(U256::from(1000u64)))
.dao(dao)
.transaction(tx)
.transactions(transactions)
Expand Down Expand Up @@ -318,11 +318,11 @@ pub fn create_secp_cellbase(shared: &Shared, parent: &HeaderView) -> Transaction
builder
.output(
CellOutput::new_builder()
.capacity(capacity.into())
.capacity(capacity)
.lock(secp_script.clone())
.build(),
)
.output_data(Bytes::new().into())
.output_data(Bytes::new())
.build()
}
}
Expand Down Expand Up @@ -395,11 +395,11 @@ pub fn gen_secp_block(
.transactions(transactions)
.proposals(proposals)
.parent_hash(p_block.hash())
.number(number.into())
.timestamp(timestamp.into())
.compact_target(epoch.compact_target().into())
.epoch(epoch.number_with_fraction(number).into())
.nonce(random::<u128>().into())
.number(number)
.timestamp(timestamp)
.compact_target(epoch.compact_target())
.epoch(epoch.number_with_fraction(number))
.nonce(random::<u128>())
.dao(dao)
.build();

Expand All @@ -412,11 +412,11 @@ fn create_transaction(parent_hash: &Byte32, lock: Script, dep: OutPoint) -> Tran
TransactionBuilder::default()
.output(
CellOutput::new_builder()
.capacity(capacity_bytes!(50_000).into())
.capacity(capacity_bytes!(50_000))
.lock(lock)
.build(),
)
.output_data(data.into())
.output_data(data)
.input(CellInput::new(OutPoint::new(parent_hash.to_owned(), 0), 0))
.cell_dep(CellDep::new_builder().out_point(dep).build())
.build()
Expand All @@ -431,7 +431,7 @@ pub fn create_2out_transaction(

let cell_inputs = inputs.into_iter().map(|pts| CellInput::new(pts, 0));
let cell_output = CellOutput::new_builder()
.capacity(capacity_bytes!(50_000).into())
.capacity(capacity_bytes!(50_000))
.lock(lock)
.build();

Expand All @@ -440,15 +440,15 @@ pub fn create_2out_transaction(
let raw = TransactionBuilder::default()
.output(cell_output.clone())
.output(cell_output)
.output_data((&data).into())
.output_data(data.into())
.output_data(&data)
.output_data(&data)
.inputs(cell_inputs)
.cell_deps(cell_deps)
.build();

let privkey: Privkey = PRIVKEY.into();
let witness: WitnessArgs = WitnessArgs::new_builder()
.lock(Some(Bytes::from(vec![0u8; 65])).into())
.lock(Some(Bytes::from(vec![0u8; 65])))
.build();
let witness_len: u64 = witness.as_bytes().len() as u64;
let non_sig_witnesses = vec![Bytes::new(); inputs_count - 1];
Expand All @@ -470,7 +470,7 @@ pub fn create_2out_transaction(
.expect("sign tx")
.serialize()
.into();
let witness = witness.as_builder().lock(Some(sig).into()).build();
let witness = witness.as_builder().lock(Some(sig)).build();

let mut witnesses = vec![witness.as_bytes().into()];
witnesses.extend(non_sig_witnesses.into_iter().map(|w| w.into()));
Expand Down
4 changes: 1 addition & 3 deletions chain/src/consume_unverified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ impl ConsumeUnverifiedBlockProcessor {
match *assume_valid_target {
Some(ref target) => {
// if the target has been reached, delete it
if target
== &ckb_types::prelude::Unpack::<H256>::unpack(&BlockView::hash(block))
{
if target == &Into::<H256>::into(&BlockView::hash(block)) {
assume_valid_target.take();
Switch::NONE
} else {
Expand Down
4 changes: 2 additions & 2 deletions chain/src/init_load_unverified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ckb_stop_handler::has_received_stop_signal;
use ckb_store::ChainStore;
use ckb_types::core::{BlockNumber, BlockView};
use ckb_types::packed;
use ckb_types::prelude::{Entity, FromSliceShouldBeOk, Pack, Reader};
use ckb_types::prelude::{Entity, FromSliceShouldBeOk, Reader};
use std::cmp;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
Expand All @@ -34,7 +34,7 @@ impl InitLoadUnverified {
}

fn find_unverified_block_hashes(&self, check_unverified_number: u64) -> Vec<packed::Byte32> {
let pack_number: packed::Uint64 = check_unverified_number.pack();
let pack_number: packed::Uint64 = check_unverified_number.into();
let prefix = pack_number.as_slice();

// If a block has `COLUMN_NUMBER_HASH` but not `BlockExt`,
Expand Down
9 changes: 4 additions & 5 deletions chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ mod utils;
pub use chain_controller::ChainController;
use ckb_logger::{error, info};
use ckb_store::{ChainDB, ChainStore};
use ckb_types::prelude::{Pack, Unpack};
use ckb_types::{BlockNumberAndHash, H256};
pub use init::start_chain_services;

Expand Down Expand Up @@ -89,11 +88,11 @@ impl From<LonelyBlock> for LonelyBlockHash {
switch,
verify_callback,
} = val;
let block_hash_h256: H256 = block.hash().unpack();
let block_hash_h256: H256 = block.hash().into();
let block_number: BlockNumber = block.number();
let parent_hash_h256: H256 = block.parent_hash().unpack();
let block_hash = block_hash_h256.pack();
let parent_hash = parent_hash_h256.pack();
let parent_hash_h256: H256 = block.parent_hash().into();
let block_hash = block_hash_h256.into();
let parent_hash = parent_hash_h256.into();

let epoch_number: EpochNumber = block.epoch().number();

Expand Down
Loading

0 comments on commit ecbd91f

Please sign in to comment.