support: BuildStorage methods to take self reference (#3884)

* support: BuildStorage methods to take self reference.

There is no reason to consume the GenesisConfig when using it to
initialize a new storage backend. Instead, build_storage and
assimilate_storage now operator on self references.

* Bump node runtime impl_version.
This commit is contained in:
Jim Posen
2019-10-25 11:44:57 +02:00
committed by GitHub
parent 44696f46c3
commit e05e624a3a
5 changed files with 21 additions and 23 deletions
+4 -4
View File
@@ -71,7 +71,7 @@ impl<G: RuntimeGenesis> GenesisSource<G> {
}
impl<'a, G: RuntimeGenesis, E> BuildStorage for &'a ChainSpec<G, E> {
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
fn build_storage(&self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
match self.genesis.resolve()? {
Genesis::Runtime(gc) => gc.build_storage(),
Genesis::Raw(map, children_map) => Ok((
@@ -85,7 +85,7 @@ impl<'a, G: RuntimeGenesis, E> BuildStorage for &'a ChainSpec<G, E> {
}
fn assimilate_storage(
self,
&self,
_: &mut (StorageOverlay, ChildrenStorageOverlay)
) -> Result<(), String> {
Err("`assimilate_storage` not implemented for `ChainSpec`.".into())
@@ -289,11 +289,11 @@ mod tests {
impl BuildStorage for Genesis {
fn assimilate_storage(
self,
&self,
storage: &mut (StorageOverlay, ChildrenStorageOverlay),
) -> Result<(), String> {
storage.0.extend(
self.0.into_iter().map(|(a, b)| (a.into_bytes(), b.into_bytes()))
self.0.iter().map(|(a, b)| (a.clone().into_bytes(), b.clone().into_bytes()))
);
Ok(())
}
+11 -13
View File
@@ -123,14 +123,14 @@ use crate::traits::IdentifyAccount;
#[cfg(feature = "std")]
pub trait BuildStorage: Sized {
/// Build the storage out of this builder.
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
fn build_storage(&self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
let mut storage = (Default::default(), Default::default());
self.assimilate_storage(&mut storage)?;
Ok(storage)
}
/// Assimilate the storage for this module into pre-existing overlays.
fn assimilate_storage(
self,
&self,
storage: &mut (StorageOverlay, ChildrenStorageOverlay),
) -> Result<(), String>;
}
@@ -140,26 +140,24 @@ pub trait BuildStorage: Sized {
pub trait BuildModuleGenesisStorage<T, I>: Sized {
/// Create the module genesis storage into the given `storage` and `child_storage`.
fn build_module_genesis_storage(
self,
&self,
storage: &mut (StorageOverlay, ChildrenStorageOverlay),
) -> Result<(), String>;
}
#[cfg(feature = "std")]
impl BuildStorage for (StorageOverlay, ChildrenStorageOverlay) {
fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> {
Ok(self)
}
fn assimilate_storage(
self,
&self,
storage: &mut (StorageOverlay, ChildrenStorageOverlay),
)-> Result<(), String> {
storage.0.extend(self.0);
for (k, other_map) in self.1.into_iter() {
storage.0.extend(self.0.iter().map(|(k, v)| (k.clone(), v.clone())));
for (k, other_map) in self.1.iter() {
let k = k.clone();
if let Some(map) = storage.1.get_mut(&k) {
map.extend(other_map);
map.extend(other_map.iter().map(|(k, v)| (k.clone(), v.clone())));
} else {
storage.1.insert(k, other_map);
storage.1.insert(k, other_map.clone());
}
}
Ok(())
@@ -522,11 +520,11 @@ macro_rules! impl_outer_config {
#[cfg(any(feature = "std", test))]
impl $crate::BuildStorage for $main {
fn assimilate_storage(
self,
&self,
storage: &mut ($crate::StorageOverlay, $crate::ChildrenStorageOverlay),
) -> std::result::Result<(), String> {
$(
if let Some(extra) = self.[< $snake $(_ $instance )? >] {
if let Some(ref extra) = self.[< $snake $(_ $instance )? >] {
$crate::impl_outer_config! {
@CALL_FN
$concrete;
+1 -1
View File
@@ -84,7 +84,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 184,
impl_version: 184,
impl_version: 185,
apis: RUNTIME_API_VERSIONS,
};
@@ -51,7 +51,7 @@ impl BuilderDef {
is_generic |= ext::expr_contains_ident(&builder, &def.module_runtime_generic);
is_generic |= line.is_generic;
data = Some(quote_spanned!(builder.span() => &(#builder)(&self)));
data = Some(quote_spanned!(builder.span() => &(#builder)(self)));
} else if let Some(config) = &line.config {
is_generic |= line.is_generic;
@@ -98,7 +98,7 @@ impl BuilderDef {
blocks.push(quote_spanned! { builder.span() =>
let extra_genesis_builder: fn(&Self) = #builder;
extra_genesis_builder(&self);
extra_genesis_builder(self);
});
}
@@ -138,7 +138,7 @@ fn impl_build_storage(
quote!{
#[cfg(feature = "std")]
impl#genesis_impl GenesisConfig#genesis_struct #genesis_where_clause {
pub fn build_storage #fn_generic (self) -> std::result::Result<
pub fn build_storage #fn_generic (&self) -> std::result::Result<
(
#scrate::sr_primitives::StorageOverlay,
#scrate::sr_primitives::ChildrenStorageOverlay,
@@ -152,7 +152,7 @@ fn impl_build_storage(
/// Assimilate the storage for this module into pre-existing overlays.
pub fn assimilate_storage #fn_generic (
self,
&self,
tuple_storage: &mut (
#scrate::sr_primitives::StorageOverlay,
#scrate::sr_primitives::ChildrenStorageOverlay,
@@ -170,7 +170,7 @@ fn impl_build_storage(
#where_clause
{
fn build_module_genesis_storage(
self,
&self,
storage: &mut (
#scrate::sr_primitives::StorageOverlay,
#scrate::sr_primitives::ChildrenStorageOverlay,