Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Nov 10, 2023
1 parent fbb9625 commit 8ff6354
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
7 changes: 4 additions & 3 deletions base_layer/core/src/base_node/proto/chain_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ use tari_common_types::{chain_metadata::ChainMetadata, types::FixedHash};

use crate::proto::base_node as proto;

const ACCUMULATED_DIFFICULTY_BYTE_SIZE: usize = 32;
impl TryFrom<proto::ChainMetadata> for ChainMetadata {
type Error = String;

fn try_from(metadata: proto::ChainMetadata) -> Result<Self, Self::Error> {
if metadata.accumulated_difficulty.len() != 32 {
if metadata.accumulated_difficulty.len() != ACCUMULATED_DIFFICULTY_BYTE_SIZE {
return Err(format!(
"Invalid accumulated difficulty byte length. {} was expected but the actual length was {}",
32,
ACCUMULATED_DIFFICULTY_BYTE_SIZE,
metadata.accumulated_difficulty.len()
));
}
Expand Down Expand Up @@ -68,7 +69,7 @@ impl TryFrom<proto::ChainMetadata> for ChainMetadata {

impl From<ChainMetadata> for proto::ChainMetadata {
fn from(metadata: ChainMetadata) -> Self {
let mut accumulated_difficulty = [0u8; 32];
let mut accumulated_difficulty = [0u8; ACCUMULATED_DIFFICULTY_BYTE_SIZE];
metadata
.accumulated_difficulty()
.to_big_endian(&mut accumulated_difficulty);
Expand Down
6 changes: 1 addition & 5 deletions base_layer/core/src/proof_of_work/accumulated_difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ impl AccumulatedDifficulty {
}

pub fn checked_add_difficulty(&self, d: Difficulty) -> Option<AccumulatedDifficulty> {
let difficulty = d.as_u64() as u128;
if u128::MAX - difficulty < self.0 {
return None;
}
Some(AccumulatedDifficulty(self.0 + difficulty))
self.0.checked_add(u128::from(d.as_u64())).map(AccumulatedDifficulty)
}

pub fn to_be_bytes(&self) -> Vec<u8> {
Expand Down

0 comments on commit 8ff6354

Please sign in to comment.