Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added assurance enable configuration, users can turn this function on and off #148

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions pallets/assurance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub mod pallet {
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
NewBidThreshold(u32),
EnableAssurance,
DisableAssurance,
}

#[pallet::error]
Expand All @@ -54,26 +56,32 @@ pub mod pallet {
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub bid_threshold: u32,
pub enable: bool,
#[serde(skip)]
pub _marker: PhantomData<T>,
}

impl<T: Config> GenesisConfig<T> {
pub fn new(bid_threshold: u32) -> Self {
Self { bid_threshold, _marker: PhantomData }
pub fn new(bid_threshold: u32, enable: bool) -> Self {
Self { bid_threshold, enable, _marker: PhantomData }
}
}

impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { bid_threshold: T::DefaultBidThreshold::get(), _marker: PhantomData }
Self {
bid_threshold: T::DefaultBidThreshold::get(),
enable: Enable::<T>::get(),
_marker: PhantomData,
}
}
}

#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
<BidThreshold<T>>::put(self.bid_threshold);
<Enable<T>>::put(self.enable);
}
}

Expand All @@ -85,6 +93,14 @@ pub mod pallet {
#[pallet::storage]
pub type BidThreshold<T> = StorageValue<_, u32, ValueQuery, DefaultBidThreshold<T>>;

#[pallet::type_value]
pub fn DefaultEnable<T: Config>() -> bool {
true
}

#[pallet::storage]
pub type Enable<T> = StorageValue<_, bool, ValueQuery, DefaultEnable<T>>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

Expand All @@ -98,6 +114,19 @@ pub mod pallet {
Self::deposit_event(Event::NewBidThreshold(blocknumber));
Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn set_enable_assurance(origin: OriginFor<T>, enable: bool) -> DispatchResult {
ensure_root(origin)?;
<Enable<T>>::put(enable);
if enable {
Self::deposit_event(Event::EnableAssurance);
} else {
Self::deposit_event(Event::DisableAssurance);
}
Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand All @@ -107,8 +136,13 @@ pub mod pallet {
}

pub fn on_relaychain(blocknumber: u32) -> bool {
let enable = Enable::<T>::get();
if !enable {
return false;
}
let last_relay_block_number =
RelaychainDataProvider::<T>::current_relay_chain_state().number;

if blocknumber > BidThreshold::<T>::get() + u32::from(last_relay_block_number) {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions pallets/assurance/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl ExtBuilder {
.unwrap();
pallet_assurance::GenesisConfig::<Test> {
bid_threshold: 8u32,
enable: true,
_marker: Default::default(),
}
.assimilate_storage(&mut t)
Expand Down
14 changes: 14 additions & 0 deletions pallets/assurance/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ fn on_relaychain_works() {
assert_eq!(on_relay_return, true);
})
}

#[test]
fn set_enable_works() {
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
let default_enable = pallet_assurance::Enable::<Test>::get();
assert_eq!(default_enable, true);
assert_ok!(Assurance::set_enable_assurance(RuntimeOrigin::root(), false));
expect_event(AssuranceEvent::DisableAssurance);
assert_eq!(pallet_assurance::Enable::<Test>::get(), false);
assert_ok!(Assurance::set_enable_assurance(RuntimeOrigin::root(), true));
expect_event(AssuranceEvent::EnableAssurance);
assert_eq!(pallet_assurance::Enable::<Test>::get(), true);
})
}
Loading