Skip to content

Commit

Permalink
Feat/split stake extrinsic #1699 (#1717)
Browse files Browse the repository at this point in the history
The goal of this PR is to split the `stake` extrinsic into two: `stake` and `provider_boost`
Closes #1707
  • Loading branch information
shannonwells committed May 21, 2024
1 parent 6dc96b4 commit f55d00b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
17 changes: 17 additions & 0 deletions e2e/scaffolding/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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();
Expand Down Expand Up @@ -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<PalletCapacityCapacityDetails> {
return (await ExtrinsicHelper.apiPromise.query.capacity.capacityLedger(providerId)).unwrap();
Expand Down
24 changes: 4 additions & 20 deletions pallets/capacity/src/tests/change_staking_target_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 12 additions & 7 deletions pallets/capacity/src/tests/testing_utils.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -64,6 +65,7 @@ pub fn create_capacity_account_and_fund(

capacity_details
}

pub fn setup_provider(
staker: &u64,
target: &MessageSourceId,
Expand All @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions pallets/capacity/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -147,6 +149,13 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
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
Expand Down Expand Up @@ -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))
}
}

0 comments on commit f55d00b

Please sign in to comment.