Skip to content

Commit

Permalink
refactor: batch variable rename
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Apr 23, 2019
1 parent 9d5e2a4 commit 63fe08f
Show file tree
Hide file tree
Showing 58 changed files with 738 additions and 781 deletions.
20 changes: 10 additions & 10 deletions benches/benches/process_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn new_chain() -> (
.build();

// create genesis block with 100 tx
let commit_transactions: Vec<Transaction> = (0..100)
let transactions: Vec<Transaction> = (0..100)
.map(|i| {
TransactionBuilder::default()
.input(CellInput::new(OutPoint::null(), 0, vec![]))
Expand All @@ -160,8 +160,8 @@ fn new_chain() -> (
.collect();

let genesis_block = BlockBuilder::default()
.commit_transaction(cellbase)
.commit_transactions(commit_transactions)
.transaction(cellbase)
.transactions(transactions)
.with_header_builder(HeaderBuilder::default().difficulty(U256::from(1000u64)));

let consensus = Consensus::default().set_genesis_block(genesis_block);
Expand Down Expand Up @@ -199,10 +199,10 @@ fn gen_block(blocks: &mut Vec<Block>, parent_index: usize) {
.build();

// spent n-2 block's tx and proposal n-1 block's tx
let commit_transactions: Vec<Transaction> = if blocks.len() > parent_index + 1 {
let transactions: Vec<Transaction> = if blocks.len() > parent_index + 1 {
let pp_block = &blocks[parent_index - 1];
pp_block
.commit_transactions()
.transactions()
.iter()
.skip(1)
.map(|tx| create_transaction(tx.hash()))
Expand All @@ -211,17 +211,17 @@ fn gen_block(blocks: &mut Vec<Block>, parent_index: usize) {
vec![]
};

let proposal_transactions: Vec<ProposalShortId> = p_block
.commit_transactions()
let proposals: Vec<ProposalShortId> = p_block
.transactions()
.iter()
.skip(1)
.map(|tx| create_transaction(tx.hash()).proposal_short_id())
.collect();

let block = BlockBuilder::default()
.commit_transaction(cellbase)
.commit_transactions(commit_transactions)
.proposal_transactions(proposal_transactions)
.transaction(cellbase)
.transactions(transactions)
.proposals(proposals)
.with_header_builder(
HeaderBuilder::default()
.parent_hash(p_block.header().hash().clone())
Expand Down
2 changes: 1 addition & 1 deletion chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl<CS: ChainStore + 'static> ChainService<CS> {
let cell_provider = OverlayCellProvider::new(&block_cp, &cell_set_overlay);

let resolved: Vec<ResolvedTransaction> = b
.commit_transactions()
.transactions()
.iter()
.map(|x| cell_provider.resolve_transaction(x))
.collect();
Expand Down
10 changes: 5 additions & 5 deletions chain/src/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn test_genesis_transaction_spend() {
let genesis_tx_hash = root_hash.clone();

let genesis_block = BlockBuilder::default()
.commit_transaction(tx)
.transaction(tx)
.with_header_builder(HeaderBuilder::default().difficulty(U256::from(1000u64)));

let consensus = Consensus::default().set_genesis_block(genesis_block);
Expand Down Expand Up @@ -90,7 +90,7 @@ fn test_transaction_spend_in_same_block() {
chain.push(new_block);
}

let last_cell_base = &chain.last().unwrap().commit_transactions()[0];
let last_cell_base = &chain.last().unwrap().transactions()[0];
let last_cell_base_hash = last_cell_base.hash().clone();
let tx1 = create_transaction(last_cell_base_hash.clone(), 1);
let tx1_hash = tx1.hash().clone();
Expand Down Expand Up @@ -196,7 +196,7 @@ fn test_transaction_conflict_in_same_block() {
chain.push(new_block);
}

let last_cell_base = &chain.last().unwrap().commit_transactions()[0];
let last_cell_base = &chain.last().unwrap().transactions()[0];
let tx1 = create_transaction(last_cell_base.hash(), 1);
let tx2 = create_transaction(tx1.hash(), 2);
let tx3 = create_transaction(tx1.hash(), 3);
Expand Down Expand Up @@ -275,7 +275,7 @@ fn test_transaction_conflict_in_different_blocks() {
chain.push(new_block);
}

let last_cell_base = &chain.last().unwrap().commit_transactions()[0];
let last_cell_base = &chain.last().unwrap().transactions()[0];
let tx1 = create_transaction(last_cell_base.hash(), 1);
let tx2 = create_transaction(tx1.hash(), 2);
let tx3 = create_transaction(tx1.hash(), 3);
Expand Down Expand Up @@ -366,7 +366,7 @@ fn test_genesis_transaction_fetch() {
let root_hash = tx.hash().clone();

let genesis_block = BlockBuilder::default()
.commit_transaction(tx)
.transaction(tx)
.with_header_builder(HeaderBuilder::default().difficulty(U256::from(1000u64)));

let consensus = Consensus::default().set_genesis_block(genesis_block);
Expand Down
4 changes: 2 additions & 2 deletions chain/src/tests/delay_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn test_dead_cell_in_same_block() {
parent = new_block.header().clone();
}

let last_cell_base = &chain2.last().unwrap().commit_transactions()[0];
let last_cell_base = &chain2.last().unwrap().transactions()[0];
let tx1 = create_transaction(last_cell_base.hash(), 1);
let tx2 = create_transaction(tx1.hash(), 2);
let tx3 = create_transaction(tx1.hash(), 3);
Expand Down Expand Up @@ -140,7 +140,7 @@ fn test_dead_cell_in_different_block() {
parent = new_block.header().clone();
}

let last_cell_base = &chain2.last().unwrap().commit_transactions()[0];
let last_cell_base = &chain2.last().unwrap().transactions()[0];
let tx1 = create_transaction(last_cell_base.hash(), 1);
let tx2 = create_transaction(tx1.hash(), 2);
let tx3 = create_transaction(tx1.hash(), 3);
Expand Down
12 changes: 6 additions & 6 deletions chain/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ fn create_cellbase(number: BlockNumber) -> Transaction {
pub(crate) fn gen_block(
parent_header: &Header,
difficulty: U256,
commit_transactions: Vec<Transaction>,
proposal_transactions: Vec<Transaction>,
transactions: Vec<Transaction>,
proposals: Vec<Transaction>,
uncles: Vec<UncleBlock>,
) -> Block {
let number = parent_header.number() + 1;
Expand All @@ -61,11 +61,11 @@ pub(crate) fn gen_block(
.difficulty(difficulty);

BlockBuilder::default()
.commit_transaction(cellbase)
.commit_transactions(commit_transactions)
.transaction(cellbase)
.transactions(transactions)
.uncles(uncles)
.proposal_transactions(
proposal_transactions
.proposals(
proposals
.iter()
.map(Transaction::proposal_short_id)
.collect(),
Expand Down
58 changes: 29 additions & 29 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ use serde_derive::{Deserialize, Serialize};
pub struct Block {
header: Header,
uncles: Vec<UncleBlock>,
commit_transactions: Vec<Transaction>,
proposal_transactions: Vec<ProposalShortId>,
transactions: Vec<Transaction>,
proposals: Vec<ProposalShortId>,
}

impl Block {
pub fn new(
header: Header,
uncles: Vec<UncleBlock>,
commit_transactions: Vec<Transaction>,
proposal_transactions: Vec<ProposalShortId>,
transactions: Vec<Transaction>,
proposals: Vec<ProposalShortId>,
) -> Block {
Block {
header,
uncles,
commit_transactions,
proposal_transactions,
transactions,
proposals,
}
}

Expand All @@ -37,12 +37,12 @@ impl Block {
self.header.is_genesis()
}

pub fn commit_transactions(&self) -> &[Transaction] {
&self.commit_transactions
pub fn transactions(&self) -> &[Transaction] {
&self.transactions
}

pub fn proposal_transactions(&self) -> &[ProposalShortId] {
&self.proposal_transactions
pub fn proposals(&self) -> &[ProposalShortId] {
&self.proposals
}

pub fn uncles(&self) -> &[UncleBlock] {
Expand All @@ -56,10 +56,10 @@ impl Block {
pub fn union_proposal_ids(&self) -> FnvHashSet<ProposalShortId> {
let mut ids = FnvHashSet::default();

ids.extend(self.proposal_transactions());
ids.extend(self.proposals());

for uc in &self.uncles {
ids.extend(uc.proposal_transactions());
ids.extend(uc.proposals());
}

ids
Expand All @@ -69,28 +69,28 @@ impl Block {
// The witness hash of cellbase transaction is assumed to be zero 0x0000....0000
let mut witnesses = vec![H256::zero()];
witnesses.extend(
self.commit_transactions()
self.transactions()
.iter()
.skip(1)
.map(Transaction::witness_hash),
);
merkle_root(&witnesses[..])
}

pub fn cal_txs_commit_root(&self) -> H256 {
pub fn cal_transactions_root(&self) -> H256 {
merkle_root(
&self
.commit_transactions
.transactions
.iter()
.map(Transaction::hash)
.collect::<Vec<_>>(),
)
}

pub fn cal_txs_proposal_root(&self) -> H256 {
pub fn cal_proposals_root(&self) -> H256 {
merkle_root(
&self
.proposal_transactions
.proposals
.iter()
.map(ProposalShortId::hash)
.collect::<Vec<_>>(),
Expand Down Expand Up @@ -140,23 +140,23 @@ impl BlockBuilder {
self
}

pub fn commit_transaction(mut self, transaction: Transaction) -> Self {
self.inner.commit_transactions.push(transaction);
pub fn transaction(mut self, transaction: Transaction) -> Self {
self.inner.transactions.push(transaction);
self
}

pub fn commit_transactions(mut self, transactions: Vec<Transaction>) -> Self {
self.inner.commit_transactions.extend(transactions);
pub fn transactions(mut self, transactions: Vec<Transaction>) -> Self {
self.inner.transactions.extend(transactions);
self
}

pub fn proposal_transaction(mut self, proposal_short_id: ProposalShortId) -> Self {
self.inner.proposal_transactions.push(proposal_short_id);
pub fn proposal(mut self, proposal_short_id: ProposalShortId) -> Self {
self.inner.proposals.push(proposal_short_id);
self
}

pub fn proposal_transactions(mut self, proposal_short_ids: Vec<ProposalShortId>) -> Self {
self.inner.proposal_transactions.extend(proposal_short_ids);
pub fn proposals(mut self, proposal_short_ids: Vec<ProposalShortId>) -> Self {
self.inner.proposals.extend(proposal_short_ids);
self
}

Expand All @@ -165,14 +165,14 @@ impl BlockBuilder {
}

pub fn with_header_builder(mut self, header_builder: HeaderBuilder) -> Block {
let txs_commit = self.inner.cal_txs_commit_root();
let transactions_root = self.inner.cal_transactions_root();
let witnesses_root = self.inner.cal_witnesses_root();
let txs_proposal = self.inner.cal_txs_proposal_root();
let proposals_root = self.inner.cal_proposals_root();
let uncles_hash = self.inner.cal_uncles_hash();

self.inner.header = header_builder
.txs_commit(txs_commit)
.txs_proposal(txs_proposal)
.transactions_root(transactions_root)
.proposals_root(proposals_root)
.witnesses_root(witnesses_root)
.uncles_hash(uncles_hash)
.uncles_count(self.inner.uncles.len() as u32)
Expand Down
12 changes: 6 additions & 6 deletions core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl<'a> BlockCellProvider<'a> {
let mut duplicate_inputs_counter = FnvHashMap::default();

let output_indices = block
.commit_transactions()
.transactions()
.iter()
.enumerate()
.map(|(idx, tx)| {
Expand Down Expand Up @@ -191,8 +191,8 @@ impl<'a> CellProvider for BlockCellProvider<'a> {
.map(|counter| *counter > 0)
{
CellStatus::Dead
} else if let Some(i) = self.output_indices.get(&out_point.hash) {
match self.block.commit_transactions()[*i]
} else if let Some(i) = self.output_indices.get(&out_point.tx_hash) {
match self.block.transactions()[*i]
.outputs()
.get(out_point.index as usize)
{
Expand Down Expand Up @@ -319,15 +319,15 @@ mod tests {
};

let p1 = OutPoint {
hash: H256::zero(),
tx_hash: H256::zero(),
index: 1,
};
let p2 = OutPoint {
hash: H256::zero(),
tx_hash: H256::zero(),
index: 2,
};
let p3 = OutPoint {
hash: H256::zero(),
tx_hash: H256::zero(),
index: 3,
};
let o = CellMeta {
Expand Down
Loading

0 comments on commit 63fe08f

Please sign in to comment.