Skip to content

Commit 204c7e4

Browse files
authored
Merge pull request #1669 from input-output-hk/djo/1648/ord_on_signed_entity_type_discriminants
Ord on signed entity type discriminants
2 parents f36ddb2 + 3f3fb02 commit 204c7e4

File tree

5 files changed

+75
-23
lines changed

5 files changed

+75
-23
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator"
3-
version = "0.5.2"
3+
version = "0.5.3"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-aggregator/src/configuration.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use mithril_common::crypto_helper::ProtocolGenesisSigner;
55
use mithril_common::era::adapters::EraReaderAdapterType;
66
use mithril_doc::{Documenter, DocumenterDefault, StructDoc};
77
use serde::{Deserialize, Serialize};
8+
use std::collections::BTreeSet;
89
use std::path::PathBuf;
910
use std::str::FromStr;
1011

@@ -267,11 +268,11 @@ impl Configuration {
267268
/// The signed entity types are discarded if they are not declared in the [SignedEntityType] enum.
268269
pub fn list_allowed_signed_entity_types_discriminants(
269270
&self,
270-
) -> StdResult<Vec<SignedEntityTypeDiscriminants>> {
271-
let default_discriminants = vec![
271+
) -> StdResult<BTreeSet<SignedEntityTypeDiscriminants>> {
272+
let default_discriminants = BTreeSet::from([
272273
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
273274
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
274-
];
275+
]);
275276

276277
let mut all_discriminants = default_discriminants;
277278

@@ -280,9 +281,7 @@ impl Configuration {
280281
.split(',')
281282
.filter_map(|name| SignedEntityTypeDiscriminants::from_str(name.trim()).ok())
282283
{
283-
if !all_discriminants.contains(&discriminant) {
284-
all_discriminants.push(discriminant)
285-
}
284+
all_discriminants.insert(discriminant);
286285
}
287286

288287
Ok(all_discriminants)
@@ -538,10 +537,10 @@ mod test {
538537
.unwrap();
539538

540539
assert_eq!(
541-
vec![
540+
BTreeSet::from([
542541
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
543542
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
544-
],
543+
]),
545544
discriminants
546545
);
547546
}
@@ -559,10 +558,10 @@ mod test {
559558
.unwrap();
560559

561560
assert_eq!(
562-
vec![
561+
BTreeSet::from([
563562
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
564563
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
565-
],
564+
]),
566565
discriminants
567566
);
568567
}
@@ -583,10 +582,10 @@ mod test {
583582
.unwrap();
584583

585584
assert_eq!(
586-
vec![
585+
BTreeSet::from([
587586
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
588587
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
589-
],
588+
]),
590589
discriminants
591590
);
592591
}
@@ -604,12 +603,12 @@ mod test {
604603
.unwrap();
605604

606605
assert_eq!(
607-
vec![
606+
BTreeSet::from([
608607
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
609608
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
610609
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
611610
SignedEntityTypeDiscriminants::CardanoTransactions,
612-
],
611+
]),
613612
discriminants
614613
);
615614
}
@@ -630,11 +629,11 @@ mod test {
630629
.unwrap();
631630

632631
assert_eq!(
633-
vec![
632+
BTreeSet::from([
634633
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
635-
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
636634
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
637-
],
635+
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
636+
]),
638637
discriminants
639638
);
640639
}
@@ -657,8 +656,8 @@ mod test {
657656
assert_eq!(
658657
vec![
659658
SignedEntityType::MithrilStakeDistribution(beacon.epoch),
660-
SignedEntityType::CardanoImmutableFilesFull(beacon.clone()),
661659
SignedEntityType::CardanoStakeDistribution(beacon.epoch),
660+
SignedEntityType::CardanoImmutableFilesFull(beacon.clone()),
662661
SignedEntityType::CardanoTransactions(beacon.clone()),
663662
],
664663
signed_entity_types

mithril-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-common"
3-
version = "0.4.2"
3+
version = "0.4.3"
44
description = "Common types, interfaces, and utilities for Mithril nodes."
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-common/src/entities/signed_entity_type.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const ENTITY_TYPE_CARDANO_TRANSACTIONS: usize = 3;
2626
/// are identified by their discriminant (i.e. index in the enum), thus the
2727
/// modification of this type should only ever consist of appending new
2828
/// variants.
29+
// Important note: The order of the variants is important as it is used for the derived Ord trait.
2930
#[derive(Display, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, EnumDiscriminants)]
3031
#[strum(serialize_all = "PascalCase")]
3132
#[strum_discriminants(derive(EnumString, AsRefStr, Serialize, Deserialize, PartialOrd, Ord))]
@@ -162,4 +163,56 @@ impl SignedEntityTypeDiscriminants {
162163
}
163164

164165
#[cfg(test)]
165-
mod tests {}
166+
mod tests {
167+
use super::*;
168+
169+
// Expected ord:
170+
// MithrilStakeDistribution < CardanoStakeDistribution < CardanoImmutableFilesFull < CardanoTransactions
171+
#[test]
172+
fn ordering_discriminant() {
173+
let mut list = vec![
174+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
175+
SignedEntityTypeDiscriminants::CardanoTransactions,
176+
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
177+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
178+
];
179+
list.sort();
180+
181+
assert_eq!(
182+
list,
183+
vec![
184+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
185+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
186+
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
187+
SignedEntityTypeDiscriminants::CardanoTransactions,
188+
]
189+
);
190+
}
191+
192+
#[test]
193+
fn ordering_discriminant_with_duplicate() {
194+
let mut list = vec![
195+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
196+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
197+
SignedEntityTypeDiscriminants::CardanoTransactions,
198+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
199+
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
200+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
201+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
202+
];
203+
list.sort();
204+
205+
assert_eq!(
206+
list,
207+
vec![
208+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
209+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
210+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
211+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
212+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
213+
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
214+
SignedEntityTypeDiscriminants::CardanoTransactions,
215+
]
216+
);
217+
}
218+
}

0 commit comments

Comments
 (0)