Replace all Rlp usages with UntrustedRlp except for ethcore views (#8233)

* Replace Rlp with UntrustedRlp and unsafely unwrap

All Rlp methods return Result<_,DecoderError> now, so for this first
pass each will be marked with `expect("TODO")`. In the next pass we can
categorise figure out how to handle each case.

* Handle DecoderError for tendermint message

* Unwrap rlp results in TestBlockcChainClient

Rlp should be valid since created manually in tests

* Replace `use rlp::*` with explicit imports

* Remove rlp decode unwraps from light cli request

* Structured rlp encoding for curr best and latest in header chain

* Propogate decoder errors from send_packet

* Fix body uncles rlp index

* Use BodyView in sync and `expect` rlp errors

* Revert bbf28f removing original Rlp for this phase

This can be done again in the next phase, in order that we can leave the ethcore views unchanged

* Restore legacy Rlp and UntrustedRlp

Use legacy Rlp for ethcore views. Will redo replacing Rlp with UntrustedRlp in  a subsequent PR

* Fix tests

* Replace boilerplate Encodable/Decodable with derive

* Use BlockView instead of Rlp, remove unwrap

* Remove rlp test_cli unwraps by using BlockView instead of Rlp directly

* Remove unneccesary change to use borrowed hash

* Construct sync block using new_from_header_and_body
This commit is contained in:
Andrew Jones
2018-03-29 10:19:45 +01:00
committed by Rando
parent 6e49ff1d98
commit e3f7b70c38
26 changed files with 113 additions and 74 deletions

View File

@@ -41,7 +41,7 @@ use ethcore::engines::epoch::{
PendingTransition as PendingEpochTransition
};
use rlp::{Encodable, Decodable, DecoderError, RlpStream, Rlp, UntrustedRlp};
use rlp::{Encodable, Decodable, DecoderError, RlpStream, UntrustedRlp};
use heapsize::HeapSizeOf;
use ethereum_types::{H256, H264, U256};
use plain_hasher::H256FastMap;
@@ -74,6 +74,22 @@ pub struct BlockDescriptor {
pub total_difficulty: U256,
}
// best block data
#[derive(RlpEncodable, RlpDecodable)]
struct BestAndLatest {
best_num: u64,
latest_num: u64
}
impl BestAndLatest {
fn new(best_num: u64, latest_num: u64) -> Self {
BestAndLatest {
best_num,
latest_num,
}
}
}
// candidate block description.
struct Candidate {
hash: H256,
@@ -212,12 +228,9 @@ impl HeaderChain {
let decoded_header = spec.genesis_header();
let chain = if let Some(current) = db.get(col, CURRENT_KEY)? {
let (best_number, highest_number) = {
let rlp = Rlp::new(&current);
(rlp.val_at(0), rlp.val_at(1))
};
let curr : BestAndLatest = ::rlp::decode(&current);
let mut cur_number = highest_number;
let mut cur_number = curr.latest_num;
let mut candidates = BTreeMap::new();
// load all era entries, referenced headers within them,
@@ -245,7 +258,7 @@ impl HeaderChain {
// fill best block block descriptor.
let best_block = {
let era = match candidates.get(&best_number) {
let era = match candidates.get(&curr.best_num) {
Some(era) => era,
None => return Err(Error::Database("Database corrupt: highest block referenced but no data.".into())),
};
@@ -253,7 +266,7 @@ impl HeaderChain {
let best = &era.candidates[0];
BlockDescriptor {
hash: best.hash,
number: best_number,
number: curr.best_num,
total_difficulty: best.total_difficulty,
}
};
@@ -542,9 +555,8 @@ impl HeaderChain {
// write the best and latest eras to the database.
{
let latest_num = *candidates.iter().rev().next().expect("at least one era just inserted; qed").0;
let mut stream = RlpStream::new_list(2);
stream.append(&best_num).append(&latest_num);
transaction.put(self.col, CURRENT_KEY, &stream.out())
let curr = BestAndLatest::new(best_num, latest_num);
transaction.put(self.col, CURRENT_KEY, &::rlp::encode(&curr))
}
Ok(pending)
}