Rename Palette to FRAME (#4182)

* palette -> frame

* PALETTE, Palette -> FRAME

* Move folder pallete -> frame

* Update docs/Structure.adoc

Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com>

* Update docs/README.adoc

Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com>

* Update README.adoc
This commit is contained in:
Shawn Tabrizi
2019-11-22 19:21:25 +01:00
committed by GitHub
parent 68351da29b
commit c9175b59ff
206 changed files with 485 additions and 483 deletions
+38
View File
@@ -0,0 +1,38 @@
[package]
name = "frame-system"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
serde = { version = "1.0.101", optional = true, features = ["derive"] }
safe-mix = { version = "1.0.0", default-features = false }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
primitives = { package = "substrate-primitives", path = "../../primitives/core", default-features = false }
rstd = { package = "sr-std", path = "../../primitives/sr-std", default-features = false }
runtime-io ={ package = "sr-io", path = "../../primitives/sr-io", default-features = false }
sr-primitives = { path = "../../primitives/sr-primitives", default-features = false }
sr-version = { path = "../../primitives/sr-version", default-features = false }
support = { package = "frame-support", path = "../support", default-features = false }
impl-trait-for-tuples = "0.1.3"
[dev-dependencies]
criterion = "0.2.11"
[features]
default = ["std"]
std = [
"serde",
"safe-mix/std",
"codec/std",
"primitives/std",
"rstd/std",
"runtime-io/std",
"support/std",
"sr-primitives/std",
"sr-version/std",
]
[[bench]]
name = "bench"
harness = false
+105
View File
@@ -0,0 +1,105 @@
// 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/>.
use criterion::{Criterion, criterion_group, criterion_main, black_box};
use frame_system as system;
use support::{decl_module, decl_event, impl_outer_origin, impl_outer_event};
use primitives::H256;
use sr_primitives::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
mod module {
use super::*;
pub trait Trait: system::Trait {
type Event: From<Event> + Into<<Self as system::Trait>::Event>;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
pub fn deposit_event() = default;
}
}
decl_event!(
pub enum Event {
Complex(Vec<u8>, u32, u16, u128),
}
);
}
impl_outer_origin!{
pub enum Origin for Runtime {}
}
impl_outer_event! {
pub enum Event for Runtime {
module,
}
}
support::parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: u32 = 4 * 1024 * 1024;
pub const MaximumBlockLength: u32 = 4 * 1024 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
}
#[derive(Clone, Eq, PartialEq)]
pub struct Runtime;
impl system::Trait for Runtime {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Call = ();
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
}
impl module::Trait for Runtime {
type Event = Event;
}
fn new_test_ext() -> runtime_io::TestExternalities {
system::GenesisConfig::default().build_storage::<Runtime>().unwrap().into()
}
fn deposit_events(n: usize) {
let mut t = new_test_ext();
t.execute_with(|| {
for _ in 0..n {
module::Module::<Runtime>::deposit_event(
module::Event::Complex(vec![1, 2, 3], 2, 3, 899)
);
}
});
}
fn sr_system_benchmark(c: &mut Criterion) {
c.bench_function("deposit 100 events", |b| {
b.iter(|| deposit_events(black_box(100)))
});
}
criterion_group!(benches, sr_system_benchmark);
criterion_main!(benches);
+23
View File
@@ -0,0 +1,23 @@
[package]
name = "frame-system-rpc"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
client = { package = "substrate-client", path = "../../../client/" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
jsonrpc-core = "14.0.3"
jsonrpc-core-client = "14.0.3"
jsonrpc-derive = "14.0.3"
log = "0.4.8"
serde = { version = "1.0.101", features = ["derive"] }
sr-primitives = { path = "../../../primitives/sr-primitives" }
frame-system-rpc-runtime-api = { path = "./runtime-api" }
substrate-primitives = { path = "../../../primitives/core" }
transaction_pool = { package = "substrate-transaction-pool", path = "../../../client/transaction-pool" }
[dev-dependencies]
test-client = { package = "substrate-test-runtime-client", path = "../../../test/utils/runtime/client" }
env_logger = "0.7.0"
futures = "0.3.1"
@@ -0,0 +1,16 @@
[package]
name = "frame-system-rpc-runtime-api"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
sr-api = { path = "../../../../primitives/sr-api", default-features = false }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
[features]
default = ["std"]
std = [
"sr-api/std",
"codec/std",
]
@@ -0,0 +1,34 @@
// 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/>.
//! Runtime API definition required by System RPC extensions.
//!
//! This API should be imported and implemented by the runtime,
//! of a node that wants to use the custom RPC extension
//! adding System access methods.
#![cfg_attr(not(feature = "std"), no_std)]
sr_api::decl_runtime_apis! {
/// The API to query account nonce (aka transaction index).
pub trait AccountNonceApi<AccountId, Index> where
AccountId: codec::Codec,
Index: codec::Codec,
{
/// Get current account nonce of given `AccountId`.
fn account_nonce(account: AccountId) -> Index;
}
}
+158
View File
@@ -0,0 +1,158 @@
// 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/>.
//! System SRML specific RPC methods.
use std::sync::Arc;
use codec::{self, Codec, Encode};
use client::blockchain::HeaderBackend;
use jsonrpc_core::{Result, Error, ErrorCode};
use jsonrpc_derive::rpc;
use sr_primitives::{
generic::BlockId,
traits,
};
use substrate_primitives::hexdisplay::HexDisplay;
use transaction_pool::txpool::{self, Pool};
pub use frame_system_rpc_runtime_api::AccountNonceApi;
pub use self::gen_client::Client as SystemClient;
/// System RPC methods.
#[rpc]
pub trait SystemApi<AccountId, Index> {
/// 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 = "system_accountNextIndex", alias("account_nextIndex"))]
fn nonce(&self, account: AccountId) -> Result<Index>;
}
const RUNTIME_ERROR: i64 = 1;
/// An implementation of System-specific RPC methods.
pub struct System<P: txpool::ChainApi, C, B> {
client: Arc<C>,
pool: Arc<Pool<P>>,
_marker: std::marker::PhantomData<B>,
}
impl<P: txpool::ChainApi, C, B> System<P, C, B> {
/// Create new `System` given client and transaction pool.
pub fn new(client: Arc<C>, pool: Arc<Pool<P>>) -> Self {
System {
client,
pool,
_marker: Default::default(),
}
}
}
impl<P, C, Block, AccountId, Index> SystemApi<AccountId, Index> for System<P, C, Block>
where
C: traits::ProvideRuntimeApi,
C: HeaderBackend<Block>,
C: Send + Sync + 'static,
C::Api: AccountNonceApi<Block, AccountId, Index>,
P: txpool::ChainApi + Sync + Send + 'static,
Block: traits::Block,
AccountId: Clone + std::fmt::Display + Codec,
Index: Clone + std::fmt::Display + Codec + traits::SimpleArithmetic,
{
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.clone();
let mut current_tag = (account.clone(), nonce.clone()).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 += traits::One::one();
current_tag = (account.clone(), current_nonce.clone()).encode();
}
}
Ok(current_nonce)
}
}
#[cfg(test)]
mod tests {
use super::*;
use futures::executor::block_on;
use test_client::{
runtime::Transfer,
AccountKeyring,
};
#[test]
fn should_return_next_nonce_for_some_account() {
// given
let _ = env_logger::try_init();
let client = Arc::new(test_client::new());
let pool = Arc::new(Pool::new(Default::default(), transaction_pool::FullChainApi::new(client.clone())));
let new_transaction = |nonce: u64| {
let t = Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Bob.into(),
amount: 5,
nonce,
};
t.into_signed_tx()
};
// Populate the pool
let ext0 = new_transaction(0);
block_on(pool.submit_one(&BlockId::number(0), ext0)).unwrap();
let ext1 = new_transaction(1);
block_on(pool.submit_one(&BlockId::number(0), ext1)).unwrap();
let accounts = System::new(client, pool);
// when
let nonce = accounts.nonce(AccountKeyring::Alice.into());
// then
assert_eq!(nonce.unwrap(), 2);
}
}
File diff suppressed because it is too large Load Diff
+164
View File
@@ -0,0 +1,164 @@
// 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/>.
//! Module helpers for offchain calls.
use codec::Encode;
use sr_primitives::app_crypto::{self, RuntimeAppPublic};
use sr_primitives::traits::{Extrinsic as ExtrinsicT, IdentifyAccount};
/// A trait responsible for signing a payload using given account.
pub trait Signer<Public, Signature> {
/// Sign any encodable payload with given account and produce a signature.
///
/// Returns `Some` if signing succeeded and `None` in case the `account` couldn't be used.
fn sign<Payload: Encode>(public: Public, payload: &Payload) -> Option<Signature>;
}
/// A `Signer` implementation for any `AppPublic` type.
///
/// This implementation additionaly supports conversion to/from multi-signature/multi-signer
/// wrappers.
/// If the wrapped crypto doesn't match `AppPublic`s crypto `None` is returned.
impl<Public, Signature, AppPublic> Signer<Public, Signature> for AppPublic where
AppPublic: RuntimeAppPublic
+ app_crypto::AppPublic
+ From<<AppPublic as app_crypto::AppPublic>::Generic>,
<AppPublic as RuntimeAppPublic>::Signature: app_crypto::AppSignature,
Signature: From<
<<AppPublic as RuntimeAppPublic>::Signature as app_crypto::AppSignature>::Generic
>,
Public: rstd::convert::TryInto<<AppPublic as app_crypto::AppPublic>::Generic>
{
fn sign<Payload: Encode>(public: Public, raw_payload: &Payload) -> Option<Signature> {
raw_payload.using_encoded(|payload| {
let public = public.try_into().ok()?;
AppPublic::from(public).sign(&payload)
.map(
<<AppPublic as RuntimeAppPublic>::Signature as app_crypto::AppSignature>
::Generic::from
)
.map(Signature::from)
})
}
}
/// Creates runtime-specific signed transaction.
pub trait CreateTransaction<T: crate::Trait, Extrinsic: ExtrinsicT> {
/// A `Public` key representing a particular `AccountId`.
type Public: IdentifyAccount<AccountId=T::AccountId> + Clone;
/// A `Signature` generated by the `Signer`.
type Signature;
/// Attempt to create signed extrinsic data that encodes call from given account.
///
/// Runtime implementation is free to construct the payload to sign and the signature
/// in any way it wants.
/// Returns `None` if signed extrinsic could not be created (either because signing failed
/// or because of any other runtime-specific reason).
fn create_transaction<F: Signer<Self::Public, Self::Signature>>(
call: Extrinsic::Call,
public: Self::Public,
account: T::AccountId,
nonce: T::Index,
) -> Option<(Extrinsic::Call, Extrinsic::SignaturePayload)>;
}
type PublicOf<T, Call, X> = <
<X as SubmitSignedTransaction<T, Call>>::CreateTransaction as CreateTransaction<
T,
<X as SubmitSignedTransaction<T, Call>>::Extrinsic,
>
>::Public;
/// A trait to sign and submit transactions in offchain calls.
pub trait SubmitSignedTransaction<T: crate::Trait, Call> {
/// Unchecked extrinsic type.
type Extrinsic: ExtrinsicT<Call=Call> + codec::Encode;
/// A runtime-specific type to produce signed data for the extrinsic.
type CreateTransaction: CreateTransaction<T, Self::Extrinsic>;
/// A type used to sign transactions created using `CreateTransaction`.
type Signer: Signer<
PublicOf<T, Call, Self>,
<Self::CreateTransaction as CreateTransaction<T, Self::Extrinsic>>::Signature,
>;
/// Sign given call and submit it to the transaction pool.
///
/// Returns `Ok` if the transaction was submitted correctly
/// and `Err` if the key for given `id` was not found or the
/// transaction was rejected from the pool.
fn sign_and_submit(call: impl Into<Call>, public: PublicOf<T, Call, Self>) -> Result<(), ()> {
let call = call.into();
let id = public.clone().into_account();
let expected = <crate::Module<T>>::account_nonce(&id);
let (call, signature_data) = Self::CreateTransaction
::create_transaction::<Self::Signer>(call, public, id, expected)
.ok_or(())?;
let xt = Self::Extrinsic::new(call, Some(signature_data)).ok_or(())?;
runtime_io::offchain::submit_transaction(xt.encode())
}
}
/// A trait to submit unsigned transactions in offchain calls.
pub trait SubmitUnsignedTransaction<T: crate::Trait, Call> {
/// Unchecked extrinsic type.
type Extrinsic: ExtrinsicT<Call=Call> + codec::Encode;
/// Submit given call to the transaction pool as unsigned transaction.
///
/// Returns `Ok` if the transaction was submitted correctly
/// and `Err` if transaction was rejected from the pool.
fn submit_unsigned(call: impl Into<Call>) -> Result<(), ()> {
let xt = Self::Extrinsic::new(call.into(), None).ok_or(())?;
runtime_io::offchain::submit_transaction(xt.encode())
}
}
/// A default type used to submit transactions to the pool.
pub struct TransactionSubmitter<S, C, E> {
_signer: rstd::marker::PhantomData<(S, C, E)>,
}
impl<S, C, E> Default for TransactionSubmitter<S, C, E> {
fn default() -> Self {
Self {
_signer: Default::default(),
}
}
}
/// A blanket implementation to simplify creation of transaction signer & submitter in the runtime.
impl<T, E, S, C, Call> SubmitSignedTransaction<T, Call> for TransactionSubmitter<S, C, E> where
T: crate::Trait,
C: CreateTransaction<T, E>,
S: Signer<<C as CreateTransaction<T, E>>::Public, <C as CreateTransaction<T, E>>::Signature>,
E: ExtrinsicT<Call=Call> + codec::Encode,
{
type Extrinsic = E;
type CreateTransaction = C;
type Signer = S;
}
/// A blanket impl to use the same submitter for usigned transactions as well.
impl<T, E, S, C, Call> SubmitUnsignedTransaction<T, Call> for TransactionSubmitter<S, C, E> where
T: crate::Trait,
E: ExtrinsicT<Call=Call> + codec::Encode,
{
type Extrinsic = E;
}