Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
refactor block_headers_from_best_block, better cli documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege committed Jan 2, 2019
1 parent fd05801 commit 248f4cb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
23 changes: 11 additions & 12 deletions ethcore/src/blockchain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,18 +667,17 @@ impl BlockChain {

/// fetches the list of blocks from best block to n, and n's parent hash
/// where n > 0
pub fn block_headers_from_best_block(&self, n: u64) -> (Vec<encoded::Header>, H256) {
(0..n)
.fold(
(vec![], self.best_block_hash()),
|(mut blocks, hash), _| {
let current_hash = self.block_header_data(&hash)
.expect("hash was gotten from previous block's parent_hash; qed");
let parent_hash = current_hash.parent_hash();
blocks.push(current_hash);
(blocks, parent_hash)
}
)
pub fn block_headers_from_best_block(&self, n: u64) -> Option<(Vec<encoded::Header>, H256)> {
let mut blocks = vec![];
let mut hash = self.best_block_hash();

for _ in 0..n {
let current_hash = self.block_header_data(&hash)?;
hash = current_hash.parent_hash();
blocks.push(current_hash);
}

Some((blocks, hash))
}

/// Returns a tree route between `from` and `to`, which is a tuple of:
Expand Down
18 changes: 11 additions & 7 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use client::{
RegistryInfo, ReopenBlock, PrepareOpenBlock, ScheduleInfo, ImportSealedBlock,
BroadcastProposalBlock, ImportBlock, StateOrBlock, StateInfo, StateClient, Call,
AccountData, BlockChain as BlockChainTrait, BlockProducer, SealedBlockImporter,
ClientIoMessage,
ClientIoMessage, BlockChainReset
};
use client::{
BlockId, TransactionId, UncleId, TraceId, ClientConfig, BlockChainClient,
Expand Down Expand Up @@ -87,7 +87,6 @@ pub use types::blockchain_info::BlockChainInfo;
pub use types::block_status::BlockStatus;
pub use blockchain::CacheSize as BlockChainCacheSize;
pub use verification::QueueInfo as BlockQueueInfo;
use client::BlockChainReset;
use db::Writable;

use_contract!(registry, "res/contracts/registrar.json");
Expand Down Expand Up @@ -475,7 +474,7 @@ impl Importer {
// it is for reconstructing the state transition.
//
// The header passed is from the original block data and is sealed.
// TODO: should return an error if ImportRoute is none
// TODO: should return an error if ImportRoute is none, issue #9910
fn commit_block<B>(&self, block: B, header: &Header, block_data: encoded::Block, client: &Client) -> ImportRoute where B: Drain {
let hash = &header.hash();
let number = header.number();
Expand Down Expand Up @@ -1342,7 +1341,8 @@ impl BlockChainReset for Client {
}

let (blocks_to_delete, best_block_hash) = self.chain.read()
.block_headers_from_best_block(num);
.block_headers_from_best_block(num)
.ok_or("Attempted to reset past genesis block")?;

let mut db_transaction = DBTransaction::with_capacity(num as usize);

Expand All @@ -1362,9 +1362,13 @@ impl BlockChainReset for Client {
.write(db_transaction)
.map_err(|err| format!("could not complete reset operation; io error occured: {}", err))?;

info!("Deleting block hashes {}", Colour::Red.bold().paint(format!("{:#?}", blocks_to_delete.iter().map(|b| b
.hash()).collect::<Vec<_>>()
)));
let hashes = blocks_to_delete.iter().map(|b| b.hash()).collect::<Vec<_>>();

info!("Deleting block hashes {}",
Colour::Red
.bold()
.paint(format!("{:#?}", hashes))
);

info!("New best block hash {}", Colour::Green.bold().paint(format!("{:?}", best_block_hash)));

Expand Down
4 changes: 2 additions & 2 deletions parity/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ usage! {
}

CMD cmd_db_reset {
"Resets the blocks in the blockchain db by the number given",
"Removes NUM latests blocks from the db",",
ARG arg_db_reset_num: (Option<u64>) = None,
"--num=[NUM]",
"<NUM>",
"Number of blocks to revert",
}
Expand Down

0 comments on commit 248f4cb

Please sign in to comment.