Skip to content

Commit

Permalink
Merge pull request #706 from zhangsoledad/zhangsoledad/ref_api
Browse files Browse the repository at this point in the history
refactor: improve core type fmt debug
  • Loading branch information
quake committed May 11, 2019
2 parents bf11bc8 + 0bedcde commit d11dbcc
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 94 deletions.
23 changes: 13 additions & 10 deletions chain/src/chain.rs
Expand Up @@ -302,22 +302,21 @@ impl<CS: ChainStore + 'static> ChainService<CS> {
}
new_best_block = true;

total_difficulty = cannon_total_difficulty;
total_difficulty = cannon_total_difficulty.clone();
} else {
batch.insert_block_ext(&block.header().hash(), &ext)?;
}
batch.commit()?;

let tip_header = block.header();
let tip_number = tip_header.number();
let tip_hash = tip_header.hash();
let txs_cnt = block.transactions().len();

if new_best_block {
info!(
target: "chain",
"block: {}, hash: {:#x}, diff: {:#x}, txs: {}",
tip_number, tip_hash, total_difficulty, txs_cnt);
"block: {}, hash: {:#x}, total_diff: {:#x}, txs: {}",
block.header().number(),
block.header().hash(),
total_difficulty,
block.transactions().len()
);
let tip_header = block.header().to_owned();
// finalize proposal_id table change
// then, update tx_pool
Expand All @@ -339,8 +338,12 @@ impl<CS: ChainStore + 'static> ChainService<CS> {
} else {
info!(
target: "chain",
"uncle: {}, hash: {:#x}, diff: {:#x}, txs: {}",
tip_number, tip_hash, total_difficulty, txs_cnt);
"uncle: {}, hash: {:#x}, total_diff: {:#x}, txs: {}",
block.header().number(),
block.header().hash(),
cannon_total_difficulty,
block.transactions().len()
);
self.notify.notify_new_uncle(block);
}

Expand Down
55 changes: 38 additions & 17 deletions core/src/cell.rs
Expand Up @@ -2,11 +2,13 @@ use crate::block::Block;
use crate::header::Header;
use crate::transaction::{CellOutPoint, CellOutput, OutPoint, Transaction};
use crate::Capacity;
use ckb_util::LowerHexOption;
use fnv::{FnvHashMap, FnvHashSet};
use numext_fixed_hash::H256;
use serde_derive::{Deserialize, Serialize};
use std::fmt;

#[derive(Clone, Eq, PartialEq, Debug, Default, Deserialize, Serialize)]
#[derive(Clone, Eq, PartialEq, Default, Deserialize, Serialize)]
pub struct CellMeta {
#[serde(skip)]
pub cell_output: Option<CellOutput>,
Expand All @@ -27,6 +29,22 @@ impl From<&CellOutput> for CellMeta {
}
}

impl fmt::Debug for CellMeta {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("CellMeta")
.field("cell_output", &self.cell_output)
.field("out_point", &self.out_point)
.field("block_number", &self.block_number)
.field("cellbase", &self.cellbase)
.field("capacity", &self.capacity)
.field(
"data_hash",
&format_args!("{:#x}", LowerHexOption(self.data_hash.as_ref())),
)
.finish()
}
}

impl CellMeta {
pub fn is_cellbase(&self) -> bool {
self.cellbase
Expand Down Expand Up @@ -353,36 +371,39 @@ pub fn resolve_transaction<'a, CP: CellProvider, HP: HeaderProvider>(

// skip resolve input of cellbase
if !transaction.is_cellbase() {
for out_point in transaction.input_pts() {
let (cell_status, header_status) = if seen_inputs.insert(out_point.clone()) {
for out_point in transaction.input_pts_iter() {
let (cell_status, header_status) = if seen_inputs.insert(out_point.to_owned()) {
(
cell_provider.cell(&out_point),
header_provider.header(&out_point),
cell_provider.cell(out_point),
header_provider.header(out_point),
)
} else {
(CellStatus::Dead, HeaderStatus::Unknown)
};

match (cell_status, header_status) {
(CellStatus::Dead, _) => {
return Err(UnresolvableError::Dead(out_point.clone()));
return Err(UnresolvableError::Dead(out_point.to_owned()));
}
(CellStatus::Unknown, _) => {
unknown_out_points.push(out_point.clone());
unknown_out_points.push(out_point.to_owned());
}
// Input cell must exist
(CellStatus::Unspecified, _) => {
return Err(UnresolvableError::UnspecifiedInputCell(out_point.clone()));
return Err(UnresolvableError::UnspecifiedInputCell(
out_point.to_owned(),
));
}
(_, HeaderStatus::Unknown) => {
// TODO: should we change transaction pool so transactions
// with unknown header can be included as orphans, waiting
// for the correct block header to enable it?
return Err(UnresolvableError::InvalidHeader(out_point.clone()));
return Err(UnresolvableError::InvalidHeader(out_point.to_owned()));
}
(_, HeaderStatus::InclusionFaliure) => {
return Err(UnresolvableError::InvalidHeader(out_point.clone()));
return Err(UnresolvableError::InvalidHeader(out_point.to_owned()));
}

(CellStatus::Live(cell_meta), HeaderStatus::Live(header)) => {
resolved_inputs.push(ResolvedOutPoint::cell_and_header(*cell_meta, *header));
}
Expand All @@ -393,25 +414,25 @@ pub fn resolve_transaction<'a, CP: CellProvider, HP: HeaderProvider>(
}
}

for out_point in transaction.dep_pts() {
let cell_status = cell_provider.cell(&out_point);
let header_status = header_provider.header(&out_point);
for out_point in transaction.deps_iter() {
let cell_status = cell_provider.cell(out_point);
let header_status = header_provider.header(out_point);

match (cell_status, header_status) {
(CellStatus::Dead, _) => {
return Err(UnresolvableError::Dead(out_point.clone()));
return Err(UnresolvableError::Dead(out_point.to_owned()));
}
(CellStatus::Unknown, _) => {
unknown_out_points.push(out_point.clone());
unknown_out_points.push(out_point.to_owned());
}
(_, HeaderStatus::Unknown) => {
// TODO: should we change transaction pool so transactions
// with unknown header can be included as orphans, waiting
// for the correct block header to enable it?
return Err(UnresolvableError::InvalidHeader(out_point.clone()));
return Err(UnresolvableError::InvalidHeader(out_point.to_owned()));
}
(_, HeaderStatus::InclusionFaliure) => {
return Err(UnresolvableError::InvalidHeader(out_point.clone()));
return Err(UnresolvableError::InvalidHeader(out_point.to_owned()));
}
(CellStatus::Live(cell_meta), HeaderStatus::Live(header)) => {
resolved_deps.push(ResolvedOutPoint::cell_and_header(*cell_meta, *header));
Expand Down
25 changes: 9 additions & 16 deletions core/src/transaction.rs
Expand Up @@ -5,6 +5,7 @@ pub use crate::Capacity;
use crate::{BlockNumber, Version};
use bincode::{deserialize, serialize};
use bytes::Bytes;
use ckb_util::LowerHexOption;
use faster_hex::hex_string;
use hash::blake2b_256;
use numext_fixed_hash::H256;
Expand Down Expand Up @@ -64,7 +65,10 @@ impl fmt::Debug for OutPoint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("OutPoint")
.field("cell", &self.cell)
.field("block_hash", &self.block_hash)
.field(
"block_hash",
&format_args!("{:#x}", LowerHexOption(self.block_hash.as_ref())),
)
.finish()
}
}
Expand Down Expand Up @@ -492,30 +496,19 @@ impl Transaction {
&self.witness_hash
}

pub fn out_points_iter(&self) -> impl Iterator<Item = &OutPoint> {
self.deps.iter().chain(
self.inputs
.iter()
.map(|input: &CellInput| &input.previous_output),
)
}

pub fn output_pts(&self) -> Vec<OutPoint> {
let h = self.hash();
(0..self.outputs.len())
.map(|x| OutPoint::new_cell(h.clone(), x as u32))
.collect()
}

pub fn input_pts(&self) -> Vec<OutPoint> {
self.inputs
.iter()
.map(|x| x.previous_output.clone())
.collect()
pub fn input_pts_iter(&self) -> impl Iterator<Item = &OutPoint> {
self.inputs.iter().map(|x| &x.previous_output)
}

pub fn dep_pts(&self) -> Vec<OutPoint> {
self.deps.clone()
pub fn deps_iter(&self) -> impl Iterator<Item = &OutPoint> {
self.deps.iter()
}

pub fn is_empty(&self) -> bool {
Expand Down
8 changes: 4 additions & 4 deletions shared/src/cell_set.rs
Expand Up @@ -16,10 +16,10 @@ pub struct CellSetDiff {
impl CellSetDiff {
pub fn push_new(&mut self, block: &Block) {
for tx in block.transactions() {
let input_pts = tx.input_pts();
let input_iter = tx.input_pts_iter();
let tx_hash = tx.hash();
let output_len = tx.outputs().len();
self.new_inputs.extend(input_pts);
self.new_inputs.extend(input_iter.cloned());
self.new_outputs.insert(
tx_hash.to_owned(),
(block.header().number(), tx.is_cellbase(), output_len),
Expand All @@ -29,10 +29,10 @@ impl CellSetDiff {

pub fn push_old(&mut self, block: &Block) {
for tx in block.transactions() {
let input_pts = tx.input_pts();
let input_iter = tx.input_pts_iter();
let tx_hash = tx.hash();

self.old_inputs.extend(input_pts);
self.old_inputs.extend(input_iter.cloned());
self.old_outputs.insert(tx_hash.to_owned());
}
}
Expand Down
4 changes: 2 additions & 2 deletions shared/src/chain_state.rs
Expand Up @@ -146,11 +146,11 @@ impl<CS: ChainStore> ChainState<CS> {
for n in 0..=number {
let hash = store.get_block_hash(n).unwrap();
for tx in store.get_block_body(&hash).unwrap() {
let inputs = tx.input_pts();
let inputs = tx.input_pts_iter();
let output_len = tx.outputs().len();

for o in inputs {
cell_set.mark_dead(&o);
cell_set.mark_dead(o);
}

cell_set.insert(tx.hash().to_owned(), n, tx.is_cellbase(), output_len);
Expand Down
4 changes: 2 additions & 2 deletions shared/src/tx_pool/orphan.rs
Expand Up @@ -102,10 +102,10 @@ impl OrphanPool {
}

pub(crate) fn remove_conflict(&mut self, tx: &Transaction) {
let inputs = tx.input_pts();
let inputs = tx.input_pts_iter();

for input in inputs {
if let Some(ids) = self.edges.remove(&input) {
if let Some(ids) = self.edges.remove(input) {
for cid in ids {
self.recursion_remove(&cid);
}
Expand Down

0 comments on commit d11dbcc

Please sign in to comment.