Skip to content

Commit

Permalink
refactor: move ChainState initialize into new
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed Apr 11, 2019
1 parent 644f7aa commit fd2ee8c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 103 deletions.
85 changes: 77 additions & 8 deletions shared/src/chain_state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::cell_set::CellSet;
use crate::cell_set::CellSetDiff;
use crate::index::ChainIndex;
use crate::tx_pool::{PoolEntry, PoolError, StagingTxResult, TxPool};
use crate::tx_pool::{PoolEntry, PoolError, StagingTxResult, TxPool, TxPoolConfig};
use crate::tx_proposal_table::TxProposalTable;
use ckb_chain_spec::consensus::Consensus;
use ckb_chain_spec::consensus::{Consensus, ProposalWindow};
use ckb_core::block::Block;
use ckb_core::cell::{
resolve_transaction, CellMeta, CellProvider, CellStatus, OverlayCellProvider,
Expand Down Expand Up @@ -39,14 +39,35 @@ impl<CI: ChainIndex> ChainState<CI> {
#[allow(clippy::too_many_arguments)]
pub fn new(
store: &Arc<CI>,
tip_header: Header,
total_difficulty: U256,
cell_set: CellSet,
proposal_ids: TxProposalTable,
tx_pool: TxPool,
txs_verify_cache: LruCache<H256, Cycle>,
consensus: Arc<Consensus>,
txs_verify_cache_size: usize,
tx_pool_config: TxPoolConfig,
) -> Self {
// check head in store or save the genesis block as head
let tip_header = {
let genesis = consensus.genesis_block();
match store.get_tip_header() {
Some(h) => h,
None => {
store.init(&genesis);
genesis.header().clone()
}
}
};

let tx_pool = TxPool::new(tx_pool_config);
let txs_verify_cache = LruCache::new(txs_verify_cache_size);

let tip_number = tip_header.number();
let proposal_window = consensus.tx_proposal_window();
let proposal_ids = Self::init_proposal_ids(&store, proposal_window, tip_number);

let cell_set = Self::init_cell_set(&store, tip_number);

let total_difficulty = store
.get_block_ext(&tip_header.hash())
.expect("block_ext stored")
.total_difficulty;
ChainState {
store: Arc::clone(store),
tip_header,
Expand All @@ -59,6 +80,54 @@ impl<CI: ChainIndex> ChainState<CI> {
}
}

fn init_proposal_ids(
store: &CI,
proposal_window: ProposalWindow,
tip_number: u64,
) -> TxProposalTable {
let mut proposal_ids = TxProposalTable::new(proposal_window);
let proposal_start = tip_number.saturating_sub(proposal_window.start());
let proposal_end = tip_number.saturating_sub(proposal_window.end());
for bn in proposal_start..=proposal_end {
if let Some(hash) = store.get_block_hash(bn) {
let mut ids_set = FnvHashSet::default();
if let Some(ids) = store.get_block_proposal_txs_ids(&hash) {
ids_set.extend(ids)
}

if let Some(us) = store.get_block_uncles(&hash) {
for u in us {
let ids = u.proposal_transactions;
ids_set.extend(ids);
}
}
proposal_ids.update_or_insert(bn, ids_set);
}
}
proposal_ids.finalize(tip_number);
proposal_ids
}

fn init_cell_set(store: &CI, number: u64) -> CellSet {
let mut cell_set = CellSet::new();

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 output_len = tx.outputs().len();

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

cell_set.insert(tx.hash(), n, tx.is_cellbase(), output_len);
}
}

cell_set
}

pub fn tip_number(&self) -> BlockNumber {
self.tip_header.number()
}
Expand Down
99 changes: 8 additions & 91 deletions shared/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use crate::chain_state::ChainState;
use crate::error::SharedError;
use crate::index::ChainIndex;
use crate::store::ChainKVStore;
use crate::tx_pool::{TxPool, TxPoolConfig};
use crate::tx_proposal_table::TxProposalTable;
use crate::tx_pool::TxPoolConfig;
use crate::{COLUMNS, COLUMN_BLOCK_HEADER};
use ckb_chain_spec::consensus::{Consensus, ProposalWindow};
use ckb_chain_spec::consensus::Consensus;
use ckb_core::block::Block;
use ckb_core::extras::BlockExt;
use ckb_core::header::{BlockNumber, Header};
Expand All @@ -16,8 +15,6 @@ use ckb_db::{CacheDB, DBConfig, KeyValueDB, MemoryKeyValueDB, RocksDB};
use ckb_traits::{BlockMedianTimeContext, ChainProvider};
use ckb_util::Mutex;
use failure::Error;
use fnv::FnvHashSet;
use lru_cache::LruCache;
use numext_fixed_hash::H256;
use numext_fixed_uint::U256;
use std::sync::Arc;
Expand All @@ -44,44 +41,12 @@ impl<CI: ChainIndex> Shared<CI> {
pub fn new(store: CI, consensus: Consensus, tx_pool_config: TxPoolConfig) -> Self {
let store = Arc::new(store);
let consensus = Arc::new(consensus);
let chain_state = {
// check head in store or save the genesis block as head
let header = {
let genesis = consensus.genesis_block();
match store.get_tip_header() {
Some(h) => h,
None => {
store
.init(&genesis)
.expect("init genesis block should be ok");
genesis.header().clone()
}
}
};

let tip_number = header.number();
let proposal_window = consensus.tx_proposal_window();
let proposal_ids = Self::init_proposal_ids(&store, proposal_window, tip_number);

let cell_set = Self::init_cell_set(&store, tip_number);

let total_difficulty = store
.get_block_ext(&header.hash())
.expect("block_ext stored")
.total_difficulty;

let txs_verify_cache_size = tx_pool_config.txs_verify_cache_size;
Arc::new(Mutex::new(ChainState::new(
&store,
header,
total_difficulty,
cell_set,
proposal_ids,
TxPool::new(tx_pool_config),
LruCache::new(txs_verify_cache_size),
Arc::clone(&consensus),
)))
};
let chain_state = Arc::new(Mutex::new(ChainState::new(
&store,
Arc::clone(&consensus),
txs_verify_cache_size,
tx_pool_config,
)));

Shared {
store,
Expand All @@ -97,54 +62,6 @@ impl<CI: ChainIndex> Shared<CI> {
pub fn store(&self) -> &Arc<CI> {
&self.store
}

pub fn init_proposal_ids(
store: &CI,
proposal_window: ProposalWindow,
tip_number: u64,
) -> TxProposalTable {
let mut proposal_ids = TxProposalTable::new(proposal_window);
let proposal_start = tip_number.saturating_sub(proposal_window.start());
let proposal_end = tip_number.saturating_sub(proposal_window.end());
for bn in proposal_start..=proposal_end {
if let Some(hash) = store.get_block_hash(bn) {
let mut ids_set = FnvHashSet::default();
if let Some(ids) = store.get_block_proposal_txs_ids(&hash) {
ids_set.extend(ids)
}

if let Some(us) = store.get_block_uncles(&hash) {
for u in us {
let ids = u.proposal_transactions;
ids_set.extend(ids);
}
}
proposal_ids.update_or_insert(bn, ids_set);
}
}
proposal_ids.finalize(tip_number);
proposal_ids
}

pub fn init_cell_set(store: &CI, number: u64) -> CellSet {
let mut cell_set = CellSet::new();

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 output_len = tx.outputs().len();

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

cell_set.insert(tx.hash(), n, tx.is_cellbase(), output_len);
}
}

cell_set
}
}

impl<CI: ChainIndex> ChainProvider for Shared<CI> {
Expand Down
6 changes: 3 additions & 3 deletions shared/src/tx_proposal_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ impl TxProposalTable {
.extend(ids);
}

pub fn get_ids_by_number(&self, number: BlockNumber) -> Option<&FnvHashSet<ProposalShortId>> {
self.table.get(&number)
}
//pub fn get_ids_by_number(&self, number: BlockNumber) -> Option<&FnvHashSet<ProposalShortId>> {
// self.table.get(&number)
//}

pub fn contains(&self, id: &ProposalShortId) -> bool {
self.set.contains(id)
Expand Down
2 changes: 1 addition & 1 deletion test/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl Node {

TransactionBuilder::default()
.output(CellOutput::new(50000, vec![], script.clone(), None))
.input(CellInput::new(OutPoint::new(hash, 0), vec![]))
.input(CellInput::new(OutPoint::new(hash, 0), 0, vec![]))
.build()
}

Expand Down
1 change: 1 addition & 0 deletions verification/src/transaction_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ where
self.inputs.verify()?;
self.capacity.verify()?;
self.duplicate_inputs.verify()?;
self.valid_since.verify()?;
let cycles = self.script.verify(max_cycles)?;
Ok(cycles)
}
Expand Down

0 comments on commit fd2ee8c

Please sign in to comment.