Skip to content

Commit

Permalink
some more updates after rebasing w/ origin branch
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonwells committed Apr 22, 2024
1 parent 0cee861 commit 57f1d42
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 56 deletions.
21 changes: 0 additions & 21 deletions common/primitives/src/capacity.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::msa::MessageSourceId;
use codec::{Encode, MaxEncodedLen};
use frame_support::traits::tokens::Balance;
use scale_info::TypeInfo;
use sp_api::Decode;
use sp_runtime::DispatchError;

/// A trait for checking that a target MSA can be staked to.
Expand Down Expand Up @@ -55,21 +52,3 @@ pub trait Replenishable {
/// Checks if an account can be replenished.
fn can_replenish(msa_id: MessageSourceId) -> bool;
}

#[derive(
Clone, Copy, Debug, Decode, Encode, TypeInfo, Eq, MaxEncodedLen, PartialEq, PartialOrd,
)]
/// The type of staking a given Staking Account is doing.
pub enum StakingType {
/// Staking account targets Providers for capacity only, no token reward
MaximumCapacity,
/// Staking account targets Providers and splits reward between capacity to the Provider
/// and token for the account holder
ProviderBoost,
}

impl Default for StakingType {
fn default() -> Self {
StakingType::MaximumCapacity
}
}
16 changes: 0 additions & 16 deletions e2e/scaffolding/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,22 +447,6 @@ export async function boostProvider(source: KeyringPair, keys: KeyringPair, prov
return Promise.reject('stakeToProvider: stakeEvent should be ExtrinsicHelper.api.events.capacity.ProviderBoosted');
}
}
export async function boostProvider(source: KeyringPair, keys: KeyringPair, providerId: u64, tokensToStake: bigint): Promise<void> {
const stakeOp = ExtrinsicHelper.providerBoost(keys, providerId, tokensToStake);
const [stakeEvent] = await stakeOp.fundAndSend(source);
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
1 change: 0 additions & 1 deletion node/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use substrate_build_script_utils::{generate_cargo_keys, rerun_if_git_head_change
feature = "frequency-no-relay",
feature = "frequency-testnet"
)))]

compile_error!(
r#"You must enable one of these features:
- Mainnet: "frequency"
Expand Down
7 changes: 1 addition & 6 deletions pallets/capacity/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ benchmarks! {

register_provider::<T>(target, "Foo");

}: _ (RawOrigin::Signed(caller.clone()), target, amount, staking_type)
}: _ (RawOrigin::Signed(caller.clone()), target, amount)
verify {
assert!(StakingAccountLedger::<T>::contains_key(&caller));
assert!(StakingTargetLedger::<T>::contains_key(&caller, target));
Expand All @@ -64,11 +64,6 @@ benchmarks! {
}
UnstakeUnlocks::<T>::set(&caller, Some(unlocking));

// set new unlock chunks using tuples of (value, thaw_at)
let new_unlocks: Vec<(u32, u32)> = Vec::from([(50u32, 3u32), (50u32, 5u32)]);
assert_eq!(true, staking_account.set_unlock_chunks(&new_unlocks));

Capacity::<T>::set_staking_account(&caller.clone(), &staking_account);
CurrentEpoch::<T>::set(T::EpochNumber::from(5u32));

}: _ (RawOrigin::Signed(caller.clone()))
Expand Down
4 changes: 2 additions & 2 deletions pallets/capacity/src/tests/other_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn set_target_details_is_successful() {
target_details.amount = 10;
target_details.capacity = 10;

Capacity::set_target_details_for(&staker, &target, &target_details);
Capacity::set_target_details_for(&staker, target, target_details);

let stored_target_details = Capacity::get_target_for(&staker, target).unwrap();

Expand All @@ -120,7 +120,7 @@ fn set_capacity_details_is_successful() {
last_replenished_epoch: 1u32,
};

Capacity::set_capacity_for(&target, &capacity_details);
Capacity::set_capacity_for(target, capacity_details);

let stored_capacity_details = Capacity::get_capacity_for(target).unwrap();

Expand Down
27 changes: 25 additions & 2 deletions pallets/capacity/src/tests/provider_boost_tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::{mock::*, testing_utils::*};
use crate::*;
use crate::{
BoostingAccountDetails, Error, Event, RewardPoolInfo, StakingAccountDetails, StakingHistory,
};
use common_primitives::{capacity::StakingType::ProviderBoost, msa::MessageSourceId};
use frame_support::{
assert_noop, assert_ok,
traits::{Len, WithdrawReasons},
};

#[test]
fn provider_boost_extrinsic_works() {
fn provider_boost_works() {
new_test_ext().execute_with(|| {
let account = 600;
let target: MessageSourceId = 1;
Expand Down Expand Up @@ -78,6 +80,27 @@ fn increase_boost_and_issue_capacity_happy_path() {
})
}

#[test]
fn provider_boost_updates_boost_account_details() {
new_test_ext().execute_with(|| {
let account = 600;
let target: MessageSourceId = 1;
let amount = 500;
register_provider(target, String::from("Foo"));
assert_ok!(Capacity::provider_boost(RuntimeOrigin::signed(account), target, amount));
let boost_details: BoostingAccountDetails<Test> =
Capacity::get_boost_details_for(account).unwrap();
assert_eq!(boost_details.staking_details.active, 500);
assert_eq!(boost_details.staking_details.total, 500);
assert!(boost_details.last_rewards_claimed_at.is_none());
assert_eq!(boost_details.boost_history.len(), 1);

let expected_history = StakingHistory { reward_era: 0, total_staked: 500 };
let actual_history = boost_details.boost_history.get(0).unwrap();
assert_eq!(actual_history, &expected_history);
})
}

#[test]
fn provider_boost_adjusts_reward_pool_total() {
new_test_ext().execute_with(|| {
Expand Down
4 changes: 2 additions & 2 deletions pallets/capacity/src/tests/stake_and_deposit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ fn assert_successful_increase_stake(
assert_ok!(Capacity::increase_stake_and_issue_capacity(
&staker,
&mut staking_account,
&target,
&staked,
target,
staked,
));

assert_eq!(staking_account.total, staked);
Expand Down
7 changes: 4 additions & 3 deletions pallets/capacity/src/tests/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn create_capacity_account_and_fund(
capacity_details.total_capacity_issued = available;
capacity_details.last_replenished_epoch = last_replenished;

Capacity::set_capacity_for(&target_msa_id, &capacity_details);
Capacity::set_capacity_for(target_msa_id, capacity_details.clone());

capacity_details.clone()
}
Expand All @@ -74,7 +74,7 @@ pub fn setup_provider(
let provider_name = String::from("Cst-") + target.to_string().as_str();
register_provider(*target, provider_name);
if amount.gt(&0u64) {
if staking_type == MaximumCapacity {
if staking_type == StakingType::MaximumCapacity {
assert_ok!(Capacity::stake(RuntimeOrigin::signed(staker.clone()), *target, *amount,));
} else {
assert_ok!(Capacity::provider_boost(
Expand All @@ -85,6 +85,7 @@ pub fn setup_provider(
}
let target = Capacity::get_target_for(staker, target).unwrap();
assert_eq!(target.amount, *amount);
assert_eq!(target.staking_type, staking_type);
let account_staking_type = Capacity::get_staking_account_for(staker).unwrap().staking_type;
assert_eq!(account_staking_type, staking_type);
}
}
2 changes: 1 addition & 1 deletion pallets/frequency-tx-payment/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,5 @@ fn create_capacity_for(target: MessageSourceId, amount: u64) {
let mut capacity_details = Capacity::get_capacity_for(target).unwrap_or_default();
let capacity: u64 = amount / (TEST_TOKEN_PER_CAPACITY as u64);
capacity_details.deposit(&amount, &capacity).unwrap();
Capacity::set_capacity_for(&target, &capacity_details);
Capacity::set_capacity_for(target, capacity_details);
}
4 changes: 2 additions & 2 deletions pallets/frequency-tx-payment/src/tests/pallet_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ fn withdraw_fee_replenishes_capacity_account_on_new_epoch_before_deducting_fee()
total_capacity_issued,
last_replenished_epoch: 10,
};
Capacity::set_capacity_for(&provider_msa_id, &capacity_details);
Capacity::set_capacity_for(provider_msa_id, capacity_details);

let call: &<Test as Config>::RuntimeCall =
&RuntimeCall::Balances(BalancesCall::transfer { dest: 2, value: 100 });
Expand Down Expand Up @@ -660,7 +660,7 @@ fn withdraw_fee_does_not_replenish_if_not_new_epoch() {
total_capacity_issued,
last_replenished_epoch,
};
Capacity::set_capacity_for(&provider_msa_id, &capacity_details);
Capacity::set_capacity_for(provider_msa_id, capacity_details);

let call: &<Test as Config>::RuntimeCall =
&RuntimeCall::Balances(BalancesCall::transfer { dest: 2, value: 100 });
Expand Down

0 comments on commit 57f1d42

Please sign in to comment.