Submit (& sign) extrinsics from the runtime. (#3514)

* Abstract constructing extrinsic and signing.

* Initial impl of signer.

* Implement get payload.

* Clean up the code.

* Improve docs.

* Bump version.

* Update core/sr-primitives/src/generic/unchecked_extrinsic.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Fix tests & address grumbles.

* Fix build.

* Fix runtime tests.

* Fix bound test.

* Fix bound test.
This commit is contained in:
Tomasz Drwięga
2019-09-01 02:19:04 +02:00
committed by Gavin Wood
parent 0cae7217d8
commit feecfc856d
15 changed files with 346 additions and 71 deletions
@@ -186,7 +186,11 @@ mod tests {
type AuthorityId = AuthorityId;
type Call = im_online::Call<Test>;
type Event = ();
type UncheckedExtrinsic = UncheckedExtrinsic<(), im_online::Call<Test>, (), ()>;
type SubmitTransaction = system::offchain::TransactionSubmitter<
(),
im_online::Call<Test>,
UncheckedExtrinsic<(), im_online::Call<Test>, (), ()>,
>;
type ReportUnresponsiveness = ();
type CurrentElectedSet = DummyCurrentElectedSet<AuthorityId>;
}
-1
View File
@@ -1187,7 +1187,6 @@ pub struct TakeFees<T: Trait<I>, I: Instance = DefaultInstance>(#[codec(compact)
impl<T: Trait<I>, I: Instance> TakeFees<T, I> {
/// utility constructor. Used only in client/factory code.
#[cfg(feature = "std")]
pub fn from(fee: T::Balance) -> Self {
Self(fee)
}
+18 -9
View File
@@ -75,7 +75,7 @@ use session::historical::IdentificationTuple;
use sr_io::Printable;
use sr_primitives::{
Perbill, ApplyError,
traits::{Convert, Extrinsic as ExtrinsicT, Member},
traits::{Convert, Member},
transaction_validity::{TransactionValidity, TransactionLongevity, ValidTransaction},
};
use sr_staking_primitives::{
@@ -87,11 +87,18 @@ use srml_support::{
Parameter, StorageValue, StorageDoubleMap,
};
use system::ensure_none;
use system::offchain::SubmitUnsignedTransaction;
pub mod sr25519 {
mod app_sr25519 {
use app_crypto::{app_crypto, key_types::IM_ONLINE, sr25519};
app_crypto!(sr25519, IM_ONLINE);
impl From<Signature> for sr_primitives::AnySignature {
fn from(sig: Signature) -> Self {
sr25519::Signature::from(sig).into()
}
}
}
/// An i'm online keypair using sr25519 as its crypto.
@@ -109,6 +116,12 @@ pub mod ed25519 {
mod app_ed25519 {
use app_crypto::{app_crypto, key_types::IM_ONLINE, ed25519};
app_crypto!(ed25519, IM_ONLINE);
impl From<Signature> for sr_primitives::AnySignature {
fn from(sig: Signature) -> Self {
ed25519::Signature::from(sig).into()
}
}
}
/// An i'm online keypair using ed25519 as its crypto.
@@ -141,7 +154,6 @@ struct WorkerStatus<BlockNumber> {
// Error which may occur while executing the off-chain code.
enum OffchainErr {
DecodeWorkerStatus,
ExtrinsicCreation,
FailedSigning,
NetworkState,
SubmitTransaction,
@@ -151,7 +163,6 @@ impl Printable for OffchainErr {
fn print(self) {
match self {
OffchainErr::DecodeWorkerStatus => print("Offchain error: decoding WorkerStatus failed!"),
OffchainErr::ExtrinsicCreation => print("Offchain error: extrinsic creation failed!"),
OffchainErr::FailedSigning => print("Offchain error: signing failed!"),
OffchainErr::NetworkState => print("Offchain error: fetching network state failed!"),
OffchainErr::SubmitTransaction => print("Offchain error: submitting transaction failed!"),
@@ -183,9 +194,8 @@ pub trait Trait: system::Trait + session::historical::Trait {
/// A dispatchable call type.
type Call: From<Call<Self>>;
/// A extrinsic right from the external world. This is unchecked and so
/// can contain a signature.
type UncheckedExtrinsic: ExtrinsicT<Call=<Self as Trait>::Call> + Encode + Decode;
/// A transaction submitter.
type SubmitTransaction: SubmitUnsignedTransaction<Self, <Self as Trait>::Call>;
/// A type that gives us the ability to submit unresponsiveness offence reports.
type ReportUnresponsiveness:
@@ -332,9 +342,8 @@ impl<T: Trait> Module<T> {
let signature = key.sign(&heartbeat_data.encode()).ok_or(OffchainErr::FailedSigning)?;
let call = Call::heartbeat(heartbeat_data, signature);
let ex = T::UncheckedExtrinsic::new_unsigned(call.into())
.ok_or(OffchainErr::ExtrinsicCreation)?;
sr_io::submit_transaction(&ex).map_err(|_| OffchainErr::SubmitTransaction)?;
T::SubmitTransaction::submit_unsigned(call)
.map_err(|_| OffchainErr::SubmitTransaction)?;
// once finished we set the worker status without comparing
// if the existing value changed in the meantime. this is
+5 -3
View File
@@ -55,14 +55,16 @@ macro_rules! impl_outer_inherent {
fn create_extrinsics(&self) ->
$crate::inherent::Vec<<$block as $crate::inherent::BlockT>::Extrinsic> {
use $crate::inherent::ProvideInherent;
use $crate::inherent::Extrinsic;
let mut inherents = Vec::new();
$(
if let Some(inherent) = $module::create_inherent(self) {
inherents.push($uncheckedextrinsic::new_unsigned(
Call::$call(inherent))
);
inherents.push($uncheckedextrinsic::new(
Call::$call(inherent),
None,
).expect("Runtime UncheckedExtrinsic is not Opaque, so it has to return `Some`; qed"));
}
)*
+14 -15
View File
@@ -121,6 +121,8 @@ use runtime_io::{TestExternalities, Blake2Hasher};
#[cfg(any(feature = "std", test))]
use primitives::ChangesTrieConfiguration;
pub mod offchain;
/// Handler for when a new account has been created.
pub trait OnNewAccount<AccountId> {
/// A new account `who` has been registered.
@@ -441,7 +443,7 @@ decl_storage! {
}
}
pub struct EnsureRoot<AccountId>(::rstd::marker::PhantomData<AccountId>);
pub struct EnsureRoot<AccountId>(rstd::marker::PhantomData<AccountId>);
impl<
O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>,
AccountId,
@@ -455,7 +457,7 @@ impl<
}
}
pub struct EnsureSigned<AccountId>(::rstd::marker::PhantomData<AccountId>);
pub struct EnsureSigned<AccountId>(rstd::marker::PhantomData<AccountId>);
impl<
O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>,
AccountId,
@@ -469,7 +471,7 @@ impl<
}
}
pub struct EnsureSignedBy<Who, AccountId>(::rstd::marker::PhantomData<(Who, AccountId)>);
pub struct EnsureSignedBy<Who, AccountId>(rstd::marker::PhantomData<(Who, AccountId)>);
impl<
O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>,
Who: Contains<AccountId>,
@@ -484,7 +486,7 @@ impl<
}
}
pub struct EnsureNone<AccountId>(::rstd::marker::PhantomData<AccountId>);
pub struct EnsureNone<AccountId>(rstd::marker::PhantomData<AccountId>);
impl<
O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>,
AccountId,
@@ -498,7 +500,7 @@ impl<
}
}
pub struct EnsureNever<T>(::rstd::marker::PhantomData<T>);
pub struct EnsureNever<T>(rstd::marker::PhantomData<T>);
impl<O, T> EnsureOrigin<O> for EnsureNever<T> {
type Success = T;
fn try_origin(o: O) -> Result<Self::Success, O> {
@@ -892,8 +894,7 @@ impl<T: Trait + Send + Sync> CheckWeight<T> {
}
}
/// Utility constructor for tests and client code.
#[cfg(feature = "std")]
/// Creates new `SignedExtension` to check weight of the extrinsic.
pub fn new() -> Self {
Self(PhantomData)
}
@@ -948,7 +949,6 @@ impl<T: Trait + Send + Sync> rstd::fmt::Debug for CheckWeight<T> {
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct CheckNonce<T: Trait>(#[codec(compact)] T::Index);
#[cfg(feature = "std")]
impl<T: Trait> CheckNonce<T> {
/// utility constructor. Used only in client/factory code.
pub fn from(nonce: T::Index) -> Self {
@@ -1022,7 +1022,6 @@ impl<T: Trait> SignedExtension for CheckNonce<T> {
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct CheckEra<T: Trait + Send + Sync>((Era, rstd::marker::PhantomData<T>));
#[cfg(feature = "std")]
impl<T: Trait + Send + Sync> CheckEra<T> {
/// utility constructor. Used only in client/factory code.
pub fn from(era: Era) -> Self {
@@ -1077,10 +1076,10 @@ impl<T: Trait + Send + Sync> rstd::fmt::Debug for CheckGenesis<T> {
}
}
#[cfg(feature = "std")]
impl<T: Trait + Send + Sync> CheckGenesis<T> {
/// Creates new `SignedExtension` to check genesis hash.
pub fn new() -> Self {
Self(std::marker::PhantomData)
Self(rstd::marker::PhantomData)
}
}
@@ -1106,10 +1105,10 @@ impl<T: Trait + Send + Sync> rstd::fmt::Debug for CheckVersion<T> {
}
}
#[cfg(feature = "std")]
impl<T: Trait + Send + Sync> CheckVersion<T> {
/// Create new `SignedExtension` to check runtime version.
pub fn new() -> Self {
Self(std::marker::PhantomData)
Self(rstd::marker::PhantomData)
}
}
@@ -1124,10 +1123,10 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckVersion<T> {
}
}
pub struct ChainContext<T>(::rstd::marker::PhantomData<T>);
pub struct ChainContext<T>(rstd::marker::PhantomData<T>);
impl<T> Default for ChainContext<T> {
fn default() -> Self {
ChainContext(::rstd::marker::PhantomData)
ChainContext(rstd::marker::PhantomData)
}
}
+135
View File
@@ -0,0 +1,135 @@
// 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::RuntimeAppPublic;
use sr_primitives::traits::Extrinsic as ExtrinsicT;
/// A trait responsible for signing a payload using given account.
pub trait Signer<Account, 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>(account: Account, payload: &Payload) -> Option<Signature>;
}
impl<Account, Signature, AppPublic> Signer<Account, Signature> for AppPublic where
AppPublic: RuntimeAppPublic + From<Account>,
AppPublic::Signature: Into<Signature>,
{
fn sign<Payload: Encode>(account: Account, raw_payload: &Payload) -> Option<Signature> {
raw_payload.using_encoded(|payload| {
AppPublic::from(account).sign(&payload).map(Into::into)
})
}
}
/// Creates runtime-specific signed transaction.
pub trait CreateTransaction<T: crate::Trait, Extrinsic: ExtrinsicT> {
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<T::AccountId, Self::Signature>>(
call: Extrinsic::Call,
account: T::AccountId,
nonce: T::Index,
) -> Option<(Extrinsic::Call, Extrinsic::SignaturePayload)>;
}
/// 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<
T::AccountId,
<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>, id: T::AccountId) -> Result<(), ()> {
let call = call.into();
let expected = <crate::Module<T>>::account_nonce(&id);
let (call, signature_data) = Self::CreateTransaction
::create_transaction::<Self::Signer>(call, id, expected)
.ok_or(())?;
let xt = Self::Extrinsic::new(call, Some(signature_data)).ok_or(())?;
runtime_io::submit_transaction(&xt)
}
}
/// 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::submit_transaction(&xt)
}
}
/// 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<T::AccountId, <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;
}