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

Expanded DappStakingApi #1126

Merged
merged 3 commits into from
Jan 8, 2024
Merged
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 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 pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
sp-api = { workspace = true }
sp-std = { workspace = true }

astar-primitives = { workspace = true }
pallet-dapp-staking-v3 = { workspace = true }
Expand All @@ -20,6 +21,7 @@ pallet-dapp-staking-v3 = { workspace = true }
default = ["std"]
std = [
"sp-api/std",
"sp-std/std",
"pallet-dapp-staking-v3/std",
"astar-primitives/std",
]
9 changes: 8 additions & 1 deletion pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#![cfg_attr(not(feature = "std"), no_std)]

use astar_primitives::BlockNumber;
use pallet_dapp_staking_v3::EraNumber;
use pallet_dapp_staking_v3::{DAppId, EraNumber, PeriodNumber, TierId};
pub use sp_std::collections::btree_map::BTreeMap;

sp_api::decl_runtime_apis! {

Expand All @@ -28,6 +29,9 @@ sp_api::decl_runtime_apis! {
/// Used to provide information otherwise not available via RPC.
pub trait DappStakingApi {

/// How many periods are there in one cycle.
fn periods_per_cycle() -> PeriodNumber;

/// For how many standard era lengths does the voting subperiod last.
fn eras_per_voting_subperiod() -> EraNumber;

Expand All @@ -36,5 +40,8 @@ sp_api::decl_runtime_apis! {

/// How many blocks are there per standard era.
fn blocks_per_era() -> BlockNumber;

/// Get dApp tier assignment for the given dApp.
fn get_dapp_tier_assignment() -> BTreeMap<DAppId, TierId>;
}
}
7 changes: 5 additions & 2 deletions pallets/dapp-staking-v3/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,11 @@ mod benchmarks {

#[block]
{
let (dapp_tiers, _) =
Pallet::<T>::get_dapp_tier_assignment(reward_era, reward_period, reward_pool);
let (dapp_tiers, _) = Pallet::<T>::get_dapp_tier_assignment_and_rewards(
reward_era,
reward_period,
reward_pool,
);
assert_eq!(dapp_tiers.dapps.len(), x as usize);
}
}
Expand Down
17 changes: 15 additions & 2 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,19 @@ pub mod pallet {
T::CycleConfiguration::blocks_per_era().saturating_mul(T::UnlockingPeriod::get().into())
}

/// Returns the dApp tier assignment for the current era, based on the current stake amounts.
pub fn get_dapp_tier_assignment() -> BTreeMap<DAppId, TierId> {
let protocol_state = ActiveProtocolState::<T>::get();

let (dapp_tiers, _count) = Self::get_dapp_tier_assignment_and_rewards(
protocol_state.era,
protocol_state.period_number(),
Balance::zero(),
);

dapp_tiers.dapps.into_inner()
}

/// Assign eligible dApps into appropriate tiers, and calculate reward for each tier.
///
/// ### Algorithm
Expand Down Expand Up @@ -1666,7 +1679,7 @@ pub mod pallet {
///
/// The returned object contains information about each dApp that made it into a tier.
/// Alongside tier assignment info, number of read DB contract stake entries is returned.
pub(crate) fn get_dapp_tier_assignment(
pub(crate) fn get_dapp_tier_assignment_and_rewards(
era: EraNumber,
period: PeriodNumber,
dapp_reward_pool: Balance,
Expand Down Expand Up @@ -1835,7 +1848,7 @@ pub mod pallet {
// To help with benchmarking, it's possible to omit real tier calculation using the `Dummy` approach.
// This must never be used in production code, obviously.
let (dapp_tier_rewards, counter) = match tier_assignment {
TierAssignment::Real => Self::get_dapp_tier_assignment(
TierAssignment::Real => Self::get_dapp_tier_assignment_and_rewards(
current_era,
protocol_state.period_number(),
dapp_reward_pool,
Expand Down
8 changes: 4 additions & 4 deletions pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2262,7 +2262,7 @@ fn force_with_incorrect_origin_fails() {
}

#[test]
fn get_dapp_tier_assignment_basic_example_works() {
fn get_dapp_tier_assignment_and_rewards_basic_example_works() {
ExtBuilder::build().execute_with(|| {
// This test will rely on the configuration inside the mock file.
// If that changes, this test will have to be updated as well.
Expand Down Expand Up @@ -2339,7 +2339,7 @@ fn get_dapp_tier_assignment_basic_example_works() {
// Finally, the actual test
let protocol_state = ActiveProtocolState::<Test>::get();
let dapp_reward_pool = 1000000;
let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment(
let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment_and_rewards(
protocol_state.era + 1,
protocol_state.period_number(),
dapp_reward_pool,
Expand Down Expand Up @@ -2393,7 +2393,7 @@ fn get_dapp_tier_assignment_basic_example_works() {
}

#[test]
fn get_dapp_tier_assignment_zero_slots_per_tier_works() {
fn get_dapp_tier_assignment_and_rewards_zero_slots_per_tier_works() {
ExtBuilder::build().execute_with(|| {
// This test will rely on the configuration inside the mock file.
// If that changes, this test might have to be updated as well.
Expand All @@ -2408,7 +2408,7 @@ fn get_dapp_tier_assignment_zero_slots_per_tier_works() {
// Calculate tier assignment (we don't need dApps for this test)
let protocol_state = ActiveProtocolState::<Test>::get();
let dapp_reward_pool = 1000000;
let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment(
let (tier_assignment, counter) = DappStaking::get_dapp_tier_assignment_and_rewards(
protocol_state.era,
protocol_state.period_number(),
dapp_reward_pool,
Expand Down
10 changes: 9 additions & 1 deletion runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use sp_runtime::{
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
ApplyExtrinsicResult, FixedPointNumber, Perbill, Permill, Perquintill, RuntimeDebug,
};
use sp_std::prelude::*;
use sp_std::{collections::btree_map::BTreeMap, prelude::*};

use astar_primitives::{
dapp_staking::{CycleConfiguration, SmartContract},
Expand Down Expand Up @@ -1728,6 +1728,10 @@ impl_runtime_apis! {
}

impl dapp_staking_v3_runtime_api::DappStakingApi<Block> for Runtime {
fn periods_per_cycle() -> pallet_dapp_staking_v3::PeriodNumber {
InflationCycleConfig::periods_per_cycle()
}

fn eras_per_voting_subperiod() -> pallet_dapp_staking_v3::EraNumber {
InflationCycleConfig::eras_per_voting_subperiod()
}
Expand All @@ -1739,6 +1743,10 @@ impl_runtime_apis! {
fn blocks_per_era() -> BlockNumber {
InflationCycleConfig::blocks_per_era()
}

fn get_dapp_tier_assignment() -> BTreeMap<pallet_dapp_staking_v3::DAppId, pallet_dapp_staking_v3::TierId> {
DappStaking::get_dapp_tier_assignment()
}
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
10 changes: 9 additions & 1 deletion runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use sp_runtime::{
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
ApplyExtrinsicResult, FixedPointNumber, Perbill, Permill, Perquintill, RuntimeDebug,
};
use sp_std::prelude::*;
use sp_std::{collections::btree_map::BTreeMap, prelude::*};

use astar_primitives::{
dapp_staking::{CycleConfiguration, SmartContract},
Expand Down Expand Up @@ -1932,6 +1932,10 @@ impl_runtime_apis! {
}

impl dapp_staking_v3_runtime_api::DappStakingApi<Block> for Runtime {
fn periods_per_cycle() -> pallet_dapp_staking_v3::PeriodNumber {
InflationCycleConfig::periods_per_cycle()
}

fn eras_per_voting_subperiod() -> pallet_dapp_staking_v3::EraNumber {
InflationCycleConfig::eras_per_voting_subperiod()
}
Expand All @@ -1943,6 +1947,10 @@ impl_runtime_apis! {
fn blocks_per_era() -> BlockNumber {
InflationCycleConfig::blocks_per_era()
}

fn get_dapp_tier_assignment() -> BTreeMap<pallet_dapp_staking_v3::DAppId, pallet_dapp_staking_v3::TierId> {
DappStaking::get_dapp_tier_assignment()
}
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
Loading