Skip to content

Commit

Permalink
chore: tidy up codebase, specially function signatures and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aesedepece committed Jun 24, 2024
1 parent eb9dc4a commit 26019e1
Show file tree
Hide file tree
Showing 28 changed files with 304 additions and 204 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion config/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub trait Defaults {
/// Set the limit of retrievals per epoch to 65_535.
/// This in practice equals no limit enforcement.
fn mining_data_request_max_retrievals_per_epoch(&self) -> u16 {
core::u16::MAX
u16::MAX
}

/// Genesis block path, "./genesis_block.json" by default
Expand Down
12 changes: 6 additions & 6 deletions data_structures/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4408,11 +4408,11 @@ where
total_rep * n / remaining_rep
};

return u32::try_from(factor).unwrap_or(u32::max_value());
return u32::try_from(factor).unwrap_or(u32::MAX);
}
}

u32::max_value()
u32::MAX
}

/// Witnessing Acts Counter
Expand Down Expand Up @@ -4510,7 +4510,7 @@ impl EpochConstants {

Epoch::from(period)
.checked_mul(epoch)
.filter(|&x| x <= Epoch::max_value() as Epoch)
.filter(|&x| x <= Epoch::MAX as Epoch)
.map(i64::from)
.and_then(|x| x.checked_add(zero))
.ok_or(EpochCalculationError::Overflow)
Expand Down Expand Up @@ -6272,7 +6272,7 @@ mod tests {
fn rep_threshold_zero() {
let rep_engine = ReputationEngine::new(1000);

assert_eq!(rep_engine.threshold_factor(1), u32::max_value());
assert_eq!(rep_engine.threshold_factor(1), u32::MAX);
}

#[test]
Expand Down Expand Up @@ -6378,7 +6378,7 @@ mod tests {
.unwrap();
rep_engine.ars_mut().push_activity(vec![id2]);

assert_eq!(rep_engine.threshold_factor(10), u32::max_value());
assert_eq!(rep_engine.threshold_factor(10), u32::MAX);
}

#[test]
Expand Down Expand Up @@ -6445,7 +6445,7 @@ mod tests {
assert_eq!(rep_engine.threshold_factor(6), 15);
assert_eq!(rep_engine.threshold_factor(7), 50);
assert_eq!(rep_engine.threshold_factor(8), 100);
assert_eq!(rep_engine.threshold_factor(9), u32::max_value());
assert_eq!(rep_engine.threshold_factor(9), u32::MAX);
}

#[test]
Expand Down
52 changes: 28 additions & 24 deletions data_structures/src/data_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ impl DataRequestPool {
/// for reveal (the node should send a reveal transaction).
/// This function must be called after `add_data_requests_from_block`, in order to update
/// the stage of all the data requests.
pub fn update_data_request_stages(&mut self, validator_count: usize) -> Vec<RevealTransaction> {
pub fn update_data_request_stages(
&mut self,
validator_count: Option<usize>,
) -> Vec<RevealTransaction> {
let validator_count = validator_count.unwrap_or(crate::DEFAULT_VALIDATOR_COUNT_FOR_TESTS);
let waiting_for_reveal = &mut self.waiting_for_reveal;
let data_requests_by_epoch = &mut self.data_requests_by_epoch;
let extra_rounds = self.extra_rounds;
Expand Down Expand Up @@ -779,7 +783,7 @@ mod tests {
);
assert!(p.to_be_stored.is_empty());

assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

(epoch, fake_block_hash, p, dr_transaction.hash())
}
Expand Down Expand Up @@ -814,7 +818,7 @@ mod tests {
);
assert!(p.to_be_stored.is_empty());

assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

(epoch, fake_block_hash, p, dr_transaction.hash())
}
Expand Down Expand Up @@ -849,7 +853,7 @@ mod tests {
);
assert!(p.to_be_stored.is_empty());

assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

(epoch, fake_block_hash, p, dr_transaction.hash())
}
Expand Down Expand Up @@ -891,7 +895,7 @@ mod tests {
assert!(p.data_requests_by_epoch[&epoch].contains(&dr_pointer));

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

// Now in reveal stage
assert_eq!(
Expand Down Expand Up @@ -938,7 +942,7 @@ mod tests {
);

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

// Now in tally stage
assert_eq!(
Expand All @@ -963,7 +967,7 @@ mod tests {
assert_eq!(p.data_request_state(&dr_pointer), None);

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

assert_eq!(p.to_be_stored.len(), 1);
assert_eq!(
Expand Down Expand Up @@ -1033,7 +1037,7 @@ mod tests {
.unwrap();

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

// Now in reveal stage 1
assert_eq!(
Expand All @@ -1054,7 +1058,7 @@ mod tests {
.unwrap();

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
// Now in reveal stage 2
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
Expand All @@ -1066,7 +1070,7 @@ mod tests {
);

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
// Now in reveal stage 3
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
Expand All @@ -1078,7 +1082,7 @@ mod tests {
);

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
// Now in tally stage
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
Expand Down Expand Up @@ -1124,7 +1128,7 @@ mod tests {
.unwrap();

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

// Now in reveal stage 1
assert_eq!(
Expand All @@ -1145,7 +1149,7 @@ mod tests {
.unwrap();

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
// Now in reveal stage 2
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
Expand All @@ -1167,7 +1171,7 @@ mod tests {
.unwrap();

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
// Now in tally stage
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
Expand Down Expand Up @@ -1210,7 +1214,7 @@ mod tests {
.unwrap();

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
// Now in reveal stage 1
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
Expand All @@ -1222,7 +1226,7 @@ mod tests {
);

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
// Now in reveal stage 2
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
Expand All @@ -1234,7 +1238,7 @@ mod tests {
);

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
// Now in reveal stage 3
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
Expand All @@ -1246,7 +1250,7 @@ mod tests {
);

// Update stages
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
// Now in tally stage, after 3 reveal stages with no reveals
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
Expand Down Expand Up @@ -1302,7 +1306,7 @@ mod tests {
);

// Update stages. This will return our reveal transaction
let my_reveals = p.update_data_request_stages();
let my_reveals = p.update_data_request_stages(None);
assert_eq!(my_reveals.len(), 1);
let my_reveal = &my_reveals[0];
assert_eq!(my_reveal, &reveal_transaction);
Expand Down Expand Up @@ -1343,7 +1347,7 @@ mod tests {

// Since extra_commit_rounds = 0, updating again in commit stage will
// move the data request to tally stage
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

// Now in tally stage
assert_eq!(
Expand All @@ -1370,7 +1374,7 @@ mod tests {
assert_eq!(p.data_request_pool[&dr_pointer].backup_witnesses(), 1);

// Second commitment round
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
DataRequestStage::COMMIT
Expand All @@ -1382,7 +1386,7 @@ mod tests {
assert_eq!(p.data_request_pool[&dr_pointer].backup_witnesses(), 2);

// Third commitment round
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());
assert_eq!(
p.data_request_pool[&dr_pointer].stage,
DataRequestStage::COMMIT
Expand All @@ -1395,7 +1399,7 @@ mod tests {

// Since extra_commit_rounds = 1, updating again in commit stage will
// move the data request to tally stage
assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

// Now in tally stage
assert_eq!(
Expand All @@ -1421,7 +1425,7 @@ mod tests {
);
assert_eq!(p.data_request_pool[&dr_pointer].backup_witnesses(), 1);

assert!(p.update_data_request_stages().is_empty());
assert!(p.update_data_request_stages(None).is_empty());

// Add commit and update stage
let (_fake_block_hash, p, dr_pointer) =
Expand Down
2 changes: 2 additions & 0 deletions data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use crate::{
proto::versioning::ProtocolVersion,
};

pub const DEFAULT_VALIDATOR_COUNT_FOR_TESTS: usize = 100;

/// Module containing functions to generate Witnet's protocol messages
pub mod builders;

Expand Down
8 changes: 4 additions & 4 deletions data_structures/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl ProtobufConvert for u8 {
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
pb <= Self::ProtoStruct::from(Self::max_value()),
pb <= Self::ProtoStruct::from(Self::MAX),
"Integer out of range"
);
Ok(pb as Self)
Expand All @@ -272,7 +272,7 @@ impl ProtobufConvert for i8 {
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
pb <= Self::ProtoStruct::from(Self::max_value()),
pb <= Self::ProtoStruct::from(Self::MAX),
"Integer out of range"
);
Ok(Self::try_from(pb)?)
Expand All @@ -286,7 +286,7 @@ impl ProtobufConvert for u16 {
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
pb <= Self::ProtoStruct::from(Self::max_value()),
pb <= Self::ProtoStruct::from(Self::MAX),
"Integer out of range"
);
Ok(Self::try_from(pb)?)
Expand All @@ -300,7 +300,7 @@ impl ProtobufConvert for i16 {
}
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> {
ensure!(
pb <= Self::ProtoStruct::from(Self::max_value()),
pb <= Self::ProtoStruct::from(Self::MAX),
"Integer out of range"
);
Ok(Self::try_from(pb)?)
Expand Down
8 changes: 8 additions & 0 deletions data_structures/src/staking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ pub type Power = u64;
/// The resulting type for all the fallible functions in this module.
pub type StakesResult<T, Address, Coins, Epoch> = Result<T, StakesError<Address, Coins, Epoch>>;

/// The resulting type for functions in this module that return a single stake entry.
pub type StakesEntryResult<Address, Coins, Epoch, Power> =
StakesResult<Stake<Address, Coins, Epoch, Power>, Address, Coins, Epoch>;

/// The resulting type for functions in this module that return a vector of stake entries.
pub type StakesVecResult<Address, Coins, Epoch, Power> =
StakesResult<Vec<Stake<Address, Coins, Epoch, Power>>, Address, Coins, Epoch>;

/// Newtype for a reference-counted and read-write-locked instance of `Stake`.
///
/// This newtype is needed for implementing `PartialEq` manually on the locked data, which cannot be done directly
Expand Down
Loading

0 comments on commit 26019e1

Please sign in to comment.