From f55d00b89be66fe994851fcedab8cd03a028053c Mon Sep 17 00:00:00 2001 From: Shannon Wells Date: Wed, 18 Oct 2023 00:41:16 +0100 Subject: [PATCH] Feat/split stake extrinsic #1699 (#1717) The goal of this PR is to split the `stake` extrinsic into two: `stake` and `provider_boost` Closes #1707 --- e2e/scaffolding/helpers.ts | 17 +++++++++++++ .../src/tests/change_staking_target_tests.rs | 24 ++++--------------- pallets/capacity/src/tests/testing_utils.rs | 19 +++++++++------ pallets/capacity/src/weights.rs | 16 +++++++++++++ 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/e2e/scaffolding/helpers.ts b/e2e/scaffolding/helpers.ts index a44258be95..907403a815 100644 --- a/e2e/scaffolding/helpers.ts +++ b/e2e/scaffolding/helpers.ts @@ -433,6 +433,22 @@ export async function stakeToProvider( return Promise.reject('stakeToProvider: stakeEvent should be capacity.Staked event'); } } +export async function boostProvider(keys: KeyringPair, providerId: u64, tokensToStake: bigint): Promise { + const stakeOp = ExtrinsicHelper.providerBoost(keys, providerId, tokensToStake); + const [stakeEvent] = await stakeOp.fundAndSend(); + assert.notEqual(stakeEvent, undefined, 'stakeToProvider: should have returned Stake event'); + + if (stakeEvent && ExtrinsicHelper.api.events.capacity.ProviderBoosted.is(stakeEvent)) { + let stakedCapacity = stakeEvent.data.capacity; + + let expectedCapacity = tokensToStake/TokenPerCapacity/BoostAdjustment; + + assert.equal(stakedCapacity, expectedCapacity, `stakeToProvider: expected ${expectedCapacity}, got ${stakedCapacity}`); + } + else { + return Promise.reject('stakeToProvider: stakeEvent should be ExtrinsicHelper.api.events.capacity.ProviderBoosted'); + } +} export async function getNextEpochBlock() { const epochInfo = await ExtrinsicHelper.apiPromise.query.capacity.currentEpochInfo(); @@ -545,6 +561,7 @@ export async function getOrCreateAvroChatMessageItemizedSchema(source: KeyringPa } export const TokenPerCapacity = 50n; +export const BoostAdjustment = 20n; // divide by 20 or 5% of Maximum Capacity export async function getCapacity(providerId: u64): Promise { return (await ExtrinsicHelper.apiPromise.query.capacity.capacityLedger(providerId)).unwrap(); diff --git a/pallets/capacity/src/tests/change_staking_target_tests.rs b/pallets/capacity/src/tests/change_staking_target_tests.rs index 8ca36cc494..7f2d8d375b 100644 --- a/pallets/capacity/src/tests/change_staking_target_tests.rs +++ b/pallets/capacity/src/tests/change_staking_target_tests.rs @@ -164,18 +164,8 @@ fn check_retarget_multiple_stakers() { setup_provider(&staker_10k, &from_msa, &647u64, ProviderBoost); setup_provider(&staker_500, &to_msa, &293u64, ProviderBoost); - assert_ok!(Capacity::stake( - RuntimeOrigin::signed(staker_600.clone()), - from_msa, - 479u64, - MaximumCapacity - )); - assert_ok!(Capacity::stake( - RuntimeOrigin::signed(staker_400.clone()), - to_msa, - 211u64, - MaximumCapacity - )); + assert_ok!(Capacity::stake(RuntimeOrigin::signed(staker_600.clone()), from_msa, 479u64,)); + assert_ok!(Capacity::stake(RuntimeOrigin::signed(staker_400.clone()), to_msa, 211u64,)); // 647 * .1 * .05 = 3 (rounded down) // 293 * .1 * .05 = 1 (rounded down) @@ -204,12 +194,7 @@ fn do_retarget_deletes_staking_target_details_if_zero_balance() { // stake additional to provider from another Msa, doesn't matter which type. // total staked to from_msa is now 22u64. - assert_ok!(Capacity::stake( - RuntimeOrigin::signed(300u64), - from_msa, - 12u64, - MaximumCapacity - )); + assert_ok!(Capacity::stake(RuntimeOrigin::signed(300u64), from_msa, 12u64,)); assert_ok!(Capacity::do_retarget(&staker, &from_msa, &to_msa, &amount, &MaximumCapacity)); @@ -362,11 +347,10 @@ fn change_staking_target_test_parametric_validity() { setup_provider(&from_account, &from_target, &staked_amount, ProviderBoost); setup_provider(&from_account, &to_target, &staked_amount, ProviderBoost); - assert_ok!(Capacity::stake( + assert_ok!(Capacity::provider_boost( RuntimeOrigin::signed(from_account), from_target, staked_amount, - ProviderBoost )); struct TestCase { diff --git a/pallets/capacity/src/tests/testing_utils.rs b/pallets/capacity/src/tests/testing_utils.rs index 117c95258c..62d22bae4b 100644 --- a/pallets/capacity/src/tests/testing_utils.rs +++ b/pallets/capacity/src/tests/testing_utils.rs @@ -1,7 +1,7 @@ use super::mock::*; use frame_support::{assert_ok, traits::Hooks}; -use common_primitives::capacity::StakingType; +use common_primitives::capacity::{StakingType, StakingType::MaximumCapacity}; #[allow(unused)] use sp_runtime::traits::SignedExtension; @@ -29,6 +29,7 @@ pub fn run_to_block(n: u32) { Capacity::on_initialize(System::block_number()); } } + // Remove capacity on_initialize, needed to emulate pre-existing block height pub fn system_run_to_block(n: u32) { while System::block_number() < n { @@ -64,6 +65,7 @@ pub fn create_capacity_account_and_fund( capacity_details } + pub fn setup_provider( staker: &u64, target: &MessageSourceId, @@ -73,12 +75,15 @@ pub fn setup_provider( let provider_name = String::from("Cst-") + target.to_string().as_str(); register_provider(*target, provider_name); if amount.gt(&0u64) { - assert_ok!(Capacity::stake( - RuntimeOrigin::signed(staker.clone()), - *target, - *amount, - staking_type.clone() - )); + if staking_type == MaximumCapacity { + assert_ok!(Capacity::stake(RuntimeOrigin::signed(staker.clone()), *target, *amount,)); + } else { + assert_ok!(Capacity::provider_boost( + RuntimeOrigin::signed(staker.clone()), + *target, + *amount + )); + } let target = Capacity::get_target_for(staker, target).unwrap(); assert_eq!(target.amount, *amount); assert_eq!(target.staking_type, staking_type); diff --git a/pallets/capacity/src/weights.rs b/pallets/capacity/src/weights.rs index 4dcf1eceb3..4bb92e63ad 100644 --- a/pallets/capacity/src/weights.rs +++ b/pallets/capacity/src/weights.rs @@ -55,6 +55,8 @@ pub trait WeightInfo { fn unstake() -> Weight; fn set_epoch_length() -> Weight; fn change_staking_target() -> Weight; + + fn provider_boost() -> Weight; } /// Weights for pallet_capacity using the Substrate node and recommended hardware. @@ -147,6 +149,13 @@ impl WeightInfo for SubstrateWeight { Weight::from_parts(1_000_000, 0) .saturating_add(T::DbWeight::get().writes(1_u64)) } + + /// Storage: + /// Proof: + fn provider_boost() -> Weight { + Weight::from_parts(1_000_000, 0) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } } // For backwards compatibility and tests @@ -238,4 +247,11 @@ impl WeightInfo for () { Weight::from_parts(1_000_000, 0) .saturating_add(RocksDbWeight::get().writes(1_u64)) } + + /// Storage: + /// Proof: + fn provider_boost() -> Weight { + Weight::from_parts(1_000_000, 0) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } }