Skip to content

Commit

Permalink
refactor: improve core type fmt debug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed May 11, 2019
1 parent 7fc14fb commit 4407a88
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 84 deletions.
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
84 changes: 42 additions & 42 deletions shared/src/tx_pool/staging.rs
Expand Up @@ -5,6 +5,7 @@ use ckb_core::cell::{CellMeta, CellProvider, CellStatus};
use ckb_core::transaction::{CellOutput, OutPoint, ProposalShortId, Transaction};
use ckb_core::Cycle;
use ckb_util::{FnvHashMap, FnvHashSet, LinkedFnvHashMap};
use std::collections::VecDeque;
use std::hash::Hash;

#[derive(Default, Debug, Clone)]
Expand Down Expand Up @@ -152,43 +153,48 @@ impl StagingPool {
})
}

pub fn remove_vertex(&mut self, id: &ProposalShortId, rtxs: &mut Vec<PoolEntry>) {
if let Some(x) = self.vertices.remove(id) {
let tx = &x.transaction;
let inputs = tx.input_pts();
let outputs = tx.output_pts();
let deps = tx.dep_pts();
pub fn remove_vertex(&mut self, id: &ProposalShortId) -> Vec<PoolEntry> {
let mut entries = Vec::new();
let mut queue = VecDeque::new();

rtxs.push(x);
queue.push_back(id.to_owned());

for i in inputs {
if self.edges.inner.remove(&i).is_none() {
self.edges.outer.remove(&i);
while let Some(id) = queue.pop_front() {
if let Some(entry) = self.vertices.remove(&id) {
let tx = &entry.transaction;
let inputs = tx.input_pts_iter();
let outputs = tx.output_pts();
let deps = tx.deps_iter();
for i in inputs {
if self.edges.inner.remove(i).is_none() {
self.edges.outer.remove(i);
}
}
}

for d in deps {
self.edges.delete_value_in_deps(&d, id);
}

for o in outputs {
if let Some(cid) = self.edges.remove_inner(&o) {
self.remove_vertex(&cid, rtxs);
for d in deps {
self.edges.delete_value_in_deps(d, &id);
}

if let Some(ids) = self.edges.remove_deps(&o) {
for cid in ids {
self.remove_vertex(&cid, rtxs);
for o in outputs {
if let Some(cid) = self.edges.remove_inner(&o) {
queue.push_back(cid);
}

if let Some(ids) = self.edges.remove_deps(&o) {
for cid in ids {
queue.push_back(cid);
}
}
}

entries.push(entry);
}
}
entries
}

pub fn remove(&mut self, id: &ProposalShortId) -> Option<Vec<PoolEntry>> {
let mut rtxs = Vec::new();

self.remove_vertex(id, &mut rtxs);
let rtxs = self.remove_vertex(id);

if rtxs.is_empty() {
None
Expand All @@ -198,32 +204,32 @@ impl StagingPool {
}

pub fn add_tx(&mut self, cycles: Cycle, tx: Transaction) {
let inputs = tx.input_pts();
let inputs = tx.input_pts_iter();
let outputs = tx.output_pts();
let deps = tx.dep_pts();
let deps = tx.deps_iter();

let id = tx.proposal_short_id();

let mut count: usize = 0;

for i in inputs {
let mut flag = true;
if let Some(x) = self.edges.get_inner_mut(&i) {
if let Some(x) = self.edges.get_inner_mut(i) {
*x = Some(id);
count += 1;
flag = false;
}

if flag {
self.edges.insert_outer(i, id);
self.edges.insert_outer(i.to_owned(), id);
}
}

for d in deps {
if self.edges.contains_key(&d) {
if self.edges.contains_key(d) {
count += 1;
}
self.edges.insert_deps(d, id);
self.edges.insert_deps(d.to_owned(), id);
}

for o in outputs {
Expand All @@ -236,8 +242,8 @@ impl StagingPool {

pub fn remove_committed_tx(&mut self, tx: &Transaction) {
let outputs = tx.output_pts();
let inputs = tx.input_pts();
let deps = tx.dep_pts();
let inputs = tx.input_pts_iter();
let deps = tx.deps_iter();
let id = tx.proposal_short_id();

if self.vertices.remove(&id).is_some() {
Expand All @@ -255,22 +261,22 @@ impl StagingPool {
}

for i in inputs {
self.edges.remove_outer(&i);
self.edges.remove_outer(i);
}

for d in deps {
self.edges.delete_value_in_deps(&d, &id)
self.edges.delete_value_in_deps(d, &id)
}
} else {
self.resolve_conflict(tx);
}
}

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

for i in inputs {
if let Some(id) = self.edges.remove_outer(&i) {
if let Some(id) = self.edges.remove_outer(i) {
self.remove(&id);
}

Expand All @@ -295,12 +301,6 @@ impl StagingPool {
self.vertices.values()
}

// pub fn inc_ref(&mut self, id: &ProposalShortId) {
// if let Some(x) = self.vertices.get_mut(&id) {
// x.refs_count += 1;
// }
// }

pub fn dec_ref(&mut self, id: &ProposalShortId) {
if let Some(x) = self.vertices.get_mut(&id) {
x.refs_count -= 1;
Expand Down

0 comments on commit 4407a88

Please sign in to comment.