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

Robin/add rpc calls #15

Open
wants to merge 10 commits into
base: add-rpc-calls
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Clippy
run: cargo clippy --bins --tests --examples --all -- -D warnings
run: cargo clippy --bins --tests --examples --
- name: Run tests
run: cargo test

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ edition = "2021"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
serde = { version = "1.0.132", default-features = false, features = [
"derive",
], optional = true }
codec = { package = "parity-scale-codec", version = "3.0.0", features = [
"derive",
], default-features = false }
Expand All @@ -27,14 +30,14 @@ sp-std = { git = "https://github.com/paritytech/substrate", default-features = f
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }

[dev-dependencies]
serde = { version = "1.0.132" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }
pallet-balances = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }

[features]
default = ["std", "runtime-benchmarks"]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
std = [
"serde",
"codec/std",
"sp-std/std",
"sp-core/std",
Expand Down
3 changes: 2 additions & 1 deletion rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ scale-info = { version = "2.1.1", default-features = false, features = [
"derive",
] }

jsonrpsee = { version = "0.14.0", features = ["server", "macros"] }
jsonrpsee = { version = "0.13.0", features = ["server", "macros"] }

sp-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }
sp-std = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }
sp-blockchain = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }

pallet-supersig = { path = ".." }
pallet-supersig-rpc-runtime-api = { path = "./runtime-api" }
10 changes: 6 additions & 4 deletions rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ license = "Apache-2.0"


[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", features = [
codec = { package = "parity-scale-codec", version = "3.0", features = [
"derive",
], default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = [
"derive",
] }

sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" }
sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24", version = "4.0.0-dev" }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24", version = "4.0.0-dev" }

pallet-supersig = { path = "../..", default-features = false, features = [] }

[features]
default = ["std"]
std = ["codec/std", "sp-std/std", "sp-api/std"]
std = ["codec/std", "sp-std/std", "sp-api/std", "pallet-supersig/std"]
11 changes: 9 additions & 2 deletions rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;
use sp_std::vec::Vec;
#[cfg(not(feature = "std"))]
use sp_std::prelude::Vec;

use pallet_supersig::{rpc::ProposalState, CallId, Role, SupersigId};

sp_api::decl_runtime_apis! {
pub trait SuperSigApi<AccountId>
where
AccountId: Codec,
{
fn get_account_supersigs(origin: AccountId) -> Vec<u128>;
fn get_supersig_id(supersig_account: AccountId) -> Option<SupersigId>;
fn get_user_supersigs(who: AccountId) -> Vec<SupersigId>;
fn list_members(supersig_id: SupersigId) -> Vec<(AccountId, Role)>;
fn list_proposals(supersig_id: SupersigId) -> (Vec<ProposalState<AccountId>>, u32);
fn get_proposal_state(supersig_id: SupersigId, call_id: CallId) -> Option<(ProposalState<AccountId>, u32)>;
}
}
87 changes: 79 additions & 8 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,47 @@ use jsonrpsee::{
proc_macros::rpc,
types::error::{CallError, ErrorObject},
};
use sp_api::BlockId;
use sp_api::BlockT;
use sp_api::ProvideRuntimeApi;
use sp_api::{BlockId, BlockT, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use std::{marker::PhantomData, sync::Arc};

pub use pallet_supersig_rpc_runtime_api::SuperSigApi as SuperSigRuntimeApi;

use pallet_supersig::{rpc::ProposalState, CallId, Role, SupersigId};

#[rpc(client, server)]
pub trait SuperSigApi<BlockHash, AccountId> {
#[method(name = "superSig_getAccountSupersigs")]
fn get_account_supersigs(&self, who: AccountId, at: Option<BlockHash>) -> RpcResult<Vec<u128>>;
#[method(name = "superSig_getSupersigId")]
fn get_supersig_id(
&self,
supersig_account: AccountId,
at: Option<BlockHash>,
) -> RpcResult<Option<SupersigId>>;
#[method(name = "superSig_getUserSupersigs")]
fn get_user_supersigs(
&self,
who: AccountId,
at: Option<BlockHash>,
) -> RpcResult<Vec<SupersigId>>;
#[method(name = "superSig_listMembers")]
fn list_members(
&self,
supersig_id: SupersigId,
at: Option<BlockHash>,
) -> RpcResult<Vec<(AccountId, Role)>>;
#[method(name = "superSig_listProposals")]
fn list_proposals(
&self,
supersig_id: SupersigId,
at: Option<BlockHash>,
) -> RpcResult<(Vec<ProposalState<AccountId>>, u32)>;
#[method(name = "superSig_getProposalState")]
fn get_proposal_state(
&self,
supersig_id: SupersigId,
call_id: CallId,
at: Option<BlockHash>,
) -> RpcResult<Option<(ProposalState<AccountId>, u32)>>;
}

/// SuperSig RPC methods.
Expand All @@ -42,14 +71,56 @@ where
Client::Api: SuperSigRuntimeApi<Block, AccountId>,
AccountId: Codec,
{
fn get_account_supersigs(
fn get_supersig_id(
&self,
supersig_account: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Option<SupersigId>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
api.get_supersig_id(&at, supersig_account).map_err(runtime_error_into_rpc_err)
}

fn get_user_supersigs(
&self,
who: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<u128>> {
) -> RpcResult<Vec<SupersigId>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
api.get_user_supersigs(&at, who).map_err(runtime_error_into_rpc_err)
}

fn list_members(
&self,
supersig_id: SupersigId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<(AccountId, Role)>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
api.list_members(&at, supersig_id).map_err(runtime_error_into_rpc_err)
}

fn list_proposals(
&self,
supersig_id: SupersigId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<(Vec<ProposalState<AccountId>>, u32)> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
api.list_proposals(&at, supersig_id).map_err(runtime_error_into_rpc_err)
}

fn get_proposal_state(
&self,
supersig_id: SupersigId,
call_id: CallId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Option<(ProposalState<AccountId>, u32)>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
api.get_account_supersigs(&at, who).map_err(runtime_error_into_rpc_err)
api.get_proposal_state(&at, supersig_id, call_id)
.map_err(runtime_error_into_rpc_err)
}
}

Expand Down
40 changes: 10 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub use sp_runtime::traits::{
};
pub use sp_std::{boxed::Box, cmp::max, mem::size_of, prelude::Vec};

pub mod rpc;
pub mod types;
pub mod weights;

Expand Down Expand Up @@ -250,7 +251,7 @@ pub mod pallet {
// A supersig should at least have one member
let member_length = members.len();
if member_length < 1 {
return Err(Error::<T>::InvalidNumberOfMembers.into());
return Err(Error::<T>::InvalidNumberOfMembers.into())
}

// Get it id and associated account
Expand Down Expand Up @@ -352,10 +353,10 @@ pub mod pallet {
let supersig_id = Self::get_supersig_id_from_account(&supersig_account)?;

if Self::calls(supersig_id, call_id).is_none() {
return Err(Error::<T>::CallNotFound.into());
return Err(Error::<T>::CallNotFound.into())
}
if Self::members_votes((supersig_id, call_id, who.clone())) {
return Err(Error::<T>::AlreadyVoted.into());
return Err(Error::<T>::AlreadyVoted.into())
}

// Different roles have different voting weight
Expand Down Expand Up @@ -427,7 +428,7 @@ pub mod pallet {

// Either the supersig or the user that created the vote can remove a call
if who != supersig_account && who != preimage.provider {
return Err(Error::<T>::NotAllowed.into());
return Err(Error::<T>::NotAllowed.into())
}

// Clean up storage and release reserved funds
Expand Down Expand Up @@ -569,7 +570,7 @@ pub mod pallet {
let supersig_id = Self::get_supersig_id_from_account(&supersig_account)?;

if Self::members(supersig_id, &who) == Role::NotMember {
return Err(Error::<T>::NotMember.into());
return Err(Error::<T>::NotMember.into())
}

// Remeber the storage state before we remove the members from it
Expand All @@ -586,7 +587,7 @@ pub mod pallet {
// A supersig should at least have one member
TotalMembers::<T>::try_mutate(supersig_id, |nb| {
if *nb == 1 {
return Err(Error::<T>::InvalidNumberOfMembers);
return Err(Error::<T>::InvalidNumberOfMembers)
};
*nb -= 1;
Ok(())
Expand All @@ -605,12 +606,12 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
fn get_supersig_id_from_account(
pub fn get_supersig_id_from_account(
supersig_account: &T::AccountId,
) -> Result<SupersigId, pallet::Error<T>> {
if let Some((account, supersig_id)) = PalletId::try_from_sub_account(supersig_account) {
if account != T::PalletId::get() || Self::total_members(supersig_id) == 0 {
return Err(Error::<T>::NotSupersig);
return Err(Error::<T>::NotSupersig)
}
Ok(supersig_id)
} else {
Expand Down Expand Up @@ -679,7 +680,7 @@ pub mod pallet {
let new_total_members =
n.saturating_sub(removed.len().try_into().map_err(|_| Error::<T>::Conversion)?);
if new_total_members < 1 {
return Err(Error::<T>::InvalidNumberOfMembers);
return Err(Error::<T>::InvalidNumberOfMembers)
}

*n = new_total_members;
Expand Down Expand Up @@ -750,24 +751,3 @@ pub mod pallet {
}
}
}

// RPC calls

#[allow(dead_code)]
impl<T: Config> Pallet<T> {
pub fn get_account_supersigs(who: T::AccountId) -> Vec<SupersigId> {
Members::<T>::iter()
.filter_map(|(supersig_id, member_id, _)| {
if member_id == who {
Some(supersig_id)
} else {
None
}
})
.collect()
}

pub fn get_members_connected_to_supersig(which: SupersigId) -> Vec<(T::AccountId, Role)> {
Members::<T>::iter_prefix(which).collect()
}
}
Loading