diff --git a/state-chain/pallets/cf-elections/src/electoral_systems/blockchain/delta_based_ingress.rs b/state-chain/pallets/cf-elections/src/electoral_systems/blockchain/delta_based_ingress.rs index db8756ffd7..5fee91f195 100644 --- a/state-chain/pallets/cf-elections/src/electoral_systems/blockchain/delta_based_ingress.rs +++ b/state-chain/pallets/cf-elections/src/electoral_systems/blockchain/delta_based_ingress.rs @@ -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 @@ -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:?}"); diff --git a/state-chain/pallets/cf-elections/src/electoral_systems/mocks.rs b/state-chain/pallets/cf-elections/src/electoral_systems/mocks.rs index a41a4858ee..bd9c9828a6 100644 --- a/state-chain/pallets/cf-elections/src/electoral_systems/mocks.rs +++ b/state-chain/pallets/cf-elections/src/electoral_systems/mocks.rs @@ -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) -> ElectionIdentifierOf + 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(), diff --git a/state-chain/pallets/cf-elections/src/electoral_systems/tests/delta_based_ingress.rs b/state-chain/pallets/cf-elections/src/electoral_systems/tests/delta_based_ingress.rs index 9f67366aae..942e5ab7d6 100644 --- a/state-chain/pallets/cf-elections/src/electoral_systems/tests/delta_based_ingress.rs +++ b/state-chain/pallets/cf-elections/src/electoral_systems/tests/delta_based_ingress.rs @@ -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. @@ -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])), ], ) @@ -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 { @@ -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