Skip to content

Commit 03db2eb

Browse files
committed
refactor: include CardanoDatabase in SignedEntityTypeMessagePart to avoid the use of panic!()
1 parent 9bda80f commit 03db2eb

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

mithril-common/src/messages/message_parts/signed_entity_type_message.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ pub enum SignedEntityTypeMessagePart {
4343
/// Full Cardano Immutable Files
4444
CardanoImmutableFilesFull(CardanoDbBeaconMessagePart),
4545

46+
/// Cardano Database
47+
CardanoDatabase(CardanoDbBeacon),
48+
4649
/// Cardano Transactions
4750
CardanoTransactions(Epoch, BlockNumber),
4851
}
@@ -110,6 +113,7 @@ impl From<SignedEntityTypeMessagePart> for SignedEntityType {
110113
SignedEntityTypeMessagePart::CardanoTransactions(epoch, block_number) => {
111114
Self::CardanoTransactions(epoch, block_number)
112115
}
116+
SignedEntityTypeMessagePart::CardanoDatabase(beacon) => Self::CardanoDatabase(beacon),
113117
}
114118
}
115119
}
@@ -133,6 +137,10 @@ impl PartialEq<SignedEntityTypeMessagePart> for SignedEntityType {
133137
&SignedEntityType::CardanoTransactions(left_epoch, left_block_number),
134138
&SignedEntityTypeMessagePart::CardanoTransactions(right_epoch, right_block_number),
135139
) => left_epoch == right_epoch && left_block_number == right_block_number,
140+
(
141+
&SignedEntityType::CardanoDatabase(left_beacon),
142+
&SignedEntityTypeMessagePart::CardanoDatabase(right_beacon),
143+
) => left_beacon == right_beacon,
136144
_ => false,
137145
}
138146
}
@@ -163,9 +171,7 @@ impl<N: Into<String>> From<(SignedEntityType, N)> for SignedEntityTypeMessagePar
163171
SignedEntityType::CardanoTransactions(epoch, block_number) => {
164172
Self::CardanoTransactions(epoch, block_number)
165173
}
166-
SignedEntityType::CardanoDatabase(_) => {
167-
panic!("This conversion must not be used, this enum is temporary and will be removed. Use the CardanoDbBeacon from SignedEntityType directly instead.")
168-
}
174+
SignedEntityType::CardanoDatabase(beacon) => Self::CardanoDatabase(beacon),
169175
}
170176
}
171177
}
@@ -235,6 +241,12 @@ mod tests {
235241
BlockNumber(4678)
236242
))
237243
);
244+
assert_eq!(
245+
SignedEntityType::CardanoDatabase(CardanoDbBeacon::new(98, 123)),
246+
SignedEntityType::from(SignedEntityTypeMessagePart::CardanoDatabase(
247+
CardanoDbBeacon::new(98, 123)
248+
))
249+
);
238250
}
239251

240252
#[test]
@@ -269,6 +281,13 @@ mod tests {
269281
"unused"
270282
))
271283
);
284+
assert_eq!(
285+
SignedEntityTypeMessagePart::CardanoDatabase(CardanoDbBeacon::new(98, 123)),
286+
SignedEntityTypeMessagePart::from((
287+
SignedEntityType::CardanoDatabase(CardanoDbBeacon::new(98, 123)),
288+
"unused"
289+
))
290+
);
272291
}
273292

274293
#[test]
@@ -378,5 +397,19 @@ mod tests {
378397
SignedEntityType::CardanoTransactions(epoch, block_number + 3),
379398
SignedEntityTypeMessagePart::CardanoTransactions(epoch, block_number),
380399
);
400+
401+
// CardanoDatabase
402+
assert_commutative_eq(
403+
SignedEntityType::CardanoDatabase(CardanoDbBeacon::new(*epoch, immutable)),
404+
SignedEntityTypeMessagePart::CardanoDatabase(CardanoDbBeacon::new(*epoch, immutable)),
405+
);
406+
assert_commutative_ne(
407+
SignedEntityType::CardanoDatabase(CardanoDbBeacon::new(*epoch + 1, immutable)),
408+
SignedEntityTypeMessagePart::CardanoDatabase(CardanoDbBeacon::new(*epoch, immutable)),
409+
);
410+
assert_commutative_ne(
411+
SignedEntityType::CardanoDatabase(CardanoDbBeacon::new(*epoch, immutable + 3)),
412+
SignedEntityTypeMessagePart::CardanoDatabase(CardanoDbBeacon::new(*epoch, immutable)),
413+
);
381414
}
382415
}

0 commit comments

Comments
 (0)