mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 07:37:57 +00:00
Simplify runtime api error handling (#8114)
* Ahh * Work work work * Fix all the compilation errors * Fix test * More fixes...
This commit is contained in:
@@ -23,7 +23,6 @@ use sp_api::{
|
||||
use sp_runtime::{traits::{GetNodeBlockType, Block as BlockT}, generic::BlockId};
|
||||
use sp_core::NativeOrEncoded;
|
||||
use substrate_test_runtime_client::runtime::Block;
|
||||
use sp_blockchain::Result;
|
||||
|
||||
/// The declaration of the `Runtime` type and the implementation of the `GetNodeBlockType`
|
||||
/// trait are done by the `construct_runtime!` macro in a real runtime.
|
||||
@@ -105,7 +104,7 @@ mock_impl_runtime_apis! {
|
||||
|
||||
#[advanced]
|
||||
fn same_name(_: &BlockId<Block>) ->
|
||||
std::result::Result<
|
||||
Result<
|
||||
NativeOrEncoded<()>,
|
||||
ApiError
|
||||
>
|
||||
@@ -115,7 +114,7 @@ mock_impl_runtime_apis! {
|
||||
|
||||
#[advanced]
|
||||
fn wild_card(at: &BlockId<Block>, _: u32) ->
|
||||
std::result::Result<
|
||||
Result<
|
||||
NativeOrEncoded<()>,
|
||||
ApiError
|
||||
>
|
||||
@@ -124,7 +123,7 @@ mock_impl_runtime_apis! {
|
||||
// yeah
|
||||
Ok(().into())
|
||||
} else {
|
||||
Err(ApiError::new("MockApi", codec::Error::from("Ohh noooo")))
|
||||
Err((Box::from("Test error") as Box<dyn std::error::Error + Send + Sync>).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,33 +142,33 @@ type TestClient = substrate_test_runtime_client::client::Client<
|
||||
|
||||
#[test]
|
||||
fn test_client_side_function_signature() {
|
||||
let _test: fn(&RuntimeApiImpl<Block, TestClient>, &BlockId<Block>, u64) -> Result<()> =
|
||||
let _test: fn(&RuntimeApiImpl<Block, TestClient>, &BlockId<Block>, u64) -> Result<(), ApiError> =
|
||||
RuntimeApiImpl::<Block, TestClient>::test;
|
||||
let _something_with_block:
|
||||
fn(&RuntimeApiImpl<Block, TestClient>, &BlockId<Block>, Block) -> Result<Block> =
|
||||
fn(&RuntimeApiImpl<Block, TestClient>, &BlockId<Block>, Block) -> Result<Block, ApiError> =
|
||||
RuntimeApiImpl::<Block, TestClient>::something_with_block;
|
||||
|
||||
#[allow(deprecated)]
|
||||
let _same_name_before_version_2:
|
||||
fn(&RuntimeApiImpl<Block, TestClient>, &BlockId<Block>) -> Result<String> =
|
||||
fn(&RuntimeApiImpl<Block, TestClient>, &BlockId<Block>) -> Result<String, ApiError> =
|
||||
RuntimeApiImpl::<Block, TestClient>::same_name_before_version_2;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_runtime_api_info() {
|
||||
assert_eq!(&Api::<Block, Error = ()>::ID, &runtime_decl_for_Api::ID);
|
||||
assert_eq!(Api::<Block, Error = ()>::VERSION, runtime_decl_for_Api::VERSION);
|
||||
assert_eq!(Api::<Block, Error = ()>::VERSION, 1);
|
||||
assert_eq!(&Api::<Block>::ID, &runtime_decl_for_Api::ID);
|
||||
assert_eq!(Api::<Block>::VERSION, runtime_decl_for_Api::VERSION);
|
||||
assert_eq!(Api::<Block>::VERSION, 1);
|
||||
|
||||
assert_eq!(
|
||||
ApiWithCustomVersion::<Block, Error = ()>::VERSION,
|
||||
ApiWithCustomVersion::<Block>::VERSION,
|
||||
runtime_decl_for_ApiWithCustomVersion::VERSION,
|
||||
);
|
||||
assert_eq!(
|
||||
&ApiWithCustomVersion::<Block, Error = ()>::ID,
|
||||
&ApiWithCustomVersion::<Block>::ID,
|
||||
&runtime_decl_for_ApiWithCustomVersion::ID,
|
||||
);
|
||||
assert_eq!(ApiWithCustomVersion::<Block, Error = ()>::VERSION, 2);
|
||||
assert_eq!(ApiWithCustomVersion::<Block>::VERSION, 2);
|
||||
}
|
||||
|
||||
fn check_runtime_api_versions_contains<T: RuntimeApiInfo + ?Sized>() {
|
||||
@@ -178,9 +177,9 @@ fn check_runtime_api_versions_contains<T: RuntimeApiInfo + ?Sized>() {
|
||||
|
||||
#[test]
|
||||
fn check_runtime_api_versions() {
|
||||
check_runtime_api_versions_contains::<dyn Api<Block, Error = ()>>();
|
||||
check_runtime_api_versions_contains::<dyn ApiWithCustomVersion<Block, Error = ()>>();
|
||||
check_runtime_api_versions_contains::<dyn sp_api::Core<Block, Error = ()>>();
|
||||
check_runtime_api_versions_contains::<dyn Api<Block>>();
|
||||
check_runtime_api_versions_contains::<dyn ApiWithCustomVersion<Block>>();
|
||||
check_runtime_api_versions_contains::<dyn sp_api::Core<Block>>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -188,9 +187,9 @@ fn mock_runtime_api_has_api() {
|
||||
let mock = MockApi { block: None };
|
||||
|
||||
assert!(
|
||||
mock.has_api::<dyn ApiWithCustomVersion<Block, Error = ()>>(&BlockId::Number(0)).unwrap(),
|
||||
mock.has_api::<dyn ApiWithCustomVersion<Block>>(&BlockId::Number(0)).unwrap(),
|
||||
);
|
||||
assert!(mock.has_api::<dyn Api<Block, Error = ()>>(&BlockId::Number(0)).unwrap());
|
||||
assert!(mock.has_api::<dyn Api<Block>>(&BlockId::Number(0)).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -209,7 +208,7 @@ fn mock_runtime_api_works_with_advanced() {
|
||||
Api::<Block>::same_name(&mock, &BlockId::Number(0)).unwrap();
|
||||
mock.wild_card(&BlockId::Number(1337), 1).unwrap();
|
||||
assert_eq!(
|
||||
ApiError::new("MockApi", ::codec::Error::from("Ohh noooo")),
|
||||
mock.wild_card(&BlockId::Number(1336), 1).unwrap_err()
|
||||
"Test error".to_string(),
|
||||
mock.wild_card(&BlockId::Number(1336), 1).unwrap_err().to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -50,10 +50,7 @@ fn calling_wasm_runtime_function() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(
|
||||
expected =
|
||||
"Could not convert parameter `param` between node and runtime: DecodeFails always fails"
|
||||
)]
|
||||
#[should_panic(expected = "FailedToConvertParameter { function: \"fail_convert_parameter\"")]
|
||||
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();
|
||||
@@ -62,7 +59,7 @@ fn calling_native_runtime_function_with_non_decodable_parameter() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Could not convert return value from runtime to node!")]
|
||||
#[should_panic(expected = "FailedToConvertReturnValue { function: \"fail_convert_return_value\"")]
|
||||
fn calling_native_runtime_function_with_non_decodable_return_value() {
|
||||
let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible).build();
|
||||
let runtime_api = client.runtime_api();
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
use substrate_test_runtime_client::runtime::Block;
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
pub trait Api {
|
||||
fn test(data: u64);
|
||||
}
|
||||
}
|
||||
|
||||
struct MockApi;
|
||||
|
||||
sp_api::mock_impl_runtime_apis! {
|
||||
impl Api<Block> for MockApi {
|
||||
type OtherData = u32;
|
||||
|
||||
fn test(data: u64) {}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -1,5 +0,0 @@
|
||||
error: Only associated type with name `Error` is allowed
|
||||
--> $DIR/mock_only_error_associated_type.rs:13:3
|
||||
|
|
||||
13 | type OtherData = u32;
|
||||
| ^^^^
|
||||
@@ -1,29 +0,0 @@
|
||||
use substrate_test_runtime_client::runtime::Block;
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
pub trait Api {
|
||||
fn test(data: u64);
|
||||
}
|
||||
|
||||
pub trait Api2 {
|
||||
fn test(data: u64);
|
||||
}
|
||||
}
|
||||
|
||||
struct MockApi;
|
||||
|
||||
sp_api::mock_impl_runtime_apis! {
|
||||
impl Api<Block> for MockApi {
|
||||
type Error = u32;
|
||||
|
||||
fn test(data: u64) {}
|
||||
}
|
||||
|
||||
impl Api2<Block> for MockApi {
|
||||
type Error = u64;
|
||||
|
||||
fn test(data: u64) {}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -1,29 +0,0 @@
|
||||
error: Error type can not change between runtime apis
|
||||
--> $DIR/mock_only_one_error_type.rs:23:3
|
||||
|
|
||||
23 | type Error = u64;
|
||||
| ^^^^
|
||||
|
||||
error: First error type was declared here.
|
||||
--> $DIR/mock_only_one_error_type.rs:17:16
|
||||
|
|
||||
17 | type Error = u32;
|
||||
| ^^^
|
||||
|
||||
error[E0277]: the trait bound `u32: From<sp_api_hidden_includes_DECL_RUNTIME_APIS::sp_api::ApiError>` is not satisfied
|
||||
--> $DIR/mock_only_one_error_type.rs:17:16
|
||||
|
|
||||
17 | type Error = u32;
|
||||
| ^^^ the trait `From<sp_api_hidden_includes_DECL_RUNTIME_APIS::sp_api::ApiError>` is not implemented for `u32`
|
||||
|
|
||||
::: $WORKSPACE/primitives/api/src/lib.rs
|
||||
|
|
||||
| type Error: std::fmt::Debug + From<ApiError>;
|
||||
| -------------- required by this bound in `sp_api_hidden_includes_DECL_RUNTIME_APIS::sp_api::ApiErrorExt::Error`
|
||||
|
|
||||
= help: the following implementations were found:
|
||||
<u32 as From<HttpError>>
|
||||
<u32 as From<HttpRequestId>>
|
||||
<u32 as From<HttpRequestStatus>>
|
||||
<u32 as From<Ipv4Addr>>
|
||||
and 18 others
|
||||
Reference in New Issue
Block a user