Skip to content

Commit

Permalink
merge master into 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume committed Apr 16, 2019
2 parents 3d817f6 + 56fed50 commit 12fe928
Show file tree
Hide file tree
Showing 37 changed files with 2,127 additions and 1,135 deletions.
347 changes: 171 additions & 176 deletions Cargo.lock

Large diffs are not rendered by default.

84 changes: 47 additions & 37 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,44 +168,34 @@ impl Chain {
archive_mode: bool,
stop_state: Arc<Mutex<StopState>>,
) -> Result<Chain, Error> {
let chain = {
// Note: We take a lock on the stop_state here and do not release it until
// we have finished chain initialization.
let stop_state_local = stop_state.clone();
let stop_lock = stop_state_local.lock();
if stop_lock.is_stopped() {
return Err(ErrorKind::Stopped.into());
}

let store = Arc::new(store::ChainStore::new(&db_root)?);

// open the txhashset, creating a new one if necessary
let mut txhashset = txhashset::TxHashSet::open(db_root.clone(), store.clone(), None)?;

setup_head(&genesis, &store, &mut txhashset)?;
Chain::log_heads(&store)?;

Chain {
db_root,
store,
adapter,
orphans: Arc::new(OrphanBlockPool::new()),
txhashset: Arc::new(RwLock::new(txhashset)),
pow_verifier,
verifier_cache,
archive_mode,
stop_state,
genesis: genesis.header.clone(),
}
};

// Run chain compaction. Laptops and other intermittent nodes
// may not run long enough to trigger daily compaction.
// So run it explicitly here on startup (its fast enough to do so).
// Note: we release the stop_lock from above as compact also requires a lock.
chain.compact()?;
// Note: We take a lock on the stop_state here and do not release it until
// we have finished chain initialization.
let stop_state_local = stop_state.clone();
let stop_lock = stop_state_local.lock();
if stop_lock.is_stopped() {
return Err(ErrorKind::Stopped.into());
}

Ok(chain)
let store = Arc::new(store::ChainStore::new(&db_root)?);

// open the txhashset, creating a new one if necessary
let mut txhashset = txhashset::TxHashSet::open(db_root.clone(), store.clone(), None)?;

setup_head(&genesis, &store, &mut txhashset)?;
Chain::log_heads(&store)?;

Ok(Chain {
db_root,
store,
adapter,
orphans: Arc::new(OrphanBlockPool::new()),
txhashset: Arc::new(RwLock::new(txhashset)),
pow_verifier,
verifier_cache,
archive_mode,
stop_state,
genesis: genesis.header.clone(),
})
}

/// Return our shared txhashset instance.
Expand Down Expand Up @@ -1055,6 +1045,26 @@ impl Chain {
/// * removes historical blocks and associated data from the db (unless archive mode)
///
pub fn compact(&self) -> Result<(), Error> {
// A node may be restarted multiple times in a short period of time.
// We compact at most once per 60 blocks in this situation by comparing
// current "head" and "tail" height to our cut-through horizon and
// allowing an additional 60 blocks in height before allowing a further compaction.
if let (Ok(tail), Ok(head)) = (self.tail(), self.head()) {
let horizon = global::cut_through_horizon() as u64;
let threshold = horizon.saturating_add(60);
debug!(
"compact: head: {}, tail: {}, diff: {}, horizon: {}",
head.height,
tail.height,
head.height.saturating_sub(tail.height),
horizon
);
if tail.height.saturating_add(threshold) > head.height {
debug!("compact: skipping compaction - threshold is 60 blocks beyond horizon.");
return Ok(());
}
}

// Note: We take a lock on the stop_state here and do not release it until
// we have finished processing this chain compaction operation.
// We want to avoid shutting the node down in the middle of compacting the data.
Expand Down
2 changes: 2 additions & 0 deletions chain/tests/data_file_integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ fn data_files() {
let chain = reload_chain(chain_dir);
chain.validate(false).unwrap();
}
// Cleanup chain directory
clean_output_dir(chain_dir);
}

fn _prepare_block(kc: &ExtKeychain, prev: &BlockHeader, chain: &Chain, diff: u64) -> Block {
Expand Down
Loading

0 comments on commit 12fe928

Please sign in to comment.