This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Allow pallet's info to be enumerated #10053
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bdc2aeb
Allow pallet's info to be enumerated
gavofyork 130bdcf
Fixes
gavofyork 5d838f9
Formatting
gavofyork f92db2d
Flat tuple for getting all pallet instances
gavofyork 9dcb776
Renaming and fixing reversedness
gavofyork 7395d26
Formatting
gavofyork 7012b0f
Fixes
gavofyork 19205f6
Back to nesting
gavofyork 14d5389
Back to nestingx
gavofyork 4b1a685
Revert executive lib
gavofyork 51d2561
Reversions
gavofyork b26a8c5
Reversions
gavofyork f690e43
Fixes
gavofyork dddc075
Fixes
gavofyork b497b08
Formatting
gavofyork 8802275
Fixes
gavofyork 9882bda
Spelling
gavofyork 206e95e
Comments
gavofyork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
|
||
use codec::{Decode, Encode}; | ||
use sp_runtime::RuntimeDebug; | ||
use sp_std::prelude::*; | ||
|
||
/// Provides information about the pallet itself and its setup in the runtime. | ||
/// | ||
|
@@ -35,6 +36,19 @@ pub trait PalletInfo { | |
fn crate_version<P: 'static>() -> Option<CrateVersion>; | ||
} | ||
|
||
/// Information regarding an instance of a pallet. | ||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug)] | ||
pub struct PalletInfoData { | ||
/// Index of the pallet as configured in the runtime. | ||
pub index: usize, | ||
/// Name of the pallet as configured in the runtime. | ||
pub name: &'static str, | ||
/// Name of the Rust module containing the pallet. | ||
pub module_name: &'static str, | ||
/// Version of the crate containing the pallet. | ||
pub crate_version: CrateVersion, | ||
} | ||
|
||
/// Provides information about the pallet itself and its setup in the runtime. | ||
/// | ||
/// Declare some information and access the information provided by [`PalletInfo`] for a specific | ||
|
@@ -50,6 +64,49 @@ pub trait PalletInfoAccess { | |
fn crate_version() -> CrateVersion; | ||
} | ||
|
||
/// Provide information about a bunch of pallets. | ||
pub trait PalletsInfoAccess { | ||
/// The number of pallets' information that this type represents. | ||
/// | ||
/// You probably don't want this function but `infos()` instead. | ||
fn count() -> usize { | ||
0 | ||
} | ||
|
||
/// Extend the given vector by all of the pallets' information that this type represents. | ||
/// | ||
/// You probably don't want this function but `infos()` instead. | ||
fn accumulate(_accumulator: &mut Vec<PalletInfoData>) {} | ||
|
||
/// All of the pallets' information that this type represents. | ||
fn infos() -> Vec<PalletInfoData> { | ||
let mut result = Vec::with_capacity(Self::count()); | ||
Self::accumulate(&mut result); | ||
result | ||
} | ||
} | ||
|
||
impl PalletsInfoAccess for () {} | ||
impl<T: PalletsInfoAccess> PalletsInfoAccess for (T,) { | ||
fn count() -> usize { | ||
T::count() | ||
} | ||
fn accumulate(acc: &mut Vec<PalletInfoData>) { | ||
T::accumulate(acc) | ||
} | ||
} | ||
|
||
impl<T1: PalletsInfoAccess, T2: PalletsInfoAccess> PalletsInfoAccess for (T1, T2) { | ||
fn count() -> usize { | ||
T1::count() + T2::count() | ||
} | ||
fn accumulate(acc: &mut Vec<PalletInfoData>) { | ||
// The AllPallets type tuplises the pallets in reverse order, so we unreverse them here. | ||
T2::accumulate(acc); | ||
T1::accumulate(acc); | ||
Comment on lines
+105
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ordering will produce vec![T2, T1], is that what we need to re-reverse the order of the pallets? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed. i'll put a comment in. |
||
} | ||
} | ||
|
||
/// The function and pallet name of the Call. | ||
#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug)] | ||
pub struct CallMetadata { | ||
|
@@ -206,6 +263,37 @@ pub trait GetStorageVersion { | |
mod tests { | ||
use super::*; | ||
|
||
struct Pallet1; | ||
impl PalletInfoAccess for Pallet1 { | ||
fn index() -> usize { | ||
1 | ||
} | ||
fn name() -> &'static str { | ||
"Pallet1" | ||
} | ||
fn module_name() -> &'static str { | ||
"pallet1" | ||
} | ||
fn crate_version() -> CrateVersion { | ||
CrateVersion::new(1, 0, 0) | ||
} | ||
} | ||
struct Pallet2; | ||
impl PalletInfoAccess for Pallet2 { | ||
fn index() -> usize { | ||
2 | ||
} | ||
fn name() -> &'static str { | ||
"Pallet2" | ||
} | ||
fn module_name() -> &'static str { | ||
"pallet2" | ||
} | ||
fn crate_version() -> CrateVersion { | ||
CrateVersion::new(1, 0, 0) | ||
} | ||
} | ||
|
||
#[test] | ||
fn check_storage_version_ordering() { | ||
let version = StorageVersion::new(1); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually prefer not to have default implementation when it needs to be overriden in most implementation, this can be error prone, someone can miss to override one implementation.