Fix block number check (makme it strict!).

This commit is contained in:
Gav Wood 2016-01-27 14:09:32 +01:00
parent f6d7adf693
commit 2f7857520e
3 changed files with 17 additions and 12 deletions

View File

@ -120,7 +120,9 @@ pub enum BlockError {
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
InvalidParentHash(Mismatch<H256>), InvalidParentHash(Mismatch<H256>),
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
InvalidNumber(OutOfBounds<BlockNumber>), InvalidNumber(Mismatch<BlockNumber>),
/// Block number isn't sensible.
RidiculousNumber(OutOfBounds<BlockNumber>),
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
UnknownParent(H256), UnknownParent(H256),
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me

View File

@ -42,6 +42,9 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
client.flush_queue(); client.flush_queue();
let imported_ok = client.import_verified_blocks(&IoChannel::disconnected()) > 0; let imported_ok = client.import_verified_blocks(&IoChannel::disconnected()) > 0;
assert_eq!(imported_ok, is_valid); assert_eq!(imported_ok, is_valid);
if imported_ok {
flushln!("Imported {}; best block {}", hash, client.chain_info().best_block_hash);
}
} }
fail_unless(client.chain_info().best_block_hash == H256::from_json(&test["lastblockhash"])); fail_unless(client.chain_info().best_block_hash == H256::from_json(&test["lastblockhash"]));
} }
@ -60,13 +63,13 @@ declare_test!{BlockchainTests_bcForkBlockTest, "BlockchainTests/bcForkBlockTest"
declare_test!{BlockchainTests_bcForkStressTest, "BlockchainTests/bcForkStressTest"} // STILL FAILS declare_test!{BlockchainTests_bcForkStressTest, "BlockchainTests/bcForkStressTest"} // STILL FAILS
declare_test!{BlockchainTests_bcForkUncle, "BlockchainTests/bcForkUncle"} // STILL FAILS declare_test!{BlockchainTests_bcForkUncle, "BlockchainTests/bcForkUncle"} // STILL FAILS
declare_test!{BlockchainTests_bcGasPricerTest, "BlockchainTests/bcGasPricerTest"} declare_test!{BlockchainTests_bcGasPricerTest, "BlockchainTests/bcGasPricerTest"}
declare_test!{BlockchainTests_bcInvalidHeaderTest, "BlockchainTests/bcInvalidHeaderTest"} declare_test!{BlockchainTests_bcInvalidHeaderTest, "BlockchainTests/bcInvalidHeaderTest"} // FAILS AGAIN?
declare_test!{BlockchainTests_bcInvalidRLPTest, "BlockchainTests/bcInvalidRLPTest"} declare_test!{BlockchainTests_bcInvalidRLPTest, "BlockchainTests/bcInvalidRLPTest"}
declare_test!{BlockchainTests_bcMultiChainTest, "BlockchainTests/bcMultiChainTest"} declare_test!{BlockchainTests_bcMultiChainTest, "BlockchainTests/bcMultiChainTest"}
declare_test!{BlockchainTests_bcRPC_API_Test, "BlockchainTests/bcRPC_API_Test"} declare_test!{BlockchainTests_bcRPC_API_Test, "BlockchainTests/bcRPC_API_Test"}
declare_test!{BlockchainTests_bcStateTest, "BlockchainTests/bcStateTest"} declare_test!{BlockchainTests_bcStateTest, "BlockchainTests/bcStateTest"}
declare_test!{BlockchainTests_bcTotalDifficultyTest, "BlockchainTests/bcTotalDifficultyTest"} declare_test!{BlockchainTests_bcTotalDifficultyTest, "BlockchainTests/bcTotalDifficultyTest"} // FAILS AGAIN?
declare_test!{BlockchainTests_bcUncleHeaderValiditiy, "BlockchainTests/bcUncleHeaderValiditiy"} // FAILS declare_test!{BlockchainTests_bcUncleHeaderValiditiy, "BlockchainTests/bcUncleHeaderValiditiy"}
declare_test!{BlockchainTests_bcUncleTest, "BlockchainTests/bcUncleTest"} // FAILS declare_test!{BlockchainTests_bcUncleTest, "BlockchainTests/bcUncleTest"}
declare_test!{BlockchainTests_bcValidBlockTest, "BlockchainTests/bcValidBlockTest"} // FAILS declare_test!{BlockchainTests_bcValidBlockTest, "BlockchainTests/bcValidBlockTest"} // STILL FAILS
declare_test!{BlockchainTests_bcWalletTest, "BlockchainTests/bcWalletTest"} // FAILS declare_test!{BlockchainTests_bcWalletTest, "BlockchainTests/bcWalletTest"}

View File

@ -162,7 +162,7 @@ pub fn verify_block_final(expected: &Header, got: &Header) -> Result<(), Error>
/// Check basic header parameters. /// Check basic header parameters.
fn verify_header(header: &Header, engine: &Engine) -> Result<(), Error> { fn verify_header(header: &Header, engine: &Engine) -> Result<(), Error> {
if header.number >= From::from(BlockNumber::max_value()) { if header.number >= From::from(BlockNumber::max_value()) {
return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: Some(From::from(BlockNumber::max_value())), min: None, found: header.number }))) return Err(From::from(BlockError::RidiculousNumber(OutOfBounds { max: Some(From::from(BlockNumber::max_value())), min: None, found: header.number })))
} }
if header.gas_used > header.gas_limit { if header.gas_used > header.gas_limit {
return Err(From::from(BlockError::TooMuchGasUsed(OutOfBounds { max: Some(header.gas_limit), min: None, found: header.gas_used }))); return Err(From::from(BlockError::TooMuchGasUsed(OutOfBounds { max: Some(header.gas_limit), min: None, found: header.gas_used })));
@ -186,8 +186,8 @@ fn verify_parent(header: &Header, parent: &Header) -> Result<(), Error> {
if header.timestamp <= parent.timestamp { if header.timestamp <= parent.timestamp {
return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: None, min: Some(parent.timestamp + 1), found: header.timestamp }))) return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: None, min: Some(parent.timestamp + 1), found: header.timestamp })))
} }
if header.number <= parent.number { if header.number != parent.number + 1 {
return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: None, min: Some(parent.number + 1), found: header.number }))); return Err(From::from(BlockError::InvalidNumber(Mismatch { expected: parent.number + 1, found: header.number })));
} }
Ok(()) Ok(())
} }
@ -400,7 +400,7 @@ mod tests {
header = good.clone(); header = good.clone();
header.number = BlockNumber::max_value(); header.number = BlockNumber::max_value();
check_fail(basic_test(&create_test_block(&header), engine.deref()), check_fail(basic_test(&create_test_block(&header), engine.deref()),
InvalidNumber(OutOfBounds { max: Some(BlockNumber::max_value()), min: None, found: header.number })); RidiculousNumber(OutOfBounds { max: Some(BlockNumber::max_value()), min: None, found: header.number }));
header = good.clone(); header = good.clone();
header.gas_used = header.gas_limit + From::from(1); header.gas_used = header.gas_limit + From::from(1);
@ -443,7 +443,7 @@ mod tests {
header = good.clone(); header = good.clone();
header.number = 9; header.number = 9;
check_fail(family_test(&create_test_block_with_data(&header, &good_transactions, &good_uncles), engine.deref(), &bc), check_fail(family_test(&create_test_block_with_data(&header, &good_transactions, &good_uncles), engine.deref(), &bc),
InvalidNumber(OutOfBounds { max: None, min: Some(parent.number + 1), found: header.number })); InvalidNumber(Mismatch { expected: parent.number + 1, found: header.number }));
header = good.clone(); header = good.clone();
let mut bad_uncles = good_uncles.clone(); let mut bad_uncles = good_uncles.clone();