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 Jul 12, 2024
1 parent 2617762 commit de866c4
Show file tree
Hide file tree
Showing 171 changed files with 4,088 additions and 3,015 deletions.
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 @@ -208,7 +208,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
30 changes: 15 additions & 15 deletions chain/src/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn test_genesis_transaction_spend() {
.input(CellInput::new(OutPoint::null(), 0))
.outputs(vec![
CellOutputBuilder::default()
.capacity(capacity_bytes!(100_000_000).into())
.capacity(capacity_bytes!(100_000_000))
.build();
100
])
Expand All @@ -85,7 +85,7 @@ fn test_genesis_transaction_spend() {
let genesis_block = BlockBuilder::default()
.transaction(tx)
.transaction(always_success_tx)
.compact_target(difficulty_to_compact(U256::from(1000u64)).into())
.compact_target(difficulty_to_compact(U256::from(1000u64)))
.dao(dao)
.build();

Expand Down Expand Up @@ -378,7 +378,7 @@ fn test_genesis_transaction_fetch() {
.input(CellInput::new(OutPoint::null(), 0))
.outputs(vec![
CellOutputBuilder::default()
.capacity(capacity_bytes!(100_000_000).into())
.capacity(capacity_bytes!(100_000_000))
.lock(Script::default())
.build();
100
Expand All @@ -393,7 +393,7 @@ fn test_genesis_transaction_fetch() {
let genesis_block = BlockBuilder::default()
.dao(dao)
.transaction(tx)
.compact_target(difficulty_to_compact(U256::from(1000u64)).into())
.compact_target(difficulty_to_compact(U256::from(1000u64)))
.build();

let consensus = ConsensusBuilder::default()
Expand Down Expand Up @@ -525,10 +525,10 @@ fn prepare_context_chain(

let new_block = BlockBuilder::default()
.parent_hash(parent.hash())
.number((parent.number() + 1).into())
.epoch(epoch.number_with_fraction(parent.number() + 1).into())
.timestamp((parent.timestamp() + timestep).into())
.compact_target(epoch.compact_target().into())
.number(parent.number() + 1)
.epoch(epoch.number_with_fraction(parent.number() + 1))
.timestamp(parent.timestamp() + timestep)
.compact_target(epoch.compact_target())
.transactions(transactions)
.dao(dao)
.build();
Expand Down Expand Up @@ -565,10 +565,10 @@ fn prepare_context_chain(
let new_block = BlockBuilder::default()
.parent_hash(parent.hash())
.uncles(uncles)
.number((parent.number() + 1).into())
.epoch(epoch.number_with_fraction(parent.number() + 1).into())
.timestamp((parent.timestamp() + timestep).into())
.compact_target(epoch.compact_target().into())
.number(parent.number() + 1)
.epoch(epoch.number_with_fraction(parent.number() + 1))
.timestamp(parent.timestamp() + timestep)
.compact_target(epoch.compact_target())
.transactions(transactions)
.dao(dao)
.build();
Expand All @@ -591,7 +591,7 @@ fn test_epoch_hash_rate_dampening() {
.build();
let dao = genesis_dao_data(vec![&cellbase]).unwrap();
let genesis_block = BlockBuilder::default()
.compact_target(difficulty_to_compact(U256::from(1000u64)).into())
.compact_target(difficulty_to_compact(U256::from(1000u64)))
.transaction(cellbase)
.dao(dao)
.build();
Expand Down Expand Up @@ -684,7 +684,7 @@ fn test_orphan_rate_estimation_overflow() {
let dao = genesis_dao_data(vec![&cellbase]).unwrap();

let genesis_block = BlockBuilder::default()
.compact_target(difficulty_to_compact(U256::from(1000u64)).into())
.compact_target(difficulty_to_compact(U256::from(1000u64)))
.transaction(cellbase)
.dao(dao)
.build();
Expand Down Expand Up @@ -738,7 +738,7 @@ fn test_next_epoch_ext() {
.build();
let dao = genesis_dao_data(vec![&cellbase]).unwrap();
let genesis_block = BlockBuilder::default()
.compact_target(difficulty_to_compact(U256::from(1000u64)).into())
.compact_target(difficulty_to_compact(U256::from(1000u64)))
.transaction(cellbase)
.dao(dao)
.build();
Expand Down
Loading

0 comments on commit de866c4

Please sign in to comment.