Skip to content

Commit

Permalink
WIP: move roles pallet from solochain
Browse files Browse the repository at this point in the history
  • Loading branch information
F3Joule committed Jun 23, 2022
1 parent 68bc45f commit 58ca4a7
Show file tree
Hide file tree
Showing 11 changed files with 1,831 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pallets/roles/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[package]
name = 'pallet-roles'
version = '0.7.3'
authors = ['DappForce <dappforce@pm.me>']
edition = '2018'
license = 'GPL-3.0-only'
homepage = 'https://subsocial.network'
repository = 'https://github.com/dappforce/dappforce-subsocial-node'
description = 'Roles management pallet'
keywords = ['blockchain', 'cryptocurrency', 'social-network', 'news-feed', 'marketplace']
categories = ['cryptography::cryptocurrencies']

[features]
default = ['std']
std = [
'codec/std',
'scale-info/std',
'frame-support/std',
'frame-system/std',
'sp-runtime/std',
'sp-std/std',
'df-traits/std',
'pallet-permissions/std',
'pallet-utils/std',
]

[dependencies.codec]
default-features = false
features = ['derive']
package = 'parity-scale-codec'
version = '2.0.0'

[dependencies]
scale-info = { version = "1.0", default-features = false, features = ["derive"] }

# Local dependencies
df-traits = { default-features = false, path = '../traits' }
pallet-permissions = { default-features = false, path = '../permissions' }
pallet-utils = { default-features = false, path = '../utils' }

# Substrate dependencies
frame-support = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
frame-system = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
pallet-timestamp = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-std = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }

[dev-dependencies]
serde = { version = '1.0.119' }

pallet-balances = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-core = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-io = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
51 changes: 51 additions & 0 deletions pallets/roles/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[package]
name = 'roles-rpc'
version = '0.7.3'
authors = ['DappForce <dappforce@pm.me>']
edition = '2018'
license = 'GPL-3.0-only'
homepage = 'https://subsocial.network'
repository = 'https://github.com/dappforce/dappforce-subsocial-node'
description = 'RPC methods for the roles pallet'
keywords = ['blockchain', 'cryptocurrency', 'social-network', 'news-feed', 'marketplace']
categories = ['cryptography::cryptocurrencies']

[dependencies.serde]
optional = true
features = ['derive']
version = '1.0.119'

[dependencies.codec]
default-features = false
features = ['derive']
package = 'parity-scale-codec'
version = '2.0.0'

[dependencies]
jsonrpc-core = '18.0.0'
jsonrpc-core-client = '18.0.0'
jsonrpc-derive = '18.0.0'

# Local dependencies
pallet-permissions = { default-features = false, path = '../../permissions' }
pallet-utils = { default-features = false, path = '../../utils' }

# Custom Runtime API
roles-runtime-api = { default-features = false, path = 'runtime-api' }

# Substrate dependencies
sp-api = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-blockchain = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-rpc = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }

[features]
default = ['std']
std = [
'serde',
'sp-runtime/std',
'sp-api/std',
'roles-runtime-api/std',
'pallet-permissions/std',
'pallet-utils/std',
]
43 changes: 43 additions & 0 deletions pallets/roles/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[package]
name = 'roles-runtime-api'
version = '0.7.3'
authors = ['DappForce <dappforce@pm.me>']
edition = '2018'
license = 'GPL-3.0-only'
homepage = 'https://subsocial.network'
repository = 'https://github.com/dappforce/dappforce-subsocial-node'
description = 'Runtime API definition for the roles pallet'
keywords = ['blockchain', 'cryptocurrency', 'social-network', 'news-feed', 'marketplace']
categories = ['cryptography::cryptocurrencies']

[dependencies.serde]
optional = true
features = ["derive"]
version = "1.0.119"

[dependencies.codec]
default-features = false
features = ['derive']
package = 'parity-scale-codec'
version = '2.0.0'

[dependencies]
# Local dependencies
pallet-permissions = { default-features = false, path = '../../../permissions' }
pallet-utils = { default-features = false, path = '../../../utils' }

# Substrate dependencies
sp-api = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-std = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }

[features]
default = ['std']
std = [
'serde',
'sp-api/std',
'sp-std/std',
'sp-runtime/std',
'pallet-permissions/std',
'pallet-utils/std',
]
18 changes: 18 additions & 0 deletions pallets/roles/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;
use sp_std::vec::Vec;
use pallet_utils::SpaceId;
use pallet_permissions::SpacePermission;

sp_api::decl_runtime_apis! {
pub trait RolesApi<AccountId> where
AccountId: Codec
{
fn get_space_permissions_by_account(account: AccountId, space_id: SpaceId) -> Vec<SpacePermission>;

fn get_accounts_with_any_role_in_space(space_id: SpaceId) -> Vec<AccountId>;

fn get_space_ids_for_account_with_any_role(account_id: AccountId) -> Vec<SpaceId>;
}
}
96 changes: 96 additions & 0 deletions pallets/roles/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use std::sync::Arc;
use codec::Codec;
use sp_blockchain::HeaderBackend;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use sp_api::ProvideRuntimeApi;
use pallet_utils::{SpaceId, rpc::map_rpc_error};
use pallet_permissions::SpacePermission;

pub use roles_runtime_api::RolesApi as RolesRuntimeApi;

#[rpc]
pub trait RolesApi<BlockHash, AccountId> {
#[rpc(name = "roles_getSpacePermissionsByAccount")]
fn get_space_permissions_by_account(
&self,
at: Option<BlockHash>,
account: AccountId,
space_id: SpaceId
) -> Result<Vec<SpacePermission>>;

#[rpc(name = "roles_getAccountsWithAnyRoleInSpace")]
fn get_accounts_with_any_role_in_space(
&self,
at: Option<BlockHash>,
space_id: SpaceId
) -> Result<Vec<AccountId>>;

#[rpc(name = "roles_getSpaceIdsForAccountWithAnyRole")]
fn get_space_ids_for_account_with_any_role(
&self,
at: Option<BlockHash>,
account_id: AccountId
) -> Result<Vec<SpaceId>>;
}

pub struct Roles<C, M> {
client: Arc<C>,
_marker: std::marker::PhantomData<M>,
}

impl<C, M> Roles<C, M> {
pub fn new(client: Arc<C>) -> Self {
Self {
client,
_marker: Default::default(),
}
}
}

impl<C, Block, AccountId> RolesApi<<Block as BlockT>::Hash, AccountId>
for Roles<C, Block>
where
Block: BlockT,
AccountId: Codec,
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: RolesRuntimeApi<Block, AccountId>,
{
fn get_space_permissions_by_account(
&self, at:
Option<<Block as BlockT>::Hash>,
account: AccountId,
space_id: SpaceId
) -> Result<Vec<SpacePermission>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));

let runtime_api_result = api.get_space_permissions_by_account(&at, account, space_id);
runtime_api_result.map_err(map_rpc_error)
}

fn get_accounts_with_any_role_in_space(
&self, at:
Option<<Block as BlockT>::Hash>,
space_id: SpaceId
) -> Result<Vec<AccountId>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));

let runtime_api_result = api.get_accounts_with_any_role_in_space(&at, space_id);
runtime_api_result.map_err(map_rpc_error)
}

fn get_space_ids_for_account_with_any_role(
&self, at:
Option<<Block as BlockT>::Hash>,
account_id: AccountId
) -> Result<Vec<SpaceId>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));

let runtime_api_result = api.get_space_ids_for_account_with_any_role(&at, account_id);
runtime_api_result.map_err(map_rpc_error)
}
}
Loading

0 comments on commit 58ca4a7

Please sign in to comment.