From b39ff1771ab1486d9fb09984794b46f5218b8126 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 15 Nov 2019 15:04:06 +0000 Subject: [PATCH] New substrate signing API, srml -> paint rename (#33) * Update jsonrpc to align with substrate * Update to new substrate signing * Default to MultiSignature * Add missing event type sizes * Rename TakeFees check to ChargeTransactionPayment * Fix extrinsic Signature * Ignore AuthorityList type size * Rename srml to paint (#32) --- Cargo.toml | 12 ++--- src/events.rs | 8 ++- src/extrinsic.rs | 30 +++++++----- src/lib.rs | 83 ++++++++++++++++++-------------- src/{srml => paint}/balances.rs | 27 +++++++---- src/{srml => paint}/contracts.rs | 31 +++++++----- src/{srml => paint}/mod.rs | 0 src/{srml => paint}/system.rs | 29 ++++++----- src/rpc.rs | 6 +-- src/runtimes.rs | 4 +- 10 files changed, 136 insertions(+), 94 deletions(-) rename src/{srml => paint}/balances.rs (83%) rename src/{srml => paint}/contracts.rs (81%) rename src/{srml => paint}/mod.rs (100%) rename src/{srml => paint}/system.rs (83%) diff --git a/Cargo.toml b/Cargo.toml index c021cd5caa..0e8a0da9cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,15 +19,15 @@ futures = "0.1.28" jsonrpc-core-client = { version = "14.0", features = ["ws"] } num-traits = { version = "0.2", default-features = false } parity-scale-codec = { version = "1.0", default-features = false, features = ["derive", "full"] } -runtime_metadata = { git = "https://github.com/paritytech/substrate/", package = "srml-metadata" } -runtime_support = { git = "https://github.com/paritytech/substrate/", package = "srml-support" } +runtime_metadata = { git = "https://github.com/paritytech/substrate/", package = "paint-metadata" } +runtime_support = { git = "https://github.com/paritytech/substrate/", package = "paint-support" } runtime_primitives = { git = "https://github.com/paritytech/substrate/", package = "sr-primitives" } serde = { version = "1.0", features = ["derive"] } sr-version = { git = "https://github.com/paritytech/substrate/", package = "sr-version" } -srml-system = { git = "https://github.com/paritytech/substrate/", package = "srml-system" } -srml-balances = { git = "https://github.com/paritytech/substrate/", package = "srml-balances" } -srml-contracts = { git = "https://github.com/paritytech/substrate/", package = "srml-contracts" } -srml-indices = { git = "https://github.com/paritytech/substrate/", package = "srml-indices" } +paint-system = { git = "https://github.com/paritytech/substrate/", package = "paint-system" } +paint-balances = { git = "https://github.com/paritytech/substrate/", package = "paint-balances" } +paint-contracts = { git = "https://github.com/paritytech/substrate/", package = "paint-contracts" } +paint-indices = { git = "https://github.com/paritytech/substrate/", package = "paint-indices" } substrate-rpc-api = { git = "https://github.com/paritytech/substrate/", package = "substrate-rpc-api" } substrate-rpc-primitives = { git = "https://github.com/paritytech/substrate/", package = "substrate-rpc-primitives" } substrate-primitives = { git = "https://github.com/paritytech/substrate/", package = "substrate-primitives" } diff --git a/src/events.rs b/src/events.rs index fbfe863d36..838f8a5439 100644 --- a/src/events.rs +++ b/src/events.rs @@ -20,7 +20,7 @@ use crate::{ Metadata, MetadataError, }, - srml::balances::Balances, + paint::balances::Balances, System, SystemEvent, }; @@ -34,7 +34,7 @@ use parity_scale_codec::{ Input, Output, }; -use srml_system::Phase; +use paint_system::Phase; use std::{ collections::{ HashMap, @@ -113,8 +113,12 @@ impl TryFrom for EventsDecoder { // Ignore these unregistered types, which are not fixed size primitives decoder.check_missing_type_sizes(vec![ "DispatchError", + "Result<(), DispatchError>", "OpaqueTimeSlot", "rstd::marker::PhantomData<(AccountId, Event)>", + // FIXME: determine type size for the following if necessary/possible + "IdentificationTuple", + "AuthorityList", ])?; Ok(decoder) } diff --git a/src/extrinsic.rs b/src/extrinsic.rs index 2af7dc072a..64a3c065b3 100644 --- a/src/extrinsic.rs +++ b/src/extrinsic.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with substrate-subxt. If not, see . -use crate::srml::{ +use crate::paint::{ balances::Balances, system::System, }; @@ -29,14 +29,18 @@ use runtime_primitives::{ SignedPayload, UncheckedExtrinsic, }, - traits::SignedExtension, + traits::{ + Verify, + IdentifyAccount, + SignedExtension, + }, transaction_validity::TransactionValidityError, }; use std::marker::PhantomData; use substrate_primitives::Pair; /// SignedExtra checks copied from substrate, in order to remove requirement to implement -/// substrate's `srml_system::Trait` +/// substrate's `paint_system::Trait` /// Ensure the runtime version registered in the transaction is the same as at present. /// @@ -168,9 +172,9 @@ where /// Require the transactor pay for themselves and maybe include a tip to gain additional priority /// in the queue. #[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)] -pub struct TakeFees(#[codec(compact)] T::Balance); +pub struct ChargeTransactionPayment(#[codec(compact)] T::Balance); -impl SignedExtension for TakeFees +impl SignedExtension for ChargeTransactionPayment where T: Balances + Send + Sync, { @@ -234,7 +238,7 @@ impl SignedExtra for DefaultExtra { CheckEra, CheckNonce, CheckWeight, - TakeFees, + ChargeTransactionPayment, CheckBlockGasLimit, ); @@ -245,7 +249,7 @@ impl SignedExtra for DefaultExtra { CheckEra((Era::Immortal, PhantomData), self.genesis_hash), CheckNonce(self.nonce), CheckWeight(PhantomData), - TakeFees(::Balance::default()), + ChargeTransactionPayment(::Balance::default()), CheckBlockGasLimit(PhantomData), ) } @@ -265,28 +269,30 @@ impl SignedExtension for DefaultExtra { } } -pub fn create_and_sign( +pub fn create_and_sign( signer: P, call: C, extra: E, ) -> Result< - UncheckedExtrinsic>::Extra>, + UncheckedExtrinsic>::Extra>, TransactionValidityError, > where P: Pair, - P::Public: Into, - P::Signature: Codec, + S: Verify + Codec + From, + S::Signer: From + IdentifyAccount, C: Encode, E: SignedExtra + SignedExtension, + T::Address: From, { let raw_payload = SignedPayload::new(call, extra.extra())?; let signature = raw_payload.using_encoded(|payload| signer.sign(payload)); let (call, extra, _) = raw_payload.deconstruct(); + let account_id = S::Signer::from(signer.public()).into_account(); Ok(UncheckedExtrinsic::new_signed( call, - signer.public().into(), + account_id.into(), signature.into(), extra, )) diff --git a/src/lib.rs b/src/lib.rs index c473b41556..65f66ed0b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,14 @@ use parity_scale_codec::{ Codec, Decode, }; -use runtime_primitives::generic::UncheckedExtrinsic; +use runtime_primitives::{ + generic::UncheckedExtrinsic, + traits::{ + Verify, + IdentifyAccount, + }, + MultiSignature, +}; use sr_version::RuntimeVersion; use std::{ convert::TryFrom, @@ -61,7 +68,7 @@ use crate::{ MapStream, Rpc, }, - srml::{ + paint::{ balances::Balances, system::{ System, @@ -79,25 +86,25 @@ mod extrinsic; mod metadata; mod rpc; mod runtimes; -mod srml; +mod paint; pub use error::Error; pub use events::RawEvent; pub use rpc::ExtrinsicSuccess; pub use runtimes::*; -pub use srml::*; +pub use paint::*; fn connect(url: &Url) -> impl Future, Error = Error> { ws::connect(url).map_err(Into::into) } /// ClientBuilder for constructing a Client. -pub struct ClientBuilder { - _marker: std::marker::PhantomData, +pub struct ClientBuilder { + _marker: std::marker::PhantomData<(T, S)>, url: Option, } -impl ClientBuilder { +impl ClientBuilder { /// Creates a new ClientBuilder. pub fn new() -> Self { Self { @@ -113,7 +120,7 @@ impl ClientBuilder { } /// Creates a new Client. - pub fn build(self) -> impl Future, Error = Error> { + pub fn build(self) -> impl Future, Error = Error> { let url = self.url.unwrap_or_else(|| { Url::parse("ws://127.0.0.1:9944").expect("Is valid url; qed") }); @@ -126,6 +133,7 @@ impl ClientBuilder { genesis_hash, metadata, runtime_version, + _marker: PhantomData, } }) }) @@ -133,25 +141,28 @@ impl ClientBuilder { } /// Client to interface with a substrate node. -pub struct Client { +pub struct Client { url: Url, genesis_hash: T::Hash, metadata: Metadata, runtime_version: RuntimeVersion, + _marker: PhantomData S>, } -impl Clone for Client { +impl Clone for Client { fn clone(&self) -> Self { Self { url: self.url.clone(), genesis_hash: self.genesis_hash.clone(), metadata: self.metadata.clone(), runtime_version: self.runtime_version.clone(), + _marker:PhantomData, } } } -impl Client { +impl Client +{ fn connect(&self) -> impl Future, Error = Error> { connect(&self.url) } @@ -234,16 +245,18 @@ impl Client { &self, signer: P, nonce: Option, - ) -> impl Future, Error = Error> + ) -> impl Future, Error = Error> where P: Pair, - P::Public: Into + Into, P::Signature: Codec, + S: Verify, + S::Signer: From + IdentifyAccount, { let client = self.clone(); + let account_id = S::Signer::from(signer.public()).into_account(); match nonce { Some(nonce) => Either::A(future::ok(nonce)), - None => Either::B(self.account_nonce(signer.public().into())), + None => Either::B(self.account_nonce(account_id)), } .map(|nonce| { let genesis_hash = client.genesis_hash.clone(); @@ -267,8 +280,8 @@ pub enum Valid {} pub enum Invalid {} /// Transaction builder. -pub struct XtBuilder { - client: Client, +pub struct XtBuilder { + client: Client, nonce: T::Index, runtime_version: RuntimeVersion, genesis_hash: T::Hash, @@ -277,7 +290,7 @@ pub struct XtBuilder { marker: PhantomData V>, } -impl XtBuilder +impl XtBuilder where P: Pair, { @@ -292,19 +305,19 @@ where } /// Sets the nonce to a new value. - pub fn set_nonce(&mut self, nonce: T::Index) -> &mut XtBuilder { + pub fn set_nonce(&mut self, nonce: T::Index) -> &mut XtBuilder { self.nonce = nonce; self } /// Increment the nonce - pub fn increment_nonce(&mut self) -> &mut XtBuilder { + pub fn increment_nonce(&mut self) -> &mut XtBuilder { self.set_nonce(self.nonce() + 1.into()); self } /// Sets the module call to a new value - pub fn set_call(&self, module: &'static str, f: F) -> XtBuilder + pub fn set_call(&self, module: &'static str, f: F) -> XtBuilder where F: FnOnce(ModuleCalls) -> Result, { @@ -326,11 +339,12 @@ where } } -impl XtBuilder +impl XtBuilder where P: Pair, - P::Public: Into, - P::Signature: Codec, + S: Verify + Codec + From, + S::Signer: From + IdentifyAccount, + T::Address: From, { /// Creates and signs an Extrinsic for the supplied `Call` pub fn create_and_sign( @@ -339,15 +353,11 @@ where UncheckedExtrinsic< T::Address, Encoded, - P::Signature, + S, as SignedExtra>::Extra, >, Error, > - where - P: Pair, - P::Public: Into, - P::Signature: Codec, { let signer = self.signer.clone(); let account_nonce = self.nonce.clone(); @@ -365,7 +375,7 @@ where ); let extra = extrinsic::DefaultExtra::new(version, account_nonce, genesis_hash); - let xt = extrinsic::create_and_sign(signer, call, extra)?; + let xt = extrinsic::create_and_sign::<_, _, _, S, _>(signer, call, extra)?; Ok(xt) } @@ -405,7 +415,7 @@ where #[cfg(test)] mod tests { use super::*; - use crate::srml::{ + use crate::paint::{ balances::{ Balances, BalancesStore, @@ -420,7 +430,6 @@ mod tests { use substrate_keyring::AccountKeyring; use substrate_primitives::{ storage::StorageKey, - Pair, }; type Index = ::Index; @@ -444,7 +453,7 @@ mod tests { let signer = AccountKeyring::Alice.pair(); let mut xt = rt.block_on(client.xt(signer, None)).unwrap(); - let dest = AccountKeyring::Bob.pair().public(); + let dest = AccountKeyring::Bob.to_account_id(); let transfer = xt .balances(|calls| calls.transfer(dest.clone().into(), 10_000)) .submit(); @@ -515,7 +524,7 @@ mod tests { fn test_state_read_free_balance() { let (mut rt, client) = test_setup(); - let account = AccountKeyring::Alice.pair().public(); + let account = AccountKeyring::Alice.to_account_id(); rt.block_on(client.free_balance(account.into())).unwrap(); } @@ -547,11 +556,11 @@ mod tests { let (_, client) = test_setup(); let balances = client.metadata().module("Balances").unwrap(); - let dest = substrate_keyring::AccountKeyring::Bob.pair().public(); + let dest = substrate_keyring::AccountKeyring::Bob.to_account_id(); let address: Address = dest.clone().into(); let amount: Balance = 10_000; - let transfer = srml_balances::Call::transfer(address.clone(), amount); + let transfer = paint_balances::Call::transfer(address.clone(), amount); let call = node_runtime::Call::Balances(transfer); let call2 = balances .call("transfer", (address, codec::compact(amount))) @@ -559,7 +568,7 @@ mod tests { assert_eq!(call.encode().to_vec(), call2.0); let free_balance = - >::hashed_key_for(&dest); + >::hashed_key_for(&dest); let free_balance_key = StorageKey(free_balance); let free_balance_key2 = balances .storage("FreeBalance") @@ -570,7 +579,7 @@ mod tests { assert_eq!(free_balance_key, free_balance_key2); let account_nonce = - >::hashed_key_for(&dest); + >::hashed_key_for(&dest); let account_nonce_key = StorageKey(account_nonce); let account_nonce_key2 = client .metadata() diff --git a/src/srml/balances.rs b/src/paint/balances.rs similarity index 83% rename from src/srml/balances.rs rename to src/paint/balances.rs index affd02ce31..0bbc91dda8 100644 --- a/src/srml/balances.rs +++ b/src/paint/balances.rs @@ -1,4 +1,4 @@ -//! Implements support for the srml_balances module. +//! Implements support for the paint_balances module. use crate::{ codec::{ compact, @@ -6,7 +6,7 @@ use crate::{ }, error::Error, metadata::MetadataError, - srml::{ + paint::{ system::System, ModuleCalls, }, @@ -23,12 +23,14 @@ use runtime_primitives::traits::{ MaybeSerialize, Member, SimpleArithmetic, + Verify, + IdentifyAccount, }; use runtime_support::Parameter; use substrate_primitives::Pair; use std::fmt::Debug; -/// The subset of the `srml_balances::Trait` that a client must implement. +/// The subset of the `paint_balances::Trait` that a client must implement. pub trait Balances: System { /// The balance of an account. type Balance: Parameter @@ -43,9 +45,9 @@ pub trait Balances: System { } /// Blanket impl for using existing runtime types -impl Balances for T +impl Balances for T where - ::Header: serde::de::DeserializeOwned, + ::Header: serde::de::DeserializeOwned, { type Balance = T::Balance; } @@ -74,7 +76,7 @@ pub trait BalancesStore { ) -> Box::Balance, Error = Error> + Send>; } -impl BalancesStore for Client { +impl BalancesStore for Client { type Balances = T; fn free_balance( @@ -105,23 +107,28 @@ pub trait BalancesXt { type Balances: Balances; /// Keypair type type Pair: Pair; + /// Signature type + type Signature: Verify; - /// Create a call for the srml balances module - fn balances(&self, f: F) -> XtBuilder + /// Create a call for the paint balances module + fn balances(&self, f: F) -> XtBuilder where F: FnOnce( ModuleCalls, ) -> Result; } -impl BalancesXt for XtBuilder +impl BalancesXt for XtBuilder where P: Pair, + S: Verify, + S::Signer: From + IdentifyAccount, { type Balances = T; type Pair = P; + type Signature = S; - fn balances(&self, f: F) -> XtBuilder + fn balances(&self, f: F) -> XtBuilder where F: FnOnce( ModuleCalls, diff --git a/src/srml/contracts.rs b/src/paint/contracts.rs similarity index 81% rename from src/srml/contracts.rs rename to src/paint/contracts.rs index d6e8e41e0e..471fa3f3ec 100644 --- a/src/srml/contracts.rs +++ b/src/paint/contracts.rs @@ -1,11 +1,11 @@ -//! Implements support for the srml_contracts module. +//! Implements support for the paint_contracts module. use crate::{ codec::{ compact, Encoded, }, metadata::MetadataError, - srml::{ + paint::{ balances::Balances, system::System, ModuleCalls, @@ -13,24 +13,28 @@ use crate::{ Valid, XtBuilder, }; +use runtime_primitives::traits::{ + IdentifyAccount, + Verify, +}; use substrate_primitives::Pair; /// Gas units are chosen to be represented by u64 so that gas metering /// instructions can operate on them efficiently. pub type Gas = u64; -/// The subset of the `srml_contracts::Trait` that a client must implement. +/// The subset of the `paint_contracts::Trait` that a client must implement. pub trait Contracts: System + Balances {} /// Blanket impl for using existing runtime types impl< - T: srml_contracts::Trait - + srml_system::Trait - + srml_balances::Trait + T: paint_contracts::Trait + + paint_system::Trait + + paint_balances::Trait + std::fmt::Debug, > Contracts for T where - ::Header: serde::de::DeserializeOwned, + ::Header: serde::de::DeserializeOwned, { } @@ -40,23 +44,28 @@ pub trait ContractsXt { type Contracts: Contracts; /// Key Pair Type type Pair: Pair; + /// Signature type + type Signature: Verify; - /// Create a call for the srml contracts module - fn contracts(&self, f: F) -> XtBuilder + /// Create a call for the paint contracts module + fn contracts(&self, f: F) -> XtBuilder where F: FnOnce( ModuleCalls, ) -> Result; } -impl ContractsXt for XtBuilder +impl ContractsXt for XtBuilder where P: Pair, + S: Verify, + S::Signer: From + IdentifyAccount, { type Contracts = T; type Pair = P; + type Signature = S; - fn contracts(&self, f: F) -> XtBuilder + fn contracts(&self, f: F) -> XtBuilder where F: FnOnce( ModuleCalls, diff --git a/src/srml/mod.rs b/src/paint/mod.rs similarity index 100% rename from src/srml/mod.rs rename to src/paint/mod.rs diff --git a/src/srml/system.rs b/src/paint/system.rs similarity index 83% rename from src/srml/system.rs rename to src/paint/system.rs index 3867df3b4e..d220bbc945 100644 --- a/src/srml/system.rs +++ b/src/paint/system.rs @@ -1,9 +1,9 @@ -//! Implements support for the srml_system module. +//! Implements support for the paint_system module. use crate::{ codec::Encoded, error::Error, metadata::MetadataError, - srml::{ + paint::{ balances::Balances, ModuleCalls, }, @@ -21,6 +21,7 @@ use runtime_primitives::traits::{ CheckEqual, Hash, Header, + IdentifyAccount, MaybeDisplay, MaybeSerialize, MaybeSerializeDeserialize, @@ -28,13 +29,14 @@ use runtime_primitives::traits::{ SimpleArithmetic, SimpleBitOps, StaticLookup, + Verify, }; use runtime_support::Parameter; use serde::de::DeserializeOwned; use substrate_primitives::Pair; use std::fmt::Debug; -/// The subset of the `srml_system::Trait` that a client must implement. +/// The subset of the `paint::Trait` that a client must implement. pub trait System: 'static + Eq + Clone + Debug { /// Account index (aka nonce) type. This stores the number of previous /// transactions associated with a sender account. @@ -85,7 +87,7 @@ pub trait System: 'static + Eq + Clone + Debug { + Ord + Default; - /// The address type. This instead of `::Source`. + /// The address type. This instead of `::Source`. type Address: Codec + Clone + PartialEq + Debug; /// The block header. @@ -95,9 +97,9 @@ pub trait System: 'static + Eq + Clone + Debug { } /// Blanket impl for using existing runtime types -impl System for T +impl System for T where - ::Header: serde::de::DeserializeOwned, + ::Header: serde::de::DeserializeOwned, { type Index = T::Index; type BlockNumber = T::BlockNumber; @@ -120,7 +122,7 @@ pub trait SystemStore { ) -> Box::Index, Error = Error> + Send>; } -impl SystemStore for Client { +impl SystemStore for Client { type System = T; fn account_nonce( @@ -149,23 +151,28 @@ pub trait SystemXt { type System: System; /// Keypair type type Pair: Pair; + /// Signature type + type Signature: Verify; - /// Create a call for the srml system module - fn system(&self, f: F) -> XtBuilder + /// Create a call for the paint system module + fn system(&self, f: F) -> XtBuilder where F: FnOnce( ModuleCalls, ) -> Result; } -impl SystemXt for XtBuilder +impl SystemXt for XtBuilder where P: Pair, + S: Verify, + S::Signer: From + IdentifyAccount, { type System = T; type Pair = P; + type Signature = S; - fn system(&self, f: F) -> XtBuilder + fn system(&self, f: F) -> XtBuilder where F: FnOnce( ModuleCalls, diff --git a/src/rpc.rs b/src/rpc.rs index 1cbd404713..205f5f6c92 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -21,7 +21,7 @@ use crate::{ RuntimeEvent, }, metadata::Metadata, - srml::{ + paint::{ balances::Balances, system::System, }, @@ -161,9 +161,9 @@ use txpool::watcher::Status; use crate::{ events::RawEvent, - srml::system::SystemEvent, + paint::system::SystemEvent, }; -use srml_system::Phase; +use paint_system::Phase; type MapClosure = Box T + Send>; pub type MapStream = stream::Map, MapClosure>; diff --git a/src/runtimes.rs b/src/runtimes.rs index 4d315618ed..a29238648a 100644 --- a/src/runtimes.rs +++ b/src/runtimes.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with substrate-subxt. If not, see . -use crate::srml::{ +use crate::paint::{ balances::Balances, contracts::Contracts, system::System, @@ -43,7 +43,7 @@ impl System for DefaultNodeRuntime { type Hash = substrate_primitives::H256; type Hashing = BlakeTwo256; type AccountId = ::Signer; - type Address = srml_indices::address::Address; + type Address = paint_indices::address::Address; type Header = Header; }