Skip to content

Commit

Permalink
Synchronize the modification of the ReferendumInfoFor state, especial…
Browse files Browse the repository at this point in the history
…ly when the proposal ends.
  • Loading branch information
SunTiebing committed Feb 10, 2025
1 parent cf9139f commit 7a7bffa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
29 changes: 21 additions & 8 deletions pallets/vtoken-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,11 @@ pub mod pallet {
T::ControlOrigin::ensure_origin(origin)?;
Self::ensure_vtoken(&vtoken)?;

if new_status.is_over() {
let current_block_number = Self::get_agent_block_number(&vtoken)?;
Self::over_referendum_info_for(vtoken, poll_index, current_block_number);
}

// Update the referendum status in storage
ReferendumVoteStatusStore::<T>::insert(vtoken, poll_index, new_status.clone());
Self::deposit_event(Event::<T>::ReferendumStatusUpdated {
Expand Down Expand Up @@ -1652,18 +1657,26 @@ pub mod pallet {
ReferendumVoteStatusStore::<T>::insert(vtoken, poll_index, status);
}

ReferendumInfoFor::<T>::mutate(vtoken, poll_index, |maybe_info| match maybe_info {
Some(info) => {
if let ReferendumInfo::Ongoing(_) = info {
*info = ReferendumInfo::Completed(current_block_number);
}
}
None => {}
});
Self::over_referendum_info_for(vtoken, *poll_index, current_block_number);
}
ReferendumTimeoutV3::<T>::remove(vtoken, time_out_block_number);
}

fn over_referendum_info_for(
vtoken: CurrencyId,
poll_index: PollIndex,
current_block_number: BlockNumberFor<T>,
) {
ReferendumInfoFor::<T>::mutate(vtoken, poll_index, |maybe_info| match maybe_info {
Some(info) => {
if let ReferendumInfo::Ongoing(_) = info {
*info = ReferendumInfo::Completed(current_block_number);
}
}
None => {}
});
}

/// This function checks whether the user's tokens can be unlocked early based on their vote status
/// and the referendum result. It returns `true` if the user's vote is opposite to the referendum result,
/// indicating that early unlock is allowed.
Expand Down
11 changes: 11 additions & 0 deletions pallets/vtoken-voting/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,14 @@ impl Default for ReferendumVoteStatus {
ReferendumVoteStatus::Ongoing
}
}

impl ReferendumVoteStatus {
pub(crate) fn is_over(&self) -> bool {
match self {
ReferendumVoteStatus::Approved => true,
ReferendumVoteStatus::Rejected => true,
ReferendumVoteStatus::None => true,
ReferendumVoteStatus::Ongoing => false,
}
}
}

0 comments on commit 7a7bffa

Please sign in to comment.