Skip to content

Commit

Permalink
pr feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ank4n committed May 13, 2024
1 parent 464cea0 commit 7782cab
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
9 changes: 5 additions & 4 deletions substrate/frame/delegated-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,17 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![deny(rustdoc::broken_intra_doc_links)]

mod impls;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
mod types;

pub use pallet::*;

mod types;

use types::*;

mod impls;

use frame_support::{
pallet_prelude::*,
traits::{
Expand Down Expand Up @@ -271,6 +269,9 @@ pub mod pallet {
/// Delegators can authorize `Agent`s to stake on their behalf by delegating their funds to
/// them. The `Agent` can then use the delegated funds to stake to [`Config::CoreStaking`].
///
/// An account that is directly staked to [`Config::CoreStaking`] cannot become an `Agent`.
/// However, they can migrate to become an agent using [`Self::migrate_to_agent`].
///
/// Implementation note: This function allows any account to become an agent. It is
/// important though that accounts that call [`StakingUnchecked::virtual_bond`] are keyless
/// accounts. This is not a problem for now since this is only used by other pallets in the
Expand Down
13 changes: 0 additions & 13 deletions substrate/frame/delegated-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,6 @@ impl delegated_staking::Config for Runtime {
type CoreStaking = Staking;
}

pub struct BalanceToU256;
impl Convert<Balance, U256> for BalanceToU256 {
fn convert(n: Balance) -> U256 {
n.into()
}
}
pub struct U256ToBalance;
impl Convert<U256, Balance> for U256ToBalance {
fn convert(n: U256) -> Balance {
n.try_into().unwrap()
}
}

parameter_types! {
pub static MaxUnbonding: u32 = 8;
}
Expand Down
46 changes: 44 additions & 2 deletions substrate/frame/delegated-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn cannot_become_agent() {
Error::<T>::AlreadyStaking
);

// an existing nominator cannot become agent
// an existing direct staker to `CoreStaking` cannot become an agent.
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(mock::GENESIS_NOMINATOR_ONE).into(),
Expand Down Expand Up @@ -193,6 +193,49 @@ fn agent_restrictions() {
),
Error::<T>::InvalidDelegation
);

// cannot delegate to non agents.
let non_agent = 201;
// give it some funds
fund(&non_agent, 200);
assert_noop!(
DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(delegator_one).into(),
non_agent,
10
),
Error::<T>::InvalidDelegation
);

// cannot delegate to a delegator
assert_noop!(
DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(delegator_one).into(),
delegator_two,
10
),
Error::<T>::InvalidDelegation
);

// delegator cannot delegate to self
assert_noop!(
DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(delegator_one).into(),
delegator_one,
10
),
Error::<T>::InvalidDelegation
);

// agent cannot delegate to self
assert_noop!(
DelegatedStaking::delegate_to_agent(
RawOrigin::Signed(agent_one).into(),
agent_one,
10
),
Error::<T>::InvalidDelegation
);
});
}

Expand Down Expand Up @@ -363,7 +406,6 @@ mod staking_integration {
Error::<T>::NotEnoughFunds
);

assert!(eq_stake(agent, total_staked, expected_active));
assert_eq!(get_agent(&agent).available_to_bond(), 0);
// full amount is still delegated
assert_eq!(get_agent(&agent).ledger.effective_balance(), total_staked);
Expand Down

0 comments on commit 7782cab

Please sign in to comment.