Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Staking and nomination pools runtime API improvements #13119

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
710bab9
Adds StakingAPI_nominations_quota and NominationPoolsApi_balanceToPoi…
gpestana Jan 10, 2023
11c4f61
Adds balance param to api_nominations_quota
gpestana Jan 11, 2023
3268088
Update frame/nomination-pools/src/lib.rs
gpestana Jan 13, 2023
ca2ed2f
Update frame/nomination-pools/src/lib.rs
gpestana Jan 13, 2023
a31efce
Addresses comments - returns zero instead of error in runtime api
gpestana Jan 13, 2023
5dfae89
Update frame/staking/runtime-api/src/lib.rs
gpestana Jan 14, 2023
9e9e793
Update frame/staking/runtime-api/src/lib.rs
gpestana Jan 14, 2023
0a8059a
Addresses PR comments
gpestana Jan 14, 2023
8b848fd
Update frame/nomination-pools/runtime-api/Cargo.toml
gpestana Feb 7, 2023
825f312
Fixes points_to_balance logic; adds tests
gpestana Feb 7, 2023
62aa1a2
test comment fix
gpestana Feb 7, 2023
05b238e
Merge remote-tracking branch 'origin/master' into gpestana/13069-nomi…
Feb 7, 2023
137a355
Update frame/nomination-pools/runtime-api/src/lib.rs
gpestana Feb 14, 2023
cfea646
Update frame/nomination-pools/runtime-api/src/lib.rs
gpestana Feb 14, 2023
bf944a4
Fix block pruning (#13323)
arkpar Feb 7, 2023
e8d7590
Referendum proposal's metadata (#12568)
muharem Feb 8, 2023
5164848
Improve test coverage of the `Notifications` protocol (#13033)
altonen Feb 8, 2023
b7d764c
refactors runtime API logic to own pallet impl block
gpestana Feb 14, 2023
cc3ad3a
Merge remote-tracking branch 'origin/master' into gpestana/13069-nomi…
Feb 14, 2023
6a54821
removes unrelated changes
gpestana Feb 14, 2023
d6ec3a9
Merge branch 'master' into gpestana/13069-nomination-staking-rpc-methods
gpestana Feb 16, 2023
3f16777
Fixes cargo doc comments lint
gpestana Feb 16, 2023
8422864
fixes node cargo
gpestana Feb 16, 2023
9c1b1fc
Merge branch 'master' into gpestana/13069-nomination-staking-rpc-methods
gpestana Feb 16, 2023
98176d0
Merge remote-tracking branch 'origin/master' into gpestana/13069-nomi…
Feb 17, 2023
b801518
Merge remote-tracking branch 'origin/master' into gpestana/13069-nomi…
Feb 17, 2023
eb28218
fixes comment
gpestana Feb 20, 2023
28b1434
Merge remote-tracking branch 'origin/master' into gpestana/13069-nomi…
Feb 20, 2023
3afd3f7
Merge remote-tracking branch 'origin/master' into gpestana/13069-nomi…
Feb 20, 2023
a1a5c3c
restart ci
gpestana Feb 20, 2023
7c23d17
restart ci
gpestana Feb 21, 2023
4492a2d
restart ci
gpestana Feb 21, 2023
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
9 changes: 9 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ members = [
"frame/staking",
"frame/staking/reward-curve",
"frame/staking/reward-fn",
"frame/staking/runtime-api",
"frame/state-trie-migration",
"frame/sudo",
"frame/root-offences",
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-runtime-api = { version = "4.0.0-dev", default-features = false, path = "../../../frame/staking/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
16 changes: 15 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,21 @@ impl_runtime_apis! {

impl pallet_nomination_pools_runtime_api::NominationPoolsApi<Block, AccountId, Balance> for Runtime {
fn pending_rewards(member_account: AccountId) -> Balance {
NominationPools::pending_rewards(member_account).unwrap_or_default()
NominationPools::api_pending_rewards(member_account).unwrap_or_default()
gpestana marked this conversation as resolved.
Show resolved Hide resolved
}

fn points_to_balance(pool_id: u32) -> Result<Balance, ()> {
gpestana marked this conversation as resolved.
Show resolved Hide resolved
NominationPools::api_points_to_balance(pool_id)
}

fn balance_to_point(pool_id: u32, new_funds: Balance) -> Result<Balance, ()> {
gpestana marked this conversation as resolved.
Show resolved Hide resolved
NominationPools::api_balance_to_point(pool_id, new_funds)
}
}

impl pallet_staking_runtime_api::StakingApi<Block> for Runtime {
fn nominations_quota() -> u32 {
gpestana marked this conversation as resolved.
Show resolved Hide resolved
Staking::api_nominations_quota()
}
}

Expand Down
11 changes: 10 additions & 1 deletion frame/nomination-pools/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ use codec::Codec;

sp_api::decl_runtime_apis! {
/// Runtime api for accessing information about nomination pools.
#[api_version(1)]
gpestana marked this conversation as resolved.
Show resolved Hide resolved
pub trait NominationPoolsApi<AccountId, Balance>
where AccountId: Codec, Balance: Codec
where
AccountId: Codec,
Balance: Codec,
{
/// Returns the pending rewards for the member that the AccountId was given for.
fn pending_rewards(member: AccountId) -> Balance;

/// Returns the points to balance conversion for a given pool.
gpestana marked this conversation as resolved.
Show resolved Hide resolved
fn points_to_balance(pool_id: u32) -> Result<Balance, ()>;

/// Returns the equivalent points of `new_funds` for a given pool
gpestana marked this conversation as resolved.
Show resolved Hide resolved
fn balance_to_point(pool_id: u32, new_funds: Balance) -> Result<Balance, ()>;
}
}
28 changes: 26 additions & 2 deletions frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2144,8 +2144,8 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
/// Returns the pending rewards for the specified `member_account`.
///
/// In the case of error, `None` is returned.
pub fn pending_rewards(member_account: T::AccountId) -> Option<BalanceOf<T>> {
gpestana marked this conversation as resolved.
Show resolved Hide resolved
/// In the case of error, `None` is returned. Used by runtime API.
pub fn api_pending_rewards(member_account: T::AccountId) -> Option<BalanceOf<T>> {
gpestana marked this conversation as resolved.
Show resolved Hide resolved
gpestana marked this conversation as resolved.
Show resolved Hide resolved
if let Some(pool_member) = PoolMembers::<T>::get(member_account) {
if let Some((reward_pool, bonded_pool)) = RewardPools::<T>::get(pool_member.pool_id)
.zip(BondedPools::<T>::get(pool_member.pool_id))
Expand All @@ -2160,6 +2160,30 @@ impl<T: Config> Pallet<T> {
None
}

/// Returns the points to balance conversion for a specified pool.
///
/// If the pool ID does not exist, return an error. Used by runtime API.
gpestana marked this conversation as resolved.
Show resolved Hide resolved
pub fn api_points_to_balance(pool_id: u32) -> Result<BalanceOf<T>, ()> {
if let Some(pool) = BondedPool::<T>::get(pool_id) {
Ok(pool.points_to_balance(pool.points))
} else {
Err(())
gpestana marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Returns the equivalent `new_funds` balance to point conversion for a specified pool.
///
/// If the pool ID does not exist, return an error. Used by runtime API.
gpestana marked this conversation as resolved.
Show resolved Hide resolved
pub fn api_balance_to_point(pool_id: u32, new_funds: BalanceOf<T>) -> Result<BalanceOf<T>, ()> {
gpestana marked this conversation as resolved.
Show resolved Hide resolved
if let Some(pool) = BondedPool::<T>::get(pool_id) {
let bonded_balance =
T::Staking::active_stake(&pool.bonded_account()).unwrap_or(Zero::zero());
Ok(Pallet::<T>::balance_to_point(bonded_balance, pool.points, new_funds))
} else {
Err(())
gpestana marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// The amount of bond that MUST REMAIN IN BONDED in ALL POOLS.
///
/// It is the responsibility of the depositor to put these funds into the pool initially. Upon
Expand Down
24 changes: 24 additions & 0 deletions frame/staking/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "pallet-staking-runtime-api"
version = "4.0.0-dev"
authors = ["Parity Technologies <[email protected]>"]
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"] }
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/api" }

[features]
default = ["std"]
std = [
"codec/std",
"sp-api/std",
]
3 changes: 3 additions & 0 deletions frame/staking/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
28 changes: 28 additions & 0 deletions frame/staking/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file is part of Substrate.

// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
gpestana marked this conversation as resolved.
Show resolved Hide resolved
// 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)]

sp_api::decl_runtime_apis! {
#[api_version(1)]
gpestana marked this conversation as resolved.
Show resolved Hide resolved
pub trait StakingApi {
/// Returns the current nominations quota for nominators.
fn nominations_quota() -> u32;
}
}
7 changes: 7 additions & 0 deletions frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ use super::{pallet::*, STAKING_ID};
const NPOS_MAX_ITERATIONS_COEFFICIENT: u32 = 2;

impl<T: Config> Pallet<T> {
/// Returns the current nominations quota for nominators.
///
/// Used by runtime API.
pub fn api_nominations_quota() -> u32 {
T::MaxNominations::get()
Ank4n marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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