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

Commit ead7e0e

Browse files
tomusdrwgavofyork
authored andcommitted
Move srml RPC extensions to separate crates (#3791)
* Move srml-system RPC out. * Fix tests for system-rpc module. * Contracts RPC moved. * Fix rpc test. * Clean up. * Update lockfile. * Bump runtime version. * Update srml/contracts/rpc/runtime-api/src/lib.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Bump impl version.
1 parent 297956d commit ead7e0e

File tree

20 files changed

+344
-182
lines changed

20 files changed

+344
-182
lines changed

Cargo.lock

Lines changed: 58 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ members = [
103103
"srml/staking/reward-curve",
104104
"srml/sudo",
105105
"srml/system",
106+
"srml/system/rpc",
106107
"srml/timestamp",
107108
"srml/treasury",
108109
"srml/utility",

core/rpc/src/state/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ fn should_return_runtime_version() {
282282
\"specVersion\":1,\"implVersion\":1,\"apis\":[[\"0xdf6acb689907609b\",2],\
283283
[\"0x37e397fc7c91f5e4\",1],[\"0xd2bc9897eed08f15\",1],[\"0x40fe3ad401f8959a\",3],\
284284
[\"0xc6e9a76309f39b09\",1],[\"0xdd718d5cc53262d4\",1],[\"0xcbca25e39f142387\",1],\
285-
[\"0xf78b278be53f454c\",1],[\"0xab3c0572291feb8b\",1]]}";
285+
[\"0xf78b278be53f454c\",1],[\"0xab3c0572291feb8b\",1],[\"0xbc9d89904f5b923f\",1]]}";
286286

287287
let runtime_version = api.runtime_version(None.into()).wait().unwrap();
288288
let serialized = serde_json::to_string(&runtime_version).unwrap();

core/test-runtime/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ cfg-if = "0.1.10"
3131
srml-babe = { path = "../../srml/babe", default-features = false }
3232
srml-timestamp = { path = "../../srml/timestamp", default-features = false }
3333
srml-system = { path = "../../srml/system", default-features = false }
34+
srml-system-rpc-runtime-api = { path = "../../srml/system/rpc/runtime-api", default-features = false }
3435

3536
[dev-dependencies]
3637
substrate-executor = { path = "../executor" }
@@ -68,6 +69,7 @@ std = [
6869
"srml-babe/std",
6970
"srml-timestamp/std",
7071
"srml-system/std",
72+
"srml-system-rpc-runtime-api/std",
7173
"app-crypto/std",
7274
"session/std",
7375
]

core/test-runtime/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,12 @@ cfg_if! {
648648
SessionKeys::generate(None)
649649
}
650650
}
651+
652+
impl srml_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Index> for Runtime {
653+
fn account_nonce(_account: AccountId) -> Index {
654+
0
655+
}
656+
}
651657
}
652658
} else {
653659
impl_runtime_apis! {
@@ -858,6 +864,12 @@ cfg_if! {
858864
SessionKeys::generate(None)
859865
}
860866
}
867+
868+
impl srml_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Index> for Runtime {
869+
fn account_nonce(_account: AccountId) -> Index {
870+
0
871+
}
872+
}
861873
}
862874
}
863875
}

core/test-runtime/src/system.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! and depositing logs.
1919
2020
use rstd::prelude::*;
21-
use runtime_io::{storage_root, storage_changes_root, twox_128, blake2_256};
21+
use runtime_io::{storage_root, storage_changes_root, blake2_256};
2222
use runtime_support::storage::{self, StorageValue, StorageMap};
2323
use runtime_support::storage_items;
2424
use sr_primitives::{
@@ -170,22 +170,14 @@ pub fn validate_transaction(utx: Extrinsic) -> TransactionValidity {
170170
return InvalidTransaction::Future.into();
171171
}
172172

173-
let hash = |from: &AccountId, nonce: u64| {
174-
twox_128(&nonce.to_keyed_vec(&from.encode())).to_vec()
175-
};
173+
let encode = |from: &AccountId, nonce: u64| (from, nonce).encode();
176174
let requires = if tx.nonce != expected_nonce && tx.nonce > 0 {
177-
let mut deps = Vec::new();
178-
deps.push(hash(&tx.from, tx.nonce - 1));
179-
deps
175+
vec![encode(&tx.from, tx.nonce - 1)]
180176
} else {
181-
Vec::new()
177+
vec![]
182178
};
183179

184-
let provides = {
185-
let mut p = Vec::new();
186-
p.push(hash(&tx.from, tx.nonce));
187-
p
188-
};
180+
let provides = vec![encode(&tx.from, tx.nonce)];
189181

190182
Ok(ValidTransaction {
191183
priority: tx.amount,
@@ -324,6 +316,7 @@ mod tests {
324316
use crate::{Header, Transfer, WASM_BINARY};
325317
use primitives::{NeverNativeValue, map, traits::CodeExecutor};
326318
use substrate_executor::{NativeExecutor, WasmExecutionMethod, native_executor_instance};
319+
use runtime_io::twox_128;
327320

328321
// Declare an instance of the native executor dispatch for the test runtime.
329322
native_executor_instance!(

node/primitives/Cargo.toml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
55
edition = "2018"
66

77
[dependencies]
8-
client = { package = "substrate-client", path = "../../core/client", default-features = false }
9-
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
108
primitives = { package = "substrate-primitives", path = "../../core/primitives", default-features = false }
11-
rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false }
12-
serde = { version = "1.0.101", optional = true, features = ["derive"] }
139
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
1410

1511
[dev-dependencies]
@@ -19,10 +15,6 @@ pretty_assertions = "0.6.1"
1915
[features]
2016
default = ["std"]
2117
std = [
22-
"client/std",
23-
"codec/std",
2418
"primitives/std",
25-
"rstd/std",
26-
"serde",
2719
"sr-primitives/std",
2820
]

node/primitives/src/lib.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@
2020

2121
#![cfg_attr(not(feature = "std"), no_std)]
2222

23-
use rstd::prelude::*;
2423
use sr_primitives::{
2524
generic, traits::{Verify, BlakeTwo256}, OpaqueExtrinsic, AnySignature
2625
};
2726

28-
#[cfg(feature = "std")]
29-
use serde::{Serialize, Deserialize};
30-
use codec::{Encode, Decode};
31-
3227
/// An index to a block.
3328
pub type BlockNumber = u32;
3429

@@ -72,43 +67,3 @@ pub type BlockId = generic::BlockId<Block>;
7267
/// Opaque, encoded, unchecked extrinsic.
7368
pub type UncheckedExtrinsic = OpaqueExtrinsic;
7469

75-
/// A result of execution of a contract.
76-
#[derive(Eq, PartialEq, Encode, Decode)]
77-
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
78-
pub enum ContractExecResult {
79-
/// The contract returned successfully.
80-
///
81-
/// There is a status code and, optionally, some data returned by the contract.
82-
Success {
83-
/// Status code returned by the contract.
84-
status: u8,
85-
/// Output data returned by the contract.
86-
///
87-
/// Can be empty.
88-
data: Vec<u8>,
89-
},
90-
/// The contract execution either trapped or returned an error.
91-
Error,
92-
}
93-
94-
client::decl_runtime_apis! {
95-
/// The API to query account account nonce (aka index).
96-
pub trait AccountNonceApi {
97-
/// Get current account nonce of given `AccountId`.
98-
fn account_nonce(account: AccountId) -> Index;
99-
}
100-
101-
/// The API to interact with contracts without using executive.
102-
pub trait ContractsApi {
103-
/// Perform a call from a specified account to a given contract.
104-
///
105-
/// See the contracts' `call` dispatchable function for more details.
106-
fn call(
107-
origin: AccountId,
108-
dest: AccountId,
109-
value: Balance,
110-
gas_limit: u64,
111-
input_data: Vec<u8>,
112-
) -> ContractExecResult;
113-
}
114-
}

node/rpc/Cargo.toml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,8 @@ edition = "2018"
77
[dependencies]
88
client = { package = "substrate-client", path = "../../core/client" }
99
jsonrpc-core = "13.2.0"
10-
jsonrpc-core-client = "13.2.0"
11-
jsonrpc-derive = "13.2.0"
12-
jsonrpc-pubsub = "13.2.0"
13-
keyring = { package = "substrate-keyring", path = "../../core/keyring" }
14-
log = "0.4.8"
1510
node-primitives = { path = "../primitives" }
16-
codec = { package = "parity-scale-codec", version = "1.0.0" }
17-
serde = { version = "1.0.101", features = ["derive"] }
1811
sr-primitives = { path = "../../core/sr-primitives" }
19-
substrate-primitives = { path = "../../core/primitives" }
20-
rpc-primitives = { package = "substrate-rpc-primitives", path = "../../core/rpc/primitives" }
12+
srml-contracts-rpc = { path = "../../srml/contracts/rpc/" }
13+
srml-system-rpc = { path = "../../srml/system/rpc/" }
2114
transaction_pool = { package = "substrate-transaction-pool", path = "../../core/transaction-pool" }
22-
23-
[dev-dependencies]
24-
node-testing = { path = "../testing" }
25-
node-runtime = { path = "../runtime" }
26-
env_logger = "0.7.0"
27-
futures03 = { package = "futures-preview", version = "=0.3.0-alpha.19" }

node/rpc/src/lib.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,32 @@
2525
//! The RPCs available in this crate however can make some assumptions
2626
//! about how the runtime is constructed and what `SRML` modules
2727
//! are part of it. Therefore all node-runtime-specific RPCs can
28-
//! be placed here.
28+
//! be placed here or imported from corresponding `SRML` RPC definitions.
2929
3030
#![warn(missing_docs)]
3131

3232
use std::sync::Arc;
3333

34-
use node_primitives::{Block, AccountNonceApi, ContractsApi};
34+
use node_primitives::{Block, AccountId, Index, Balance};
3535
use sr_primitives::traits::ProvideRuntimeApi;
3636
use transaction_pool::txpool::{ChainApi, Pool};
3737

38-
pub mod accounts;
39-
pub mod contracts;
40-
41-
mod constants {
42-
/// A status code indicating an error happened while trying to call into the runtime.
43-
///
44-
/// This typically means that the runtime trapped.
45-
pub const RUNTIME_ERROR: i64 = 1;
46-
}
47-
4838
/// Instantiate all RPC extensions.
4939
pub fn create<C, P, M>(client: Arc<C>, pool: Arc<Pool<P>>) -> jsonrpc_core::IoHandler<M> where
5040
C: ProvideRuntimeApi,
5141
C: client::blockchain::HeaderBackend<Block>,
5242
C: Send + Sync + 'static,
53-
C::Api: AccountNonceApi<Block> + ContractsApi<Block>,
43+
C::Api: srml_system_rpc::AccountNonceApi<Block, AccountId, Index>,
44+
C::Api: srml_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance>,
5445
P: ChainApi + Sync + Send + 'static,
5546
M: jsonrpc_core::Metadata + Default,
5647
{
57-
use self::{
58-
accounts::{Accounts, AccountsApi},
59-
contracts::{Contracts, ContractsApi},
60-
};
48+
use srml_system_rpc::{System, SystemApi};
49+
use srml_contracts_rpc::{Contracts, ContractsApi};
6150

6251
let mut io = jsonrpc_core::IoHandler::default();
6352
io.extend_with(
64-
AccountsApi::to_delegate(Accounts::new(client.clone(), pool))
53+
SystemApi::to_delegate(System::new(client.clone(), pool))
6554
);
6655
io.extend_with(
6756
ContractsApi::to_delegate(Contracts::new(client))

0 commit comments

Comments
 (0)