Skip to content

Commit 4c2603b

Browse files
committed
chore: implement some coderabbit suggestions
Signed-off-by: etorreborre <etorreborre@yahoo.com>
1 parent 0fc993c commit 4c2603b

3 files changed

Lines changed: 61 additions & 14 deletions

File tree

  • crates/amaru-consensus/src/consensus/headers_tree/data_generation
  • simulation/amaru-sim/src/simulator

crates/amaru-consensus/src/consensus/headers_tree/data_generation/actions.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,16 @@ impl FromStr for Ratio {
336336
let denominator = parts[1]
337337
.parse::<u32>()
338338
.map_err(|_| format!("Invalid denominator in ratio: {}", parts[1]))?;
339+
340+
if denominator == 0 {
341+
return Err("Ratio denominator must be > 0".to_string());
342+
}
343+
if numerator > denominator {
344+
return Err(format!(
345+
"Ratio must be <= 1.0, got {}/{}",
346+
numerator, denominator
347+
));
348+
}
339349
Ok(Ratio(numerator, denominator))
340350
}
341351
}
@@ -615,6 +625,8 @@ where
615625

616626
#[cfg(test)]
617627
mod tests {
628+
use super::*;
629+
618630
#[test]
619631
fn transpose_works() {
620632
let rows = vec![
@@ -631,7 +643,37 @@ mod tests {
631643
vec![3, 8],
632644
vec![9],
633645
];
634-
let result = super::transpose(rows);
646+
let result = transpose(rows);
635647
assert_eq!(result, expected);
636648
}
649+
650+
#[test]
651+
fn parse_ratio_works() {
652+
let ratio: Ratio = "3/4".parse().unwrap();
653+
assert_eq!(ratio, Ratio(3, 4));
654+
655+
let ratio: super::Ratio = "0/1".parse().unwrap();
656+
assert_eq!(ratio, Ratio(0, 1));
657+
658+
let ratio: super::Ratio = "1/1".parse().unwrap();
659+
assert_eq!(ratio, Ratio(1, 1));
660+
}
661+
662+
#[test]
663+
fn parse_ratio_with_errors() {
664+
let err = "3".parse::<Ratio>().unwrap_err();
665+
assert_eq!(err, "Ratio must be in the form 'numerator/denominator'");
666+
667+
let err = "a/2".parse::<Ratio>().unwrap_err();
668+
assert_eq!(err, "Invalid numerator in ratio: a");
669+
670+
let err = "1/b".parse::<Ratio>().unwrap_err();
671+
assert_eq!(err, "Invalid denominator in ratio: b");
672+
673+
let err = "1/0".parse::<Ratio>().unwrap_err();
674+
assert_eq!(err, "Ratio denominator must be > 0");
675+
676+
let err = "5/4".parse::<Ratio>().unwrap_err();
677+
assert_eq!(err, "Ratio must be <= 1.0, got 5/4");
678+
}
637679
}

simulation/amaru-sim/src/simulator/data_generation/generate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn generate_entries<R: Rng>(
3030
node_config: &NodeConfig,
3131
start_time: Instant,
3232
mean_millis: f64,
33-
) -> impl Fn(&mut R) -> Vec<Entry<ChainSyncMessage>> + use<'_, R> {
33+
) -> impl Fn(&mut R) -> Vec<Entry<ChainSyncMessage>> {
3434
move |rng: &mut R| {
3535
let actions = run_with_rng(
3636
rng,

simulation/amaru-sim/src/simulator/run.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,25 @@ pub fn make_best_chain_from_downstream_messages(history: &History<ChainSyncMessa
319319
};
320320
match &message.body {
321321
msg @ ChainSyncMessage::Fwd { .. } => {
322-
best_chain.push(msg.decode_block_header().unwrap())
322+
if let Some(header) = msg.decode_block_header() {
323+
best_chain.push(header)
324+
}
323325
}
324326
msg @ ChainSyncMessage::Bck { .. } => {
325-
let header_hash = msg.header_hash().unwrap();
326-
let rollback_position = best_chain.iter().position(|h| h.hash() == header_hash);
327-
assert!(
328-
rollback_position.is_some(),
329-
"after the action {}, we have a rollback position that does not exist with hash {}.\nThe best chain is:\n{}. The history is:\n{}",
330-
i + 1,
331-
header_hash,
332-
best_chain.list_to_string(",\n"),
333-
history.0.iter().collect::<Vec<_>>().list_debug("\n")
334-
);
335-
best_chain.truncate(rollback_position.unwrap() + 1);
327+
if let Some(header_hash) = msg.header_hash() {
328+
let rollback_position = best_chain.iter().position(|h| h.hash() == header_hash);
329+
if let Some(rollback_position) = rollback_position {
330+
best_chain.truncate(rollback_position + 1);
331+
} else {
332+
panic!(
333+
"after the action {}, we have a rollback position that does not exist with hash {}.\nThe best chain is:\n{}. The history is:\n{}",
334+
i + 1,
335+
header_hash,
336+
best_chain.list_to_string(",\n"),
337+
history.0.iter().collect::<Vec<_>>().list_debug("\n")
338+
);
339+
}
340+
}
336341
}
337342
_ => (),
338343
}

0 commit comments

Comments
 (0)