Skip to content

Commit

Permalink
chore: fix flaky test + remove unused code (#6486)
Browse files Browse the repository at this point in the history
Description
---
Fix flaky test `test_listening_initial_fallen_behind`
Removed unused code

Motivation and Context
---
We should not have unused code the code base.

How Has This Been Tested?
---
unit tests
  • Loading branch information
SWvheerden authored Aug 20, 2024
1 parent 7909a75 commit 6f99248
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 49 deletions.
47 changes: 1 addition & 46 deletions base_layer/core/src/proof_of_work/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,6 @@ impl Default for Difficulty {
}
}

/// This trait is used to add a type to `CheckedAdd`, which greatly simplifies usage in the code.
/// It is implemented for `Difficulty` and `u64`.
pub trait CheckedAdd<T> {
fn checked_add(&self, other: T) -> Option<Self>
where Self: Sized;
}

impl CheckedAdd<Difficulty> for Difficulty {
fn checked_add(&self, other: Difficulty) -> Option<Self> {
self.0.checked_add(other.0).map(Difficulty)
}
}

impl CheckedAdd<u64> for Difficulty {
fn checked_add(&self, other: u64) -> Option<Self> {
self.checked_add(Difficulty(other))
}
}

impl From<Difficulty> for u64 {
fn from(value: Difficulty) -> Self {
value.0
Expand Down Expand Up @@ -164,25 +145,7 @@ pub trait DifficultyAdjustment {
mod test {
use primitive_types::U256;

use crate::proof_of_work::{
difficulty::{CheckedAdd, MIN_DIFFICULTY},
Difficulty,
};
#[test]
fn add_difficulty() {
assert_eq!(
Difficulty::from_u64(1_000).unwrap().checked_add(8_000).unwrap(),
Difficulty::from_u64(9_000).unwrap()
);
assert_eq!(
Difficulty::default().checked_add(42).unwrap(),
Difficulty::from_u64(MIN_DIFFICULTY + 42).unwrap()
);
assert_eq!(
Difficulty::from_u64(15).unwrap().checked_add(5).unwrap(),
Difficulty::from_u64(20).unwrap()
);
}
use crate::proof_of_work::{difficulty::MIN_DIFFICULTY, Difficulty};

#[test]
fn test_format() {
Expand All @@ -203,14 +166,6 @@ mod test {
assert_eq!(Difficulty::max().as_u64(), u64::MAX);
}

#[test]
fn addition_does_not_overflow() {
let d1 = Difficulty::from_u64(100).unwrap();
assert!(d1.checked_add(1).is_some());
let d2 = Difficulty::max();
assert!(d2.checked_add(1).is_none());
}

#[test]
fn be_high_target() {
let target: &[u8] = &[
Expand Down
6 changes: 3 additions & 3 deletions base_layer/core/src/test_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use tari_utilities::epoch_time::EpochTime;
use crate::{
blocks::{Block, BlockHeader, BlockHeaderAccumulatedData, ChainHeader},
consensus::{ConsensusConstants, ConsensusManager},
proof_of_work::{difficulty::CheckedAdd, sha3x_difficulty, AchievedTargetDifficulty, Difficulty},
proof_of_work::{sha3x_difficulty, AchievedTargetDifficulty, Difficulty},
transactions::{
generate_coinbase_with_wallet_output,
key_manager::{MemoryDbKeyManager, TariKeyId},
Expand Down Expand Up @@ -185,8 +185,8 @@ pub fn create_peer_manager<P: AsRef<Path>>(data_path: P) -> Arc<PeerManager> {
pub fn create_chain_header(header: BlockHeader, prev_accum: &BlockHeaderAccumulatedData) -> ChainHeader {
let achieved_target_diff = AchievedTargetDifficulty::try_construct(
header.pow_algo(),
Difficulty::min().checked_add(1).unwrap(),
Difficulty::min().checked_add(1).unwrap(),
Difficulty::from_u64(Difficulty::min().as_u64() + 1).unwrap(),
Difficulty::from_u64(Difficulty::min().as_u64() + 1).unwrap(),
)
.unwrap();
let accumulated_data = BlockHeaderAccumulatedData::builder(prev_accum)
Expand Down
3 changes: 3 additions & 0 deletions base_layer/core/tests/tests/node_state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use tokio::{
sync::{broadcast, watch},
task,
time,
time::sleep,
};

use crate::helpers::{
Expand Down Expand Up @@ -233,6 +234,8 @@ async fn test_listening_initial_fallen_behind() {

let (state_change_event_publisher, _) = broadcast::channel(10);
let (status_event_sender, _status_event_receiver) = watch::channel(StatusInfo::new());
// This is here so that we can let the initial states run through the channels
sleep(Duration::from_secs(1)).await;
let mut alice_state_machine = BaseNodeStateMachine::new(
alice_node.blockchain_db.clone().into(),
alice_node.local_nci.clone(),
Expand Down

0 comments on commit 6f99248

Please sign in to comment.