Skip to content

Commit

Permalink
Merge pull request #43 from Magport/Magnet-Stack-toints
Browse files Browse the repository at this point in the history
merge from Magnet stack toints
  • Loading branch information
toints authored Sep 5, 2024
2 parents 3d01897 + 5135ad0 commit a47caa1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
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);
})
}
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
/// up by `pallet_aura` to implement `fn slot_duration()`.
///
/// Change this to adjust the block time.
pub const MILLISECS_PER_BLOCK: u64 = 6000;
pub const MILLISECS_PER_BLOCK: u64 = 12000;
pub const WEIGHT_MILLISECS_PER_BLOCK: u64 = 2000;

// NOTE: Currently it is not possible to change the slot duration after the chain has started.
Expand Down

0 comments on commit a47caa1

Please sign in to comment.