diff --git a/substrate/frame/delegated-staking/src/lib.rs b/substrate/frame/delegated-staking/src/lib.rs index b0b6c3d302ec..210f69d9c839 100644 --- a/substrate/frame/delegated-staking/src/lib.rs +++ b/substrate/frame/delegated-staking/src/lib.rs @@ -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::{ @@ -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 diff --git a/substrate/frame/delegated-staking/src/mock.rs b/substrate/frame/delegated-staking/src/mock.rs index bba4088d7c98..7399cb7a6e9b 100644 --- a/substrate/frame/delegated-staking/src/mock.rs +++ b/substrate/frame/delegated-staking/src/mock.rs @@ -151,19 +151,6 @@ impl delegated_staking::Config for Runtime { type CoreStaking = Staking; } -pub struct BalanceToU256; -impl Convert for BalanceToU256 { - fn convert(n: Balance) -> U256 { - n.into() - } -} -pub struct U256ToBalance; -impl Convert for U256ToBalance { - fn convert(n: U256) -> Balance { - n.try_into().unwrap() - } -} - parameter_types! { pub static MaxUnbonding: u32 = 8; } diff --git a/substrate/frame/delegated-staking/src/tests.rs b/substrate/frame/delegated-staking/src/tests.rs index 86e8f95bf7b6..46ec3e71ed2d 100644 --- a/substrate/frame/delegated-staking/src/tests.rs +++ b/substrate/frame/delegated-staking/src/tests.rs @@ -74,7 +74,7 @@ fn cannot_become_agent() { Error::::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(), @@ -193,6 +193,49 @@ fn agent_restrictions() { ), Error::::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::::InvalidDelegation + ); + + // cannot delegate to a delegator + assert_noop!( + DelegatedStaking::delegate_to_agent( + RawOrigin::Signed(delegator_one).into(), + delegator_two, + 10 + ), + Error::::InvalidDelegation + ); + + // delegator cannot delegate to self + assert_noop!( + DelegatedStaking::delegate_to_agent( + RawOrigin::Signed(delegator_one).into(), + delegator_one, + 10 + ), + Error::::InvalidDelegation + ); + + // agent cannot delegate to self + assert_noop!( + DelegatedStaking::delegate_to_agent( + RawOrigin::Signed(agent_one).into(), + agent_one, + 10 + ), + Error::::InvalidDelegation + ); }); } @@ -363,7 +406,6 @@ mod staking_integration { Error::::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);