Skip to content

Commit

Permalink
Add remove_announcement
Browse files Browse the repository at this point in the history
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro committed Oct 4, 2021
1 parent 4be3a5d commit 27b8f54
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions frame/alliance/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ fn ally1<T: Config<I>, I: 'static>() -> T::AccountId {
funded_account::<T, I>("ally", 1)
}

fn set_announcement<T: Config<I>, I: 'static>(cid: Cid) {
Announcements::<T, I>::put(vec![cid]);
}

fn set_candidates<T: Config<I>, I: 'static>() {
let candidates = vec![funded_account::<T, I>("candidate", 1)];
candidates.iter().for_each(|who| {
Expand Down Expand Up @@ -143,6 +147,19 @@ benchmarks_instance_pallet! {
assert_last_event::<T, I>(Event::NewAnnouncement(announcement).into());
}

remove_announcement {
set_members::<T, I>();
let announcement = test_cid();
set_announcement::<T, I>(announcement);

let call = Call::<T, I>::remove_announcement { announcement };
let origin = T::SuperMajorityOrigin::successful_origin();
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert!(Alliance::<T, I>::announcements().is_empty());
assert_last_event::<T, I>(Event::AnnouncementRemoved(announcement).into());
}

submit_candidacy {
set_members::<T, I>();

Expand Down
17 changes: 17 additions & 0 deletions frame/alliance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ pub mod pallet {
MissingProposalHash,
/// The proposal is not vetoable.
NotVetoableProposal,
/// The Announcement is not found.
MissingAnnouncement,
}

#[pallet::event]
Expand All @@ -294,6 +296,8 @@ pub mod pallet {
NewRule(Cid),
/// A new announcement has been proposed. \[announcement\]
NewAnnouncement(Cid),
/// A on-chain announcement has been removed. \[announcement\]
AnnouncementRemoved(Cid),
/// Some accounts have been initialized to founders. \[founders\]
FoundersInitialized(Vec<T::AccountId>),
/// An account has been added as a candidate and lock its deposit. \[candidate, nominator,
Expand Down Expand Up @@ -571,6 +575,19 @@ pub mod pallet {
Ok(().into())
}

#[pallet::weight(0)]
pub fn remove_announcement(origin: OriginFor<T>, announcement: Cid) -> DispatchResultWithPostInfo {
T::SuperMajorityOrigin::ensure_origin(origin)?;

let mut announcements = <Announcements<T, I>>::get();
let pos = announcements.binary_search(&announcement).ok().ok_or(Error::<T, I>::MissingAnnouncement)?;
announcements.remove(pos);
<Announcements<T, I>>::put(announcements);

Self::deposit_event(Event::AnnouncementRemoved(announcement));
Ok(().into())
}

/// Submit oneself for candidacy.
/// Account must have enough transferable funds in it to pay the candidate deposit.
#[pallet::weight(0)]
Expand Down
2 changes: 2 additions & 0 deletions frame/alliance/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ ord_parameter_types! {
parameter_types! {
pub const CandidateDeposit: u64 = 25;
pub const MaxBlacklistCount: u32 = 100;
pub const MaxWebsiteUrlLength: u32 = 255;
}
impl Config for Test {
type Event = Event;
Expand All @@ -165,6 +166,7 @@ impl Config for Test {
type IdentityVerifier = AllianceIdentityVerifier;
type ProposalProvider = AllianceProposalProvider;
type MaxBlacklistCount = MaxBlacklistCount;
type MaxWebsiteUrlLength = MaxWebsiteUrlLength;
type CandidateDeposit = CandidateDeposit;
}

Expand Down
16 changes: 16 additions & 0 deletions frame/alliance/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ fn announce_works() {
});
}

#[test]
fn remove_announcement_works() {
new_test_ext().execute_with(|| {
let cid = test_cid();
assert_ok!(Alliance::announce(Origin::signed(1), cid));
assert_eq!(Alliance::announcements(), vec![cid]);
System::assert_last_event(mock::Event::Alliance(crate::Event::NewAnnouncement(cid)));

System::set_block_number(2);

assert_ok!(Alliance::remove_announcement(Origin::signed(1), cid));
assert_eq!(Alliance::announcements(), vec![]);
System::assert_last_event(mock::Event::Alliance(crate::Event::AnnouncementRemoved(cid)));
});
}

#[test]
fn submit_candidacy_works() {
new_test_ext().execute_with(|| {
Expand Down

0 comments on commit 27b8f54

Please sign in to comment.