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
+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();
}