From 801486d903085b071d38a0d17d71226deead4f09 Mon Sep 17 00:00:00 2001 From: Dino Pacandi Date: Thu, 4 Jan 2024 14:41:10 +0100 Subject: [PATCH] Expanded DappStakingApi --- Cargo.lock | 1 + .../rpc/runtime-api/Cargo.toml | 2 ++ .../rpc/runtime-api/src/lib.rs | 9 +++++- .../dapp-staking-v3/src/benchmarking/mod.rs | 7 +++-- pallets/dapp-staking-v3/src/lib.rs | 31 +++++++++++++++++-- pallets/dapp-staking-v3/src/test/tests.rs | 8 ++--- runtime/local/src/lib.rs | 10 +++++- runtime/shibuya/src/lib.rs | 10 +++++- 8 files changed, 67 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7eb99fcf53..7d42871b9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2441,6 +2441,7 @@ dependencies = [ "astar-primitives", "pallet-dapp-staking-v3", "sp-api", + "sp-std", ] [[package]] diff --git a/pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml b/pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml index 559d5f299b..63ef539a0c 100644 --- a/pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml +++ b/pallets/dapp-staking-v3/rpc/runtime-api/Cargo.toml @@ -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 } @@ -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", ] diff --git a/pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs b/pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs index dde32bf695..d829e2b275 100644 --- a/pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs +++ b/pallets/dapp-staking-v3/rpc/runtime-api/src/lib.rs @@ -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! { @@ -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; @@ -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; } } diff --git a/pallets/dapp-staking-v3/src/benchmarking/mod.rs b/pallets/dapp-staking-v3/src/benchmarking/mod.rs index 11663401d9..abb37ee071 100644 --- a/pallets/dapp-staking-v3/src/benchmarking/mod.rs +++ b/pallets/dapp-staking-v3/src/benchmarking/mod.rs @@ -946,8 +946,11 @@ mod benchmarks { #[block] { - let (dapp_tiers, _) = - Pallet::::get_dapp_tier_assignment(reward_era, reward_period, reward_pool); + let (dapp_tiers, _) = Pallet::::get_dapp_tier_assignment_and_rewards( + reward_era, + reward_period, + reward_pool, + ); assert_eq!(dapp_tiers.dapps.len(), x as usize); } } diff --git a/pallets/dapp-staking-v3/src/lib.rs b/pallets/dapp-staking-v3/src/lib.rs index 1d6fbca5c9..5489818091 100644 --- a/pallets/dapp-staking-v3/src/lib.rs +++ b/pallets/dapp-staking-v3/src/lib.rs @@ -55,6 +55,9 @@ use astar_primitives::{ Balance, BlockNumber, }; +// TODO: remove this after rebase +use sp_std::collections::btree_map::BTreeMap; + pub use pallet::*; #[cfg(test)] @@ -1637,6 +1640,30 @@ pub mod pallet { T::CycleConfiguration::blocks_per_era().saturating_mul(T::UnlockingPeriod::get().into()) } + // TODO: finish this after rebase/merge with latest pending updates + // Maybe define an interface for this. + pub fn get_dapp_tier_assignment() -> BTreeMap { + let protocol_state = ActiveProtocolState::::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_iter() + .filter_map(|dapp_tier| { + if let Some(tier_id) = dapp_tier.tier_id { + Some((dapp_tier.dapp_id, tier_id)) + } else { + None + } + }) + .collect() + } + /// Assign eligible dApps into appropriate tiers, and calculate reward for each tier. /// /// ### Algorithm @@ -1666,7 +1693,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, @@ -1838,7 +1865,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, diff --git a/pallets/dapp-staking-v3/src/test/tests.rs b/pallets/dapp-staking-v3/src/test/tests.rs index 852c02ecfd..1df394d507 100644 --- a/pallets/dapp-staking-v3/src/test/tests.rs +++ b/pallets/dapp-staking-v3/src/test/tests.rs @@ -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. @@ -2339,7 +2339,7 @@ fn get_dapp_tier_assignment_basic_example_works() { // Finally, the actual test let protocol_state = ActiveProtocolState::::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, @@ -2402,7 +2402,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. @@ -2417,7 +2417,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::::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, diff --git a/runtime/local/src/lib.rs b/runtime/local/src/lib.rs index b1bec5ead9..99b20909c1 100644 --- a/runtime/local/src/lib.rs +++ b/runtime/local/src/lib.rs @@ -59,7 +59,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}, @@ -1731,6 +1731,10 @@ impl_runtime_apis! { } impl dapp_staking_v3_runtime_api::DappStakingApi 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() } @@ -1742,6 +1746,10 @@ impl_runtime_apis! { fn blocks_per_era() -> BlockNumber { InflationCycleConfig::blocks_per_era() } + + fn get_dapp_tier_assignment() -> BTreeMap { + DappStaking::get_dapp_tier_assignment() + } } #[cfg(feature = "runtime-benchmarks")] diff --git a/runtime/shibuya/src/lib.rs b/runtime/shibuya/src/lib.rs index b98dcae097..8fa461694d 100644 --- a/runtime/shibuya/src/lib.rs +++ b/runtime/shibuya/src/lib.rs @@ -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}, @@ -1926,6 +1926,10 @@ impl_runtime_apis! { } impl dapp_staking_v3_runtime_api::DappStakingApi 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() } @@ -1937,6 +1941,10 @@ impl_runtime_apis! { fn blocks_per_era() -> BlockNumber { InflationCycleConfig::blocks_per_era() } + + fn get_dapp_tier_assignment() -> BTreeMap { + DappStaking::get_dapp_tier_assignment() + } } #[cfg(feature = "runtime-benchmarks")]