From ce57d6d356ef2f6015377d25ac93c8c3e8333af4 Mon Sep 17 00:00:00 2001 From: sulijia <984115358@qq.com> Date: Wed, 28 Aug 2024 19:24:47 +0800 Subject: [PATCH 1/3] When async backing is disabled, the block time should be set to 12s. --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 51d8f53..6c68881 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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. From 5cd03ab7ff360f57b3632df81621b536fdd898d8 Mon Sep 17 00:00:00 2001 From: sulijia <984115358@qq.com> Date: Mon, 2 Sep 2024 13:38:14 +0800 Subject: [PATCH 2/3] If BidThreshold is set to 0, the assurance function is disabled. --- pallets/assurance/src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pallets/assurance/src/lib.rs b/pallets/assurance/src/lib.rs index e0bb760..7fac343 100644 --- a/pallets/assurance/src/lib.rs +++ b/pallets/assurance/src/lib.rs @@ -107,9 +107,14 @@ pub mod pallet { } pub fn on_relaychain(blocknumber: u32) -> bool { + let bid_threshold = BidThreshold::::get(); + // If BidThreshold is set to 0, the assurance function is disabled. + if bid_threshold == 0 { + return false; + } let last_relay_block_number = RelaychainDataProvider::::current_relay_chain_state().number; - if blocknumber > BidThreshold::::get() + u32::from(last_relay_block_number) { + if blocknumber > bid_threshold + u32::from(last_relay_block_number) { return true; } From f2c777983d513b6848f49cbe39b223aa55a4f64b Mon Sep 17 00:00:00 2001 From: sulijia <984115358@qq.com> Date: Mon, 2 Sep 2024 15:09:21 +0800 Subject: [PATCH 3/3] Added assurance enable configuration, users can turn this function on and off --- pallets/assurance/src/lib.rs | 43 ++++++++++++++++++++++++++++------ pallets/assurance/src/mock.rs | 1 + pallets/assurance/src/tests.rs | 14 +++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/pallets/assurance/src/lib.rs b/pallets/assurance/src/lib.rs index 7fac343..0fdc705 100644 --- a/pallets/assurance/src/lib.rs +++ b/pallets/assurance/src/lib.rs @@ -46,6 +46,8 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { NewBidThreshold(u32), + EnableAssurance, + DisableAssurance, } #[pallet::error] @@ -54,19 +56,24 @@ pub mod pallet { #[pallet::genesis_config] pub struct GenesisConfig { pub bid_threshold: u32, + pub enable: bool, #[serde(skip)] pub _marker: PhantomData, } impl GenesisConfig { - 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 Default for GenesisConfig { fn default() -> Self { - Self { bid_threshold: T::DefaultBidThreshold::get(), _marker: PhantomData } + Self { + bid_threshold: T::DefaultBidThreshold::get(), + enable: Enable::::get(), + _marker: PhantomData, + } } } @@ -74,6 +81,7 @@ pub mod pallet { impl BuildGenesisConfig for GenesisConfig { fn build(&self) { >::put(self.bid_threshold); + >::put(self.enable); } } @@ -85,6 +93,14 @@ pub mod pallet { #[pallet::storage] pub type BidThreshold = StorageValue<_, u32, ValueQuery, DefaultBidThreshold>; + #[pallet::type_value] + pub fn DefaultEnable() -> bool { + true + } + + #[pallet::storage] + pub type Enable = StorageValue<_, bool, ValueQuery, DefaultEnable>; + #[pallet::hooks] impl Hooks> for Pallet {} @@ -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, enable: bool) -> DispatchResult { + ensure_root(origin)?; + >::put(enable); + if enable { + Self::deposit_event(Event::EnableAssurance); + } else { + Self::deposit_event(Event::DisableAssurance); + } + Ok(()) + } } impl Pallet { @@ -107,14 +136,14 @@ pub mod pallet { } pub fn on_relaychain(blocknumber: u32) -> bool { - let bid_threshold = BidThreshold::::get(); - // If BidThreshold is set to 0, the assurance function is disabled. - if bid_threshold == 0 { + let enable = Enable::::get(); + if !enable { return false; } let last_relay_block_number = RelaychainDataProvider::::current_relay_chain_state().number; - if blocknumber > bid_threshold + u32::from(last_relay_block_number) { + + if blocknumber > BidThreshold::::get() + u32::from(last_relay_block_number) { return true; } diff --git a/pallets/assurance/src/mock.rs b/pallets/assurance/src/mock.rs index 44f77c7..6ec6322 100644 --- a/pallets/assurance/src/mock.rs +++ b/pallets/assurance/src/mock.rs @@ -193,6 +193,7 @@ impl ExtBuilder { .unwrap(); pallet_assurance::GenesisConfig:: { bid_threshold: 8u32, + enable: true, _marker: Default::default(), } .assimilate_storage(&mut t) diff --git a/pallets/assurance/src/tests.rs b/pallets/assurance/src/tests.rs index 4798313..f3cae0e 100644 --- a/pallets/assurance/src/tests.rs +++ b/pallets/assurance/src/tests.rs @@ -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::::get(); + assert_eq!(default_enable, true); + assert_ok!(Assurance::set_enable_assurance(RuntimeOrigin::root(), false)); + expect_event(AssuranceEvent::DisableAssurance); + assert_eq!(pallet_assurance::Enable::::get(), false); + assert_ok!(Assurance::set_enable_assurance(RuntimeOrigin::root(), true)); + expect_event(AssuranceEvent::EnableAssurance); + assert_eq!(pallet_assurance::Enable::::get(), true); + }) +}