Skip to content

Commit

Permalink
feat: refresh solana elections instead of recreate (#5629)
Browse files Browse the repository at this point in the history
feat: refresh solana ingress elections instead of recreate.
  • Loading branch information
MxmUrw authored Feb 11, 2025
1 parent a4e7ac6 commit f5aff96
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ where
}
}

let election_access = ElectoralAccess::election_mut(election_identifier);
let mut election_access = ElectoralAccess::election_mut(election_identifier);

if new_properties.is_empty() {
// Note: it's possible that there are still some remaining pending totals, but if
Expand All @@ -340,11 +340,15 @@ where
election_access.delete();
} else if new_properties != properties {
log::debug!("recreate delta based ingress election: recreate since properties changed from: {properties:?}, to: {new_properties:?}");
election_access.delete();
ElectoralAccess::new_election(
Default::default(),

election_access.clear_votes();
election_access.set_state(new_pending_ingress_totals)?;
election_access.refresh(
election_identifier
.extra()
.checked_add(1)
.ok_or_else(CorruptStorageError::new)?,
new_properties,
new_pending_ingress_totals,
)?;
} else {
log::debug!("recreate delta based ingress election: keeping old because properties didn't change: {properties:?}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ register_checks! {
"Expected the election id to be incremented.",
);
},
election_id_updated_by(pre_finalize, post_finalize, update: impl Fn(ElectionIdentifierOf<ES>) -> ElectionIdentifierOf<ES> + Clone + 'static) {
assert_eq!(
update(pre_finalize.election_identifiers[0]),
post_finalize.election_identifiers[0],
"Expected the election id to be updated by given `update` function.",
);
},
all_elections_deleted(pre_finalize, post_finalize) {
assert!(
!pre_finalize.election_identifiers.is_empty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,15 @@ mod channel_closure {
vec![
Check::channel_not_closed(DEFAULT_CHANNEL_ACCOUNT),
Check::ingressed(vec![(DEFAULT_CHANNEL_ACCOUNT, Asset::Sol, DEPOSIT_AMOUNT)]),
Check::election_id_incremented(),
Check::election_id_updated_by(|id| {
ElectionIdentifier::new(*id.unique_monotonic(), id.extra() + 1)
}),
// Channel state not cleaned up yet, since the channel is not yet closed.
Check::ended_at_state_map_state([DepositChannel {
total_ingressed: DEPOSIT_AMOUNT,
block_number: DEPOSIT_BLOCK,
..DEFAULT_CHANNEL
}]),
],
)
// Chain tracking reaches close block, channel is closed.
Expand Down Expand Up @@ -958,7 +966,9 @@ fn pending_ingresses_update_with_consensus() {
deposit_channel_pending.asset,
deposit_channel_pending.total_ingressed,
)]),
Check::election_id_incremented(),
Check::election_id_updated_by(|id| {
ElectionIdentifier::new(*id.unique_monotonic(), id.extra() + 1)
}),
Check::ended_at_state(to_state(vec![deposit_channel_with_next_deposit])),
],
)
Expand Down Expand Up @@ -1035,7 +1045,12 @@ mod multiple_deposits {
.test_on_finalize(
&{ TOTAL_1.block_number - 1 },
|_| {},
[Check::ingressed(vec![]), Check::election_id_incremented()],
[
Check::ingressed(vec![]),
Check::election_id_updated_by(|id| {
ElectionIdentifier::new(*id.unique_monotonic(), id.extra() + 1)
}),
],
)
// Simulate a second deposit at a later block.
.force_consensus_update(ConsensusStatus::Gained {
Expand All @@ -1049,7 +1064,9 @@ mod multiple_deposits {
|_| {},
[
Check::ingressed(vec![(DEPOSIT_ADDRESS, Asset::Sol, TOTAL_1.amount)]),
Check::election_id_incremented(),
Check::election_id_updated_by(|id| {
ElectionIdentifier::new(*id.unique_monotonic(), id.extra() + 1)
}),
],
)
// Finalize with chain tracking at the block of the second deposit. Both should be
Expand Down

0 comments on commit f5aff96

Please sign in to comment.