Skip to content

Commit dc3e9a5

Browse files
committed
add SystemEpochInfoEvent utils
1 parent aa32600 commit dc3e9a5

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

crates/iota-core/src/authority.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4675,24 +4675,8 @@ impl AuthorityState {
46754675
.events
46764676
.data
46774677
.iter()
4678-
.find(|event| {
4679-
event.is_system_epoch_info_event_v1() || event.is_system_epoch_info_event_v2()
4680-
})
4681-
.map(|event| {
4682-
if event.is_system_epoch_info_event_v2() {
4683-
SystemEpochInfoEvent::V2(
4684-
bcs::from_bytes::<SystemEpochInfoEventV2>(&event.contents).expect(
4685-
"event deserialization should succeed as type was pre-validated",
4686-
),
4687-
)
4688-
} else {
4689-
SystemEpochInfoEvent::V1(
4690-
bcs::from_bytes::<SystemEpochInfoEventV1>(&event.contents).expect(
4691-
"event deserialization should succeed as type was pre-validated",
4692-
),
4693-
)
4694-
}
4695-
});
4678+
.find(|event| event.is_system_epoch_info_event())
4679+
.map(|event| SystemEpochInfoEvent::from(event));
46964680
// The system epoch info event can be `None` in case if the `advance_epoch`
46974681
// Move function call failed and was executed in the safe mode.
46984682
assert!(system_epoch_info_event.is_some() || system_obj.safe_mode());

crates/iota-core/src/checkpoints/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,14 +1452,8 @@ impl CheckpointBuilder {
14521452
// SAFETY: The number of minted and burnt tokens easily fit into an i64 and due
14531453
// to those small numbers, no overflows will occur during conversion or
14541454
// subtraction.
1455-
let epoch_supply_change = system_epoch_info_event.map_or(0, |event| match event {
1456-
SystemEpochInfoEvent::V1(event) => {
1457-
event.minted_tokens_amount as i64 - event.burnt_tokens_amount as i64
1458-
}
1459-
SystemEpochInfoEvent::V2(event) => {
1460-
event.minted_tokens_amount as i64 - event.burnt_tokens_amount as i64
1461-
}
1462-
});
1455+
let epoch_supply_change =
1456+
system_epoch_info_event.map_or(0, |event| event.supply_change());
14631457

14641458
let committee = system_state_obj
14651459
.get_current_epoch_committee()

crates/iota-types/src/event.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ impl Event {
149149
&& self.type_.module.as_ident_str() == ident_str!("iota_system_state_inner")
150150
&& self.type_.name.as_ident_str() == ident_str!("SystemEpochInfoEventV2")
151151
}
152+
153+
pub fn is_system_epoch_info_event(&self) -> bool {
154+
self.is_system_epoch_info_event_v1() || self.is_system_epoch_info_event_v2()
155+
}
152156
}
153157

154158
impl Event {
@@ -174,6 +178,35 @@ pub enum SystemEpochInfoEvent {
174178
V2(SystemEpochInfoEventV2),
175179
}
176180

181+
impl SystemEpochInfoEvent {
182+
pub fn supply_change(&self) -> i64 {
183+
match self {
184+
SystemEpochInfoEvent::V1(event) => {
185+
event.minted_tokens_amount as i64 - event.burnt_tokens_amount as i64
186+
}
187+
SystemEpochInfoEvent::V2(event) => {
188+
event.minted_tokens_amount as i64 - event.burnt_tokens_amount as i64
189+
}
190+
}
191+
}
192+
}
193+
194+
impl From<Event> for SystemEpochInfoEvent {
195+
fn from(event: Event) -> Self {
196+
if event.is_system_epoch_info_event_v2() {
197+
SystemEpochInfoEvent::V2(
198+
bcs::from_bytes::<SystemEpochInfoEventV2>(&event.contents)
199+
.expect("event deserialization should succeed as type was pre-validated"),
200+
)
201+
} else {
202+
SystemEpochInfoEvent::V1(
203+
bcs::from_bytes::<SystemEpochInfoEventV1>(&event.contents)
204+
.expect("event deserialization should succeed as type was pre-validated"),
205+
)
206+
}
207+
}
208+
}
209+
177210
/// Event emitted in move code `fun advance_epoch` in protocol versions 1 to 3
178211
#[derive(Deserialize)]
179212
pub struct SystemEpochInfoEventV1 {

0 commit comments

Comments
 (0)