Custom RPC implementation for node. (#3109)

* Allow RPCs to be customized.

* Implement node-rpc extensions.

* Working on a test.

* Add node-testing crate.

* Fix genesis test config

* Fix nonce lookups.

* Clean up.

* Fix expected block type.

* Make the RPC extension function optional.

* Fix service doc test.

* Bump jsonrpc.

* Bump client version.

* Update Cargo.lock

* Update jsonrpc.

* Fix build.

* Remove unused imports.

* Fix signed extra.

* Post merge clean up.

* Fix tests.

* Patch hashmap-core.

* Fix build.

* Fix build.

* Remove hashmap_core patches.
This commit is contained in:
Tomasz Drwięga
2019-08-20 11:06:35 +02:00
committed by Gavin Wood
parent 95abffc8e4
commit 56296386ab
29 changed files with 838 additions and 278 deletions
+3
View File
@@ -11,6 +11,7 @@ log = "0.4"
tokio = "0.1.7"
futures = "0.1"
exit-future = "0.1"
jsonrpc-core = "13.0.0"
cli = { package = "substrate-cli", path = "../../core/cli" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
sr-io = { path = "../../core/sr-io" }
@@ -18,8 +19,10 @@ client = { package = "substrate-client", path = "../../core/client" }
primitives = { package = "substrate-primitives", path = "../../core/primitives" }
inherents = { package = "substrate-inherents", path = "../../core/inherents" }
node-runtime = { path = "../runtime" }
node-rpc = { path = "../rpc" }
node-primitives = { path = "../primitives" }
hex-literal = "0.2"
substrate-rpc = { package = "substrate-rpc", path = "../../core/rpc" }
substrate-basic-authorship = { path = "../../core/basic-authorship" }
substrate-service = { path = "../../core/service" }
transaction_pool = { package = "substrate-transaction-pool", path = "../../core/transaction-pool" }
+11
View File
@@ -278,6 +278,17 @@ construct_service_factory! {
FinalityProofProvider = { |client: Arc<FullClient<Self>>| {
Ok(Some(Arc::new(GrandpaFinalityProofProvider::new(client.clone(), client)) as _))
}},
RpcExtensions = jsonrpc_core::IoHandler<substrate_rpc::Metadata>
{ |client, pool| {
use node_rpc::accounts::{Accounts, AccountsApi};
let mut io = jsonrpc_core::IoHandler::default();
io.extend_with(
AccountsApi::to_delegate(Accounts::new(client, pool))
);
io
}},
}
}
+1 -2
View File
@@ -17,13 +17,12 @@ node-primitives = { path = "../primitives" }
node-runtime = { path = "../runtime" }
[dev-dependencies]
node-testing = { path = "../testing" }
test-client = { package = "substrate-test-client", path = "../../core/test-client" }
keyring = { package = "substrate-keyring", path = "../../core/keyring" }
sr-primitives = { path = "../../core/sr-primitives" }
runtime_support = { package = "srml-support", path = "../../srml/support" }
balances = { package = "srml-balances", path = "../../srml/balances" }
session = { package = "srml-session", path = "../../srml/session" }
staking = { package = "srml-staking", path = "../../srml/staking" }
system = { package = "srml-system", path = "../../srml/system" }
timestamp = { package = "srml-timestamp", path = "../../srml/timestamp" }
treasury = { package = "srml-treasury", path = "../../srml/treasury" }
+20 -152
View File
@@ -37,30 +37,30 @@ native_executor_instance!(
#[cfg(test)]
mod tests {
use super::Executor;
use {balances, contracts, indices, staking, system, timestamp};
use {balances, contracts, indices, system, timestamp};
use runtime_io;
use substrate_executor::WasmExecutor;
use codec::{Encode, Decode, Joiner};
use keyring::{AccountKeyring, Ed25519Keyring, Sr25519Keyring};
use runtime_support::{Hashable, StorageValue, StorageMap, assert_eq_error_rate, traits::Currency};
use state_machine::{CodeExecutor, Externalities, TestExternalities as CoreTestExternalities};
use primitives::{twox_128, blake2_256, Blake2Hasher, ChangesTrieConfiguration, NeverNativeValue, NativeOrEncoded};
use node_primitives::{Hash, BlockNumber, AccountId, Balance, Index};
use primitives::{
twox_128, blake2_256, Blake2Hasher, NeverNativeValue, NativeOrEncoded, map
};
use sr_primitives::traits::{Header as HeaderT, Hash as HashT, Convert};
use sr_primitives::{generic::Era, ApplyOutcome, ApplyError, ApplyResult, Perbill};
use sr_primitives::{ApplyOutcome, ApplyError, ApplyResult};
use sr_primitives::weights::{WeightMultiplier, GetDispatchInfo};
use contracts::ContractAddressFor;
use system::{EventRecord, Phase};
use node_primitives::{Hash, BlockNumber, Balance};
use node_runtime::{
Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances, BuildStorage,
GenesisConfig, BalancesConfig, SessionConfig, StakingConfig, System, SystemConfig,
GrandpaConfig, IndicesConfig, ContractsConfig, Event, SessionKeys, SignedExtra,
TransferFee, TransactionBaseFee, TransactionByteFee
System, Event,
TransferFee, TransactionBaseFee, TransactionByteFee,
};
use node_runtime::constants::currency::*;
use node_runtime::impls::WeightToFee;
use node_testing::keyring::*;
use wabt;
use primitives::map;
/// The wasm runtime code.
///
@@ -83,6 +83,10 @@ mod tests {
type TestExternalities<H> = CoreTestExternalities<H, u64>;
fn sign(xt: CheckedExtrinsic) -> UncheckedExtrinsic {
node_testing::keyring::sign(xt, VERSION, GENESIS_HASH)
}
/// Default transfer fee
fn transfer_fee<E: Encode>(extrinsic: &E) -> Balance {
let length_fee = TransactionBaseFee::get() +
@@ -97,65 +101,6 @@ mod tests {
length_fee + weight_fee + TransferFee::get()
}
fn alice() -> AccountId {
AccountKeyring::Alice.into()
}
fn bob() -> AccountId {
AccountKeyring::Bob.into()
}
fn charlie() -> AccountId {
AccountKeyring::Charlie.into()
}
fn dave() -> AccountId {
AccountKeyring::Dave.into()
}
fn eve() -> AccountId {
AccountKeyring::Eve.into()
}
fn ferdie() -> AccountId {
AccountKeyring::Ferdie.into()
}
fn sign(xt: CheckedExtrinsic) -> UncheckedExtrinsic {
match xt.signed {
Some((signed, extra)) => {
let payload = (xt.function, extra.clone(), VERSION, GENESIS_HASH, GENESIS_HASH);
let key = AccountKeyring::from_public(&signed).unwrap();
let signature = payload.using_encoded(|b| {
if b.len() > 256 {
key.sign(&runtime_io::blake2_256(b))
} else {
key.sign(b)
}
}).into();
UncheckedExtrinsic {
signature: Some((indices::address::Address::Id(signed), signature, extra)),
function: payload.0,
}
}
None => UncheckedExtrinsic {
signature: None,
function: xt.function,
},
}
}
fn signed_extra(nonce: Index, extra_fee: Balance) -> SignedExtra {
(
system::CheckVersion::new(),
system::CheckGenesis::new(),
system::CheckEra::from(Era::mortal(256, 0)),
system::CheckNonce::from(nonce),
system::CheckWeight::new(),
balances::TakeFees::from(extra_fee)
)
}
fn default_transfer_call() -> balances::Call<Runtime> {
balances::Call::transfer::<Runtime>(bob().into(), 69 * DOLLARS)
}
@@ -319,85 +264,11 @@ mod tests {
});
}
fn to_session_keys(
ed25519_keyring: &Ed25519Keyring,
sr25519_keyring: &Sr25519Keyring,
) -> SessionKeys {
SessionKeys {
grandpa: ed25519_keyring.to_owned().public().into(),
babe: sr25519_keyring.to_owned().public().into(),
im_online: sr25519_keyring.to_owned().public().into(),
}
}
fn new_test_ext(code: &[u8], support_changes_trie: bool) -> TestExternalities<Blake2Hasher> {
let mut ext = TestExternalities::new_with_code(code, GenesisConfig {
system: Some(SystemConfig {
changes_trie_config: if support_changes_trie { Some(ChangesTrieConfiguration {
digest_interval: 2,
digest_levels: 2,
}) } else { None },
.. Default::default()
}),
indices: Some(IndicesConfig {
ids: vec![alice(), bob(), charlie(), dave(), eve(), ferdie()],
}),
balances: Some(BalancesConfig {
balances: vec![
(alice(), 111 * DOLLARS),
(bob(), 100 * DOLLARS),
(charlie(), 100_000_000 * DOLLARS),
(dave(), 111 * DOLLARS),
(eve(), 101 * DOLLARS),
(ferdie(), 100 * DOLLARS),
],
vesting: vec![],
}),
session: Some(SessionConfig {
keys: vec![
(alice(), to_session_keys(
&Ed25519Keyring::Alice,
&Sr25519Keyring::Alice,
)),
(bob(), to_session_keys(
&Ed25519Keyring::Bob,
&Sr25519Keyring::Bob,
)),
(charlie(), to_session_keys(
&Ed25519Keyring::Charlie,
&Sr25519Keyring::Charlie,
)),
]
}),
staking: Some(StakingConfig {
current_era: 0,
stakers: vec![
(dave(), alice(), 111 * DOLLARS, staking::StakerStatus::Validator),
(eve(), bob(), 100 * DOLLARS, staking::StakerStatus::Validator),
(ferdie(), charlie(), 100 * DOLLARS, staking::StakerStatus::Validator)
],
validator_count: 3,
minimum_validator_count: 0,
slash_reward_fraction: Perbill::from_percent(10),
invulnerables: vec![alice(), bob(), charlie()],
.. Default::default()
}),
contracts: Some(ContractsConfig {
current_schedule: Default::default(),
gas_price: 1 * MILLICENTS,
}),
babe: Some(Default::default()),
grandpa: Some(GrandpaConfig {
authorities: vec![],
}),
im_online: Some(Default::default()),
democracy: Some(Default::default()),
collective_Instance1: Some(Default::default()),
collective_Instance2: Some(Default::default()),
membership_Instance1: Some(Default::default()),
elections: Some(Default::default()),
sudo: Some(Default::default()),
}.build_storage().unwrap());
let mut ext = TestExternalities::new_with_code(
code,
node_testing::genesis::config(support_changes_trie, Some(code)).build_storage().unwrap(),
);
ext.changes_trie_storage().insert(0, GENESIS_HASH.into(), Default::default());
ext
}
@@ -955,15 +826,12 @@ mod tests {
#[test]
fn should_import_block_with_test_client() {
use test_client::{ClientExt, TestClientBuilder, consensus::BlockOrigin};
let client = TestClientBuilder::default()
.build_with_native_executor::<Block, node_runtime::RuntimeApi, _>(executor())
.0;
use node_testing::client::{ClientExt, TestClientBuilderExt, TestClientBuilder, consensus::BlockOrigin};
let client = TestClientBuilder::new().build();
let block1 = changes_trie_block();
let block_data = block1.0;
let block = Block::decode(&mut &block_data[..]).unwrap();
let block = node_primitives::Block::decode(&mut &block_data[..]).unwrap();
client.import(BlockOrigin::Own, block).unwrap();
}
+4 -2
View File
@@ -5,10 +5,11 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
serde = { version = "1.0", optional = true, features = ["derive"] }
client = { package = "substrate-client", path = "../../core/client", default-features = false }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
primitives = { package = "substrate-primitives", path = "../../core/primitives", default-features = false }
rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false }
serde = { version = "1.0", optional = true, features = ["derive"] }
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
[dev-dependencies]
@@ -18,9 +19,10 @@ pretty_assertions = "0.6.1"
[features]
default = ["std"]
std = [
"client/std",
"codec/std",
"primitives/std",
"rstd/std",
"sr-primitives/std",
"serde",
"sr-primitives/std",
]
+8
View File
@@ -66,3 +66,11 @@ pub type BlockId = generic::BlockId<Block>;
/// Opaque, encoded, unchecked extrinsic.
pub type UncheckedExtrinsic = OpaqueExtrinsic;
client::decl_runtime_apis! {
/// The API to query account account nonce (aka index).
pub trait AccountNonceApi {
/// Get current account nonce of given `AccountId`.
fn account_nonce(account: AccountId) -> Index;
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ edition = "2018"
env_logger = "0.6"
futures = "0.1.26"
hyper = "0.12"
jsonrpc-core-client = { version = "12.0.0", features = ["http", "ws"] }
jsonrpc-core-client = { version = "13.0.0", features = ["http", "ws"] }
log = "0.4"
node-primitives = { path = "../primitives" }
substrate-rpc = { path = "../../core/rpc", version = "2.0.0" }
+25
View File
@@ -0,0 +1,25 @@
[package]
name = "node-rpc"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
client = { package = "substrate-client", path = "../../core/client" }
jsonrpc-core = "13.0.0"
jsonrpc-core-client = "13.0.0"
jsonrpc-derive = "13.0.0"
jsonrpc-pubsub = "13.0.0"
keyring = { package = "substrate-keyring", path = "../../core/keyring" }
log = "0.4"
node-primitives = { path = "../primitives" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
serde = { version = "1.0", features = ["derive"] }
sr-primitives = { path = "../../core/sr-primitives" }
substrate-primitives = { path = "../../core/primitives" }
transaction_pool = { package = "substrate-transaction-pool", path = "../../core/transaction-pool" }
[dev-dependencies]
node-testing = { path = "../testing" }
node-runtime = { path = "../runtime" }
env_logger = "0.6"
+156
View File
@@ -0,0 +1,156 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Node-specific RPC methods for Accounts.
use std::sync::Arc;
use client::blockchain::HeaderBackend;
use jsonrpc_core::{Result, Error, ErrorCode};
use jsonrpc_derive::rpc;
use node_primitives::{
AccountId, Index, AccountNonceApi, Block, BlockId,
};
use codec::Encode;
use sr_primitives::traits;
use substrate_primitives::hexdisplay::HexDisplay;
use transaction_pool::txpool::{self, Pool};
pub use self::gen_client::Client as AccountsClient;
const RUNTIME_ERROR: i64 = 1;
/// Accounts RPC methods.
#[rpc]
pub trait AccountsApi {
/// Returns the next valid index (aka nonce) for given account.
///
/// This method takes into consideration all pending transactions
/// currently in the pool and if no transactions are found in the pool
/// it fallbacks to query the index from the runtime (aka. state nonce).
#[rpc(name = "account_nextIndex")]
fn nonce(&self, account: AccountId) -> Result<Index>;
}
/// An implementation of Accounts specific RPC methods.
pub struct Accounts<P: txpool::ChainApi, C> {
client: Arc<C>,
pool: Arc<Pool<P>>,
}
impl<P: txpool::ChainApi, C> Accounts<P, C> {
/// Create new `Accounts` given client and transaction pool.
pub fn new(client: Arc<C>, pool: Arc<Pool<P>>) -> Self {
Accounts {
client,
pool
}
}
}
impl<P, C> AccountsApi for Accounts<P, C>
where
C: traits::ProvideRuntimeApi,
C: HeaderBackend<Block>,
C: Send + Sync + 'static,
C::Api: AccountNonceApi<Block>,
P: txpool::ChainApi + Sync + Send + 'static,
{
fn nonce(&self, account: AccountId) -> Result<Index> {
let api = self.client.runtime_api();
let best = self.client.info().best_hash;
let at = BlockId::hash(best);
let nonce = api.account_nonce(&at, account.clone()).map_err(|e| Error {
code: ErrorCode::ServerError(RUNTIME_ERROR),
message: "Unable to query nonce.".into(),
data: Some(format!("{:?}", e).into()),
})?;
log::debug!(target: "rpc", "State nonce for {}: {}", account, nonce);
// Now we need to query the transaction pool
// and find transactions originating from the same sender.
//
// Since extrinsics are opaque to us, we look for them using
// `provides` tag. And increment the nonce if we find a transaction
// that matches the current one.
let mut current_nonce = nonce;
let mut current_tag = (account.clone(), nonce).encode();
for tx in self.pool.ready() {
log::debug!(
target: "rpc",
"Current nonce to {:?}, checking {} vs {:?}",
current_nonce,
HexDisplay::from(&current_tag),
tx.provides.iter().map(|x| format!("{}", HexDisplay::from(x))).collect::<Vec<_>>(),
);
// since transactions in `ready()` need to be ordered by nonce
// it's fine to continue with current iterator.
if tx.provides.get(0) == Some(&current_tag) {
current_nonce += 1;
current_tag = (account.clone(), current_nonce).encode();
}
}
Ok(current_nonce)
}
}
#[cfg(test)]
mod tests {
use super::*;
use node_runtime::{CheckedExtrinsic, Call, TimestampCall};
use codec::Decode;
use node_testing::{
client::{ClientExt, TestClientBuilder, TestClientBuilderExt},
keyring::{self, alice, signed_extra},
};
const VERSION: u32 = node_runtime::VERSION.spec_version;
#[test]
fn should_return_next_nonce_for_some_account() {
// given
let _ = env_logger::try_init();
let client = Arc::new(TestClientBuilder::new().build());
let pool = Arc::new(Pool::new(Default::default(), transaction_pool::ChainApi::new(client.clone())));
let new_transaction = |extra| {
let ex = CheckedExtrinsic {
signed: Some((alice().into(), extra)),
function: Call::Timestamp(TimestampCall::set(5)),
};
let xt = keyring::sign(ex, VERSION, client.genesis_hash().into());
// Convert to OpaqueExtrinsic
let encoded = xt.encode();
node_primitives::UncheckedExtrinsic::decode(&mut &*encoded).unwrap()
};
// Populate the pool
let ext0 = new_transaction(signed_extra(0, 0));
pool.submit_one(&BlockId::number(0), ext0).unwrap();
let ext1 = new_transaction(signed_extra(1, 0));
pool.submit_one(&BlockId::number(0), ext1).unwrap();
let accounts = Accounts::new(client, pool);
// when
let nonce = accounts.nonce(alice().into());
// then
assert_eq!(nonce.unwrap(), 2);
}
}
+32
View File
@@ -0,0 +1,32 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! A collection of node-specific RPC methods.
//!
//! Since `substrate` core functionality makes no assumptions
//! about the modules used inside the runtime, so do
//! RPC methods defined in `substrate-rpc` crate.
//! It means that `core/rpc` can't have any methods that
//! need some strong assumptions about the particular runtime.
//!
//! The RPCs available in this crate however can make some assumptions
//! about how the runtime is constructed and what `SRML` modules
//! are part of it. Therefore all node-runtime-specific RPCs can
//! be placed here.
#![warn(missing_docs)]
pub mod accounts;
+6
View File
@@ -576,6 +576,12 @@ impl_runtime_apis! {
}
}
impl node_primitives::AccountNonceApi<Block> for Runtime {
fn account_nonce(account: AccountId) -> Index {
System::account_nonce(account)
}
}
impl consensus_primitives::ConsensusApi<Block, babe_primitives::AuthorityId> for Runtime {
fn authorities() -> Vec<babe_primitives::AuthorityId> {
Babe::authorities().into_iter().map(|(a, _)| a).collect()
+30
View File
@@ -0,0 +1,30 @@
[package]
name = "node-testing"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Test utilities for Substrate node."
edition = "2018"
[dependencies]
balances = { package = "srml-balances", path = "../../srml/balances" }
client = { package = "substrate-client", path = "../../core/client" }
contracts = { package = "srml-contracts", path = "../../srml/contracts" }
grandpa = { package = "srml-grandpa", path = "../../srml/grandpa" }
indices = { package = "srml-indices", path = "../../srml/indices" }
keyring = { package = "substrate-keyring", path = "../../core/keyring" }
node-executor = { path = "../executor" }
node-primitives = { path = "../primitives" }
node-runtime = { path = "../runtime" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
primitives = { package = "substrate-primitives", path = "../../core/primitives" }
sr-io = { path = "../../core/sr-io" }
sr-primitives = { path = "../../core/sr-primitives" }
runtime_support = { package = "srml-support", path = "../../srml/support" }
session = { package = "srml-session", path = "../../srml/session" }
staking = { package = "srml-staking", path = "../../srml/staking" }
substrate-executor = { path = "../../core/executor" }
system = { package = "srml-system", path = "../../srml/system" }
test-client = { package = "substrate-test-client", path = "../../core/test-client" }
timestamp = { package = "srml-timestamp", path = "../../srml/timestamp" }
treasury = { package = "srml-treasury", path = "../../srml/treasury" }
wabt = "~0.7.4"
+73
View File
@@ -0,0 +1,73 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Utilites to build a `TestClient` for `node-runtime`.
use sr_primitives::BuildStorage;
/// Re-export test-client utilities.
pub use test_client::*;
/// Call executor for `node-runtime` `TestClient`.
pub type Executor = substrate_executor::NativeExecutor<node_executor::Executor>;
/// Default backend type.
pub type Backend = client_db::Backend<node_primitives::Block>;
/// Test client type.
pub type Client = client::Client<
Backend,
client::LocalCallExecutor<Backend, Executor>,
node_primitives::Block,
node_runtime::RuntimeApi,
>;
/// Genesis configuration parameters for `TestClient`.
#[derive(Default)]
pub struct GenesisParameters {
support_changes_trie: bool,
}
impl test_client::GenesisInit for GenesisParameters {
fn genesis_storage(&self) -> (StorageOverlay, ChildrenStorageOverlay) {
crate::genesis::config(self.support_changes_trie, None).build_storage().unwrap()
}
}
/// A `test-runtime` extensions to `TestClientBuilder`.
pub trait TestClientBuilderExt: Sized {
/// Create test client builder.
fn new() -> Self;
/// Build the test client.
fn build(self) -> Client;
}
impl TestClientBuilderExt for test_client::TestClientBuilder<
client::LocalCallExecutor<Backend, Executor>,
Backend,
GenesisParameters,
> {
fn new() -> Self{
Self::default()
}
fn build(self) -> Client {
self.build_with_native_executor(None).0
}
}
+99
View File
@@ -0,0 +1,99 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Genesis Configuration.
use crate::keyring::*;
use keyring::{Ed25519Keyring, Sr25519Keyring};
use node_runtime::{
GenesisConfig, BalancesConfig, SessionConfig, StakingConfig, SystemConfig,
GrandpaConfig, IndicesConfig, ContractsConfig, WASM_BINARY,
};
use node_runtime::constants::currency::*;
use primitives::ChangesTrieConfiguration;
use sr_primitives::Perbill;
/// Create genesis runtime configuration for tests.
pub fn config(support_changes_trie: bool, code: Option<&[u8]>) -> GenesisConfig {
GenesisConfig {
system: Some(SystemConfig {
changes_trie_config: if support_changes_trie { Some(ChangesTrieConfiguration {
digest_interval: 2,
digest_levels: 2,
}) } else { None },
code: code.map(|x| x.to_vec()).unwrap_or_else(|| WASM_BINARY.to_vec()),
}),
indices: Some(IndicesConfig {
ids: vec![alice(), bob(), charlie(), dave(), eve(), ferdie()],
}),
balances: Some(BalancesConfig {
balances: vec![
(alice(), 111 * DOLLARS),
(bob(), 100 * DOLLARS),
(charlie(), 100_000_000 * DOLLARS),
(dave(), 111 * DOLLARS),
(eve(), 101 * DOLLARS),
(ferdie(), 100 * DOLLARS),
],
vesting: vec![],
}),
session: Some(SessionConfig {
keys: vec![
(alice(), to_session_keys(
&Ed25519Keyring::Alice,
&Sr25519Keyring::Alice,
)),
(bob(), to_session_keys(
&Ed25519Keyring::Bob,
&Sr25519Keyring::Bob,
)),
(charlie(), to_session_keys(
&Ed25519Keyring::Charlie,
&Sr25519Keyring::Charlie,
)),
]
}),
staking: Some(StakingConfig {
current_era: 0,
stakers: vec![
(dave(), alice(), 111 * DOLLARS, staking::StakerStatus::Validator),
(eve(), bob(), 100 * DOLLARS, staking::StakerStatus::Validator),
(ferdie(), charlie(), 100 * DOLLARS, staking::StakerStatus::Validator)
],
validator_count: 3,
minimum_validator_count: 0,
slash_reward_fraction: Perbill::from_percent(10),
invulnerables: vec![alice(), bob(), charlie()],
.. Default::default()
}),
contracts: Some(ContractsConfig {
current_schedule: Default::default(),
gas_price: 1 * MILLICENTS,
}),
babe: Some(Default::default()),
grandpa: Some(GrandpaConfig {
authorities: vec![],
}),
im_online: Some(Default::default()),
democracy: Some(Default::default()),
collective_Instance1: Some(Default::default()),
collective_Instance2: Some(Default::default()),
membership_Instance1: Some(Default::default()),
elections: Some(Default::default()),
sudo: Some(Default::default()),
}
}
+103
View File
@@ -0,0 +1,103 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Test accounts.
use keyring::{AccountKeyring, Sr25519Keyring, Ed25519Keyring};
use node_primitives::{AccountId, Balance, Index};
use node_runtime::{CheckedExtrinsic, UncheckedExtrinsic, SessionKeys, SignedExtra};
use sr_primitives::generic::Era;
use codec::Encode;
/// Alice's account id.
pub fn alice() -> AccountId {
AccountKeyring::Alice.into()
}
/// Bob's account id.
pub fn bob() -> AccountId {
AccountKeyring::Bob.into()
}
/// Charlie's account id.
pub fn charlie() -> AccountId {
AccountKeyring::Charlie.into()
}
/// Dave's account id.
pub fn dave() -> AccountId {
AccountKeyring::Dave.into()
}
/// Eve's account id.
pub fn eve() -> AccountId {
AccountKeyring::Eve.into()
}
/// Ferdie's account id.
pub fn ferdie() -> AccountId {
AccountKeyring::Ferdie.into()
}
/// Convert keyrings into `SessionKeys`.
pub fn to_session_keys(
ed25519_keyring: &Ed25519Keyring,
sr25519_keyring: &Sr25519Keyring,
) -> SessionKeys {
SessionKeys {
grandpa: ed25519_keyring.to_owned().public().into(),
babe: sr25519_keyring.to_owned().public().into(),
im_online: sr25519_keyring.to_owned().public().into(),
}
}
/// Returns transaction extra.
pub fn signed_extra(nonce: Index, extra_fee: Balance) -> SignedExtra {
(
system::CheckVersion::new(),
system::CheckGenesis::new(),
system::CheckEra::from(Era::mortal(256, 0)),
system::CheckNonce::from(nonce),
system::CheckWeight::new(),
balances::TakeFees::from(extra_fee)
)
}
/// Sign given `CheckedExtrinsic`.
pub fn sign(xt: CheckedExtrinsic, version: u32, genesis_hash: [u8; 32]) -> UncheckedExtrinsic {
match xt.signed {
Some((signed, extra)) => {
let payload = (xt.function, extra.clone(), version, genesis_hash, genesis_hash);
let key = AccountKeyring::from_public(&signed).unwrap();
let signature = payload.using_encoded(|b| {
if b.len() > 256 {
key.sign(&sr_io::blake2_256(b))
} else {
key.sign(b)
}
}).into();
UncheckedExtrinsic {
signature: Some((indices::address::Address::Id(signed), signature, extra)),
function: payload.0,
}
}
None => UncheckedExtrinsic {
signature: None,
function: xt.function,
},
}
}
+24
View File
@@ -0,0 +1,24 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! A set of testing utilities for Substrate Node.
#![warn(missing_docs)]
pub mod client;
pub mod genesis;
pub mod keyring;