This repository was 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.7k
ChainSpec trait #5185
Merged
Merged
ChainSpec trait #5185
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a85086c
ChainSpec trait
arkpar 5cfae27
Apply suggestions from code review
arkpar ca1596e
Added docs
arkpar b960f5f
Fixed build
arkpar 4fa9746
Fixed build
arkpar 3c0e174
Merge branch 'master' of github.com:paritytech/substrate into a-dyn-spec
arkpar 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ | |
| //! Chain Spec extensions helpers. | ||
|
|
||
| use std::fmt::Debug; | ||
| use std::any::{TypeId, Any}; | ||
|
|
||
| use std::collections::BTreeMap; | ||
|
|
||
| use serde::{Serialize, Deserialize, de::DeserializeOwned}; | ||
|
|
@@ -120,6 +122,8 @@ pub trait Extension: Serialize + DeserializeOwned + Clone { | |
|
|
||
| /// Get an extension of specific type. | ||
| fn get<T: 'static>(&self) -> Option<&T>; | ||
| /// Get an extension of specific type as refernce to `Any` | ||
| fn get_any(&self, t: TypeId) -> &dyn Any; | ||
|
|
||
| /// Get forkable extensions of specific type. | ||
| fn forks<BlockNumber, T>(&self) -> Option<Forks<BlockNumber, T>> where | ||
|
|
@@ -137,6 +141,7 @@ impl Extension for crate::NoExtension { | |
| type Forks = Self; | ||
|
|
||
| fn get<T: 'static>(&self) -> Option<&T> { None } | ||
| fn get_any(&self, _t: TypeId) -> &dyn Any { self } | ||
| } | ||
|
|
||
| pub trait IsForks { | ||
|
|
@@ -225,22 +230,25 @@ impl<B, E> Extension for Forks<B, E> where | |
| type Forks = Self; | ||
|
|
||
| fn get<T: 'static>(&self) -> Option<&T> { | ||
| use std::any::{TypeId, Any}; | ||
|
|
||
| match TypeId::of::<T>() { | ||
| x if x == TypeId::of::<E>() => Any::downcast_ref(&self.base), | ||
| _ => self.base.get(), | ||
| } | ||
| } | ||
|
|
||
| fn get_any(&self, t: TypeId) -> &dyn Any { | ||
| match t { | ||
| x if x == TypeId::of::<E>() => &self.base, | ||
| _ => self.base.get_any(t), | ||
| } | ||
| } | ||
|
|
||
| fn forks<BlockNumber, T>(&self) -> Option<Forks<BlockNumber, T>> where | ||
| BlockNumber: Ord + Clone + 'static, | ||
| T: Group + 'static, | ||
| <Self::Forks as IsForks>::Extension: Extension, | ||
| <<Self::Forks as IsForks>::Extension as Group>::Fork: Extension, | ||
| { | ||
| use std::any::{TypeId, Any}; | ||
|
|
||
| if TypeId::of::<BlockNumber>() == TypeId::of::<B>() { | ||
| Any::downcast_ref(&self.for_type::<T>()?).cloned() | ||
| } else { | ||
|
|
@@ -250,6 +258,24 @@ impl<B, E> Extension for Forks<B, E> where | |
| } | ||
| } | ||
|
|
||
| /// A subset if the `Extension` trait that only allows for quering extensions. | ||
| pub trait GetExtension { | ||
| /// Get an extension of specific type. | ||
| fn get_any(&self, t: TypeId) -> &dyn Any; | ||
| } | ||
|
|
||
| impl <E: Extension> GetExtension for E { | ||
| fn get_any(&self, t: TypeId) -> &dyn Any { | ||
| Extension::get_any(self, t) | ||
| } | ||
| } | ||
|
|
||
| /// Helper function that queries an extension by type from `GetExtension` | ||
| /// trait object. | ||
| pub fn get_extension<T: 'static>(e: &dyn GetExtension) -> Option<&T> { | ||
|
Member
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. Docs missing. |
||
| Any::downcast_ref(GetExtension::get_any(e, TypeId::of::<T>())) | ||
|
Contributor
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. Why is this function needed and why can't we just use
Member
Author
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. Trait objects can't have generic methods. |
||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.