From f6c4c47b50b5ff8cbc0b4d76dc08d73833ada1d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 30 Sep 2019 18:03:13 +0200 Subject: [PATCH] Give more information why conversion between native and runtime failed (#3720) * Give more information why conversion between native and runtime failed This adds the SCALE error description to the error when the conversion between native and runtime failed. * Fixes tests --- substrate/core/client/src/call_executor.rs | 8 ++++---- substrate/core/client/src/client.rs | 2 +- substrate/core/client/src/light/call_executor.rs | 8 ++++---- substrate/core/client/src/runtime_api.rs | 2 +- substrate/core/executor/src/native_executor.rs | 2 +- substrate/core/primitives/src/traits.rs | 2 +- .../core/sr-api-macros/src/decl_runtime_apis.rs | 12 ++++++------ .../core/sr-api-macros/src/impl_runtime_apis.rs | 2 +- substrate/core/sr-api-macros/tests/runtime_calls.rs | 5 ++++- substrate/core/state-machine/src/lib.rs | 10 +++++----- 10 files changed, 28 insertions(+), 25 deletions(-) diff --git a/substrate/core/client/src/call_executor.rs b/substrate/core/client/src/call_executor.rs index b49a58a0e5..ebb882709d 100644 --- a/substrate/core/client/src/call_executor.rs +++ b/substrate/core/client/src/call_executor.rs @@ -72,7 +72,7 @@ where Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >( &self, initialize_block_fn: IB, @@ -104,7 +104,7 @@ where Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >(&self, state: &S, overlay: &mut OverlayedChanges, @@ -234,7 +234,7 @@ where Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >( &self, initialize_block_fn: IB, @@ -342,7 +342,7 @@ where Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >(&self, state: &S, changes: &mut OverlayedChanges, diff --git a/substrate/core/client/src/client.rs b/substrate/core/client/src/client.rs index 8b7d2dc995..595aa0e9c7 100644 --- a/substrate/core/client/src/client.rs +++ b/substrate/core/client/src/client.rs @@ -1419,7 +1419,7 @@ impl CallRuntimeAt for Client where fn call_api_at< 'a, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, C: CoreApi, >( &self, diff --git a/substrate/core/client/src/light/call_executor.rs b/substrate/core/client/src/light/call_executor.rs index 2c9c1f2995..ec182cca11 100644 --- a/substrate/core/client/src/light/call_executor.rs +++ b/substrate/core/client/src/light/call_executor.rs @@ -99,7 +99,7 @@ impl CallExecutor for Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >( &self, initialize_block_fn: IB, @@ -160,7 +160,7 @@ impl CallExecutor for Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >(&self, _state: &S, _changes: &mut OverlayedChanges, @@ -334,7 +334,7 @@ mod tests { Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >( &self, _initialize_block_fn: IB, @@ -364,7 +364,7 @@ mod tests { Result, Self::Error> ) -> Result, Self::Error>, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, >(&self, _state: &S, _overlay: &mut OverlayedChanges, diff --git a/substrate/core/client/src/runtime_api.rs b/substrate/core/client/src/runtime_api.rs index ed5c9fad48..a5700951e9 100644 --- a/substrate/core/client/src/runtime_api.rs +++ b/substrate/core/client/src/runtime_api.rs @@ -136,7 +136,7 @@ pub trait CallRuntimeAt { fn call_api_at< 'a, R: Encode + Decode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, C: Core, >( &self, diff --git a/substrate/core/executor/src/native_executor.rs b/substrate/core/executor/src/native_executor.rs index 3de795fc27..cdf0be7f76 100644 --- a/substrate/core/executor/src/native_executor.rs +++ b/substrate/core/executor/src/native_executor.rs @@ -126,7 +126,7 @@ impl CodeExecutor for NativeExecutor, R:Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe + NC: FnOnce() -> result::Result + UnwindSafe >( &self, ext: &mut E, diff --git a/substrate/core/primitives/src/traits.rs b/substrate/core/primitives/src/traits.rs index 0274c44ace..b173d8512c 100644 --- a/substrate/core/primitives/src/traits.rs +++ b/substrate/core/primitives/src/traits.rs @@ -193,7 +193,7 @@ pub trait CodeExecutor: Sized + Send + Sync { fn call< E: Externalities, R: codec::Codec + PartialEq, - NC: FnOnce() -> Result + UnwindSafe, + NC: FnOnce() -> Result + UnwindSafe, >( &self, ext: &mut E, diff --git a/substrate/core/sr-api-macros/src/decl_runtime_apis.rs b/substrate/core/sr-api-macros/src/decl_runtime_apis.rs index 0e69c2b76d..12639bd1c1 100644 --- a/substrate/core/sr-api-macros/src/decl_runtime_apis.rs +++ b/substrate/core/sr-api-macros/src/decl_runtime_apis.rs @@ -188,11 +188,11 @@ fn generate_native_call_generators(decl: &ItemTrait) -> Result { fn convert_between_block_types ( input: &I, error_desc: &'static str, - ) -> ::std::result::Result + ) -> std::result::Result { ::decode( - &mut &#crate_::runtime_api::Encode::encode(input)[..] - ).map_err(|_| error_desc) + &mut &#crate_::runtime_api::Encode::encode(input)[..], + ).map_err(|e| format!("{} {}", error_desc, e.what())) } )); @@ -203,13 +203,13 @@ fn generate_native_call_generators(decl: &ItemTrait) -> Result { let fn_name = generate_native_call_generator_fn_name(&fn_.ident); let output = return_type_replace_block_with_node_block(fn_.decl.output.clone()); let output_ty = return_type_extract_type(&output); - let output = quote!( ::std::result::Result<#output_ty, &'static str> ); + let output = quote!( std::result::Result<#output_ty, String> ); // Every type that is using the `Block` generic parameter, we need to encode/decode, // to make it compatible between the runtime/node. let conversions = params.iter().filter(|v| type_is_using_block(&v.1)).map(|(n, t, _)| { let name_str = format!( - "Could not convert parameter `{}` between node and runtime!", quote!(#n) + "Could not convert parameter `{}` between node and runtime:", quote!(#n) ); quote!( let #n: #t = convert_between_block_types(&#n, #name_str)?; @@ -398,7 +398,7 @@ fn generate_call_api_at_calls(decl: &ItemTrait) -> Result { #[cfg(any(feature = "std", test))] pub fn #fn_name< R: #crate_::runtime_api::Encode + #crate_::runtime_api::Decode + PartialEq, - NC: FnOnce() -> ::std::result::Result + ::std::panic::UnwindSafe, + NC: FnOnce() -> std::result::Result + std::panic::UnwindSafe, Block: #crate_::runtime_api::BlockT, T: #crate_::runtime_api::CallRuntimeAt, C: #crate_::runtime_api::Core, diff --git a/substrate/core/sr-api-macros/src/impl_runtime_apis.rs b/substrate/core/sr-api-macros/src/impl_runtime_apis.rs index 973fa0558a..fb154aa112 100644 --- a/substrate/core/sr-api-macros/src/impl_runtime_apis.rs +++ b/substrate/core/sr-api-macros/src/impl_runtime_apis.rs @@ -282,7 +282,7 @@ fn generate_runtime_api_base_structures(impls: &[ItemImpl]) -> Result> #crate_::runtime_api::ApiExt<#block> for RuntimeApiImpl { - fn map_api_result ::std::result::Result, R, E>( + fn map_api_result std::result::Result, R, E>( &self, map_call: F ) -> ::std::result::Result where Self: Sized { diff --git a/substrate/core/sr-api-macros/tests/runtime_calls.rs b/substrate/core/sr-api-macros/tests/runtime_calls.rs index 6b79e52ee2..f33a9e257a 100644 --- a/substrate/core/sr-api-macros/tests/runtime_calls.rs +++ b/substrate/core/sr-api-macros/tests/runtime_calls.rs @@ -50,7 +50,10 @@ fn calling_wasm_runtime_function() { } #[test] -#[should_panic(expected = "Could not convert parameter `param` between node and runtime!")] +#[should_panic( + expected = + "Could not convert parameter `param` between node and runtime: DecodeFails always fails" +)] fn calling_native_runtime_function_with_non_decodable_parameter() { let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible).build(); let runtime_api = client.runtime_api(); diff --git a/substrate/core/state-machine/src/lib.rs b/substrate/core/state-machine/src/lib.rs index 92dd813b89..5220b4b31d 100644 --- a/substrate/core/state-machine/src/lib.rs +++ b/substrate/core/state-machine/src/lib.rs @@ -250,7 +250,7 @@ impl<'a, B, H, N, T, O, Exec> StateMachine<'a, B, H, N, T, O, Exec> where Option>, ) where R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, { let mut externalities = ext::Ext::new( self.overlay, @@ -295,7 +295,7 @@ impl<'a, B, H, N, T, O, Exec> StateMachine<'a, B, H, N, T, O, Exec> where on_consensus_failure: Handler, ) -> (CallResult, Option<(B::Transaction, H::Out)>, Option>) where R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, Handler: FnOnce( CallResult, CallResult @@ -334,7 +334,7 @@ impl<'a, B, H, N, T, O, Exec> StateMachine<'a, B, H, N, T, O, Exec> where orig_prospective: OverlayedChangeSet, ) -> (CallResult, Option<(B::Transaction, H::Out)>, Option>) where R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, { let (result, was_native, storage_delta, changes_delta) = self.execute_aux( compute_tx, @@ -374,7 +374,7 @@ impl<'a, B, H, N, T, O, Exec> StateMachine<'a, B, H, N, T, O, Exec> where Option>, ), Box> where R: Decode + Encode + PartialEq, - NC: FnOnce() -> result::Result + UnwindSafe, + NC: FnOnce() -> result::Result + UnwindSafe, Handler: FnOnce( CallResult, CallResult, @@ -753,7 +753,7 @@ mod tests { impl CodeExecutor for DummyCodeExecutor { type Error = u8; - fn call, R: Encode + Decode + PartialEq, NC: FnOnce() -> result::Result>( + fn call, R: Encode + Decode + PartialEq, NC: FnOnce() -> result::Result>( &self, ext: &mut E, _method: &str,