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

Rewrite impl_runtime_apis! and decl_runtime_apis! as proc-macro #1174

Merged
merged 26 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cb92ea5
Rewrites `impl_runtime_apis!` macro as `proc-macro`
bkchr Nov 19, 2018
a13fbd8
Adds some documentation
bkchr Nov 19, 2018
4c15648
Require the `impl_runtime_apis` to use a path for accessing the trait
bkchr Nov 19, 2018
9e92aa9
Make the runtime implement `GetNodeBlockType`
bkchr Nov 20, 2018
0a8bf17
Moves first chunk of runtime api code into the `impl_runtime_apis` macro
bkchr Nov 20, 2018
39aac74
Make `impl_runtime_apis` use `runtime` api version automatically
bkchr Nov 21, 2018
8530d30
`decl_runtime_apis` automatically adds `Block: BlockT` as generic par…
bkchr Nov 21, 2018
6a19b47
Remove function generic arguments in block builder api
bkchr Nov 22, 2018
0612b1a
Remove some unnused stuff from the `decl_runtime_apis` macro
bkchr Nov 22, 2018
b2deacb
Make `InherentData` working again
bkchr Nov 23, 2018
7cb68fa
Make `impl_runtime_apis!` implement the `RuntimeApi` side as well
bkchr Nov 26, 2018
5eff9ce
Make it compile again after rebasing with master
bkchr Nov 26, 2018
ab1b1d5
Split `sr-api-macros` into multiple files
bkchr Nov 27, 2018
4e4748a
Reimplement `decl_runtime_apis!` as proc_macro
bkchr Nov 27, 2018
0d80d35
Use `decl_runtime_apis!` for `Core` as well and improve error reporting
bkchr Nov 28, 2018
6b97e39
Adds documentation for `decl_runtime_apis!` and `impl_runtime_apis!`
bkchr Nov 28, 2018
c459d63
Move some code
bkchr Nov 28, 2018
4fa4998
Adds compile fail tests
bkchr Nov 28, 2018
1aad491
Adds a test and fixes some bugs
bkchr Nov 28, 2018
3caf2cc
Make `impl_runtime_apis!` support `_` as parameter name
bkchr Nov 28, 2018
7a8c86e
Fixes build errors with wasm
bkchr Nov 28, 2018
6da60cf
Wasm rebuild after master rebase
bkchr Nov 28, 2018
43006a2
Apply suggestions from code review
gnunicorn Nov 29, 2018
df80266
Addresses some grumbles
bkchr Nov 29, 2018
9b3285e
Adds test to ensure that method signatures need to match
bkchr Nov 29, 2018
61f32d7
New wasm files
bkchr Nov 29, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ members = [
"core/serializer",
"core/service",
"core/service/test",
"core/state-db",
"core/sr-api-macros",
"core/state-machine",
"core/test-runtime",
"core/telemetry",
Expand Down
1 change: 1 addition & 0 deletions core/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ substrate-primitives = { path = "../primitives", default-features = false }
sr-primitives = { path = "../sr-primitives", default-features = false }
sr-version = { path = "../sr-version", default-features = false }
sr-std = { path = "../sr-std", default-features = false }
sr-api-macros = { path = "../sr-api-macros" }

[dev-dependencies]
substrate-test-client = { path = "../test-client" }
Expand Down
12 changes: 4 additions & 8 deletions core/client/src/block_builder/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@

//! The runtime api for building blocks.

use runtime_primitives::{traits::Block as BlockT, ApplyResult};
use runtime_primitives::{traits::Block as BlockT, ApplyResult, InherentData, CheckInherentError};
use rstd::vec::Vec;

decl_runtime_apis! {
/// The `BlockBuilder` api trait that provides required functions for building a block for a runtime.
pub trait BlockBuilder<Block: BlockT> {
pub trait BlockBuilder {
/// Apply the given extrinsics.
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult;
/// Finish the current block.
fn finalise_block() -> <Block as BlockT>::Header;
/// Generate inherent extrinsics.
fn inherent_extrinsics<InherentExtrinsic, UncheckedExtrinsic>(
inherent: InherentExtrinsic
) -> Vec<UncheckedExtrinsic>;
fn inherent_extrinsics(inherent: InherentData) -> Vec<<Block as BlockT>::Extrinsic>;
/// Check that the inherents are valid.
fn check_inherents<InherentData, Error>(
block: Block, data: InherentData
) -> Result<(), Error>;
fn check_inherents(block: Block, data: InherentData) -> Result<(), CheckInherentError>;
/// Generate a random seed.
fn random_seed() -> <Block as BlockT>::Hash;
}
Expand Down
12 changes: 6 additions & 6 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,14 +1221,14 @@ pub(crate) mod tests {
use consensus::BlockOrigin;
use test_client::client::backend::Backend as TestBackend;
use test_client::BlockBuilderExt;
use test_client::runtime::{self, Block, Transfer, ClientWithApi, test_api::TestAPI};
use test_client::runtime::{self, Block, Transfer, RuntimeApi, test_api::TestAPI};

/// Returns tuple, consisting of:
/// 1) test client pre-filled with blocks changing balances;
/// 2) roots of changes tries for these blocks
/// 3) test cases in form (begin, end, key, vec![(block, extrinsic)]) that are required to pass
pub fn prepare_client_with_key_changes() -> (
test_client::client::Client<test_client::Backend, test_client::Executor, Block, ClientWithApi>,
test_client::client::Client<test_client::Backend, test_client::Executor, Block, RuntimeApi>,
Vec<H256>,
Vec<(u64, u64, Vec<u8>, Vec<(u64, u32)>)>,
) {
Expand Down Expand Up @@ -1303,14 +1303,14 @@ pub(crate) mod tests {
assert_eq!(
client.runtime_api().balance_of(
&BlockId::Number(client.info().unwrap().chain.best_number),
&Keyring::Alice.to_raw_public()
&Keyring::Alice.to_raw_public().into()
).unwrap(),
1000
);
assert_eq!(
client.runtime_api().balance_of(
&BlockId::Number(client.info().unwrap().chain.best_number),
&Keyring::Ferdie.to_raw_public()
&Keyring::Ferdie.to_raw_public().into()
).unwrap(),
0
);
Expand Down Expand Up @@ -1359,14 +1359,14 @@ pub(crate) mod tests {
assert_eq!(
client.runtime_api().balance_of(
&BlockId::Number(client.info().unwrap().chain.best_number),
&Keyring::Alice.to_raw_public()
&Keyring::Alice.to_raw_public().into()
).unwrap(),
958
);
assert_eq!(
client.runtime_api().balance_of(
&BlockId::Number(client.info().unwrap().chain.best_number),
&Keyring::Ferdie.to_raw_public()
&Keyring::Ferdie.to_raw_public().into()
).unwrap(),
42
);
Expand Down
5 changes: 5 additions & 0 deletions core/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ extern crate substrate_state_machine as state_machine;
extern crate substrate_consensus_common as consensus;
extern crate sr_version as runtime_version;
extern crate sr_std as rstd;
#[macro_use]
extern crate sr_api_macros;
#[cfg(test)]
extern crate substrate_keyring as keyring;
#[cfg(test)]
Expand Down Expand Up @@ -114,3 +116,6 @@ pub use notifications::{StorageEventStream, StorageChangeSet};
pub use state_machine::ExecutionStrategy;
#[cfg(feature = "std")]
pub use leaves::LeafSet;

#[doc(inline)]
pub use sr_api_macros::{decl_runtime_apis, impl_runtime_apis};
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,27 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! All the functionality required for declaring and implementing runtime api's.
//! Core api's are also declared here.
//! All the functionality required for declaring and implementing runtime apis.

#[doc(hidden)]
#[cfg(feature = "std")]
pub use state_machine::OverlayedChanges;
#[doc(hidden)]
pub use runtime_primitives::{traits::Block as BlockT, generic::BlockId};
#[cfg(feature = "std")]
use runtime_primitives::traits::ApiRef;
pub use runtime_version::ApiId;
pub use runtime_primitives::{
traits::{Block as BlockT, GetNodeBlockType, GetRuntimeBlockType, ApiRef}, generic::BlockId,
transaction_validity::TransactionValidity
};
pub use runtime_version::{ApiId, RuntimeVersion};
#[doc(hidden)]
pub use rstd::slice;
#[cfg(feature = "std")]
use rstd::result;
pub use codec::{Encode, Decode};
#[cfg(feature = "std")]
use error;
pub use runtime_version::RuntimeVersion;
use rstd::vec::Vec;
use primitives::{AuthorityId, OpaqueMetadata};

mod core;
#[macro_use]
mod macros;
mod traits;

/// Something that can be constructed to a runtime api.
#[cfg(feature = "std")]
Expand Down Expand Up @@ -113,11 +110,29 @@ pub mod id {
pub const METADATA: ApiId = *b"metadata";
}

pub use self::core::*;
pub use self::traits::*;
decl_runtime_apis! {
/// The `Core` api trait that is mandantory for each runtime.
#[core_trait]
pub trait Core {
/// Returns the version of the runtime.
fn version() -> RuntimeVersion;
/// Returns the authorities.
fn authorities() -> Vec<AuthorityId>;
/// Execute the given block.
fn execute_block(block: Block);
/// Initialise a block with the given header.
fn initialise_block(header: <Block as BlockT>::Header);
}

/// The runtime apis that should be implemented for the `Runtime`.
pub mod runtime {
pub use super::core::runtime::Core;
pub use super::traits::runtime::*;
/// The `Metadata` api trait that returns metadata for the runtime.
pub trait Metadata {
/// Returns the metadata of a runtime.
fn metadata() -> OpaqueMetadata;
}

/// The `TaggedTransactionQueue` api trait for interfering with the new transaction queue.
pub trait TaggedTransactionQueue {
/// Validate the given transaction.
fn validate_transaction(tx: <Block as BlockT>::Extrinsic) -> TransactionValidity;
}
}
62 changes: 0 additions & 62 deletions core/client/src/runtime_api/core.rs

This file was deleted.

Loading