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
This commit is contained in:
Bastian Köcher
2019-09-30 18:03:13 +02:00
committed by GitHub
parent 98879a2bdb
commit f6c4c47b50
10 changed files with 28 additions and 25 deletions
+4 -4
View File
@@ -72,7 +72,7 @@ where
Result<NativeOrEncoded<R>, Self::Error> Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>, ) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
>( >(
&self, &self,
initialize_block_fn: IB, initialize_block_fn: IB,
@@ -104,7 +104,7 @@ where
Result<NativeOrEncoded<R>, Self::Error> Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>, ) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
>(&self, >(&self,
state: &S, state: &S,
overlay: &mut OverlayedChanges, overlay: &mut OverlayedChanges,
@@ -234,7 +234,7 @@ where
Result<NativeOrEncoded<R>, Self::Error> Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>, ) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
>( >(
&self, &self,
initialize_block_fn: IB, initialize_block_fn: IB,
@@ -342,7 +342,7 @@ where
Result<NativeOrEncoded<R>, Self::Error> Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>, ) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
>(&self, >(&self,
state: &S, state: &S,
changes: &mut OverlayedChanges, changes: &mut OverlayedChanges,
+1 -1
View File
@@ -1419,7 +1419,7 @@ impl<B, E, Block, RA> CallRuntimeAt<Block> for Client<B, E, Block, RA> where
fn call_api_at< fn call_api_at<
'a, 'a,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
C: CoreApi<Block>, C: CoreApi<Block>,
>( >(
&self, &self,
@@ -99,7 +99,7 @@ impl<Block, B, Local> CallExecutor<Block, Blake2Hasher> for
Result<NativeOrEncoded<R>, Self::Error> Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>, ) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
>( >(
&self, &self,
initialize_block_fn: IB, initialize_block_fn: IB,
@@ -160,7 +160,7 @@ impl<Block, B, Local> CallExecutor<Block, Blake2Hasher> for
Result<NativeOrEncoded<R>, Self::Error> Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>, ) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
>(&self, >(&self,
_state: &S, _state: &S,
_changes: &mut OverlayedChanges, _changes: &mut OverlayedChanges,
@@ -334,7 +334,7 @@ mod tests {
Result<NativeOrEncoded<R>, Self::Error> Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>, ) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
>( >(
&self, &self,
_initialize_block_fn: IB, _initialize_block_fn: IB,
@@ -364,7 +364,7 @@ mod tests {
Result<NativeOrEncoded<R>, Self::Error> Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>, ) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
>(&self, >(&self,
_state: &S, _state: &S,
_overlay: &mut OverlayedChanges, _overlay: &mut OverlayedChanges,
+1 -1
View File
@@ -136,7 +136,7 @@ pub trait CallRuntimeAt<Block: BlockT> {
fn call_api_at< fn call_api_at<
'a, 'a,
R: Encode + Decode + PartialEq, R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
C: Core<Block>, C: Core<Block>,
>( >(
&self, &self,
@@ -126,7 +126,7 @@ impl<D: NativeExecutionDispatch> CodeExecutor<Blake2Hasher> for NativeExecutor<D
< <
E: Externalities<Blake2Hasher>, E: Externalities<Blake2Hasher>,
R:Decode + Encode + PartialEq, R:Decode + Encode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe NC: FnOnce() -> result::Result<R, String> + UnwindSafe
>( >(
&self, &self,
ext: &mut E, ext: &mut E,
+1 -1
View File
@@ -193,7 +193,7 @@ pub trait CodeExecutor<H: Hasher>: Sized + Send + Sync {
fn call< fn call<
E: Externalities<H>, E: Externalities<H>,
R: codec::Codec + PartialEq, R: codec::Codec + PartialEq,
NC: FnOnce() -> Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> Result<R, String> + UnwindSafe,
>( >(
&self, &self,
ext: &mut E, ext: &mut E,
@@ -188,11 +188,11 @@ fn generate_native_call_generators(decl: &ItemTrait) -> Result<TokenStream> {
fn convert_between_block_types fn convert_between_block_types
<I: #crate_::runtime_api::Encode, R: #crate_::runtime_api::Decode>( <I: #crate_::runtime_api::Encode, R: #crate_::runtime_api::Decode>(
input: &I, error_desc: &'static str, input: &I, error_desc: &'static str,
) -> ::std::result::Result<R, &'static str> ) -> std::result::Result<R, String>
{ {
<R as #crate_::runtime_api::Decode>::decode( <R as #crate_::runtime_api::Decode>::decode(
&mut &#crate_::runtime_api::Encode::encode(input)[..] &mut &#crate_::runtime_api::Encode::encode(input)[..],
).map_err(|_| error_desc) ).map_err(|e| format!("{} {}", error_desc, e.what()))
} }
)); ));
@@ -203,13 +203,13 @@ fn generate_native_call_generators(decl: &ItemTrait) -> Result<TokenStream> {
let fn_name = generate_native_call_generator_fn_name(&fn_.ident); 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 = return_type_replace_block_with_node_block(fn_.decl.output.clone());
let output_ty = return_type_extract_type(&output); 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, // Every type that is using the `Block` generic parameter, we need to encode/decode,
// to make it compatible between the runtime/node. // to make it compatible between the runtime/node.
let conversions = params.iter().filter(|v| type_is_using_block(&v.1)).map(|(n, t, _)| { let conversions = params.iter().filter(|v| type_is_using_block(&v.1)).map(|(n, t, _)| {
let name_str = format!( 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!( quote!(
let #n: #t = convert_between_block_types(&#n, #name_str)?; let #n: #t = convert_between_block_types(&#n, #name_str)?;
@@ -398,7 +398,7 @@ fn generate_call_api_at_calls(decl: &ItemTrait) -> Result<TokenStream> {
#[cfg(any(feature = "std", test))] #[cfg(any(feature = "std", test))]
pub fn #fn_name< pub fn #fn_name<
R: #crate_::runtime_api::Encode + #crate_::runtime_api::Decode + PartialEq, R: #crate_::runtime_api::Encode + #crate_::runtime_api::Decode + PartialEq,
NC: FnOnce() -> ::std::result::Result<R, &'static str> + ::std::panic::UnwindSafe, NC: FnOnce() -> std::result::Result<R, String> + std::panic::UnwindSafe,
Block: #crate_::runtime_api::BlockT, Block: #crate_::runtime_api::BlockT,
T: #crate_::runtime_api::CallRuntimeAt<Block>, T: #crate_::runtime_api::CallRuntimeAt<Block>,
C: #crate_::runtime_api::Core<Block>, C: #crate_::runtime_api::Core<Block>,
@@ -282,7 +282,7 @@ fn generate_runtime_api_base_structures(impls: &[ItemImpl]) -> Result<TokenStrea
impl<C: #crate_::runtime_api::CallRuntimeAt<#block>> #crate_::runtime_api::ApiExt<#block> impl<C: #crate_::runtime_api::CallRuntimeAt<#block>> #crate_::runtime_api::ApiExt<#block>
for RuntimeApiImpl<C> for RuntimeApiImpl<C>
{ {
fn map_api_result<F: FnOnce(&Self) -> ::std::result::Result<R, E>, R, E>( fn map_api_result<F: FnOnce(&Self) -> std::result::Result<R, E>, R, E>(
&self, &self,
map_call: F map_call: F
) -> ::std::result::Result<R, E> where Self: Sized { ) -> ::std::result::Result<R, E> where Self: Sized {
@@ -50,7 +50,10 @@ fn calling_wasm_runtime_function() {
} }
#[test] #[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() { fn calling_native_runtime_function_with_non_decodable_parameter() {
let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible).build(); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
+5 -5
View File
@@ -250,7 +250,7 @@ impl<'a, B, H, N, T, O, Exec> StateMachine<'a, B, H, N, T, O, Exec> where
Option<ChangesTrieTransaction<H, N>>, Option<ChangesTrieTransaction<H, N>>,
) where ) where
R: Decode + Encode + PartialEq, R: Decode + Encode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
{ {
let mut externalities = ext::Ext::new( let mut externalities = ext::Ext::new(
self.overlay, 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, on_consensus_failure: Handler,
) -> (CallResult<R, Exec::Error>, Option<(B::Transaction, H::Out)>, Option<ChangesTrieTransaction<H, N>>) where ) -> (CallResult<R, Exec::Error>, Option<(B::Transaction, H::Out)>, Option<ChangesTrieTransaction<H, N>>) where
R: Decode + Encode + PartialEq, R: Decode + Encode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
Handler: FnOnce( Handler: FnOnce(
CallResult<R, Exec::Error>, CallResult<R, Exec::Error>,
CallResult<R, Exec::Error> CallResult<R, Exec::Error>
@@ -334,7 +334,7 @@ impl<'a, B, H, N, T, O, Exec> StateMachine<'a, B, H, N, T, O, Exec> where
orig_prospective: OverlayedChangeSet, orig_prospective: OverlayedChangeSet,
) -> (CallResult<R, Exec::Error>, Option<(B::Transaction, H::Out)>, Option<ChangesTrieTransaction<H, N>>) where ) -> (CallResult<R, Exec::Error>, Option<(B::Transaction, H::Out)>, Option<ChangesTrieTransaction<H, N>>) where
R: Decode + Encode + PartialEq, R: Decode + Encode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
{ {
let (result, was_native, storage_delta, changes_delta) = self.execute_aux( let (result, was_native, storage_delta, changes_delta) = self.execute_aux(
compute_tx, compute_tx,
@@ -374,7 +374,7 @@ impl<'a, B, H, N, T, O, Exec> StateMachine<'a, B, H, N, T, O, Exec> where
Option<ChangesTrieTransaction<H, N>>, Option<ChangesTrieTransaction<H, N>>,
), Box<dyn Error>> where ), Box<dyn Error>> where
R: Decode + Encode + PartialEq, R: Decode + Encode + PartialEq,
NC: FnOnce() -> result::Result<R, &'static str> + UnwindSafe, NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
Handler: FnOnce( Handler: FnOnce(
CallResult<R, Exec::Error>, CallResult<R, Exec::Error>,
CallResult<R, Exec::Error>, CallResult<R, Exec::Error>,
@@ -753,7 +753,7 @@ mod tests {
impl<H: Hasher> CodeExecutor<H> for DummyCodeExecutor { impl<H: Hasher> CodeExecutor<H> for DummyCodeExecutor {
type Error = u8; type Error = u8;
fn call<E: Externalities<H>, R: Encode + Decode + PartialEq, NC: FnOnce() -> result::Result<R, &'static str>>( fn call<E: Externalities<H>, R: Encode + Decode + PartialEq, NC: FnOnce() -> result::Result<R, String>>(
&self, &self,
ext: &mut E, ext: &mut E,
_method: &str, _method: &str,