From 5ac97f2cf0c6807b09b6a0f358054fe1a7500285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Tue, 30 Aug 2022 16:13:05 +0200 Subject: [PATCH 01/33] Setting up AlephBFT version --- bin/runtime/src/lib.rs | 4 ++++ pallets/aleph/src/lib.rs | 18 ++++++++++++++++++ primitives/src/lib.rs | 1 + 3 files changed, 23 insertions(+) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 8ca6f3b84f..f0232322c6 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -895,6 +895,10 @@ impl_runtime_apis! { Aleph::queued_emergency_finalizer(), )) } + + fn aleph_bft_version() -> Vec { + Aleph::aleph_bft_version() + } } impl pallet_contracts_rpc_runtime_api::ContractsApi for Runtime { diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 6f41fe00af..6118fdbd36 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -44,6 +44,7 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), + ChangeAlephBFTVersion(Vec), } #[pallet::pallet] @@ -93,6 +94,10 @@ pub mod pallet { #[pallet::storage] type NextEmergencyFinalizer = StorageValue<_, T::AuthorityId, OptionQuery>; + #[pallet::storage] + #[pallet::getter(fn aleph_bft_version)] + pub(super) type AlephBFTVersion = StorageValue<_, Vec, ValueQuery>; + impl Pallet { pub(crate) fn initialize_authorities(authorities: &[T::AuthorityId]) { if !authorities.is_empty() { @@ -121,6 +126,10 @@ pub mod pallet { pub(crate) fn set_next_emergency_finalizer(emergency_finalizer: T::AuthorityId) { >::put(emergency_finalizer); } + + pub(crate) fn update_aleph_bft_version(version: Vec) { + >::put(version); + } } #[pallet::call] @@ -137,6 +146,15 @@ pub mod pallet { Self::deposit_event(Event::ChangeEmergencyFinalizer(emergency_finalizer)); Ok(()) } + + // TODO: verify weight + #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] + pub fn set_aleph_bft_version(origin: OriginFor, version: Vec) -> DispatchResult { + ensure_root(origin)?; + Self::update_aleph_bft_version(version.clone()); + Self::deposit_event(Event::ChangeAlephBFTVersion(version)); + Ok(()) + } } impl BoundToRuntimeAppPublic for Pallet { diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 93493d5d9e..61731719cd 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -133,6 +133,7 @@ sp_api::decl_runtime_apis! { fn authority_data() -> SessionAuthorityData; fn session_period() -> u32; fn millisecs_per_block() -> u64; + fn aleph_bft_version() -> Vec; } } From 459d1458092452ca2b99358d867b445e13c0f29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Wed, 31 Aug 2022 22:31:44 +0200 Subject: [PATCH 02/33] Changed handling to map from session to version --- bin/runtime/src/lib.rs | 9 +++++---- pallets/aleph/src/lib.rs | 22 +++++++++++++++------- primitives/src/lib.rs | 10 +++++++++- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index f0232322c6..980f8a2034 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -35,8 +35,9 @@ use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustm pub use primitives::Balance; use primitives::{ staking::MAX_NOMINATORS_REWARDED_PER_VALIDATOR, wrap_methods, ApiError as AlephApiError, - AuthorityId as AlephId, SessionAuthorityData, ADDRESSES_ENCODING, DEFAULT_SESSIONS_PER_ERA, - DEFAULT_SESSION_PERIOD, MILLISECS_PER_BLOCK, TOKEN, + AuthorityId as AlephId, SessionAuthorityData, SessionIndex, Version as AlephBFTVersion, + ADDRESSES_ENCODING, DEFAULT_SESSIONS_PER_ERA, DEFAULT_SESSION_PERIOD, MILLISECS_PER_BLOCK, + TOKEN, }; use sp_api::impl_runtime_apis; use sp_consensus_aura::{sr25519::AuthorityId as AuraId, SlotDuration}; @@ -896,8 +897,8 @@ impl_runtime_apis! { )) } - fn aleph_bft_version() -> Vec { - Aleph::aleph_bft_version() + fn aleph_bft_version(session: SessionIndex) -> Result { + Aleph::aleph_bft_version(session).ok_or(AlephApiError::VersionNotSet) } } diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 6118fdbd36..b208ec1c76 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -18,6 +18,7 @@ use frame_support::{ traits::{OneSessionHandler, StorageVersion}, }; pub use pallet::*; +use primitives::{SessionIndex, Version}; use sp_std::prelude::*; /// The current storage version. @@ -44,7 +45,7 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), - ChangeAlephBFTVersion(Vec), + ChangeAlephBFTVersion(Version), } #[pallet::pallet] @@ -96,7 +97,8 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn aleph_bft_version)] - pub(super) type AlephBFTVersion = StorageValue<_, Vec, ValueQuery>; + pub(super) type AlephBFTVersion = + StorageMap<_, Twox64Concat, SessionIndex, Version, OptionQuery>; impl Pallet { pub(crate) fn initialize_authorities(authorities: &[T::AuthorityId]) { @@ -127,8 +129,11 @@ pub mod pallet { >::put(emergency_finalizer); } - pub(crate) fn update_aleph_bft_version(version: Vec) { - >::put(version); + pub(crate) fn update_aleph_bft_version_for_session( + session: SessionIndex, + version: Version, + ) { + >::set(session, Some(version)); } } @@ -147,11 +152,14 @@ pub mod pallet { Ok(()) } - // TODO: verify weight #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] - pub fn set_aleph_bft_version(origin: OriginFor, version: Vec) -> DispatchResult { + pub fn set_aleph_bft_version_for_session( + origin: OriginFor, + session: SessionIndex, + version: Version, + ) -> DispatchResult { ensure_root(origin)?; - Self::update_aleph_bft_version(version.clone()); + Self::update_aleph_bft_version_for_session(session, version.clone()); Self::deposit_event(Event::ChangeAlephBFTVersion(version)); Ok(()) } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 61731719cd..87fdf9035e 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -98,6 +98,7 @@ impl Default for EraValidators { #[derive(Encode, Decode, PartialEq, Eq, Debug)] pub enum ApiError { DecodeKey, + VersionNotSet, } /// All the data needed to verify block finalization justifications. @@ -124,6 +125,13 @@ impl SessionAuthorityData { } } +/// Current/legacy version identifier. +#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] +pub enum Version { + Legacy, + Current, +} + sp_api::decl_runtime_apis! { pub trait AlephSessionApi { @@ -133,7 +141,7 @@ sp_api::decl_runtime_apis! { fn authority_data() -> SessionAuthorityData; fn session_period() -> u32; fn millisecs_per_block() -> u64; - fn aleph_bft_version() -> Vec; + fn aleph_bft_version(session: SessionIndex) -> Result; } } From ac02944b7b02c6b50c66e8f96f9c30dfdf9b1396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Thu, 1 Sep 2022 15:26:47 +0200 Subject: [PATCH 03/33] Changed logic to handle serving last set AlephBFT version --- bin/runtime/src/lib.rs | 22 ++++++++++++++++++++-- pallets/aleph/src/lib.rs | 21 +++++++++++++++++++-- primitives/src/lib.rs | 19 ++++++++++++++++--- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 980f8a2034..030c704579 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -897,8 +897,26 @@ impl_runtime_apis! { )) } - fn aleph_bft_version(session: SessionIndex) -> Result { - Aleph::aleph_bft_version(session).ok_or(AlephApiError::VersionNotSet) + // Read the last AlephBFT version set before or on a specified session. The specified + // session may be in the future relative to the current session. If the specified session + // is not in the future, the scanning is performed backwards. + fn aleph_bft_version(session: SessionIndex) -> AlephBFTVersion { + let current_version = Aleph::current_aleph_bft_version(); + let session_when_current_version_set = current_version.session_when_set; + + // Prevents potentially long loops for session indices far ahead of the current session. + if session > session_when_current_version_set { + return current_version.version + }; + + let mut version = Aleph::aleph_bft_version(session); + let mut idx = session; + + while version.is_none() && idx != 0 { + idx -= 1; + version = Aleph::aleph_bft_version(idx); + } + version.unwrap_or(AlephBFTVersion::Legacy) } } diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index b208ec1c76..dc88144dad 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -18,7 +18,7 @@ use frame_support::{ traits::{OneSessionHandler, StorageVersion}, }; pub use pallet::*; -use primitives::{SessionIndex, Version}; +use primitives::{CurrentVersion, SessionIndex, Version}; use sp_std::prelude::*; /// The current storage version. @@ -100,6 +100,10 @@ pub mod pallet { pub(super) type AlephBFTVersion = StorageMap<_, Twox64Concat, SessionIndex, Version, OptionQuery>; + #[pallet::storage] + #[pallet::getter(fn current_aleph_bft_version)] + pub(super) type CurrentAlephBFTVersion = StorageValue<_, CurrentVersion, ValueQuery>; + impl Pallet { pub(crate) fn initialize_authorities(authorities: &[T::AuthorityId]) { if !authorities.is_empty() { @@ -129,11 +133,24 @@ pub mod pallet { >::put(emergency_finalizer); } + pub(crate) fn update_current_aleph_bft_version(session: SessionIndex, version: Version) { + let current_version = >::get(); + let session_when_current_version_set = current_version.session_when_set; + if session > session_when_current_version_set { + let updated_current_version = CurrentVersion { + session_when_set: session, + version, + }; + >::put(updated_current_version); + } + } + pub(crate) fn update_aleph_bft_version_for_session( session: SessionIndex, version: Version, ) { - >::set(session, Some(version)); + >::set(session, Some(version.clone())); + Self::update_current_aleph_bft_version(session, version); } } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 87fdf9035e..16ae7d60db 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -98,7 +98,6 @@ impl Default for EraValidators { #[derive(Encode, Decode, PartialEq, Eq, Debug)] pub enum ApiError { DecodeKey, - VersionNotSet, } /// All the data needed to verify block finalization justifications. @@ -125,13 +124,27 @@ impl SessionAuthorityData { } } -/// Current/legacy version identifier. #[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] pub enum Version { Legacy, Current, } +#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] +pub struct CurrentVersion { + pub session_when_set: SessionIndex, + pub version: Version, +} + +impl Default for CurrentVersion { + fn default() -> Self { + Self { + session_when_set: 0, + version: Version::Legacy, + } + } +} + sp_api::decl_runtime_apis! { pub trait AlephSessionApi { @@ -141,7 +154,7 @@ sp_api::decl_runtime_apis! { fn authority_data() -> SessionAuthorityData; fn session_period() -> u32; fn millisecs_per_block() -> u64; - fn aleph_bft_version(session: SessionIndex) -> Result; + fn aleph_bft_version(session: SessionIndex) -> Version; } } From c707921fd532283d720790d9f4fbfc6c95828137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Mon, 5 Sep 2022 19:33:13 +0200 Subject: [PATCH 04/33] Changed logic to handle setting of version in future only --- bin/runtime/src/lib.rs | 26 ++++++------------- pallets/aleph/src/lib.rs | 54 +++++++++++++++++++++++----------------- primitives/src/lib.rs | 8 +++--- 3 files changed, 43 insertions(+), 45 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 030c704579..a3e950a3cd 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -897,26 +897,16 @@ impl_runtime_apis! { )) } - // Read the last AlephBFT version set before or on a specified session. The specified - // session may be in the future relative to the current session. If the specified session - // is not in the future, the scanning is performed backwards. + // Read the last AlephBFT version set on a specified session or before with scanning + // performed backwards. The specified session may be in the future relative to the current + // session. fn aleph_bft_version(session: SessionIndex) -> AlephBFTVersion { - let current_version = Aleph::current_aleph_bft_version(); - let session_when_current_version_set = current_version.session_when_set; - - // Prevents potentially long loops for session indices far ahead of the current session. - if session > session_when_current_version_set { - return current_version.version - }; - - let mut version = Aleph::aleph_bft_version(session); - let mut idx = session; - - while version.is_none() && idx != 0 { - idx -= 1; - version = Aleph::aleph_bft_version(idx); + for idx in (0..session + 1).rev() { + if let Some(version) = Aleph::aleph_bft_version(idx) { + return version + } } - version.unwrap_or(AlephBFTVersion::Legacy) + AlephBFTVersion::Legacy } } diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index dc88144dad..6731db6aaa 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -18,7 +18,7 @@ use frame_support::{ traits::{OneSessionHandler, StorageVersion}, }; pub use pallet::*; -use primitives::{CurrentVersion, SessionIndex, Version}; +use primitives::{SessionIndex, Version, VersionChange}; use sp_std::prelude::*; /// The current storage version. @@ -45,7 +45,7 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), - ChangeAlephBFTVersion(Version), + ChangeAlephBFTVersion(VersionChange), } #[pallet::pallet] @@ -101,8 +101,8 @@ pub mod pallet { StorageMap<_, Twox64Concat, SessionIndex, Version, OptionQuery>; #[pallet::storage] - #[pallet::getter(fn current_aleph_bft_version)] - pub(super) type CurrentAlephBFTVersion = StorageValue<_, CurrentVersion, ValueQuery>; + #[pallet::getter(fn aleph_bft_version_change)] + pub(super) type AlephBFTVersionChange = StorageValue<_, VersionChange, ValueQuery>; impl Pallet { pub(crate) fn initialize_authorities(authorities: &[T::AuthorityId]) { @@ -133,24 +133,28 @@ pub mod pallet { >::put(emergency_finalizer); } - pub(crate) fn update_current_aleph_bft_version(session: SessionIndex, version: Version) { - let current_version = >::get(); - let session_when_current_version_set = current_version.session_when_set; - if session > session_when_current_version_set { - let updated_current_version = CurrentVersion { - session_when_set: session, - version, - }; - >::put(updated_current_version); + pub(crate) fn set_next_aleph_bft_version( + next_version_change: VersionChange, + current_session: SessionIndex, + ) { + let session_to_set = next_version_change.session; + let version_to_set = next_version_change.version.clone(); + assert!( + session_to_set > current_session, + "Can only set AlephBFT version for future sessions!" + ); + + let previous_version_change = >::get(); + let previous_version_change_session = previous_version_change.session; + + // If a new version is scheduled before the session of the previous version change, + // undo the previous version change. + if current_session < previous_version_change_session { + >::set(previous_version_change_session, None); } - } - pub(crate) fn update_aleph_bft_version_for_session( - session: SessionIndex, - version: Version, - ) { - >::set(session, Some(version.clone())); - Self::update_current_aleph_bft_version(session, version); + >::put(next_version_change); + >::set(session_to_set, Some(version_to_set)); } } @@ -169,15 +173,19 @@ pub mod pallet { Ok(()) } + /// Sets AlephBFT version for a future session. If such a feature version is already set, + /// it is replaced with the specified one. #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] - pub fn set_aleph_bft_version_for_session( + pub fn set_aleph_bft_version( origin: OriginFor, session: SessionIndex, + current_session: SessionIndex, version: Version, ) -> DispatchResult { ensure_root(origin)?; - Self::update_aleph_bft_version_for_session(session, version.clone()); - Self::deposit_event(Event::ChangeAlephBFTVersion(version)); + let version_change = VersionChange { session, version }; + Self::set_next_aleph_bft_version(version_change.clone(), current_session); + Self::deposit_event(Event::ChangeAlephBFTVersion(version_change)); Ok(()) } } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 16ae7d60db..ff838cf4b0 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -131,15 +131,15 @@ pub enum Version { } #[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] -pub struct CurrentVersion { - pub session_when_set: SessionIndex, +pub struct VersionChange { + pub session: SessionIndex, pub version: Version, } -impl Default for CurrentVersion { +impl Default for VersionChange { fn default() -> Self { Self { - session_when_set: 0, + session: 0, version: Version::Legacy, } } From afa232be488e6ef4abc79c25c1fa7936f2aef87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Tue, 6 Sep 2022 13:22:49 +0200 Subject: [PATCH 05/33] Reworked storage and accessors to only store one version plus session to change it --- bin/runtime/src/lib.rs | 18 +++++----------- pallets/aleph/src/lib.rs | 45 ++++++++++++++-------------------------- primitives/src/lib.rs | 8 ++++--- 3 files changed, 25 insertions(+), 46 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index a3e950a3cd..713447fd71 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -35,9 +35,8 @@ use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustm pub use primitives::Balance; use primitives::{ staking::MAX_NOMINATORS_REWARDED_PER_VALIDATOR, wrap_methods, ApiError as AlephApiError, - AuthorityId as AlephId, SessionAuthorityData, SessionIndex, Version as AlephBFTVersion, - ADDRESSES_ENCODING, DEFAULT_SESSIONS_PER_ERA, DEFAULT_SESSION_PERIOD, MILLISECS_PER_BLOCK, - TOKEN, + AuthorityId as AlephId, SessionAuthorityData, Version as AlephBFTVersion, ADDRESSES_ENCODING, + DEFAULT_SESSIONS_PER_ERA, DEFAULT_SESSION_PERIOD, MILLISECS_PER_BLOCK, TOKEN, }; use sp_api::impl_runtime_apis; use sp_consensus_aura::{sr25519::AuthorityId as AuraId, SlotDuration}; @@ -897,16 +896,9 @@ impl_runtime_apis! { )) } - // Read the last AlephBFT version set on a specified session or before with scanning - // performed backwards. The specified session may be in the future relative to the current - // session. - fn aleph_bft_version(session: SessionIndex) -> AlephBFTVersion { - for idx in (0..session + 1).rev() { - if let Some(version) = Aleph::aleph_bft_version(idx) { - return version - } - } - AlephBFTVersion::Legacy + fn aleph_bft_version() -> AlephBFTVersion { + let version_change = Aleph::aleph_bft_version(); + version_change.version } } diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 6731db6aaa..f61d20af39 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -97,12 +97,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn aleph_bft_version)] - pub(super) type AlephBFTVersion = - StorageMap<_, Twox64Concat, SessionIndex, Version, OptionQuery>; - - #[pallet::storage] - #[pallet::getter(fn aleph_bft_version_change)] - pub(super) type AlephBFTVersionChange = StorageValue<_, VersionChange, ValueQuery>; + pub(super) type AlephBFTVersion = StorageValue<_, VersionChange, ValueQuery>; impl Pallet { pub(crate) fn initialize_authorities(authorities: &[T::AuthorityId]) { @@ -133,28 +128,16 @@ pub mod pallet { >::put(emergency_finalizer); } - pub(crate) fn set_next_aleph_bft_version( - next_version_change: VersionChange, - current_session: SessionIndex, - ) { - let session_to_set = next_version_change.session; - let version_to_set = next_version_change.version.clone(); - assert!( - session_to_set > current_session, - "Can only set AlephBFT version for future sessions!" - ); - - let previous_version_change = >::get(); - let previous_version_change_session = previous_version_change.session; + pub(crate) fn set_next_aleph_bft_version(version_change: VersionChange) { + let previous_version_change = >::get(); + let previous_session = previous_version_change.session_change; - // If a new version is scheduled before the session of the previous version change, - // undo the previous version change. - if current_session < previous_version_change_session { - >::set(previous_version_change_session, None); - } + let session = version_change.session_change; - >::put(next_version_change); - >::set(session_to_set, Some(version_to_set)); + assert!( + session > previous_session, + "Cannot set AlephBFT version for sessions prior to the current version! Session for current version: {}, attempted to set: {}.", previous_session, session); + >::put(version_change); } } @@ -178,13 +161,15 @@ pub mod pallet { #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] pub fn set_aleph_bft_version( origin: OriginFor, - session: SessionIndex, - current_session: SessionIndex, version: Version, + session_change: SessionIndex, ) -> DispatchResult { ensure_root(origin)?; - let version_change = VersionChange { session, version }; - Self::set_next_aleph_bft_version(version_change.clone(), current_session); + let version_change = VersionChange { + version, + session_change, + }; + Self::set_next_aleph_bft_version(version_change.clone()); Self::deposit_event(Event::ChangeAlephBFTVersion(version_change)); Ok(()) } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index ff838cf4b0..2ffcbfad12 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -132,15 +132,17 @@ pub enum Version { #[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] pub struct VersionChange { - pub session: SessionIndex, pub version: Version, + pub session_change: SessionIndex, } +// At genesis, the session of the next change of the AlephBFT version is unknown. Assume placeholder +// value of 0. impl Default for VersionChange { fn default() -> Self { Self { - session: 0, version: Version::Legacy, + session_change: 0, } } } @@ -154,7 +156,7 @@ sp_api::decl_runtime_apis! { fn authority_data() -> SessionAuthorityData; fn session_period() -> u32; fn millisecs_per_block() -> u64; - fn aleph_bft_version(session: SessionIndex) -> Version; + fn aleph_bft_version() -> Version; } } From 357e2d97672ad10c16c0a9987b55698ebc0fa93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Wed, 7 Sep 2022 12:55:48 +0200 Subject: [PATCH 06/33] Treat version change as a delimiting flag --- bin/runtime/src/lib.rs | 20 ++++++++++++----- pallets/aleph/src/lib.rs | 47 +++++++++++++++++++++++++--------------- primitives/src/lib.rs | 14 +++++------- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 713447fd71..8f5faae374 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -35,8 +35,9 @@ use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustm pub use primitives::Balance; use primitives::{ staking::MAX_NOMINATORS_REWARDED_PER_VALIDATOR, wrap_methods, ApiError as AlephApiError, - AuthorityId as AlephId, SessionAuthorityData, Version as AlephBFTVersion, ADDRESSES_ENCODING, - DEFAULT_SESSIONS_PER_ERA, DEFAULT_SESSION_PERIOD, MILLISECS_PER_BLOCK, TOKEN, + AuthorityId as AlephId, SessionAuthorityData, SessionIndex, Version as AlephBFTVersion, + ADDRESSES_ENCODING, DEFAULT_SESSIONS_PER_ERA, DEFAULT_SESSION_PERIOD, MILLISECS_PER_BLOCK, + TOKEN, }; use sp_api::impl_runtime_apis; use sp_consensus_aura::{sr25519::AuthorityId as AuraId, SlotDuration}; @@ -896,9 +897,18 @@ impl_runtime_apis! { )) } - fn aleph_bft_version() -> AlephBFTVersion { - let version_change = Aleph::aleph_bft_version(); - version_change.version + fn aleph_bft_version(session: SessionIndex) -> AlephBFTVersion { + let version_change = Aleph::aleph_bft_version_change(); + let version_incoming = version_change.version_incoming; + let version_current = match version_incoming { + AlephBFTVersion::Legacy => AlephBFTVersion::Current, + _ => AlephBFTVersion::Legacy, + }; + if session < version_change.session { + version_current + } else { + version_incoming + } } } diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index f61d20af39..1ed24ed155 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -45,7 +45,7 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), - ChangeAlephBFTVersion(VersionChange), + ScheduleAlephBFTVersionChange(VersionChange), } #[pallet::pallet] @@ -96,8 +96,8 @@ pub mod pallet { type NextEmergencyFinalizer = StorageValue<_, T::AuthorityId, OptionQuery>; #[pallet::storage] - #[pallet::getter(fn aleph_bft_version)] - pub(super) type AlephBFTVersion = StorageValue<_, VersionChange, ValueQuery>; + #[pallet::getter(fn aleph_bft_version_change)] + pub(super) type AlephBFTVersionChange = StorageValue<_, VersionChange, ValueQuery>; impl Pallet { pub(crate) fn initialize_authorities(authorities: &[T::AuthorityId]) { @@ -128,16 +128,27 @@ pub mod pallet { >::put(emergency_finalizer); } - pub(crate) fn set_next_aleph_bft_version(version_change: VersionChange) { - let previous_version_change = >::get(); - let previous_session = previous_version_change.session_change; + pub(crate) fn set_next_aleph_bft_version_change(version_change: VersionChange) { + let previous_version_change = >::get(); + let previous_session = previous_version_change.session; + let previous_version = previous_version_change.version_incoming; - let session = version_change.session_change; + let session = version_change.session; + let version = version_change.version_incoming; assert!( session > previous_session, - "Cannot set AlephBFT version for sessions prior to the current version! Session for current version: {}, attempted to set: {}.", previous_session, session); - >::put(version_change); + "Tried to schedule an AlephBFT version change for a session ({}) prior to the session ({}) of the currently scheduled version change.", + session, + previous_session + ); + + assert_ne!( + previous_version, version, + "Tried to schedule an AlephBFT version change with the same version as the currently scheduled one: {:?}!", + previous_version + ); + >::put(version_change); } } @@ -156,21 +167,21 @@ pub mod pallet { Ok(()) } - /// Sets AlephBFT version for a future session. If such a feature version is already set, - /// it is replaced with the specified one. + /// Schedules an AlephBFT version change for a future session. If such a scheduled future + /// version is already set, it is replaced with the specified one. #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] - pub fn set_aleph_bft_version( + pub fn set_aleph_bft_version_change( origin: OriginFor, - version: Version, - session_change: SessionIndex, + version_incoming: Version, + session: SessionIndex, ) -> DispatchResult { ensure_root(origin)?; let version_change = VersionChange { - version, - session_change, + version_incoming, + session, }; - Self::set_next_aleph_bft_version(version_change.clone()); - Self::deposit_event(Event::ChangeAlephBFTVersion(version_change)); + Self::set_next_aleph_bft_version_change(version_change.clone()); + Self::deposit_event(Event::ScheduleAlephBFTVersionChange(version_change)); Ok(()) } } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 2ffcbfad12..47ad21dbc5 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -124,7 +124,7 @@ impl SessionAuthorityData { } } -#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] +#[derive(Clone, Copy, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] pub enum Version { Legacy, Current, @@ -132,17 +132,15 @@ pub enum Version { #[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] pub struct VersionChange { - pub version: Version, - pub session_change: SessionIndex, + pub version_incoming: Version, + pub session: SessionIndex, } -// At genesis, the session of the next change of the AlephBFT version is unknown. Assume placeholder -// value of 0. impl Default for VersionChange { fn default() -> Self { Self { - version: Version::Legacy, - session_change: 0, + version_incoming: Version::Legacy, + session: 0, } } } @@ -156,7 +154,7 @@ sp_api::decl_runtime_apis! { fn authority_data() -> SessionAuthorityData; fn session_period() -> u32; fn millisecs_per_block() -> u64; - fn aleph_bft_version() -> Version; + fn aleph_bft_version(session: SessionIndex) -> Version; } } From b74359d0446eaa56d822bbd0a39956de93f8ec50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Wed, 7 Sep 2022 16:07:21 +0200 Subject: [PATCH 07/33] Loose coupling between pallet_aleph and pallet_session to access current session index --- bin/runtime/src/lib.rs | 1 + pallets/aleph/src/lib.rs | 32 +++++++++++++++++++++++--------- pallets/aleph/src/traits.rs | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 pallets/aleph/src/traits.rs diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 8f5faae374..4bfc4f2d41 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -313,6 +313,7 @@ impl pallet_sudo::Config for Runtime { impl pallet_aleph::Config for Runtime { type AuthorityId = AlephId; type Event = Event; + type SessionInfoProvider = Session; } impl_opaque_keys! { diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 1ed24ed155..9231e99976 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -10,6 +10,8 @@ mod mock; #[cfg(test)] mod tests; +mod traits; + mod migrations; use frame_support::{ @@ -34,11 +36,13 @@ pub mod pallet { use pallets_support::StorageMigration; use super::*; + use crate::traits::SessionInfoProvider; #[pallet::config] pub trait Config: frame_system::Config { type AuthorityId: Member + Parameter + RuntimeAppPublic + MaybeSerializeDeserialize; type Event: From> + IsType<::Event>; + type SessionInfoProvider: SessionInfoProvider; } #[pallet::event] @@ -128,6 +132,10 @@ pub mod pallet { >::put(emergency_finalizer); } + pub(crate) fn current_session() -> u32 { + T::SessionInfoProvider::current_session() + } + pub(crate) fn set_next_aleph_bft_version_change(version_change: VersionChange) { let previous_version_change = >::get(); let previous_session = previous_version_change.session; @@ -136,18 +144,24 @@ pub mod pallet { let session = version_change.session; let version = version_change.version_incoming; + let current_session = Self::current_session(); + assert!( - session > previous_session, - "Tried to schedule an AlephBFT version change for a session ({}) prior to the session ({}) of the currently scheduled version change.", - session, - previous_session + session > current_session, + "Cannot schedule AlephBFT version changes for sessions in the past!", ); - assert_ne!( - previous_version, version, - "Tried to schedule an AlephBFT version change with the same version as the currently scheduled one: {:?}!", - previous_version - ); + // If a scheduled future version change is rescheduled to a different session, + // it should be possible to reschedule it to the same version. + // If a scheduled version change has moved into the past, a new future version change + // needs to set a different version. + if previous_session < current_session { + assert_ne!( + previous_version, version, + "Tried to schedule an AlephBFT version change with the same version as the current version: {:?}!", + previous_version + ); + } >::put(version_change); } } diff --git a/pallets/aleph/src/traits.rs b/pallets/aleph/src/traits.rs new file mode 100644 index 0000000000..241a05c88b --- /dev/null +++ b/pallets/aleph/src/traits.rs @@ -0,0 +1,14 @@ +use primitives::SessionIndex; + +pub trait SessionInfoProvider { + fn current_session() -> SessionIndex; +} + +impl SessionInfoProvider for pallet_session::Pallet +where + T: pallet_session::Config, +{ + fn current_session() -> SessionIndex { + pallet_session::CurrentIndex::::get() + } +} From 7c900a3b2498ad39be5c821af4cfc99cf4bf1fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Thu, 8 Sep 2022 09:18:42 +0200 Subject: [PATCH 08/33] Fixed trait implementation for mock --- pallets/aleph/src/mock.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/pallets/aleph/src/mock.rs b/pallets/aleph/src/mock.rs index f4119d5c25..fb5995d19a 100644 --- a/pallets/aleph/src/mock.rs +++ b/pallets/aleph/src/mock.rs @@ -133,6 +133,7 @@ impl pallet_timestamp::Config for Test { impl Config for Test { type AuthorityId = AuthorityId; type Event = Event; + type SessionInfoProvider = Session; } pub fn to_authority(id: &u64) -> AuthorityId { From 4ae4f4c3e9204af0fd4e10e493f6682662a312fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Thu, 8 Sep 2022 09:22:48 +0200 Subject: [PATCH 09/33] Added comment to trait --- pallets/aleph/src/traits.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/pallets/aleph/src/traits.rs b/pallets/aleph/src/traits.rs index 241a05c88b..92de3efd4b 100644 --- a/pallets/aleph/src/traits.rs +++ b/pallets/aleph/src/traits.rs @@ -1,5 +1,6 @@ use primitives::SessionIndex; +/// Information provider from `pallet_session`. Loose pallet coupling via traits. pub trait SessionInfoProvider { fn current_session() -> SessionIndex; } From 71c1538921f70cc9af402d22fc7eafdf605d8947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Thu, 8 Sep 2022 14:14:25 +0200 Subject: [PATCH 10/33] Replaced panics with Results in AlephBFT version handling --- pallets/aleph/src/lib.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 9231e99976..f1fc3cac9d 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -136,7 +136,9 @@ pub mod pallet { T::SessionInfoProvider::current_session() } - pub(crate) fn set_next_aleph_bft_version_change(version_change: VersionChange) { + pub(crate) fn set_next_aleph_bft_version_change( + version_change: VersionChange, + ) -> Result<(), &'static str> { let previous_version_change = >::get(); let previous_session = previous_version_change.session; let previous_version = previous_version_change.version_incoming; @@ -146,23 +148,20 @@ pub mod pallet { let current_session = Self::current_session(); - assert!( - session > current_session, - "Cannot schedule AlephBFT version changes for sessions in the past!", - ); + if session < current_session { + return Err("Cannot schedule AlephBFT version changes for sessions in the past!"); + } // If a scheduled future version change is rescheduled to a different session, // it should be possible to reschedule it to the same version. // If a scheduled version change has moved into the past, a new future version change // needs to set a different version. - if previous_session < current_session { - assert_ne!( - previous_version, version, - "Tried to schedule an AlephBFT version change with the same version as the current version: {:?}!", - previous_version - ); + if previous_session < current_session && previous_version == version { + return Err("Tried to schedule an AlephBFT version change which does not change the current version!"); } + >::put(version_change); + Ok(()) } } @@ -194,7 +193,9 @@ pub mod pallet { version_incoming, session, }; - Self::set_next_aleph_bft_version_change(version_change.clone()); + if let Err(e) = Self::set_next_aleph_bft_version_change(version_change.clone()) { + return Err(DispatchError::Other(e)); + }; Self::deposit_event(Event::ScheduleAlephBFTVersionChange(version_change)); Ok(()) } From 9ef7f8fba654399c4a081338856efd1da06ed957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Mon, 12 Sep 2022 16:09:27 +0200 Subject: [PATCH 11/33] Changed logic to work with numbered versions --- bin/runtime/src/lib.rs | 19 ++++------ pallets/aleph/src/impls.rs | 49 ++++++++++++++++++++++++ pallets/aleph/src/lib.rs | 77 ++++++++++++++++++++++++++++---------- primitives/src/lib.rs | 19 ++-------- 4 files changed, 118 insertions(+), 46 deletions(-) create mode 100644 pallets/aleph/src/impls.rs diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 4bfc4f2d41..354bf49325 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -898,18 +898,15 @@ impl_runtime_apis! { )) } - fn aleph_bft_version(session: SessionIndex) -> AlephBFTVersion { - let version_change = Aleph::aleph_bft_version_change(); - let version_incoming = version_change.version_incoming; - let version_current = match version_incoming { - AlephBFTVersion::Legacy => AlephBFTVersion::Current, - _ => AlephBFTVersion::Legacy, - }; - if session < version_change.session { - version_current - } else { - version_incoming + // Scan for the most recent historical change relative to the provided session or return + // the scheduled version. + fn aleph_bft_version(session: SessionIndex) -> Result { + if let Some(scheduled_version_change) = Aleph::aleph_bft_version_change() { + if session >= scheduled_version_change.session { + return Ok(scheduled_version_change.version_incoming) + } } + Aleph::find_historical_aleph_bft_version_for_session(session).or(Err(AlephApiError::AlephBFTVersion)) } } diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs new file mode 100644 index 0000000000..0be3412d7c --- /dev/null +++ b/pallets/aleph/src/impls.rs @@ -0,0 +1,49 @@ +use crate::{Config, Pallet}; + +impl pallet_session::Session_Manager for Pallet +where + T: Config, +{ + fn new_session(new_index: SessionIndex) -> Option> { + None + } + + fn new_session_genesis(new_index: SessionIndex) -> Option> { + Self::new_session() + } + + fn end_session(end_index: SessionIndex) { } + + fn start_session(start_index: SessionIndex) { + Self::update_version_change_history_and_schedule(); + } +} + +impl Pallet +where T: Config, +{ + // Check if a schedule version change has moved into the past. If so, update history. + // Does not reset the scheduled version. + fn update_version_change_history() { + let current_session = Self::current_session(); + + if let Some(previously_scheduled_version_change) = + >::get() + { + let previously_scheduled_session = previously_scheduled_version_change.session; + let previously_scheduled_version = + previously_scheduled_version_change.version_incoming; + + if previously_scheduled_session < current_session { + // Record the previously scheduled version in version change history. + >::set( + previously_scheduled_session, + previously_scheduled_version, + ); + Self::deposit_event(Event::UpdateAlephBFTVersionHistory( + previously_scheduled_version_change, + )); + } + } + } +} \ No newline at end of file diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index f1fc3cac9d..c9c591aad1 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -50,6 +50,7 @@ pub mod pallet { pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), ScheduleAlephBFTVersionChange(VersionChange), + UpdateAlephBFTVersionHistory(VersionChange), } #[pallet::pallet] @@ -99,9 +100,17 @@ pub mod pallet { #[pallet::storage] type NextEmergencyFinalizer = StorageValue<_, T::AuthorityId, OptionQuery>; + /// AlephBFT version change history. + #[pallet::storage] + #[pallet::getter(fn aleph_bft_version)] + pub(super) type AlephBFTVersion = + StorageMap<_, Twox64Concat, SessionIndex, Version, ValueQuery>; + + /// Scheduled AlephBFT version change. #[pallet::storage] #[pallet::getter(fn aleph_bft_version_change)] - pub(super) type AlephBFTVersionChange = StorageValue<_, VersionChange, ValueQuery>; + pub(super) type AlephBFTScheduledVersionChange = + StorageValue<_, VersionChange, OptionQuery>; impl Pallet { pub(crate) fn initialize_authorities(authorities: &[T::AuthorityId]) { @@ -136,33 +145,59 @@ pub mod pallet { T::SessionInfoProvider::current_session() } - pub(crate) fn set_next_aleph_bft_version_change( + pub(crate) fn schedule_next_aleph_bft_version_change( version_change: VersionChange, ) -> Result<(), &'static str> { - let previous_version_change = >::get(); - let previous_session = previous_version_change.session; - let previous_version = previous_version_change.version_incoming; - - let session = version_change.session; - let version = version_change.version_incoming; - let current_session = Self::current_session(); - if session < current_session { + let session_to_schedule = version_change.session; + let version_to_schedule = version_change.version_incoming; + + if session_to_schedule < current_session { return Err("Cannot schedule AlephBFT version changes for sessions in the past!"); + } else if session_to_schedule < current_session + 2 { + return Err( + "Tried to schedule AlephBFT version change less than 2 sessions in advance!", + ); } // If a scheduled future version change is rescheduled to a different session, // it should be possible to reschedule it to the same version. - // If a scheduled version change has moved into the past, a new future version change - // needs to set a different version. - if previous_session < current_session && previous_version == version { - return Err("Tried to schedule an AlephBFT version change which does not change the current version!"); + // If a scheduled version change has moved into the past, + // `pallet_session::Session_Manager` records it in history and a new future version + // change needs to set a different version. + if let Some(previously_scheduled_version_change) = + >::get() + { + let previously_scheduled_session = previously_scheduled_version_change.session; + // If the scheduled version is recorded in history, it is in the past. + if let Ok(current_version) = + >::try_get(previously_scheduled_session) + { + if current_version == version_to_schedule { + return Err("Tried to schedule an AlephBFT version change which does not change the current version!"); + } + } } - >::put(version_change); + // Update the scheduled version change with the supplied version change. + >::put(version_change); + Ok(()) } + + // Scan for the most recent historical version relative to the provided session. + pub fn find_historical_aleph_bft_version_for_session( + session: SessionIndex, + ) -> Result { + for idx in (0..session + 1).rev() { + if let Ok(version) = >::try_get(idx) { + return Ok(version); + } + } + + Err("No AlephBFT version has been recorded in history!") + } } #[pallet::call] @@ -180,10 +215,14 @@ pub mod pallet { Ok(()) } - /// Schedules an AlephBFT version change for a future session. If such a scheduled future - /// version is already set, it is replaced with the specified one. + /// Schedules an AlephBFT version change for a future session. Checks whether a previously + /// scheduled version change has moved into the past and updates the version change history + /// if needed. If such a scheduled future version is already set, it is replaced with the + /// provided one. + /// Any rescheduling of a future version change needs to occur at least 2 sessions in + /// advance of the provided session of the version change. #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] - pub fn set_aleph_bft_version_change( + pub fn schedule_aleph_bft_version_change( origin: OriginFor, version_incoming: Version, session: SessionIndex, @@ -193,7 +232,7 @@ pub mod pallet { version_incoming, session, }; - if let Err(e) = Self::set_next_aleph_bft_version_change(version_change.clone()) { + if let Err(e) = Self::schedule_next_aleph_bft_version_change(version_change.clone()) { return Err(DispatchError::Other(e)); }; Self::deposit_event(Event::ScheduleAlephBFTVersionChange(version_change)); diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 47ad21dbc5..3f640553db 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -98,6 +98,7 @@ impl Default for EraValidators { #[derive(Encode, Decode, PartialEq, Eq, Debug)] pub enum ApiError { DecodeKey, + AlephBFTVersion, } /// All the data needed to verify block finalization justifications. @@ -124,27 +125,13 @@ impl SessionAuthorityData { } } -#[derive(Clone, Copy, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] -pub enum Version { - Legacy, - Current, -} +pub type Version = u32; #[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, TypeInfo)] pub struct VersionChange { pub version_incoming: Version, pub session: SessionIndex, } - -impl Default for VersionChange { - fn default() -> Self { - Self { - version_incoming: Version::Legacy, - session: 0, - } - } -} - sp_api::decl_runtime_apis! { pub trait AlephSessionApi { @@ -154,7 +141,7 @@ sp_api::decl_runtime_apis! { fn authority_data() -> SessionAuthorityData; fn session_period() -> u32; fn millisecs_per_block() -> u64; - fn aleph_bft_version(session: SessionIndex) -> Version; + fn aleph_bft_version(session: SessionIndex) -> Result; } } From e0d9fb3ade5bae9f781841a2dc5de284c76a4b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Tue, 13 Sep 2022 12:00:38 +0200 Subject: [PATCH 12/33] Changed behavior on session start and end --- pallets/aleph/src/impls.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index 0be3412d7c..fbd5ca686f 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -5,16 +5,19 @@ where T: Config, { fn new_session(new_index: SessionIndex) -> Option> { - None + ::SessionManager::new_session(new_index); } fn new_session_genesis(new_index: SessionIndex) -> Option> { Self::new_session() } - fn end_session(end_index: SessionIndex) { } + fn end_session(end_index: SessionIndex) { + ::SessionManager::end_session(end_index); + } fn start_session(start_index: SessionIndex) { + ::SessionManager::start_session(start_index); Self::update_version_change_history_and_schedule(); } } From 7eea19cf169afb01155907cbf53ac093b1a8f594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Thu, 15 Sep 2022 13:42:00 +0200 Subject: [PATCH 13/33] Fixed version change update --- .github/scripts/run_consensus.sh | 3 +++ pallets/aleph/src/impls.rs | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/scripts/run_consensus.sh b/.github/scripts/run_consensus.sh index 9feddbe32b..aa6bfc29db 100755 --- a/.github/scripts/run_consensus.sh +++ b/.github/scripts/run_consensus.sh @@ -6,6 +6,9 @@ set -euo pipefail # change when increasing the number of node containers NODE_COUNT=5 +# default minimum validator count +MIN_VALIDATOR_COUNT=4 + export NODE_IMAGE=aleph-node:latest mkdir -p docker/data/ diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index fbd5ca686f..1bba01c13e 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -18,7 +18,7 @@ where fn start_session(start_index: SessionIndex) { ::SessionManager::start_session(start_index); - Self::update_version_change_history_and_schedule(); + Self::update_version_change_history(); } } @@ -37,7 +37,7 @@ where T: Config, let previously_scheduled_version = previously_scheduled_version_change.version_incoming; - if previously_scheduled_session < current_session { + if previously_scheduled_session <= current_session { // Record the previously scheduled version in version change history. >::set( previously_scheduled_session, From 5ff7256ab357a2aeadb2bcae046e45426c7a8e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Fri, 16 Sep 2022 11:39:34 +0200 Subject: [PATCH 14/33] Linked SessionManager in runtime --- bin/runtime/src/lib.rs | 1 + pallets/aleph/src/impls.rs | 2 +- pallets/aleph/src/lib.rs | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 354bf49325..1b40994ff7 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -314,6 +314,7 @@ impl pallet_aleph::Config for Runtime { type AuthorityId = AlephId; type Event = Event; type SessionInfoProvider = Session; + type SessionManager = Elections; } impl_opaque_keys! { diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index 1bba01c13e..be71df882a 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -1,6 +1,6 @@ use crate::{Config, Pallet}; -impl pallet_session::Session_Manager for Pallet +impl pallet_session::SessionManager for Pallet where T: Config, { diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index c9c591aad1..1afc8ab552 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -33,6 +33,7 @@ pub mod pallet { ensure_root, pallet_prelude::{BlockNumberFor, OriginFor}, }; + use pallet_session::SessionManager; use pallets_support::StorageMigration; use super::*; @@ -43,6 +44,7 @@ pub mod pallet { type AuthorityId: Member + Parameter + RuntimeAppPublic + MaybeSerializeDeserialize; type Event: From> + IsType<::Event>; type SessionInfoProvider: SessionInfoProvider; + type SessionManager: SessionManager<::AccountId>; } #[pallet::event] @@ -164,7 +166,7 @@ pub mod pallet { // If a scheduled future version change is rescheduled to a different session, // it should be possible to reschedule it to the same version. // If a scheduled version change has moved into the past, - // `pallet_session::Session_Manager` records it in history and a new future version + // `pallet_session::SessionManager` records it in history and a new future version // change needs to set a different version. if let Some(previously_scheduled_version_change) = >::get() From f63875bcea3b32e8d0fdea64d4267633b33cc943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Fri, 16 Sep 2022 13:18:22 +0200 Subject: [PATCH 15/33] Mock impl --- Cargo.lock | 1 + pallets/aleph/Cargo.toml | 1 + pallets/aleph/src/mock.rs | 12 ++++++++++++ 3 files changed, 14 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index f37e9757a5..2464d52549 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4627,6 +4627,7 @@ dependencies = [ "frame-support", "frame-system", "pallet-balances", + "pallet-elections", "pallet-session", "pallet-timestamp", "pallets-support", diff --git a/pallets/aleph/Cargo.toml b/pallets/aleph/Cargo.toml index b29b10b10c..3e95f8d047 100644 --- a/pallets/aleph/Cargo.toml +++ b/pallets/aleph/Cargo.toml @@ -17,6 +17,7 @@ pallet-session = { default-features = false, git = "https://github.com/Cardinal- sp-io = { default-features = false, git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.26" } sp-std = { default-features = false, git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.26" } +pallet-elections = { path = "../elections", default-features = false } pallets-support = { path = "../support", default-features = false } primitives = { path = "../../primitives", default-features = false } diff --git a/pallets/aleph/src/mock.rs b/pallets/aleph/src/mock.rs index fb5995d19a..6bd650f206 100644 --- a/pallets/aleph/src/mock.rs +++ b/pallets/aleph/src/mock.rs @@ -32,6 +32,7 @@ construct_runtime!( Aleph: pallet_aleph::{Pallet, Storage, Event}, Session: pallet_session::{Pallet, Call, Storage, Event, Config}, Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent}, + Elections: pallet_elections::{Pallet, Event}, } ); @@ -130,10 +131,21 @@ impl pallet_timestamp::Config for Test { type WeightInfo = (); } +impl pallet_elections::Config for Test { + type EraInfoProvider = (); + type Event = Event; + type DataProvider = (); + type SessionInfoProvider = Session; + type SessionPeriod = (); + type SessionManager = (); + type ValidatorRewardsHandler = (); +} + impl Config for Test { type AuthorityId = AuthorityId; type Event = Event; type SessionInfoProvider = Session; + type SessionManager = Elections; } pub fn to_authority(id: &u64) -> AuthorityId { From 3f685228682af1957f371e18e90f95d2d1d5b4fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Fri, 16 Sep 2022 14:28:20 +0200 Subject: [PATCH 16/33] Reworked SessionManage link --- bin/runtime/src/lib.rs | 2 +- pallets/aleph/src/impls.rs | 19 +++++++++++-------- pallets/aleph/src/lib.rs | 9 ++++----- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 1b40994ff7..3cbf6667fe 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -350,7 +350,7 @@ impl pallet_session::Config for Runtime { type ValidatorIdOf = pallet_staking::StashOf; type ShouldEndSession = pallet_session::PeriodicSessions; type NextSessionRotation = pallet_session::PeriodicSessions; - type SessionManager = Elections; + type SessionManager = Aleph; type SessionHandler = ::KeyTypeIdProviders; type Keys = SessionKeys; type WeightInfo = pallet_session::weights::SubstrateWeight; diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index be71df882a..b4cc69b62d 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -1,15 +1,18 @@ -use crate::{Config, Pallet}; +use primitives::SessionIndex; +use sp_std::vec::Vec; + +use crate::{Config, Event, Pallet, AlephBFTVersion, AlephBFTScheduledVersionChange}; impl pallet_session::SessionManager for Pallet where T: Config, { fn new_session(new_index: SessionIndex) -> Option> { - ::SessionManager::new_session(new_index); + ::SessionManager::new_session(new_index) } fn new_session_genesis(new_index: SessionIndex) -> Option> { - Self::new_session() + ::SessionManager::new_session_genesis(new_index) } fn end_session(end_index: SessionIndex) { @@ -23,7 +26,8 @@ where } impl Pallet -where T: Config, +where + T: Config, { // Check if a schedule version change has moved into the past. If so, update history. // Does not reset the scheduled version. @@ -31,11 +35,10 @@ where T: Config, let current_session = Self::current_session(); if let Some(previously_scheduled_version_change) = - >::get() + >::get() { let previously_scheduled_session = previously_scheduled_version_change.session; - let previously_scheduled_version = - previously_scheduled_version_change.version_incoming; + let previously_scheduled_version = previously_scheduled_version_change.version_incoming; if previously_scheduled_session <= current_session { // Record the previously scheduled version in version change history. @@ -49,4 +52,4 @@ where T: Config, } } } -} \ No newline at end of file +} diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 1afc8ab552..e978511a2a 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -10,9 +10,9 @@ mod mock; #[cfg(test)] mod tests; -mod traits; - +mod impls; mod migrations; +mod traits; use frame_support::{ log, @@ -165,9 +165,8 @@ pub mod pallet { // If a scheduled future version change is rescheduled to a different session, // it should be possible to reschedule it to the same version. - // If a scheduled version change has moved into the past, - // `pallet_session::SessionManager` records it in history and a new future version - // change needs to set a different version. + // If a scheduled version change has moved into the past, `SessionManager` records it + // in history and a new future version change needs to set a different version. if let Some(previously_scheduled_version_change) = >::get() { From 002a2b5b6816444568986e3e9cbb98fb47e052a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Fri, 16 Sep 2022 14:34:12 +0200 Subject: [PATCH 17/33] Linter --- pallets/aleph/src/impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index b4cc69b62d..8e73b51af8 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -1,7 +1,7 @@ use primitives::SessionIndex; use sp_std::vec::Vec; -use crate::{Config, Event, Pallet, AlephBFTVersion, AlephBFTScheduledVersionChange}; +use crate::{AlephBFTScheduledVersionChange, AlephBFTVersion, Config, Event, Pallet}; impl pallet_session::SessionManager for Pallet where From 9b70cc4204dea02dda773e842d83a1c6324b914e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Fri, 16 Sep 2022 15:17:23 +0200 Subject: [PATCH 18/33] Removed pallet_elections from mock --- Cargo.lock | 1 - pallets/aleph/Cargo.toml | 1 - pallets/aleph/src/impls.rs | 7 ++++++- pallets/aleph/src/mock.rs | 15 ++------------- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2464d52549..f37e9757a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4627,7 +4627,6 @@ dependencies = [ "frame-support", "frame-system", "pallet-balances", - "pallet-elections", "pallet-session", "pallet-timestamp", "pallets-support", diff --git a/pallets/aleph/Cargo.toml b/pallets/aleph/Cargo.toml index 3e95f8d047..b29b10b10c 100644 --- a/pallets/aleph/Cargo.toml +++ b/pallets/aleph/Cargo.toml @@ -17,7 +17,6 @@ pallet-session = { default-features = false, git = "https://github.com/Cardinal- sp-io = { default-features = false, git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.26" } sp-std = { default-features = false, git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.26" } -pallet-elections = { path = "../elections", default-features = false } pallets-support = { path = "../support", default-features = false } primitives = { path = "../../primitives", default-features = false } diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index 8e73b51af8..2f78b48bae 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -1,4 +1,4 @@ -use primitives::SessionIndex; +use primitives::{SessionIndex, VersionChange}; use sp_std::vec::Vec; use crate::{AlephBFTScheduledVersionChange, AlephBFTVersion, Config, Event, Pallet}; @@ -33,6 +33,11 @@ where // Does not reset the scheduled version. fn update_version_change_history() { let current_session = Self::current_session(); + // TODO: delete this event + Self::deposit_event(Event::UpdateAlephBFTVersionHistory(VersionChange { + version_incoming: 100, + session: 100, + })); if let Some(previously_scheduled_version_change) = >::get() diff --git a/pallets/aleph/src/mock.rs b/pallets/aleph/src/mock.rs index 6bd650f206..ac9a692c4f 100644 --- a/pallets/aleph/src/mock.rs +++ b/pallets/aleph/src/mock.rs @@ -32,7 +32,6 @@ construct_runtime!( Aleph: pallet_aleph::{Pallet, Storage, Event}, Session: pallet_session::{Pallet, Call, Storage, Event, Config}, Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent}, - Elections: pallet_elections::{Pallet, Event}, } ); @@ -106,7 +105,7 @@ impl pallet_session::Config for Test { type ValidatorIdOf = ConvertInto; type ShouldEndSession = pallet_session::PeriodicSessions; type NextSessionRotation = pallet_session::PeriodicSessions; - type SessionManager = (); + type SessionManager = Aleph; type SessionHandler = ::KeyTypeIdProviders; type Keys = TestSessionKeys; type WeightInfo = (); @@ -131,21 +130,11 @@ impl pallet_timestamp::Config for Test { type WeightInfo = (); } -impl pallet_elections::Config for Test { - type EraInfoProvider = (); - type Event = Event; - type DataProvider = (); - type SessionInfoProvider = Session; - type SessionPeriod = (); - type SessionManager = (); - type ValidatorRewardsHandler = (); -} - impl Config for Test { type AuthorityId = AuthorityId; type Event = Event; type SessionInfoProvider = Session; - type SessionManager = Elections; + type SessionManager = Aleph; } pub fn to_authority(id: &u64) -> AuthorityId { From 4853d8b63e103dfc65bffb3d831a35ef81cc260e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Fri, 16 Sep 2022 15:18:39 +0200 Subject: [PATCH 19/33] Removed event --- pallets/aleph/src/impls.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index 2f78b48bae..9a0bd6098a 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -33,12 +33,7 @@ where // Does not reset the scheduled version. fn update_version_change_history() { let current_session = Self::current_session(); - // TODO: delete this event - Self::deposit_event(Event::UpdateAlephBFTVersionHistory(VersionChange { - version_incoming: 100, - session: 100, - })); - + if let Some(previously_scheduled_version_change) = >::get() { From 706c9b1cc816136e6517c511cbc1d615d918b6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Fri, 16 Sep 2022 15:24:09 +0200 Subject: [PATCH 20/33] linter --- pallets/aleph/src/impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index 9a0bd6098a..52a447d379 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -33,7 +33,7 @@ where // Does not reset the scheduled version. fn update_version_change_history() { let current_session = Self::current_session(); - + if let Some(previously_scheduled_version_change) = >::get() { From 66989bf171febe02b5c86e2d588234f5b3f05ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Fri, 16 Sep 2022 16:00:20 +0200 Subject: [PATCH 21/33] Linter --- pallets/aleph/src/impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index 52a447d379..8e73b51af8 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -1,4 +1,4 @@ -use primitives::{SessionIndex, VersionChange}; +use primitives::SessionIndex; use sp_std::vec::Vec; use crate::{AlephBFTScheduledVersionChange, AlephBFTVersion, Config, Event, Pallet}; From 23ae4a8fa5e3bc399de74687aa23d3ba36b9ec2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Mon, 19 Sep 2022 15:59:52 +0200 Subject: [PATCH 22/33] Reworked AlephBFT version history updates --- pallets/aleph/src/impls.rs | 37 +++++++++++++++++++++---------------- pallets/aleph/src/lib.rs | 2 +- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index 8e73b51af8..1775828c4b 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -29,25 +29,30 @@ impl Pallet where T: Config, { - // Check if a schedule version change has moved into the past. If so, update history. - // Does not reset the scheduled version. + // Check if a schedule version change has moved into the past. Update history, even if there is + // no change. Does not reset the scheduled version. fn update_version_change_history() { let current_session = Self::current_session(); - if let Some(previously_scheduled_version_change) = - >::get() - { - let previously_scheduled_session = previously_scheduled_version_change.session; - let previously_scheduled_version = previously_scheduled_version_change.version_incoming; - - if previously_scheduled_session <= current_session { - // Record the previously scheduled version in version change history. - >::set( - previously_scheduled_session, - previously_scheduled_version, - ); - Self::deposit_event(Event::UpdateAlephBFTVersionHistory( - previously_scheduled_version_change, + // Carry over version from previous session. + if current_session != 0 { + if let Ok(version_for_previous_session) = + >::try_get(current_session - 1) + { + >::set(current_session, version_for_previous_session); + } + } + + if let Some(scheduled_version_change) = >::get() { + let scheduled_session = scheduled_version_change.session; + let scheduled_version = scheduled_version_change.version_incoming; + + // Record the scheduled version in version change history as it moves into the past. + if scheduled_session == current_session { + >::set(current_session, scheduled_version); + + Self::deposit_event(Event::ReachedScheduledAlephBFTVersionChange( + scheduled_version_change, )); } } diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index e978511a2a..84bede092a 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -52,7 +52,7 @@ pub mod pallet { pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), ScheduleAlephBFTVersionChange(VersionChange), - UpdateAlephBFTVersionHistory(VersionChange), + ReachedScheduledAlephBFTVersionChange(VersionChange), } #[pallet::pallet] From f23314ef191bcfebb9882b4c56ecab0b0df43e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Thu, 22 Sep 2022 13:22:50 +0200 Subject: [PATCH 23/33] Reworked storage to use value instead of map --- bin/runtime/src/lib.rs | 16 ++++---------- pallets/aleph/src/impls.rs | 22 ++++++------------- pallets/aleph/src/lib.rs | 45 ++++++++++---------------------------- primitives/src/lib.rs | 2 +- 4 files changed, 23 insertions(+), 62 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 3cbf6667fe..bca10fadea 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -35,9 +35,8 @@ use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustm pub use primitives::Balance; use primitives::{ staking::MAX_NOMINATORS_REWARDED_PER_VALIDATOR, wrap_methods, ApiError as AlephApiError, - AuthorityId as AlephId, SessionAuthorityData, SessionIndex, Version as AlephBFTVersion, - ADDRESSES_ENCODING, DEFAULT_SESSIONS_PER_ERA, DEFAULT_SESSION_PERIOD, MILLISECS_PER_BLOCK, - TOKEN, + AuthorityId as AlephId, SessionAuthorityData, Version as AlephBFTVersion, ADDRESSES_ENCODING, + DEFAULT_SESSIONS_PER_ERA, DEFAULT_SESSION_PERIOD, MILLISECS_PER_BLOCK, TOKEN, }; use sp_api::impl_runtime_apis; use sp_consensus_aura::{sr25519::AuthorityId as AuraId, SlotDuration}; @@ -899,15 +898,8 @@ impl_runtime_apis! { )) } - // Scan for the most recent historical change relative to the provided session or return - // the scheduled version. - fn aleph_bft_version(session: SessionIndex) -> Result { - if let Some(scheduled_version_change) = Aleph::aleph_bft_version_change() { - if session >= scheduled_version_change.session { - return Ok(scheduled_version_change.version_incoming) - } - } - Aleph::find_historical_aleph_bft_version_for_session(session).or(Err(AlephApiError::AlephBFTVersion)) + fn aleph_bft_version() -> Result { + Aleph::aleph_bft_version().ok_or(AlephApiError::AlephBFTVersion) } } diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index 1775828c4b..158d218d56 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -30,30 +30,22 @@ where T: Config, { // Check if a schedule version change has moved into the past. Update history, even if there is - // no change. Does not reset the scheduled version. + // no change. Resets the scheduled version. fn update_version_change_history() { let current_session = Self::current_session(); - // Carry over version from previous session. - if current_session != 0 { - if let Ok(version_for_previous_session) = - >::try_get(current_session - 1) - { - >::set(current_session, version_for_previous_session); - } - } - if let Some(scheduled_version_change) = >::get() { let scheduled_session = scheduled_version_change.session; let scheduled_version = scheduled_version_change.version_incoming; - // Record the scheduled version in version change history as it moves into the past. + // Record the scheduled version as the current version as it moves into the past. if scheduled_session == current_session { - >::set(current_session, scheduled_version); + >::put(scheduled_version); + + // Reset the scheduled version. + >::kill(); - Self::deposit_event(Event::ReachedScheduledAlephBFTVersionChange( - scheduled_version_change, - )); + Self::deposit_event(Event::AlephBFTVersionChange(scheduled_version_change)); } } } diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 84bede092a..c77842c2a7 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -52,7 +52,7 @@ pub mod pallet { pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), ScheduleAlephBFTVersionChange(VersionChange), - ReachedScheduledAlephBFTVersionChange(VersionChange), + AlephBFTVersionChange(VersionChange), } #[pallet::pallet] @@ -102,11 +102,10 @@ pub mod pallet { #[pallet::storage] type NextEmergencyFinalizer = StorageValue<_, T::AuthorityId, OptionQuery>; - /// AlephBFT version change history. + /// Current AlephBFT version. #[pallet::storage] #[pallet::getter(fn aleph_bft_version)] - pub(super) type AlephBFTVersion = - StorageMap<_, Twox64Concat, SessionIndex, Version, ValueQuery>; + pub(super) type AlephBFTVersion = StorageValue<_, Version, OptionQuery>; /// Scheduled AlephBFT version change. #[pallet::storage] @@ -163,21 +162,14 @@ pub mod pallet { ); } + // Can only schedule version changes with a version different than the current one. // If a scheduled future version change is rescheduled to a different session, - // it should be possible to reschedule it to the same version. + // it should be possible to reschedule it with the same version as initially. // If a scheduled version change has moved into the past, `SessionManager` records it - // in history and a new future version change needs to set a different version. - if let Some(previously_scheduled_version_change) = - >::get() - { - let previously_scheduled_session = previously_scheduled_version_change.session; - // If the scheduled version is recorded in history, it is in the past. - if let Ok(current_version) = - >::try_get(previously_scheduled_session) - { - if current_version == version_to_schedule { - return Err("Tried to schedule an AlephBFT version change which does not change the current version!"); - } + // as the current version. + if let Some(current_version) = >::get() { + if current_version == version_to_schedule { + return Err("Tried to schedule an AlephBFT version change which does not change the current version!"); } } @@ -186,19 +178,6 @@ pub mod pallet { Ok(()) } - - // Scan for the most recent historical version relative to the provided session. - pub fn find_historical_aleph_bft_version_for_session( - session: SessionIndex, - ) -> Result { - for idx in (0..session + 1).rev() { - if let Ok(version) = >::try_get(idx) { - return Ok(version); - } - } - - Err("No AlephBFT version has been recorded in history!") - } } #[pallet::call] @@ -216,10 +195,8 @@ pub mod pallet { Ok(()) } - /// Schedules an AlephBFT version change for a future session. Checks whether a previously - /// scheduled version change has moved into the past and updates the version change history - /// if needed. If such a scheduled future version is already set, it is replaced with the - /// provided one. + /// Schedules an AlephBFT version change for a future session. If such a scheduled future + /// version is already set, it is replaced with the provided one. /// Any rescheduling of a future version change needs to occur at least 2 sessions in /// advance of the provided session of the version change. #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 3f640553db..824098f282 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -141,7 +141,7 @@ sp_api::decl_runtime_apis! { fn authority_data() -> SessionAuthorityData; fn session_period() -> u32; fn millisecs_per_block() -> u64; - fn aleph_bft_version(session: SessionIndex) -> Result; + fn aleph_bft_version() -> Result; } } From f08cb5fcc809bc55893577145cf7907a896e8eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Thu, 22 Sep 2022 16:14:52 +0200 Subject: [PATCH 24/33] Added unit test --- pallets/aleph/src/mock.rs | 2 +- pallets/aleph/src/tests.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pallets/aleph/src/mock.rs b/pallets/aleph/src/mock.rs index ac9a692c4f..2fc82e96e4 100644 --- a/pallets/aleph/src/mock.rs +++ b/pallets/aleph/src/mock.rs @@ -134,7 +134,7 @@ impl Config for Test { type AuthorityId = AuthorityId; type Event = Event; type SessionInfoProvider = Session; - type SessionManager = Aleph; + type SessionManager = (); } pub fn to_authority(id: &u64) -> AuthorityId { diff --git a/pallets/aleph/src/tests.rs b/pallets/aleph/src/tests.rs index 7db90914a3..b54f4697fe 100644 --- a/pallets/aleph/src/tests.rs +++ b/pallets/aleph/src/tests.rs @@ -1,6 +1,7 @@ #![cfg(test)] use frame_support::{storage_alias, traits::OneSessionHandler}; +use primitives::VersionChange; use crate::mock::*; @@ -130,3 +131,32 @@ fn test_emergency_signer() { assert_eq!(Aleph::queued_emergency_finalizer(), Some(to_authority(&37))); }) } + +#[test] +fn test_aleph_bft_version_scheduling() { + new_test_ext(&[(1u64, 1u64), (2u64, 2u64)]).execute_with(|| { + initialize_session(); + + run_session(1); + + let version_to_schedule = VersionChange { + version_incoming: 1, + session: 4, + }; + + let scheduling_result = + Aleph::schedule_next_aleph_bft_version_change(version_to_schedule.clone()); + assert_eq!(scheduling_result, Ok(())); + + let scheduled_version_change = Aleph::aleph_bft_version_change(); + assert_eq!(scheduled_version_change, Some(version_to_schedule.clone())); + + run_session(4); + + let current_version = Aleph::aleph_bft_version(); + assert_eq!(current_version, Some(version_to_schedule.version_incoming)); + + let scheduled_version_change = Aleph::aleph_bft_version_change(); + assert_eq!(scheduled_version_change, None); + }) +} From f11a76c26841d6466bc7eef41871456ee5a15a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Fri, 23 Sep 2022 16:01:33 +0200 Subject: [PATCH 25/33] Added version change unscheduling and next session version --- bin/runtime/src/lib.rs | 4 +++ pallets/aleph/src/lib.rs | 61 +++++++++++++++++++++++++++++++--------- primitives/src/lib.rs | 2 ++ 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index bca10fadea..e20668bcb1 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -901,6 +901,10 @@ impl_runtime_apis! { fn aleph_bft_version() -> Result { Aleph::aleph_bft_version().ok_or(AlephApiError::AlephBFTVersion) } + + fn next_session_aleph_bft_version() -> Result { + Aleph::next_session_aleph_bft_version().ok_or(AlephApiError::AlephBFTVersionNextSession) + } } impl pallet_contracts_rpc_runtime_api::ContractsApi for Runtime { diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index c77842c2a7..6d60bbb303 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -52,6 +52,7 @@ pub mod pallet { pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), ScheduleAlephBFTVersionChange(VersionChange), + UnscheduleAlephBFTVersionChange(VersionChange), AlephBFTVersionChange(VersionChange), } @@ -146,13 +147,16 @@ pub mod pallet { T::SessionInfoProvider::current_session() } + // If a scheduled future version change is rescheduled to a different session, + // it should be possible to reschedule it with the same version as initially. + // If a scheduled version change has moved into the past, `SessionManager` records it + // as the current version. pub(crate) fn schedule_next_aleph_bft_version_change( version_change: VersionChange, ) -> Result<(), &'static str> { let current_session = Self::current_session(); let session_to_schedule = version_change.session; - let version_to_schedule = version_change.version_incoming; if session_to_schedule < current_session { return Err("Cannot schedule AlephBFT version changes for sessions in the past!"); @@ -162,22 +166,33 @@ pub mod pallet { ); } - // Can only schedule version changes with a version different than the current one. - // If a scheduled future version change is rescheduled to a different session, - // it should be possible to reschedule it with the same version as initially. - // If a scheduled version change has moved into the past, `SessionManager` records it - // as the current version. - if let Some(current_version) = >::get() { - if current_version == version_to_schedule { - return Err("Tried to schedule an AlephBFT version change which does not change the current version!"); - } - } - // Update the scheduled version change with the supplied version change. >::put(version_change); Ok(()) } + + pub(crate) fn remove_next_aleph_bft_version_change() -> Result + { + let version_change_to_remove = Self::aleph_bft_version_change() + .ok_or("No AlephBFT version change to unschedule!")?; + >::kill(); + + Ok(version_change_to_remove) + } + + pub fn next_session_aleph_bft_version() -> Option { + let next_session = Self::current_session() + 1; + let scheduled_version_change = Self::aleph_bft_version_change(); + + if let Some(version_change) = scheduled_version_change { + if next_session == version_change.session { + return Some(version_change.version_incoming); + } + } + + Self::aleph_bft_version() + } } #[pallet::call] @@ -206,16 +221,36 @@ pub mod pallet { session: SessionIndex, ) -> DispatchResult { ensure_root(origin)?; + let version_change = VersionChange { version_incoming, session, }; + if let Err(e) = Self::schedule_next_aleph_bft_version_change(version_change.clone()) { return Err(DispatchError::Other(e)); - }; + } + Self::deposit_event(Event::ScheduleAlephBFTVersionChange(version_change)); Ok(()) } + + /// Unschedules a future AlephBFT version change. Does not allow to unschedule non-existent + /// version changes. + #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] + pub fn remove_aleph_bft_version_change(origin: OriginFor) -> DispatchResult { + ensure_root(origin)?; + + let unscheduled_version_change = Self::remove_next_aleph_bft_version_change(); + + match unscheduled_version_change { + Ok(version_change) => { + Self::deposit_event(Event::UnscheduleAlephBFTVersionChange(version_change)); + Ok(()) + } + Err(e) => Err(DispatchError::Other(e)), + } + } } impl BoundToRuntimeAppPublic for Pallet { diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 824098f282..e18b0bfdcf 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -99,6 +99,7 @@ impl Default for EraValidators { pub enum ApiError { DecodeKey, AlephBFTVersion, + AlephBFTVersionNextSession, } /// All the data needed to verify block finalization justifications. @@ -142,6 +143,7 @@ sp_api::decl_runtime_apis! { fn session_period() -> u32; fn millisecs_per_block() -> u64; fn aleph_bft_version() -> Result; + fn next_session_aleph_bft_version() -> Result; } } From 8c46f55a04cf05d051a1f188ea796046c618537c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Mon, 26 Sep 2022 15:43:06 +0200 Subject: [PATCH 26/33] Removed explicit version unscheduling; added version default --- bin/runtime/src/lib.rs | 8 +++---- pallets/aleph/src/lib.rs | 47 ++++++++++++-------------------------- pallets/aleph/src/tests.rs | 10 +++++++- primitives/src/lib.rs | 7 +++--- 4 files changed, 31 insertions(+), 41 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index e20668bcb1..94fa118f3b 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -898,12 +898,12 @@ impl_runtime_apis! { )) } - fn aleph_bft_version() -> Result { - Aleph::aleph_bft_version().ok_or(AlephApiError::AlephBFTVersion) + fn aleph_bft_version() -> AlephBFTVersion { + Aleph::aleph_bft_version() } - fn next_session_aleph_bft_version() -> Result { - Aleph::next_session_aleph_bft_version().ok_or(AlephApiError::AlephBFTVersionNextSession) + fn next_session_aleph_bft_version() -> AlephBFTVersion { + Aleph::next_session_aleph_bft_version() } } diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 6d60bbb303..618fb2b14f 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -52,7 +52,6 @@ pub mod pallet { pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), ScheduleAlephBFTVersionChange(VersionChange), - UnscheduleAlephBFTVersionChange(VersionChange), AlephBFTVersionChange(VersionChange), } @@ -87,6 +86,12 @@ pub mod pallet { } } + /// Default AlephBFT version. Relevant for sessions before the first version change occurs. + #[pallet::type_value] + pub(crate) fn DefaultAlephBFTVersion() -> Version { + 0 + } + #[pallet::storage] #[pallet::getter(fn authorities)] pub(super) type Authorities = StorageValue<_, Vec, ValueQuery>; @@ -106,7 +111,8 @@ pub mod pallet { /// Current AlephBFT version. #[pallet::storage] #[pallet::getter(fn aleph_bft_version)] - pub(super) type AlephBFTVersion = StorageValue<_, Version, OptionQuery>; + pub(super) type AlephBFTVersion = + StorageValue<_, Version, ValueQuery, DefaultAlephBFTVersion>; /// Scheduled AlephBFT version change. #[pallet::storage] @@ -148,7 +154,8 @@ pub mod pallet { } // If a scheduled future version change is rescheduled to a different session, - // it should be possible to reschedule it with the same version as initially. + // it is possible to reschedule it with the same version as initially. + // To cancel a future version change, reschedule it with the current version. // If a scheduled version change has moved into the past, `SessionManager` records it // as the current version. pub(crate) fn schedule_next_aleph_bft_version_change( @@ -162,7 +169,7 @@ pub mod pallet { return Err("Cannot schedule AlephBFT version changes for sessions in the past!"); } else if session_to_schedule < current_session + 2 { return Err( - "Tried to schedule AlephBFT version change less than 2 sessions in advance!", + "Tried to schedule an AlephBFT version change less than 2 sessions in advance!", ); } @@ -172,22 +179,13 @@ pub mod pallet { Ok(()) } - pub(crate) fn remove_next_aleph_bft_version_change() -> Result - { - let version_change_to_remove = Self::aleph_bft_version_change() - .ok_or("No AlephBFT version change to unschedule!")?; - >::kill(); - - Ok(version_change_to_remove) - } - - pub fn next_session_aleph_bft_version() -> Option { + pub fn next_session_aleph_bft_version() -> Version { let next_session = Self::current_session() + 1; let scheduled_version_change = Self::aleph_bft_version_change(); if let Some(version_change) = scheduled_version_change { if next_session == version_change.session { - return Some(version_change.version_incoming); + return version_change.version_incoming; } } @@ -214,6 +212,8 @@ pub mod pallet { /// version is already set, it is replaced with the provided one. /// Any rescheduling of a future version change needs to occur at least 2 sessions in /// advance of the provided session of the version change. + /// In order to cancel a scheduled version change, a new version change should be scheduled + /// with the same version as the current one. #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] pub fn schedule_aleph_bft_version_change( origin: OriginFor, @@ -234,23 +234,6 @@ pub mod pallet { Self::deposit_event(Event::ScheduleAlephBFTVersionChange(version_change)); Ok(()) } - - /// Unschedules a future AlephBFT version change. Does not allow to unschedule non-existent - /// version changes. - #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] - pub fn remove_aleph_bft_version_change(origin: OriginFor) -> DispatchResult { - ensure_root(origin)?; - - let unscheduled_version_change = Self::remove_next_aleph_bft_version_change(); - - match unscheduled_version_change { - Ok(version_change) => { - Self::deposit_event(Event::UnscheduleAlephBFTVersionChange(version_change)); - Ok(()) - } - Err(e) => Err(DispatchError::Other(e)), - } - } } impl BoundToRuntimeAppPublic for Pallet { diff --git a/pallets/aleph/src/tests.rs b/pallets/aleph/src/tests.rs index b54f4697fe..b651389d6b 100644 --- a/pallets/aleph/src/tests.rs +++ b/pallets/aleph/src/tests.rs @@ -154,9 +154,17 @@ fn test_aleph_bft_version_scheduling() { run_session(4); let current_version = Aleph::aleph_bft_version(); - assert_eq!(current_version, Some(version_to_schedule.version_incoming)); + assert_eq!(current_version, version_to_schedule.version_incoming); let scheduled_version_change = Aleph::aleph_bft_version_change(); assert_eq!(scheduled_version_change, None); + + let version_to_schedule = VersionChange { + version_incoming: 1, + session: 5, + }; + + let scheduling_result = Aleph::schedule_next_aleph_bft_version_change(version_to_schedule); + assert!(scheduling_result.is_err()); }) } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index e18b0bfdcf..4cd0ccb689 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -98,8 +98,6 @@ impl Default for EraValidators { #[derive(Encode, Decode, PartialEq, Eq, Debug)] pub enum ApiError { DecodeKey, - AlephBFTVersion, - AlephBFTVersionNextSession, } /// All the data needed to verify block finalization justifications. @@ -133,6 +131,7 @@ pub struct VersionChange { pub version_incoming: Version, pub session: SessionIndex, } + sp_api::decl_runtime_apis! { pub trait AlephSessionApi { @@ -142,8 +141,8 @@ sp_api::decl_runtime_apis! { fn authority_data() -> SessionAuthorityData; fn session_period() -> u32; fn millisecs_per_block() -> u64; - fn aleph_bft_version() -> Result; - fn next_session_aleph_bft_version() -> Result; + fn aleph_bft_version() -> Version; + fn next_session_aleph_bft_version() -> Version; } } From b76b5d354081725fa60d3ff5f656c591ab89eff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Tue, 27 Sep 2022 11:58:37 +0200 Subject: [PATCH 27/33] Extended docs for pallet aleph --- pallets/aleph/src/lib.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 618fb2b14f..7c7829cda8 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -1,7 +1,18 @@ -//! This pallet is a runtime companion of Aleph finality gadget. +//! This pallet is the runtime companion of the Aleph finality gadget. //! //! Currently, it only provides support for changing sessions but in the future //! it will allow reporting equivocation in AlephBFT. +//! +//! This pallet relies on an extension of the `AlephSessionApi` Runtime API to handle the AlephBFT +//! version. The scheduled version change is persisted as `AlephBFTScheduledVersionChange`. This +//! value stores the information about a scheduled AlephBFT version change, where `version_incoming` +//! is the version to be set and `session` is the session on which the new version will be set. +//! A `pallet_session::Session_Manager` checks whether a scheduled version change has moved into +//! the past and, if so, records it as the current version represented as `AlephBFTVersion`, +//! and clears `AlephBFTScheduledVersionChange`. +//! It is always possible to reschedule a version change. In order to cancel a scheduled version +//! change rather than reschedule it, a new version change should be scheduled with +//! `version_incoming` set to the current value of `AlephBFTVersion`. #![cfg_attr(not(feature = "std"), no_std)] From ab33ed186c5e062bdd45f99e341ff37bdd7152de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Tue, 27 Sep 2022 12:06:15 +0200 Subject: [PATCH 28/33] Moved default AlephBFT version to const --- pallets/aleph/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 7c7829cda8..29ce017fbe 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -37,6 +37,8 @@ use sp_std::prelude::*; /// The current storage version. const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); +const DEFAULT_ALEPH_BFT_VERSION: Version = 0; + #[frame_support::pallet] pub mod pallet { use frame_support::{pallet_prelude::*, sp_runtime::RuntimeAppPublic}; @@ -100,7 +102,7 @@ pub mod pallet { /// Default AlephBFT version. Relevant for sessions before the first version change occurs. #[pallet::type_value] pub(crate) fn DefaultAlephBFTVersion() -> Version { - 0 + DEFAULT_ALEPH_BFT_VERSION } #[pallet::storage] From cc89848b1560c2ca4c279eeea3a620f73f6960e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Mon, 3 Oct 2022 09:17:40 +0200 Subject: [PATCH 29/33] Renamed helper function --- pallets/aleph/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index 29ce017fbe..b6c2065af4 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -171,7 +171,7 @@ pub mod pallet { // To cancel a future version change, reschedule it with the current version. // If a scheduled version change has moved into the past, `SessionManager` records it // as the current version. - pub(crate) fn schedule_next_aleph_bft_version_change( + pub(crate) fn do_schedule_aleph_bft_version_change( version_change: VersionChange, ) -> Result<(), &'static str> { let current_session = Self::current_session(); @@ -240,7 +240,7 @@ pub mod pallet { session, }; - if let Err(e) = Self::schedule_next_aleph_bft_version_change(version_change.clone()) { + if let Err(e) = Self::do_schedule_aleph_bft_version_change(version_change.clone()) { return Err(DispatchError::Other(e)); } From 94519a234372f03994065398efb98ce65e608391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Mon, 3 Oct 2022 15:02:54 +0200 Subject: [PATCH 30/33] Fixed test --- pallets/aleph/src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/aleph/src/tests.rs b/pallets/aleph/src/tests.rs index b651389d6b..3d1c1d48f7 100644 --- a/pallets/aleph/src/tests.rs +++ b/pallets/aleph/src/tests.rs @@ -145,7 +145,7 @@ fn test_aleph_bft_version_scheduling() { }; let scheduling_result = - Aleph::schedule_next_aleph_bft_version_change(version_to_schedule.clone()); + Aleph::do_schedule_aleph_bft_version_change(version_to_schedule.clone()); assert_eq!(scheduling_result, Ok(())); let scheduled_version_change = Aleph::aleph_bft_version_change(); @@ -164,7 +164,7 @@ fn test_aleph_bft_version_scheduling() { session: 5, }; - let scheduling_result = Aleph::schedule_next_aleph_bft_version_change(version_to_schedule); + let scheduling_result = Aleph::do_schedule_aleph_bft_version_change(version_to_schedule); assert!(scheduling_result.is_err()); }) } From 1e9daef3af6eeaa9ea9106c1520ad50cbbea09ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Mon, 3 Oct 2022 15:37:05 +0200 Subject: [PATCH 31/33] Changed naming --- bin/runtime/src/lib.rs | 10 +++---- pallets/aleph/src/impls.rs | 10 +++---- pallets/aleph/src/lib.rs | 60 +++++++++++++++++++------------------- pallets/aleph/src/tests.rs | 12 ++++---- primitives/src/lib.rs | 4 +-- 5 files changed, 48 insertions(+), 48 deletions(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 94fa118f3b..01f1a06c82 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -35,7 +35,7 @@ use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustm pub use primitives::Balance; use primitives::{ staking::MAX_NOMINATORS_REWARDED_PER_VALIDATOR, wrap_methods, ApiError as AlephApiError, - AuthorityId as AlephId, SessionAuthorityData, Version as AlephBFTVersion, ADDRESSES_ENCODING, + AuthorityId as AlephId, SessionAuthorityData, Version as FinalityVersion, ADDRESSES_ENCODING, DEFAULT_SESSIONS_PER_ERA, DEFAULT_SESSION_PERIOD, MILLISECS_PER_BLOCK, TOKEN, }; use sp_api::impl_runtime_apis; @@ -898,12 +898,12 @@ impl_runtime_apis! { )) } - fn aleph_bft_version() -> AlephBFTVersion { - Aleph::aleph_bft_version() + fn finality_version() -> FinalityVersion { + Aleph::finality_version() } - fn next_session_aleph_bft_version() -> AlephBFTVersion { - Aleph::next_session_aleph_bft_version() + fn next_session_finality_version() -> FinalityVersion { + Aleph::next_session_finality_version() } } diff --git a/pallets/aleph/src/impls.rs b/pallets/aleph/src/impls.rs index 158d218d56..4a1cba814d 100644 --- a/pallets/aleph/src/impls.rs +++ b/pallets/aleph/src/impls.rs @@ -1,7 +1,7 @@ use primitives::SessionIndex; use sp_std::vec::Vec; -use crate::{AlephBFTScheduledVersionChange, AlephBFTVersion, Config, Event, Pallet}; +use crate::{Config, Event, FinalityScheduledVersionChange, FinalityVersion, Pallet}; impl pallet_session::SessionManager for Pallet where @@ -34,18 +34,18 @@ where fn update_version_change_history() { let current_session = Self::current_session(); - if let Some(scheduled_version_change) = >::get() { + if let Some(scheduled_version_change) = >::get() { let scheduled_session = scheduled_version_change.session; let scheduled_version = scheduled_version_change.version_incoming; // Record the scheduled version as the current version as it moves into the past. if scheduled_session == current_session { - >::put(scheduled_version); + >::put(scheduled_version); // Reset the scheduled version. - >::kill(); + >::kill(); - Self::deposit_event(Event::AlephBFTVersionChange(scheduled_version_change)); + Self::deposit_event(Event::FinalityVersionChange(scheduled_version_change)); } } } diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index b6c2065af4..eed1fc3e6e 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -3,16 +3,16 @@ //! Currently, it only provides support for changing sessions but in the future //! it will allow reporting equivocation in AlephBFT. //! -//! This pallet relies on an extension of the `AlephSessionApi` Runtime API to handle the AlephBFT -//! version. The scheduled version change is persisted as `AlephBFTScheduledVersionChange`. This -//! value stores the information about a scheduled AlephBFT version change, where `version_incoming` +//! This pallet relies on an extension of the `AlephSessionApi` Runtime API to handle the finality +//! version. The scheduled version change is persisted as `FinalityScheduledVersionChange`. This +//! value stores the information about a scheduled finality version change, where `version_incoming` //! is the version to be set and `session` is the session on which the new version will be set. //! A `pallet_session::Session_Manager` checks whether a scheduled version change has moved into -//! the past and, if so, records it as the current version represented as `AlephBFTVersion`, -//! and clears `AlephBFTScheduledVersionChange`. +//! the past and, if so, records it as the current version represented as `FinalityVersion`, +//! and clears `FinalityScheduledVersionChange`. //! It is always possible to reschedule a version change. In order to cancel a scheduled version //! change rather than reschedule it, a new version change should be scheduled with -//! `version_incoming` set to the current value of `AlephBFTVersion`. +//! `version_incoming` set to the current value of `FinalityVersion`. #![cfg_attr(not(feature = "std"), no_std)] @@ -37,7 +37,7 @@ use sp_std::prelude::*; /// The current storage version. const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); -const DEFAULT_ALEPH_BFT_VERSION: Version = 0; +const DEFAULT_FINALITY_VERSION: Version = 0; #[frame_support::pallet] pub mod pallet { @@ -64,8 +64,8 @@ pub mod pallet { #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { ChangeEmergencyFinalizer(T::AuthorityId), - ScheduleAlephBFTVersionChange(VersionChange), - AlephBFTVersionChange(VersionChange), + ScheduleFinalityVersionChange(VersionChange), + FinalityVersionChange(VersionChange), } #[pallet::pallet] @@ -99,10 +99,10 @@ pub mod pallet { } } - /// Default AlephBFT version. Relevant for sessions before the first version change occurs. + /// Default finality version. Relevant for sessions before the first version change occurs. #[pallet::type_value] - pub(crate) fn DefaultAlephBFTVersion() -> Version { - DEFAULT_ALEPH_BFT_VERSION + pub(crate) fn DefaultFinalityVersion() -> Version { + DEFAULT_FINALITY_VERSION } #[pallet::storage] @@ -121,16 +121,16 @@ pub mod pallet { #[pallet::storage] type NextEmergencyFinalizer = StorageValue<_, T::AuthorityId, OptionQuery>; - /// Current AlephBFT version. + /// Current finality version. #[pallet::storage] - #[pallet::getter(fn aleph_bft_version)] - pub(super) type AlephBFTVersion = - StorageValue<_, Version, ValueQuery, DefaultAlephBFTVersion>; + #[pallet::getter(fn finality_version)] + pub(super) type FinalityVersion = + StorageValue<_, Version, ValueQuery, DefaultFinalityVersion>; - /// Scheduled AlephBFT version change. + /// Scheduled finality version change. #[pallet::storage] - #[pallet::getter(fn aleph_bft_version_change)] - pub(super) type AlephBFTScheduledVersionChange = + #[pallet::getter(fn finality_version_change)] + pub(super) type FinalityScheduledVersionChange = StorageValue<_, VersionChange, OptionQuery>; impl Pallet { @@ -171,7 +171,7 @@ pub mod pallet { // To cancel a future version change, reschedule it with the current version. // If a scheduled version change has moved into the past, `SessionManager` records it // as the current version. - pub(crate) fn do_schedule_aleph_bft_version_change( + pub(crate) fn do_schedule_finality_version_change( version_change: VersionChange, ) -> Result<(), &'static str> { let current_session = Self::current_session(); @@ -179,22 +179,22 @@ pub mod pallet { let session_to_schedule = version_change.session; if session_to_schedule < current_session { - return Err("Cannot schedule AlephBFT version changes for sessions in the past!"); + return Err("Cannot schedule finality version changes for sessions in the past!"); } else if session_to_schedule < current_session + 2 { return Err( - "Tried to schedule an AlephBFT version change less than 2 sessions in advance!", + "Tried to schedule an finality version change less than 2 sessions in advance!", ); } // Update the scheduled version change with the supplied version change. - >::put(version_change); + >::put(version_change); Ok(()) } - pub fn next_session_aleph_bft_version() -> Version { + pub fn next_session_finality_version() -> Version { let next_session = Self::current_session() + 1; - let scheduled_version_change = Self::aleph_bft_version_change(); + let scheduled_version_change = Self::finality_version_change(); if let Some(version_change) = scheduled_version_change { if next_session == version_change.session { @@ -202,7 +202,7 @@ pub mod pallet { } } - Self::aleph_bft_version() + Self::finality_version() } } @@ -221,14 +221,14 @@ pub mod pallet { Ok(()) } - /// Schedules an AlephBFT version change for a future session. If such a scheduled future + /// Schedules a finality version change for a future session. If such a scheduled future /// version is already set, it is replaced with the provided one. /// Any rescheduling of a future version change needs to occur at least 2 sessions in /// advance of the provided session of the version change. /// In order to cancel a scheduled version change, a new version change should be scheduled /// with the same version as the current one. #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))] - pub fn schedule_aleph_bft_version_change( + pub fn schedule_finality_version_change( origin: OriginFor, version_incoming: Version, session: SessionIndex, @@ -240,11 +240,11 @@ pub mod pallet { session, }; - if let Err(e) = Self::do_schedule_aleph_bft_version_change(version_change.clone()) { + if let Err(e) = Self::do_schedule_finality_version_change(version_change.clone()) { return Err(DispatchError::Other(e)); } - Self::deposit_event(Event::ScheduleAlephBFTVersionChange(version_change)); + Self::deposit_event(Event::ScheduleFinalityVersionChange(version_change)); Ok(()) } } diff --git a/pallets/aleph/src/tests.rs b/pallets/aleph/src/tests.rs index 3d1c1d48f7..74eb4b1aca 100644 --- a/pallets/aleph/src/tests.rs +++ b/pallets/aleph/src/tests.rs @@ -133,7 +133,7 @@ fn test_emergency_signer() { } #[test] -fn test_aleph_bft_version_scheduling() { +fn test_finality_version_scheduling() { new_test_ext(&[(1u64, 1u64), (2u64, 2u64)]).execute_with(|| { initialize_session(); @@ -145,18 +145,18 @@ fn test_aleph_bft_version_scheduling() { }; let scheduling_result = - Aleph::do_schedule_aleph_bft_version_change(version_to_schedule.clone()); + Aleph::do_schedule_finality_version_change(version_to_schedule.clone()); assert_eq!(scheduling_result, Ok(())); - let scheduled_version_change = Aleph::aleph_bft_version_change(); + let scheduled_version_change = Aleph::finality_version_change(); assert_eq!(scheduled_version_change, Some(version_to_schedule.clone())); run_session(4); - let current_version = Aleph::aleph_bft_version(); + let current_version = Aleph::finality_version(); assert_eq!(current_version, version_to_schedule.version_incoming); - let scheduled_version_change = Aleph::aleph_bft_version_change(); + let scheduled_version_change = Aleph::finality_version_change(); assert_eq!(scheduled_version_change, None); let version_to_schedule = VersionChange { @@ -164,7 +164,7 @@ fn test_aleph_bft_version_scheduling() { session: 5, }; - let scheduling_result = Aleph::do_schedule_aleph_bft_version_change(version_to_schedule); + let scheduling_result = Aleph::do_schedule_finality_version_change(version_to_schedule); assert!(scheduling_result.is_err()); }) } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 4cd0ccb689..270abf06d6 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -141,8 +141,8 @@ sp_api::decl_runtime_apis! { fn authority_data() -> SessionAuthorityData; fn session_period() -> u32; fn millisecs_per_block() -> u64; - fn aleph_bft_version() -> Version; - fn next_session_aleph_bft_version() -> Version; + fn finality_version() -> Version; + fn next_session_finality_version() -> Version; } } From 27a1207f6c075fe42ad7d215656f75908bbcf21c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Mon, 3 Oct 2022 21:33:27 +0200 Subject: [PATCH 32/33] Bumped spec version --- bin/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 01f1a06c82..a3da54153a 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -107,7 +107,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("aleph-node"), impl_name: create_runtime_str!("aleph-node"), authoring_version: 1, - spec_version: 34, + spec_version: 35, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 9, From 6599a4e22902436e9d69dad13126e3be76c1b0b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBelaszczyk?= Date: Tue, 4 Oct 2022 09:03:57 +0200 Subject: [PATCH 33/33] Bumped transaction version --- bin/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index a3da54153a..859f0d54c9 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -110,7 +110,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_version: 35, impl_version: 1, apis: RUNTIME_API_VERSIONS, - transaction_version: 9, + transaction_version: 10, state_version: 0, };