From 5b0e6af790ee70b27a2f0fbac0f133f81549ed2d Mon Sep 17 00:00:00 2001 From: David Craven Date: Tue, 26 May 2020 11:36:01 +0200 Subject: [PATCH] Add support for sudo. --- src/frame/mod.rs | 1 + src/frame/sudo.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 15 ++++++++---- src/runtimes.rs | 3 +++ 4 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 src/frame/sudo.rs diff --git a/src/frame/mod.rs b/src/frame/mod.rs index 7244b8c35f..1c8e419e0f 100644 --- a/src/frame/mod.rs +++ b/src/frame/mod.rs @@ -34,6 +34,7 @@ use sp_core::storage::StorageKey; pub mod balances; pub mod contracts; +pub mod sudo; pub mod system; /// Store trait. diff --git a/src/frame/sudo.rs b/src/frame/sudo.rs new file mode 100644 index 0000000000..cb06d1e05d --- /dev/null +++ b/src/frame/sudo.rs @@ -0,0 +1,61 @@ +// Copyright 2019-2020 Parity Technologies (UK) Ltd. +// This file is part of substrate-subxt. +// +// subxt 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. +// +// subxt 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-subxt. If not, see . + +//! Implements support for the frame_sudo module. + +use crate::{ + frame::system::{ + System, + SystemEventsDecoder, + }, + Encoded, +}; +use codec::Encode; +use core::marker::PhantomData; + +/// The subset of the `frame_sudo::Trait` that a client must implement. +#[module] +pub trait Sudo: System {} + +/// Execute a transaction with sudo permissions. +#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)] +pub struct SudoCall<'a, T: Sudo> { + /// Runtime marker. + pub _runtime: PhantomData, + /// Encoded transaction. + pub call: &'a Encoded, +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::frame::balances::TransferCall; + + subxt_test!({ + name: test_sudo, + step: { + call: SudoCall { + _runtime: PhantomData, + call: &client.encode( + TransferCall { + to: &bob.clone().into(), + amount: 10_000, + } + ).unwrap(), + }, + } + }); +} diff --git a/src/lib.rs b/src/lib.rs index 9d38535627..7a14259f73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -300,6 +300,14 @@ where S: Encode + Send + Sync + 'static, E: SignedExtra + SignedExtension + Send + Sync + 'static, { + /// Encodes a call. + pub fn encode>(&self, call: C) -> Result { + Ok(self + .metadata() + .module_with_calls(C::MODULE) + .and_then(|module| module.call(C::FUNCTION, call))?) + } + /// Creates an unsigned extrinsic. /// /// If `nonce` is `None` the nonce will be fetched from the chain. @@ -317,10 +325,7 @@ where let spec_version = self.runtime_version.spec_version; let tx_version = self.runtime_version.transaction_version; let genesis_hash = self.genesis_hash; - let call = self - .metadata() - .module_with_calls(C::MODULE) - .and_then(|module| module.call(C::FUNCTION, call))?; + let call = self.encode(call)?; let extra: E = E::new(spec_version, tx_version, account_nonce, genesis_hash); let raw_payload = SignedPayload::new(call, extra.extra())?; Ok(raw_payload) @@ -402,7 +407,7 @@ where /// Wraps an already encoded byte vector, prevents being encoded as a raw byte vector as part of /// the transaction payload -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct Encoded(pub Vec); impl codec::Encode for Encoded { diff --git a/src/runtimes.rs b/src/runtimes.rs index 3ced7ce0b5..2ca64ebe41 100644 --- a/src/runtimes.rs +++ b/src/runtimes.rs @@ -31,6 +31,7 @@ use crate::frame::{ Balances, }, contracts::Contracts, + sudo::Sudo, system::System, }; @@ -61,6 +62,8 @@ impl Balances for DefaultNodeRuntime { impl Contracts for DefaultNodeRuntime {} +impl Sudo for DefaultNodeRuntime {} + /// Concrete type definitions compatible with those for kusama, v0.7 /// /// # Note