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

Commit

Permalink
Light clippy(fy) (#9473)
Browse files Browse the repository at this point in the history
* wasm tests

* `clippyfy` light-client

* Revert inefficient change `collect_ready()`
  • Loading branch information
niklasad1 authored and 5chdn committed Sep 6, 2018
1 parent 4e8e5bb commit 6888a96
Show file tree
Hide file tree
Showing 22 changed files with 323 additions and 345 deletions.
16 changes: 8 additions & 8 deletions ethcore/light/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use ethereum_types::{H256, U256};
use memory_cache::MemoryLruCache;

/// Configuration for how much data to cache.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct CacheSizes {
/// Maximum size, in bytes, of cached headers.
pub headers: usize,
Expand Down Expand Up @@ -83,33 +83,33 @@ impl Cache {
receipts: MemoryLruCache::new(sizes.receipts),
chain_score: MemoryLruCache::new(sizes.chain_score),
corpus: None,
corpus_expiration: corpus_expiration,
corpus_expiration,
}
}

/// Query header by hash.
pub fn block_header(&mut self, hash: &H256) -> Option<encoded::Header> {
self.headers.get_mut(hash).map(|x| x.clone())
self.headers.get_mut(hash).cloned()
}

/// Query hash by number.
pub fn block_hash(&mut self, num: &BlockNumber) -> Option<H256> {
self.canon_hashes.get_mut(num).map(|x| x.clone())
pub fn block_hash(&mut self, num: BlockNumber) -> Option<H256> {
self.canon_hashes.get_mut(&num).map(|h| *h)
}

/// Query block body by block hash.
pub fn block_body(&mut self, hash: &H256) -> Option<encoded::Body> {
self.bodies.get_mut(hash).map(|x| x.clone())
self.bodies.get_mut(hash).cloned()
}

/// Query block receipts by block hash.
pub fn block_receipts(&mut self, hash: &H256) -> Option<Vec<Receipt>> {
self.receipts.get_mut(hash).map(|x| x.clone())
self.receipts.get_mut(hash).cloned()
}

/// Query chain score by block hash.
pub fn chain_score(&mut self, hash: &H256) -> Option<U256> {
self.chain_score.get_mut(hash).map(|x| x.clone())
self.chain_score.get_mut(hash).map(|h| *h)
}

/// Cache the given header.
Expand Down
6 changes: 3 additions & 3 deletions ethcore/light/src/cht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct BlockInfo {
/// Build an in-memory CHT from a closure which provides necessary information
/// about blocks. If the fetcher ever fails to provide the info, the CHT
/// will not be generated.
pub fn build<F>(cht_num: u64, mut fetcher: F) -> Option<CHT<MemoryDB<KeccakHasher>>>
pub fn build<F>(cht_num: u64, mut fetcher: F) -> Option<CHT<MemoryDB<KeccakHasher>>>
where F: FnMut(BlockId) -> Option<BlockInfo>
{
let mut db = MemoryDB::<KeccakHasher>::new();
Expand All @@ -118,8 +118,8 @@ pub fn build<F>(cht_num: u64, mut fetcher: F) -> Option<CHT<MemoryDB<KeccakHash
}

Some(CHT {
db: db,
root: root,
db,
root,
number: cht_num,
})
}
Expand Down
85 changes: 43 additions & 42 deletions ethcore/light/src/client/header_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ use smallvec::SmallVec;
const HISTORY: u64 = 2048;

/// The best block key. Maps to an RLP list: [best_era, last_era]
const CURRENT_KEY: &'static [u8] = &*b"best_and_latest";
const CURRENT_KEY: &[u8] = &*b"best_and_latest";

/// Key storing the last canonical epoch transition.
const LAST_CANONICAL_TRANSITION: &'static [u8] = &*b"canonical_transition";
const LAST_CANONICAL_TRANSITION: &[u8] = &*b"canonical_transition";

/// Information about a block.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -97,9 +97,10 @@ struct Entry {

impl HeapSizeOf for Entry {
fn heap_size_of_children(&self) -> usize {
match self.candidates.spilled() {
false => 0,
true => self.candidates.capacity() * ::std::mem::size_of::<Candidate>(),
if self.candidates.spilled() {
self.candidates.capacity() * ::std::mem::size_of::<Candidate>()
} else {
0
}
}
}
Expand Down Expand Up @@ -134,7 +135,7 @@ impl Decodable for Entry {
// rely on the invariant that the canonical entry is always first.
let canon_hash = candidates[0].hash;
Ok(Entry {
candidates: candidates,
candidates,
canonical_hash: canon_hash,
})
}
Expand Down Expand Up @@ -269,9 +270,9 @@ impl HeaderChain {
best_block: RwLock::new(best_block),
candidates: RwLock::new(candidates),
live_epoch_proofs: RwLock::new(live_epoch_proofs),
db: db,
col: col,
cache: cache,
db,
col,
cache,
}

} else {
Expand All @@ -285,8 +286,8 @@ impl HeaderChain {
candidates: RwLock::new(BTreeMap::new()),
live_epoch_proofs: RwLock::new(live_epoch_proofs),
db: db.clone(),
col: col,
cache: cache,
col,
cache,
};

// insert the hardcoded sync into the database.
Expand All @@ -302,9 +303,8 @@ impl HeaderChain {
let decoded_header_num = decoded_header.number();

// write the block in the DB.
info!(target: "chain", "Inserting hardcoded block #{} in chain",
decoded_header_num);
let pending = chain.insert_with_td(&mut batch, decoded_header,
info!(target: "chain", "Inserting hardcoded block #{} in chain", decoded_header_num);
let pending = chain.insert_with_td(&mut batch, &decoded_header,
hardcoded_sync.total_difficulty, None)?;

// check that we have enough hardcoded CHT roots. avoids panicking later.
Expand All @@ -324,7 +324,7 @@ impl HeaderChain {
};

// instantiate genesis epoch data if it doesn't exist.
if let None = chain.db.get(col, LAST_CANONICAL_TRANSITION)? {
if chain.db.get(col, LAST_CANONICAL_TRANSITION)?.is_none() {
let genesis_data = spec.genesis_epoch_data()?;

{
Expand All @@ -349,7 +349,7 @@ impl HeaderChain {
pub fn insert(
&self,
transaction: &mut DBTransaction,
header: Header,
header: &Header,
transition_proof: Option<Vec<u8>>,
) -> Result<PendingChanges, BlockImportError> {
self.insert_inner(transaction, header, None, transition_proof)
Expand All @@ -361,7 +361,7 @@ impl HeaderChain {
pub fn insert_with_td(
&self,
transaction: &mut DBTransaction,
header: Header,
header: &Header,
total_difficulty: U256,
transition_proof: Option<Vec<u8>>,
) -> Result<PendingChanges, BlockImportError> {
Expand All @@ -371,7 +371,7 @@ impl HeaderChain {
fn insert_inner(
&self,
transaction: &mut DBTransaction,
header: Header,
header: &Header,
total_difficulty: Option<U256>,
transition_proof: Option<Vec<u8>>,
) -> Result<PendingChanges, BlockImportError> {
Expand All @@ -381,7 +381,7 @@ impl HeaderChain {
let transition = transition_proof.map(|proof| EpochTransition {
block_hash: hash,
block_number: number,
proof: proof,
proof,
});

let mut pending = PendingChanges {
Expand Down Expand Up @@ -415,9 +415,9 @@ impl HeaderChain {
let cur_era = candidates.entry(number)
.or_insert_with(|| Entry { candidates: SmallVec::new(), canonical_hash: hash });
cur_era.candidates.push(Candidate {
hash: hash,
parent_hash: parent_hash,
total_difficulty: total_difficulty,
hash,
parent_hash,
total_difficulty,
});

// fix ordering of era before writing.
Expand Down Expand Up @@ -479,9 +479,9 @@ impl HeaderChain {

trace!(target: "chain", "New best block: ({}, {}), TD {}", number, hash, total_difficulty);
pending.best_block = Some(BlockDescriptor {
hash: hash,
number: number,
total_difficulty: total_difficulty,
hash,
number,
total_difficulty,
});

// produce next CHT root if it's time.
Expand Down Expand Up @@ -651,7 +651,7 @@ impl HeaderChain {
Ok(db_value) => {
db_value.map(|x| x.into_vec()).map(encoded::Header::new)
.and_then(|header| {
cache.insert_block_header(hash.clone(), header.clone());
cache.insert_block_header(hash, header.clone());
Some(header)
})
},
Expand Down Expand Up @@ -772,16 +772,17 @@ impl HeaderChain {

/// Get block status.
pub fn status(&self, hash: &H256) -> BlockStatus {
match self.db.get(self.col, &*hash).ok().map_or(false, |x| x.is_some()) {
true => BlockStatus::InChain,
false => BlockStatus::Unknown,
if self.db.get(self.col, hash).ok().map_or(false, |x| x.is_some()) {
BlockStatus::InChain
} else {
BlockStatus::Unknown
}
}

/// Insert a pending transition.
pub fn insert_pending_transition(&self, batch: &mut DBTransaction, hash: H256, t: PendingEpochTransition) {
pub fn insert_pending_transition(&self, batch: &mut DBTransaction, hash: H256, t: &PendingEpochTransition) {
let key = pending_transition_key(hash);
batch.put(self.col, &*key, &*::rlp::encode(&t));
batch.put(self.col, &*key, &*::rlp::encode(t));
}

/// Get pending transition for a specific block hash.
Expand Down Expand Up @@ -865,7 +866,7 @@ mod tests {
use ethcore::ids::BlockId;
use ethcore::header::Header;
use ethcore::spec::Spec;
use cache::Cache;
use cache::Cache;
use kvdb::KeyValueDB;
use kvdb_memorydb;

Expand Down Expand Up @@ -897,7 +898,7 @@ mod tests {
parent_hash = header.hash();

let mut tx = db.transaction();
let pending = chain.insert(&mut tx, header, None).unwrap();
let pending = chain.insert(&mut tx, &header, None).unwrap();
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand Down Expand Up @@ -930,7 +931,7 @@ mod tests {
parent_hash = header.hash();

let mut tx = db.transaction();
let pending = chain.insert(&mut tx, header, None).unwrap();
let pending = chain.insert(&mut tx, &header, None).unwrap();
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand All @@ -949,7 +950,7 @@ mod tests {
parent_hash = header.hash();

let mut tx = db.transaction();
let pending = chain.insert(&mut tx, header, None).unwrap();
let pending = chain.insert(&mut tx, &header, None).unwrap();
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand All @@ -973,7 +974,7 @@ mod tests {
parent_hash = header.hash();

let mut tx = db.transaction();
let pending = chain.insert(&mut tx, header, None).unwrap();
let pending = chain.insert(&mut tx, &header, None).unwrap();
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand Down Expand Up @@ -1026,7 +1027,7 @@ mod tests {
parent_hash = header.hash();

let mut tx = db.transaction();
let pending = chain.insert(&mut tx, header, None).unwrap();
let pending = chain.insert(&mut tx, &header, None).unwrap();
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand Down Expand Up @@ -1066,7 +1067,7 @@ mod tests {
parent_hash = header.hash();

let mut tx = db.transaction();
let pending = chain.insert(&mut tx, header, None).unwrap();
let pending = chain.insert(&mut tx, &header, None).unwrap();
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand All @@ -1083,7 +1084,7 @@ mod tests {
parent_hash = header.hash();

let mut tx = db.transaction();
let pending = chain.insert(&mut tx, header, None).unwrap();
let pending = chain.insert(&mut tx, &header, None).unwrap();
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand Down Expand Up @@ -1141,7 +1142,7 @@ mod tests {
None
};

let pending = chain.insert(&mut tx, header, epoch_proof).unwrap();
let pending = chain.insert(&mut tx, &header, epoch_proof).unwrap();
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand Down Expand Up @@ -1169,7 +1170,7 @@ mod tests {
parent_hash = header.hash();

let mut tx = db.transaction();
let pending = chain.insert(&mut tx, header, None).unwrap();
let pending = chain.insert(&mut tx, &header, None).unwrap();
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand Down Expand Up @@ -1208,7 +1209,7 @@ mod tests {
parent_hash = header.hash();

let mut tx = db.transaction();
let pending = chain.insert(&mut tx, header, None).expect("failed inserting a transaction");
let pending = chain.insert(&mut tx, &header, None).expect("failed inserting a transaction");
db.write(tx).unwrap();
chain.apply_pending(pending);

Expand Down
Loading

0 comments on commit 6888a96

Please sign in to comment.