Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pmnoxx committed Aug 11, 2020
1 parent 558f5e1 commit 1aefeca
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions chain/client/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn sample_binary(n: u64, k: u64) -> bool {
}

pub struct BlockStats {
hash2height: HashMap<CryptoHash, u64>,
hash2depth: HashMap<CryptoHash, u64>,
num_blocks: u64,
max_chain_length: u64,
last_check: Instant,
Expand All @@ -216,7 +216,7 @@ pub struct BlockStats {
impl BlockStats {
fn new() -> BlockStats {
BlockStats {
hash2height: HashMap::new(),
hash2depth: HashMap::new(),
num_blocks: 0,
max_chain_length: 0,
last_check: Instant::now(),
Expand All @@ -227,8 +227,8 @@ impl BlockStats {
}

fn calculate_distance(&mut self, mut lhs: CryptoHash, mut rhs: CryptoHash) -> u64 {
let mut dlhs = *self.hash2height.get(&lhs).unwrap();
let mut drhs = *self.hash2height.get(&rhs).unwrap();
let mut dlhs = *self.hash2depth.get(&lhs).unwrap();
let mut drhs = *self.hash2depth.get(&rhs).unwrap();

let mut result: u64 = 0;
while dlhs > drhs {
Expand All @@ -250,11 +250,11 @@ impl BlockStats {
}

fn add_block(&mut self, block: &Block) {
if self.hash2height.contains_key(block.hash()) {
if self.hash2depth.contains_key(block.hash()) {
return;
}
let prev_height = self.hash2height.get(block.header().prev_hash()).map(|v| *v).unwrap_or(0);
self.hash2height.insert(*block.hash(), prev_height + 1);
let prev_height = self.hash2depth.get(block.header().prev_hash()).map(|v| *v).unwrap_or(0);
self.hash2depth.insert(*block.hash(), prev_height + 1);
self.num_blocks += 1;
self.max_chain_length = max(self.max_chain_length, prev_height + 1);
self.parent.insert(*block.hash(), *block.header().prev_hash());
Expand Down

0 comments on commit 1aefeca

Please sign in to comment.