Skip to content

Commit

Permalink
feat: make extra conditional judge
Browse files Browse the repository at this point in the history
  • Loading branch information
wangminqi committed Jan 9, 2025
1 parent 17a234a commit b359a36
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 48 deletions.
25 changes: 22 additions & 3 deletions parachain/pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,12 @@ pub mod pallet {
/// Success forbids future delegation requests until the request is invoked or cancelled.
pub fn schedule_leave_delegators(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let delegator = ensure_signed(origin)?;
Self::delegator_schedule_revoke_all(delegator)
let _ = Self::delegator_schedule_revoke_all(delegator.clone())?;
if T::LeaveDelegatorsDelay::get() == 0u32 {
Self::delegator_execute_scheduled_revoke_all(delegator)
} else {
Ok(().into())
}
}
#[pallet::call_index(22)]
#[pallet::weight(< T as Config >::WeightInfo::execute_leave_delegators(
Expand Down Expand Up @@ -1281,7 +1286,12 @@ pub mod pallet {
collator: T::AccountId,
) -> DispatchResultWithPostInfo {
let delegator = ensure_signed(origin)?;
Self::delegation_schedule_revoke(collator, delegator)
let _ = Self::delegation_schedule_revoke(collator.clone(), delegator.clone())?;
if T::RevokeDelegationDelay::get() == 0u32 {
Self::delegation_execute_scheduled_request(collator, delegator)
} else {
Ok(().into())
}
}

#[pallet::call_index(25)]
Expand Down Expand Up @@ -1317,7 +1327,16 @@ pub mod pallet {
less: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let delegator = ensure_signed(origin)?;
Self::delegation_schedule_bond_decrease(candidate, delegator, less)
let _ = Self::delegation_schedule_bond_decrease(
candidate.clone(),
delegator.clone(),
less,
)?;
if T::DelegationBondLessDelay::get() == 0u32 {
Self::delegation_execute_scheduled_request(collator, delegator)
} else {
Ok(().into())
}
}

#[pallet::call_index(27)]
Expand Down
72 changes: 27 additions & 45 deletions parachain/pallets/parachain-staking/src/tests_zero_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.
use crate::mock_zero_delay::{ExtBuilder, RuntimeCall, RuntimeOrigin, Utility};
use frame_support::assert_ok;
use crate::mock_zero_delay::{
ExtBuilder, ParachainStaking, RuntimeCall, RuntimeOrigin, Test, Utility,
};
use frame_support::{assert_noop, assert_ok};

use crate::Call as ParachainStakingCall;

Expand All @@ -26,18 +28,13 @@ fn batch_unstake_and_leave_delegators_works_if_zero_delay() {
.with_delegations(vec![(2, 1, 10)])
.build()
.execute_with(|| {
// can execute immediately
assert_ok!(Utility::batch_all(
RuntimeOrigin::signed(2),
vec![
RuntimeCall::ParachainStaking(
ParachainStakingCall::schedule_leave_delegators {}
),
RuntimeCall::ParachainStaking(ParachainStakingCall::execute_leave_delegators {
delegator: 2
}),
]
));
// Execute immediately
assert_ok!(ParachainStaking::schedule_leave_delegators(RuntimeOrigin::signed(2)));

assert_noop!(
ParachainStaking::execute_leave_delegators(RuntimeOrigin::signed(2)),
Error::<Test>::PendingDelegationRequestDNE
);
});
}

Expand Down Expand Up @@ -71,24 +68,17 @@ fn batch_unstake_and_delegator_bond_less_works_if_zero_delay() {
.with_delegations(vec![(2, 1, 10)])
.build()
.execute_with(|| {
// can execute immediately
assert_ok!(Utility::batch_all(
// Execute immediately
assert_ok!(ParachainStaking::schedule_delegator_bond_less(
RuntimeOrigin::signed(2),
vec![
RuntimeCall::ParachainStaking(
ParachainStakingCall::schedule_delegator_bond_less {
candidate: 1,
less: 1
}
),
RuntimeCall::ParachainStaking(
ParachainStakingCall::execute_delegation_request {
delegator: 2,
candidate: 1
}
),
]
1,
1
));

assert_noop!(
ParachainStaking::execute_delegation_request(RuntimeOrigin::signed(2), 2, 1),
Error::<Test>::PendingDelegationRequestDNE
);
});
}

Expand All @@ -100,21 +90,13 @@ fn batch_unstake_and_revoke_delegation_works_if_zero_delay() {
.with_delegations(vec![(2, 1, 10)])
.build()
.execute_with(|| {
// can execute immediately
assert_ok!(Utility::batch_all(
RuntimeOrigin::signed(2),
vec![
RuntimeCall::ParachainStaking(
ParachainStakingCall::schedule_revoke_delegation { collator: 1 }
),
RuntimeCall::ParachainStaking(
ParachainStakingCall::execute_delegation_request {
delegator: 2,
candidate: 1
}
),
]
));
// Execute immediately
assert_ok!(ParachainStaking::schedule_revoke_delegation(RuntimeOrigin::signed(2), 1));

assert_noop!(
ParachainStaking::execute_delegation_request(RuntimeOrigin::signed(2), 2, 1),
Error::<Test>::PendingDelegationRequestDNE
);
});
}

Expand Down

0 comments on commit b359a36

Please sign in to comment.