Introduce code_substitute (#8898)

This introduces a new field `code_substitute` into the chain spec. This
can be used to substitute the on-chain wasm starting from a given block
until there is another wasm on chain (determined through the
`spec_version`). This can be used to fix broken on chain wasm runtimes.
This commit is contained in:
Bastian Köcher
2021-05-25 16:07:36 +02:00
committed by GitHub
parent a7a326ee07
commit 53e28b98e9
12 changed files with 286 additions and 43 deletions
+12 -1
View File
@@ -21,7 +21,7 @@
use std::{borrow::Cow, fs::File, path::PathBuf, sync::Arc, collections::HashMap};
use serde::{Serialize, Deserialize};
use sp_core::storage::{StorageKey, StorageData, ChildInfo, Storage, StorageChild};
use sp_core::{storage::{StorageKey, StorageData, ChildInfo, Storage, StorageChild}, Bytes};
use sp_runtime::BuildStorage;
use serde_json as json;
use crate::{RuntimeGenesis, ChainType, extension::GetExtension, Properties};
@@ -160,6 +160,12 @@ struct ClientSpec<E> {
#[serde(skip_serializing)]
genesis: serde::de::IgnoredAny,
light_sync_state: Option<SerializableLightSyncState>,
/// Mapping from `block_hash` to `wasm_code`.
///
/// The given `wasm_code` will be used to substitute the on-chain wasm code from the given
/// block hash onwards.
#[serde(default)]
code_substitutes: HashMap<String, Bytes>,
}
/// A type denoting empty extensions.
@@ -249,6 +255,7 @@ impl<G, E> ChainSpec<G, E> {
consensus_engine: (),
genesis: Default::default(),
light_sync_state: None,
code_substitutes: HashMap::new(),
};
ChainSpec {
@@ -395,6 +402,10 @@ where
fn set_light_sync_state(&mut self, light_sync_state: SerializableLightSyncState) {
ChainSpec::set_light_sync_state(self, light_sync_state)
}
fn code_substitutes(&self) -> std::collections::HashMap<String, Vec<u8>> {
self.client_spec.code_substitutes.iter().map(|(h, c)| (h.clone(), c.0.clone())).collect()
}
}
/// Hardcoded infomation that allows light clients to sync quickly.
+2
View File
@@ -161,6 +161,8 @@ pub trait ChainSpec: BuildStorage + Send + Sync {
fn set_storage(&mut self, storage: Storage);
/// Hardcode infomation to allow light clients to sync quickly into the chain spec.
fn set_light_sync_state(&mut self, light_sync_state: SerializableLightSyncState);
/// Returns code substitutes that should be used for the on chain wasm.
fn code_substitutes(&self) -> std::collections::HashMap<String, Vec<u8>>;
}
impl std::fmt::Debug for dyn ChainSpec {