This repository was archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Reset blockchain properly #10669
Merged
Merged
Reset blockchain properly #10669
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ use blockchain::{BlockReceipts, BlockChain, BlockChainDB, BlockProvider, TreeRou | |
| use bytes::Bytes; | ||
| use call_contract::{CallContract, RegistryInfo}; | ||
| use ethcore_miner::pool::VerifiedTransaction; | ||
| use ethereum_types::{H256, Address, U256}; | ||
| use ethereum_types::{H256, H264, Address, U256}; | ||
| use evm::Schedule; | ||
| use hash::keccak; | ||
| use io::IoChannel; | ||
|
|
@@ -86,7 +86,7 @@ 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 db::Writable; | ||
| use db::{Writable, Readable, keys::BlockDetails}; | ||
|
|
||
| use_contract!(registry, "res/contracts/registrar.json"); | ||
|
|
||
|
|
@@ -1327,37 +1327,60 @@ impl BlockChainReset for Client { | |
| fn reset(&self, num: u32) -> Result<(), String> { | ||
| if num as u64 > self.pruning_history() { | ||
| return Err("Attempting to reset to block with pruned state".into()) | ||
| } else if num == 0 { | ||
| return Err("invalid number of blocks to reset".into()) | ||
| } | ||
|
|
||
| let (blocks_to_delete, best_block_hash) = self.chain.read() | ||
| .block_headers_from_best_block(num) | ||
| .ok_or("Attempted to reset past genesis block")?; | ||
| let mut blocks_to_delete = Vec::with_capacity(num as usize); | ||
| let mut best_block_hash = self.chain.read().best_block_hash(); | ||
| let mut batch = DBTransaction::with_capacity(blocks_to_delete.len()); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. damn, this should've been |
||
|
|
||
| let mut db_transaction = DBTransaction::with_capacity((num + 1) as usize); | ||
| for _ in 0..num { | ||
| let current_header = self.chain.read().block_header_data(&best_block_hash) | ||
| .expect("best_block_hash was fetched from db; block_header_data should exist in db; qed"); | ||
| best_block_hash = current_header.parent_hash(); | ||
|
|
||
| for hash in &blocks_to_delete { | ||
| db_transaction.delete(::db::COL_HEADERS, &hash.hash()); | ||
| db_transaction.delete(::db::COL_BODIES, &hash.hash()); | ||
| db_transaction.delete(::db::COL_EXTRA, &hash.hash()); | ||
| let (number, hash) = (current_header.number(), current_header.hash()); | ||
| batch.delete(::db::COL_HEADERS, &hash); | ||
| batch.delete(::db::COL_BODIES, &hash); | ||
| Writable::delete::<BlockDetails, H264> | ||
| (&mut batch, ::db::COL_EXTRA, &hash); | ||
| Writable::delete::<H256, BlockNumberKey> | ||
| (&mut db_transaction, ::db::COL_EXTRA, &hash.number()); | ||
| (&mut batch, ::db::COL_EXTRA, &number); | ||
|
|
||
| blocks_to_delete.push((number, hash)); | ||
| } | ||
|
|
||
| let hashes = blocks_to_delete.iter().map(|(_, hash)| hash).collect::<Vec<_>>(); | ||
| info!("Deleting block hashes {}", | ||
| Colour::Red | ||
| .bold() | ||
| .paint(format!("{:#?}", hashes)) | ||
| ); | ||
|
|
||
| let mut best_block_details = Readable::read::<BlockDetails, H264>( | ||
| &**self.db.read().key_value(), | ||
| ::db::COL_EXTRA, | ||
| &best_block_hash | ||
| ).expect("block was previously imported; best_block_details should exist; qed"); | ||
|
|
||
| let (_, last_hash) = blocks_to_delete.last() | ||
| .expect("num is > 0; blocks_to_delete can't be empty; qed"); | ||
| // remove the last block as a child so that it can be re-imported | ||
| // ethcore/blockchain/src/blockchain.rs/Blockchain::is_known_child() | ||
| best_block_details.children.retain(|h| *h != *last_hash); | ||
| batch.write( | ||
| ::db::COL_EXTRA, | ||
| &best_block_hash, | ||
| &best_block_details | ||
| ); | ||
| // update the new best block hash | ||
| db_transaction.put(::db::COL_EXTRA, b"best", &*best_block_hash); | ||
| batch.put(::db::COL_EXTRA, b"best", &best_block_hash); | ||
|
|
||
| self.db.read() | ||
| .key_value() | ||
| .write(db_transaction) | ||
| .map_err(|err| format!("could not complete reset operation; io error occured: {}", err))?; | ||
|
|
||
| let hashes = blocks_to_delete.iter().map(|b| b.hash()).collect::<Vec<_>>(); | ||
|
|
||
| info!("Deleting block hashes {}", | ||
| Colour::Red | ||
| .bold() | ||
| .paint(format!("{:#?}", hashes)) | ||
| ); | ||
| .write(batch) | ||
| .map_err(|err| format!("could not delete blocks; io error occurred: {}", err))?; | ||
|
|
||
| info!("New best block hash {}", Colour::Green.bold().paint(format!("{:?}", best_block_hash))); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.