Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sudo #115

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use sp_core::storage::StorageKey;

pub mod balances;
pub mod contracts;
pub mod sudo;
pub mod system;

/// Store trait.
Expand Down
61 changes: 61 additions & 0 deletions src/frame/sudo.rs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

//! 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<T>,
/// 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(),
},
}
});
}
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ where
S: Encode + Send + Sync + 'static,
E: SignedExtra<T> + SignedExtension + Send + Sync + 'static,
{
/// Encodes a call.
pub fn encode<C: Call<T>>(&self, call: C) -> Result<Encoded, Error> {
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.
Expand All @@ -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)
Expand Down Expand Up @@ -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<u8>);

impl codec::Encode for Encoded {
Expand Down
3 changes: 3 additions & 0 deletions src/runtimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::frame::{
Balances,
},
contracts::Contracts,
sudo::Sudo,
system::System,
};

Expand Down Expand Up @@ -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
Expand Down