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

Commit 1986c4e

Browse files
debrisdvdplm
authored andcommitted
fixed verify_uncles error type (#11276)
* fixed verify_uncles error type * cleanup and document fn verify_uncles bounds checking * find_uncle_headers and find_uncle_hashes take u64 instead of u32 as an input param * Update ethcore/verification/src/verification.rs Co-Authored-By: David <[email protected]>
1 parent df1c5ac commit 1986c4e

File tree

4 files changed

+21
-31
lines changed

4 files changed

+21
-31
lines changed

ethcore/blockchain/src/blockchain.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,13 +1323,14 @@ impl BlockChain {
13231323
}
13241324

13251325
/// Given a block's `parent`, find every block header which represents a valid possible uncle.
1326-
pub fn find_uncle_headers(&self, parent: &H256, uncle_generations: usize) -> Option<Vec<encoded::Header>> {
1326+
pub fn find_uncle_headers(&self, parent: &H256, uncle_generations: u64) -> Option<Vec<encoded::Header>> {
13271327
self.find_uncle_hashes(parent, uncle_generations)
13281328
.map(|v| v.into_iter().filter_map(|h| self.block_header_data(&h)).collect())
13291329
}
13301330

13311331
/// Given a block's `parent`, find every block hash which represents a valid possible uncle.
1332-
pub fn find_uncle_hashes(&self, parent: &H256, uncle_generations: usize) -> Option<Vec<H256>> {
1332+
pub fn find_uncle_hashes(&self, parent: &H256, uncle_generations: u64) -> Option<Vec<H256>> {
1333+
let uncle_generations = uncle_generations as usize;
13331334
if !self.is_known(parent) {
13341335
return None;
13351336
}

ethcore/types/src/engines/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub enum SealingState {
9292
}
9393

9494
/// The number of generations back that uncles can be.
95-
pub const MAX_UNCLE_AGE: usize = 6;
95+
pub const MAX_UNCLE_AGE: u64 = 6;
9696

9797
/// Default EIP-210 contract code.
9898
/// As defined in https://github.com/ethereum/EIPs/pull/210

ethcore/types/src/errors/block_error.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ pub enum BlockError {
4444
/// Uncles hash in header is invalid.
4545
#[display(fmt = "Block has invalid uncles hash: {}", _0)]
4646
InvalidUnclesHash(Mismatch<H256>),
47-
/// An uncle is from a generation too old.
47+
/// An uncle is from a wrong generation.
4848
#[display(fmt = "Uncle block is too old. {}", _0)]
49-
UncleTooOld(OutOfBounds<BlockNumber>),
50-
/// An uncle is from the same generation as the block.
51-
#[display(fmt = "Uncle from same generation as block. {}", _0)]
52-
UncleIsBrother(OutOfBounds<BlockNumber>),
49+
UncleOutOfBounds(OutOfBounds<BlockNumber>),
5350
/// An uncle is already in the chain.
5451
#[display(fmt = "Uncle {} already in chain", _0)]
5552
UncleInChain(H256),

ethcore/verification/src/verification.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,30 +179,22 @@ fn verify_uncles(block: &PreverifiedBlock, bc: &dyn BlockProvider, engine: &dyn
179179
return Err(From::from(BlockError::DuplicateUncle(uncle.hash())))
180180
}
181181

182-
// m_currentBlock.number() - uncle.number() m_cB.n - uP.n()
183-
// 1 2
184-
// 2
185-
// 3
186-
// 4
187-
// 5
188-
// 6 7
189-
// (8 Invalid)
190-
191-
let depth = if header.number() > uncle.number() { header.number() - uncle.number() } else { 0 };
192-
if depth > MAX_UNCLE_AGE as u64 {
193-
return Err(From::from(BlockError::UncleTooOld(OutOfBounds {
194-
min: Some(header.number() - depth),
182+
// uncle.number() needs to be within specific number range which is
183+
// [header.number() - MAX_UNCLE_AGE, header.number() - 1]
184+
//
185+
// depth is the difference between uncle.number() and header.number()
186+
// and the previous condition implies that it is always in range
187+
// [1, MAX_UNCLE_AGE]
188+
let depth = if header.number() > uncle.number() &&
189+
uncle.number() + MAX_UNCLE_AGE >= header.number() {
190+
header.number() - uncle.number()
191+
} else {
192+
return Err(BlockError::UncleOutOfBounds(OutOfBounds {
193+
min: Some(header.number() - MAX_UNCLE_AGE),
195194
max: Some(header.number() - 1),
196195
found: uncle.number()
197-
})));
198-
}
199-
else if depth < 1 {
200-
return Err(From::from(BlockError::UncleIsBrother(OutOfBounds {
201-
min: Some(header.number() - depth),
202-
max: Some(header.number() - 1),
203-
found: uncle.number()
204-
})));
205-
}
196+
}).into());
197+
};
206198

207199
// cB
208200
// cB.p^1 1 depth, valid uncle
@@ -215,7 +207,7 @@ fn verify_uncles(block: &PreverifiedBlock, bc: &dyn BlockProvider, engine: &dyn
215207
// cB.p^8
216208
let mut expected_uncle_parent = header.parent_hash().clone();
217209
let uncle_parent = bc.block_header_data(&uncle.parent_hash())
218-
.ok_or_else(|| Error::from(BlockError::UnknownUncleParent(*uncle.parent_hash())))?;
210+
.ok_or_else(|| BlockError::UnknownUncleParent(*uncle.parent_hash()))?;
219211
for _ in 0..depth {
220212
match bc.block_details(&expected_uncle_parent) {
221213
Some(details) => {

0 commit comments

Comments
 (0)