Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Implement runtime api client side directly in the runtime #1094

Merged
merged 19 commits into from
Nov 13, 2018

Conversation

bkchr
Copy link
Member

@bkchr bkchr commented Nov 9, 2018

This pull requests moves the implementation of the runtime api client side into the runtime as well. This makes the api safer, as the compiler now enforces the user to implement a certain api when he wants to use it. The compile time checks obviously only work for the native runtime interface.
The Core api is now also enforced by the compiler by making each other api derive from it.

This pull requests also moves the sr-api crate into substrate-client and thus, the client is required to compile on wasm as well. On wasm the client only exposes the api stuff, the rest is disabled. I made this design decision to minimize the size of a where clause when using a runtime api to made the api more ergonomic. Otherwise, the user would needed to specify the error each time and that blowed up easily. I'm still not quite happy with this solution, maybe while reviewing this pull request, we can come to a better solution.

In a follow up pull request, I will provide some sort of macro to auto implement much of the stuff that is now hand written code in the test-runtime and node/runtime. So, all the ClientWithApi stuff should be gone with this follow up pr.

Progress on: #1031

@bkchr bkchr requested a review from gavofyork November 9, 2018 10:12
@bkchr bkchr added the A0-please_review Pull request needs code review. label Nov 9, 2018
//! Utility struct to build a block.

#[cfg(feature = "std")]
mod block_builder;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errant space

pub use rstd::slice;
#[doc(hidden)]
pub use codec::{Encode, Decode};
//! Macros for declaring and implementing the runtime API's.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grocer's apostrophe

@gavofyork
Copy link
Member

good to hear the ClientWithApi stuff will be gone soon.

@gavofyork
Copy link
Member

would be nice to have an example of a new API being added and used from within node. (perhaps not in this PR though)

@bkchr
Copy link
Member Author

bkchr commented Nov 9, 2018

Fixed the grumbles and the merge conflict. :)
I also created an issue for adding the API example.

pub mod runtime {
use super::*;

/// The `Core` api trait that is mandantory for each runtime.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces!


#[cfg(feature = "std")]
impl client::runtime_api::Core<GBlock> for ClientWithApi {
fn version(&self, at: &GBlockId) -> Result<RuntimeVersion, client::error::Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this code due to be auto-generated?

}

#[cfg(feature = "std")]
impl client::runtime_api::Core<Block> for ClientWithApi {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is very boilerplatey - is it due to be autogenerated?

#[cfg(feature = "std")]
pub trait Core<Block: BlockT>: 'static + Send + Sync + ConstructRuntimeApi<Block> + ApiExt {
/// Returns the version of the runtime.
fn version(&self, at: &BlockId<Block>) -> Result<RuntimeVersion>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file looks very boilerplatey given there are two instances of the same API with minor differences...

@gavofyork
Copy link
Member

some questions. other than those concerns it looks good.

@bkchr
Copy link
Member Author

bkchr commented Nov 12, 2018

Everything where ClientWithApi is involved in runtime and test-runtime, will be auto-generated (at least that is the plan). I'm currently looking into it on how that can be done in the best way.

@gavofyork gavofyork added A7-looksgoodcantmerge and removed A0-please_review Pull request needs code review. labels Nov 12, 2018
@bkchr bkchr force-pushed the bkchr-refactor-runtime-api-v2 branch from 43d5781 to 696e416 Compare November 13, 2018 10:49
@bkchr
Copy link
Member Author

bkchr commented Nov 13, 2018

@gavofyork resolved the merge conflict.

:100644 100644 898aadc7 49217199 M	Cargo.lock
:100644 100644 61570436 465ed664 M	core/client/src/backend.rs
:100644 100644 5d0c886b 64d710fd M	core/client/src/block_builder.rs
:100644 100644 c447855e 5ecbe474 M	core/client/src/client.rs
:100644 100644 139cef13 f90dbf3d M	core/client/src/error.rs
:100644 100644 2800c503 3298e66a M	core/client/src/runtime_api.rs
:100644 100644 affa1c5c 809b08bc M	core/primitives/src/lib.rs
:100644 100644 2877dfa9 d5547413 M	core/sr-api/Cargo.toml
:100644 100644 9a49784d 6a625a03 M	core/sr-api/src/lib.rs
:100644 100644 7c28e1c7 a1a444a9 M	core/sr-primitives/src/traits.rs
:100644 100644 2e113ab6 dcc01a6d M	srml/metadata/Cargo.toml
:100644 100644 ea722a70 0809531a M	srml/metadata/src/lib.rs
@bkchr bkchr force-pushed the bkchr-refactor-runtime-api-v2 branch from 696e416 to f3cca84 Compare November 13, 2018 11:31
@gavofyork gavofyork merged commit 0cc0a79 into master Nov 13, 2018
@gavofyork gavofyork deleted the bkchr-refactor-runtime-api-v2 branch November 13, 2018 12:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants