Implement runtime version checks in set_code (#4548)

* Implement runtime version checks in `set_code`

Check that the new runtime code given to `set_code` fullfills some
requirements:

- `spec_name` matches
- `spec_version` does not decreases
- `impl_version` does not decreases
- Either `spec_version` and `impl_version` increase

* Make tests almost work

* Some fixes after master merge

* Fix tests

* Add missed file

* Make depedency check happy?

* Remove leftover `sc-executor`

* AHHHHH

* Reset debug stuff

* Remove some 'static

* More 'static

* Some docs

* Update `Cargo.lock`
This commit is contained in:
Bastian Köcher
2020-01-16 13:58:37 +01:00
committed by Gavin Wood
parent 437772be9e
commit afc3318f21
38 changed files with 584 additions and 279 deletions
+22 -7
View File
@@ -24,7 +24,7 @@ use hash_db::Hasher;
use codec::{Decode, Encode, Codec};
use sp_core::{
storage::{well_known_keys, ChildInfo}, NativeOrEncoded, NeverNativeValue,
traits::CodeExecutor, hexdisplay::HexDisplay
traits::{CodeExecutor, CallInWasmExt}, hexdisplay::HexDisplay
};
use overlayed_changes::OverlayedChangeSet;
use sp_externalities::Extensions;
@@ -191,7 +191,7 @@ pub struct StateMachine<'a, B, H, N, T, Exec>
impl<'a, B, H, N, T, Exec> StateMachine<'a, B, H, N, T, Exec> where
H: Hasher,
H::Out: Ord + 'static + codec::Codec,
Exec: CodeExecutor,
Exec: CodeExecutor + Clone + 'static,
B: Backend<H>,
T: ChangesTrieStorage<H, N>,
N: crate::changes_trie::BlockNumber,
@@ -204,8 +204,10 @@ impl<'a, B, H, N, T, Exec> StateMachine<'a, B, H, N, T, Exec> where
exec: &'a Exec,
method: &'a str,
call_data: &'a [u8],
extensions: Extensions,
mut extensions: Extensions,
) -> Self {
extensions.register(CallInWasmExt::new(exec.clone()));
Self {
backend,
exec,
@@ -451,7 +453,7 @@ where
B: Backend<H>,
H: Hasher,
H::Out: Ord + 'static + codec::Codec,
Exec: CodeExecutor,
Exec: CodeExecutor + Clone + 'static,
{
let trie_backend = backend.as_trie_backend()
.ok_or_else(|| Box::new(ExecutionError::UnableToGenerateProof) as Box<dyn Error>)?;
@@ -478,7 +480,7 @@ where
S: trie_backend_essence::TrieBackendStorage<H>,
H: Hasher,
H::Out: Ord + 'static + codec::Codec,
Exec: CodeExecutor,
Exec: CodeExecutor + 'static + Clone,
{
let proving_backend = proving_backend::ProvingBackend::new(trie_backend);
let mut sm = StateMachine::<_, H, _, InMemoryChangesTrieStorage<H, u64>, Exec>::new(
@@ -504,7 +506,7 @@ pub fn execution_proof_check<H, Exec>(
) -> Result<Vec<u8>, Box<dyn Error>>
where
H: Hasher,
Exec: CodeExecutor,
Exec: CodeExecutor + Clone + 'static,
H::Out: Ord + 'static + codec::Codec,
{
let trie_backend = create_proof_check_backend::<H>(root.into(), proof)?;
@@ -522,7 +524,7 @@ pub fn execution_proof_check_on_trie_backend<H, Exec>(
where
H: Hasher,
H::Out: Ord + 'static + codec::Codec,
Exec: CodeExecutor,
Exec: CodeExecutor + Clone + 'static,
{
let mut sm = StateMachine::<_, H, _, InMemoryChangesTrieStorage<H, u64>, Exec>::new(
trie_backend, None, overlay, exec, method, call_data, Extensions::default(),
@@ -741,6 +743,7 @@ mod tests {
};
use sp_core::{Blake2Hasher, map, traits::Externalities, storage::ChildStorageKey};
#[derive(Clone)]
struct DummyCodeExecutor {
change_changes_trie_config: bool,
native_available: bool,
@@ -797,6 +800,18 @@ mod tests {
}
}
impl sp_core::traits::CallInWasm for DummyCodeExecutor {
fn call_in_wasm(
&self,
_: &[u8],
_: &str,
_: &[u8],
_: &mut dyn Externalities,
) -> std::result::Result<Vec<u8>, String> {
unimplemented!("Not required in tests.")
}
}
#[test]
fn execute_works() {
let backend = trie_backend::tests::test_trie();