Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
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
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ members = [
"frame/staking",
"frame/staking/reward-curve",
"frame/staking/reward-fn",
"frame/staking/rpc",
"frame/staking/rpc/runtime-api",
"frame/state-trie-migration",
"frame/sudo",
"frame/root-offences",
Expand Down
1 change: 1 addition & 0 deletions bin/node/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
jsonrpsee = { version = "0.16.2", features = ["server"] }
node-primitives = { version = "2.0.0", path = "../primitives" }
pallet-transaction-payment-rpc = { version = "4.0.0-dev", path = "../../../frame/transaction-payment/rpc/" }
pallet-staking-rpc = { version = "4.0.0-dev", path = "../../../frame/staking/rpc/" }
mmr-rpc = { version = "4.0.0-dev", path = "../../../client/merkle-mountain-range/rpc/" }
sc-chain-spec = { version = "4.0.0-dev", path = "../../../client/chain-spec" }
sc-client-api = { version = "4.0.0-dev", path = "../../../client/api" }
Expand Down
3 changes: 3 additions & 0 deletions bin/node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ where
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
C::Api: mmr_rpc::MmrRuntimeApi<Block, <Block as sp_runtime::traits::Block>::Hash, BlockNumber>,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
C::Api: pallet_staking_rpc::StakingRuntimeApi<Block, Balance>,
C::Api: BabeApi<Block>,
C::Api: BlockBuilder<Block>,
P: TransactionPool + 'static,
Expand All @@ -118,6 +119,7 @@ where
B::State: sc_client_api::backend::StateBackend<sp_runtime::traits::HashFor<Block>>,
{
use mmr_rpc::{Mmr, MmrApiServer};
use pallet_staking_rpc::{Staking, StakingApiServer};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use sc_consensus_babe_rpc::{Babe, BabeApiServer};
use sc_finality_grandpa_rpc::{Grandpa, GrandpaApiServer};
Expand Down Expand Up @@ -150,6 +152,7 @@ where
// These RPCs should use an asynchronous caller instead.
io.merge(Mmr::new(client.clone()).into_rpc())?;
io.merge(TransactionPayment::new(client.clone()).into_rpc())?;
io.merge(Staking::new(client.clone()).into_rpc())?;
io.merge(
Babe::new(
client.clone(),
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pallet-session = { version = "4.0.0-dev", features = [ "historical" ], path = ".
pallet-session-benchmarking = { version = "4.0.0-dev", path = "../../../frame/session/benchmarking", default-features = false, optional = true }
pallet-staking = { version = "4.0.0-dev", default-features = false, path = "../../../frame/staking" }
pallet-staking-reward-curve = { version = "4.0.0-dev", default-features = false, path = "../../../frame/staking/reward-curve" }
pallet-staking-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, path = "../../../frame/staking/rpc/runtime-api/" }
pallet-state-trie-migration = { version = "4.0.0-dev", default-features = false, path = "../../../frame/state-trie-migration" }
pallet-scheduler = { version = "4.0.0-dev", default-features = false, path = "../../../frame/scheduler" }
pallet-society = { version = "4.0.0-dev", default-features = false, path = "../../../frame/society" }
Expand Down
10 changes: 10 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,16 @@ impl_runtime_apis! {
}
}

impl pallet_staking_rpc_runtime_api::StakingApi<Block, Balance> for Runtime {
fn query_nominations_quota() -> u32 {
Staking::query_nominations_quota()
}

fn query_points_to_balance(points: Balance, pool_id: u32) -> Result<Balance, ()> {
NominationPools::query_points_to_balance(points, pool_id)
}
}

impl pallet_mmr::primitives::MmrApi<
Block,
mmr::Hash,
Expand Down
9 changes: 9 additions & 0 deletions frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,6 +2140,15 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
// Query points to balance for a given pool. Used for custom RPC call.
pub fn query_points_to_balance(points: BalanceOf<T>, pool_id: u32) -> Result<BalanceOf<T>, ()> {
if let Some(pool) = BondedPool::<T>::get(pool_id).defensive() {
Ok(pool.points_to_balance(points))
} else {
Err(())
}
}

/// Returns the pending rewards for the specified `member_account`.
///
/// In the case of error, `None` is returned.
Expand Down
24 changes: 24 additions & 0 deletions frame/staking/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "pallet-staking-rpc"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "RPC interface for the staking pallet."
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0" }
jsonrpsee = { version = "0.16.2", features = ["client-core", "server", "macros"] }
pallet-staking-rpc-runtime-api = { version = "4.0.0-dev", path = "./runtime-api" }
sp-api = { version = "4.0.0-dev", path = "../../../primitives/api" }
sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" }
sp-core = { version = "7.0.0", path = "../../../primitives/core" }
sp-rpc = { version = "6.0.0", path = "../../../primitives/rpc" }
sp-runtime = { version = "7.0.0", path = "../../../primitives/runtime" }
sp-weights = { version = "4.0.0", path = "../../../primitives/weights" }
3 changes: 3 additions & 0 deletions frame/staking/rpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RPC interface for the staking pallet.

License: Apache-2.0
30 changes: 30 additions & 0 deletions frame/staking/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "pallet-staking-rpc-runtime-api"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "RPC runtime API for transaction payment FRAME pallet"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
pallet-staking = { version = "4.0.0-dev", default-features = false, path = "../../../staking" }
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../../primitives/api" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../../../../primitives/runtime" }
sp-weights = { version = "4.0.0", default-features = false, path = "../../../../primitives/weights" }

[features]
default = ["std"]
std = [
"codec/std",
"pallet-staking/std",
"sp-api/std",
"sp-runtime/std",
"sp-weights/std",
]
3 changes: 3 additions & 0 deletions frame/staking/rpc/runtime-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Runtime API definition for the staking pallet.

License: Apache-2.0
33 changes: 33 additions & 0 deletions frame/staking/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This file is part of Substrate.

// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Runtime API definition for the staking pallet.

#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;
use sp_runtime::traits::MaybeDisplay;

sp_api::decl_runtime_apis! {
#[api_version(1)]
pub trait StakingApi<Balance> where
Balance: Codec + MaybeDisplay,
{
fn query_nominations_quota() -> u32;
fn query_points_to_balance(points: Balance, pool_id: u32) -> Result<Balance, ()>;
}
}
113 changes: 113 additions & 0 deletions frame/staking/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// This file is part of Substrate.

// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! RPC interface for the staking pallet.

use std::sync::Arc;

use codec::Codec;
use jsonrpsee::{
core::RpcResult,
proc_macros::rpc,
types::error::{CallError, ErrorObject},
};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_rpc::number::NumberOrHex;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, MaybeDisplay},
};

pub use pallet_staking_rpc_runtime_api::StakingApi as StakingRuntimeApi;

#[rpc(client, server)]
pub trait StakingApi<Balance> {
#[method(name = "staking_nominationsQuota")]
fn query_nominations_quota(&self) -> RpcResult<u32>;
#[method(name = "nominationPools_pointsToBalance")]
fn query_points_to_balance(&self, balance: Balance, pool_id: u32) -> RpcResult<Balance>;
}

pub struct Staking<C, P> {
client: Arc<C>,
_marker: std::marker::PhantomData<P>,
}

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

/// Error type of this RPC api.
pub enum ApiError {
/// The call to runtime failed.
RuntimeError,
}

impl From<ApiError> for i32 {
fn from(e: ApiError) -> i32 {
match e {
ApiError::RuntimeError => 1,
}
}
}

impl<C, Block, Balance> StakingApiServer<Balance> for Staking<C, Block>
where
Block: BlockT,
C: ProvideRuntimeApi<Block> + HeaderBackend<Block> + Send + Sync + 'static,
C::Api: StakingRuntimeApi<Block, Balance>,
Balance: Codec + MaybeDisplay + Copy + TryInto<NumberOrHex> + Send + Sync + 'static,
{
fn query_nominations_quota(&self) -> RpcResult<u32> {
let api = self.client.runtime_api();
let at = BlockId::hash(self.client.info().best_hash);

let runtime_api_result = api.query_nominations_quota(&at);

Ok(runtime_api_result.map_err(|e| {
CallError::Custom(ErrorObject::owned(
ApiError::RuntimeError.into(),
"Unable to query the nominations quota.",
Some(e.to_string()),
))
})?)
}

fn query_points_to_balance(&self, balance: Balance, pool_id: u32) -> RpcResult<Balance> {
let api = self.client.runtime_api();
let at = BlockId::hash(self.client.info().best_hash);

let result = api.query_points_to_balance(&at, balance, pool_id).map_err(|e| {
CallError::Custom(ErrorObject::owned(
ApiError::RuntimeError.into(),
"Unable to query the points to balance conversion for pool.",
Some(e.to_string()),
))
})?;

Ok(result.map_err(|_| {
CallError::Custom(ErrorObject::owned(
ApiError::RuntimeError.into(),
"Pool ID not found",
None::<String>,
))
})?)
}
}
5 changes: 5 additions & 0 deletions frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ use super::{pallet::*, STAKING_ID};
const NPOS_MAX_ITERATIONS_COEFFICIENT: u32 = 2;

impl<T: Config> Pallet<T> {
/// Query nominations quota for a given balance. Used for custom RPC call.
pub fn query_nominations_quota() -> u32 {
T::MaxNominations::get()
}

/// The total balance that can be slashed from a stash account as of right now.
pub fn slashable_balance_of(stash: &T::AccountId) -> BalanceOf<T> {
// Weight note: consider making the stake accessible through stash.
Expand Down