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
Add debug getbadblocks #9207
Closed
Closed
Add debug getbadblocks #9207
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
27dfca8
Add bad blocks to Blockchain implementation
9e0624c
Return blocks for getBadBlocks endpoint
602a7c4
Use one line declaration
d040683
Fix bad_blocks function
51cfb69
Fix bad_blocks function from using unwrap
9659c09
Use match on bad blocks for return value
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,6 +213,7 @@ pub struct BlockChain { | |
| block_hashes: RwLock<HashMap<BlockNumber, H256>>, | ||
| transaction_addresses: RwLock<HashMap<H256, TransactionAddress>>, | ||
| block_receipts: RwLock<HashMap<H256, BlockReceipts>>, | ||
| bad_blocks: RwLock<HashMap<H256, BlockInfo>>, | ||
|
|
||
| db: Arc<BlockChainDB>, | ||
|
|
||
|
|
@@ -527,6 +528,7 @@ impl BlockChain { | |
| block_hashes: RwLock::new(HashMap::new()), | ||
| transaction_addresses: RwLock::new(HashMap::new()), | ||
| block_receipts: RwLock::new(HashMap::new()), | ||
| bad_blocks: RwLock::new(HashMap::new()), | ||
| db: db.clone(), | ||
| cache_man: Mutex::new(cache_man), | ||
| pending_best_block: RwLock::new(None), | ||
|
|
@@ -985,6 +987,11 @@ impl BlockChain { | |
|
|
||
| let info = self.block_info(&header, route, &extras); | ||
|
|
||
| if self.is_blacklisted_hash(hash) { | ||
| self.bad_blocks.write().insert(hash, info); | ||
| return ImportRoute::none(); | ||
| } | ||
|
|
||
| if let BlockLocation::BranchBecomingCanonChain(ref d) = info.location { | ||
| info!(target: "reorg", "Reorg to {} ({} {} {})", | ||
| Colour::Yellow.bold().paint(format!("#{} {}", info.number, info.hash)), | ||
|
|
@@ -1364,6 +1371,26 @@ impl BlockChain { | |
| } | ||
| } | ||
|
|
||
| /// Determine if a hash is blacklisted (part of a hard fork) | ||
| fn is_blacklisted_hash(&self, hash: H256) -> bool { | ||
| if self.blacklisted_hashes().contains_key(&hash) { | ||
| return true | ||
| } | ||
| false | ||
| } | ||
|
|
||
| /// Get all blacklisted hashes | ||
| fn blacklisted_hashes(&self) -> HashMap<H256, bool> { | ||
| let mut blacklist = HashMap::new(); | ||
| blacklist.insert("05bef30ef572270f654746da22639a7a0c97dd97a7050b9e252391996aaeb689".into(), true); | ||
|
Contributor
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. What are these and why are they inserted here?
Contributor
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. These represent the list of bad hashes (usually hard forks) that determine whether a block is considered "bad". Similar to the implementation in geth: https://github.com/ethereum/go-ethereum/blob/master/core/blocks.go |
||
| blacklist.insert("7d05d08cbc596a2e5e4f13b80a743e53e09221b5323c3a61946b20873e58583f".into(), true); | ||
| blacklist | ||
| } | ||
|
|
||
| pub fn bad_blocks(&self) -> Vec<H256> { | ||
| self.bad_blocks.read().iter().map(|(hash, _)| *hash).collect::<Vec<H256>>() | ||
| } | ||
|
|
||
| /// Get best block hash. | ||
| pub fn best_block_hash(&self) -> H256 { | ||
| self.best_block.read().header.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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you describe how this lock is used? Locks are generally bad and increase complexity, but if its use and pattern is straightforward then it can obviously be completely fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason behind using a lock for those hashes is due to the potential for a blockchain client to try and read what the current bad blocks are. At the same time a bad block could be inserted into our hashmap of bad blocks. Thus a race condition for the same resource so it made sense to have a lock.