From fcdd1b436dcd3253280d7979156f21d33440c9ec Mon Sep 17 00:00:00 2001 From: NingBo Wang <2536935847@qq.com> Date: Thu, 17 Aug 2023 15:00:27 +0800 Subject: [PATCH] Fix slp (#1016) * Add v0.9.80_sudo * Fix slp --- .../slp/src/agents/moonbeam_agent/agent.rs | 154 ++----------- .../slp/src/agents/moonbeam_agent/types.rs | 8 +- pallets/slp/src/lib.rs | 2 +- pallets/slp/src/tests/moonriver_tests.rs | 216 ------------------ runtime/bifrost-kusama/Cargo.toml | 2 +- 5 files changed, 28 insertions(+), 354 deletions(-) diff --git a/pallets/slp/src/agents/moonbeam_agent/agent.rs b/pallets/slp/src/agents/moonbeam_agent/agent.rs index 65445fb355..e81a121200 100644 --- a/pallets/slp/src/agents/moonbeam_agent/agent.rs +++ b/pallets/slp/src/agents/moonbeam_agent/agent.rs @@ -44,6 +44,7 @@ use node_primitives::{ }; use orml_traits::MultiCurrency; use polkadot_parachain::primitives::Sibling; +use sp_arithmetic::Percent; use sp_runtime::{ traits::{ AccountIdConversion, CheckedAdd, CheckedSub, Convert, Saturating, UniqueSaturatedInto, Zero, @@ -204,9 +205,11 @@ impl let delegation_count: u32 = mins_maxs.validators_back_maximum; // Construct xcm message. - let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::Delegate( + let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::DelegateWithAutoCompound( validator_account_id_20, amount, + Percent::from_percent(100), + candidate_delegation_count, candidate_delegation_count, delegation_count, )); @@ -413,52 +416,10 @@ impl /// function. fn unbond_all( &self, - who: &MultiLocation, - currency_id: CurrencyId, + _who: &MultiLocation, + _currency_id: CurrencyId, ) -> Result> { - // check if the delegator exists. - let ledger_option = DelegatorLedgers::::get(currency_id, who); - - if let Some(Ledger::Moonbeam(ledger)) = ledger_option { - // check if the delegator is in the state of leaving. - ensure!(ledger.status == OneToManyDelegatorStatus::Active, Error::::AlreadyLeaving); - } else { - Err(Error::::DelegatorNotExist)?; - } - - // Construct xcm message. - let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::ScheduleLeaveDelegators); - - // Wrap the xcm message as it is sent from a subaccount of the parachain account, and - // send it out. - let (query_id, timeout, fee, xcm_message) = - Self::construct_xcm_as_subaccount_with_query_id( - XcmOperation::Chill, - call, - who, - currency_id, - )?; - - // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the - // process. - Self::burn_fee_from_source_account(fee, currency_id)?; - - // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. - Self::insert_delegator_ledger_update_entry( - who, - None, - MoonbeamLedgerUpdateOperation::LeaveDelegator, - Zero::zero(), - query_id, - timeout, - currency_id, - )?; - - // Send out the xcm message. - let dest = Self::get_moonbeam_para_multilocation(currency_id)?; - send_xcm::(dest, xcm_message).map_err(|_e| Error::::XcmFailure)?; - - Ok(query_id) + Err(Error::::Unsupported) } /// Cancel pending request @@ -553,7 +514,6 @@ impl targets: &Vec, currency_id: CurrencyId, ) -> Result> { - let mins_maxs = MinimumsAndMaximums::::get(currency_id).ok_or(Error::::NotExist)?; let validator = targets.first().ok_or(Error::::ValidatorNotProvided)?; // First, check if the delegator exists. @@ -566,18 +526,6 @@ impl // Second, check the validators one by one to see if all exist. ensure!(ledger.delegations.contains_key(validator), Error::::ValidatorNotBonded); ensure!(!ledger.request_briefs.contains_key(validator), Error::::AlreadyRequested); - let unbond_amount = ledger.delegations.get(&validator).ok_or(Error::::OverFlow)?; - - // Check after undelegating all these validators, if the delegator still meets the - // requirement. - let active = - ledger.total.checked_sub(&ledger.less_total).ok_or(Error::::UnderFlow)?; - let unbond_after_amount = - active.checked_sub(&unbond_amount).ok_or(Error::::UnderFlow)?; - ensure!( - unbond_after_amount >= mins_maxs.delegator_bonded_minimum, - Error::::LowerThanMinimum - ); } else { Err(Error::::DelegatorNotExist)?; } @@ -624,55 +572,11 @@ impl /// Cancel leave delegator set. fn redelegate( &self, - who: &MultiLocation, + _who: &MultiLocation, _targets: &Option>, - currency_id: CurrencyId, + _currency_id: CurrencyId, ) -> Result> { - // first check if the delegator exists. - let ledger_option = DelegatorLedgers::::get(currency_id, who); - if let Some(Ledger::Moonbeam(ledger)) = ledger_option { - // check if the delegator is in the state of leaving. - match ledger.status { - OneToManyDelegatorStatus::Leaving(_) => Ok(()), - _ => Err(Error::::DelegatorNotLeaving), - }?; - } else { - Err(Error::::DelegatorNotExist)?; - } - // do the cancellation. - // Construct xcm message. - let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::CancelLeaveDelegators); - - // Wrap the xcm message as it is sent from a subaccount of the parachain account, and - // send it out. - let (query_id, timeout, fee, xcm_message) = - Self::construct_xcm_as_subaccount_with_query_id( - XcmOperation::CancelLeave, - call, - who, - currency_id, - )?; - - // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the - // process. - Self::burn_fee_from_source_account(fee, currency_id)?; - - // Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue. - Self::insert_delegator_ledger_update_entry( - who, - None, - MoonbeamLedgerUpdateOperation::CancelLeave, - Zero::zero(), - query_id, - timeout, - currency_id, - )?; - - // Send out the xcm message. - let dest = Self::get_moonbeam_para_multilocation(currency_id)?; - send_xcm::(dest, xcm_message).map_err(|_e| Error::::XcmFailure)?; - - Ok(query_id) + Err(Error::::Unsupported) } /// Initiate payout for a certain delegator. @@ -700,7 +604,6 @@ impl let mut leaving = false; let now = T::VtokenMinting::get_ongoing_time_unit(currency_id) .ok_or(Error::::TimeUnitNotExist)?; - let mins_maxs = MinimumsAndMaximums::::get(currency_id).ok_or(Error::::NotExist)?; let ledger_option = DelegatorLedgers::::get(currency_id, who); let mut due_amount = Zero::zero(); @@ -725,33 +628,19 @@ impl // Construct xcm message. let delegator_h160_account = Pallet::::multilocation_to_h160_account(who)?; - let call; - let (query_id, timeout, fee, xcm_message) = if leaving { - call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::ExecuteLeaveDelegators( - delegator_h160_account, - mins_maxs.validators_back_maximum, - )); - - Self::construct_xcm_as_subaccount_with_query_id( - XcmOperation::ExecuteLeave, - call.clone(), - who, - currency_id, - ) - } else { - let validator_h160_account = Pallet::::multilocation_to_h160_account(&collator)?; - call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::ExecuteDelegationRequest( - delegator_h160_account, - validator_h160_account, - )); + let validator_h160_account = Pallet::::multilocation_to_h160_account(&collator)?; + let call = MoonbeamCall::Staking(MoonbeamParachainStakingCall::ExecuteDelegationRequest( + delegator_h160_account, + validator_h160_account, + )); + let (query_id, timeout, fee, xcm_message) = Self::construct_xcm_as_subaccount_with_query_id( XcmOperation::Liquidize, call.clone(), who, currency_id, - ) - }?; + )?; // withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the // process. @@ -788,8 +677,8 @@ impl } /// The same as unbondAll, leaving delegator set. - fn chill(&self, who: &MultiLocation, currency_id: CurrencyId) -> Result> { - Self::unbond_all(&self, who, currency_id) + fn chill(&self, _who: &MultiLocation, _currency_id: CurrencyId) -> Result> { + Err(Error::::Unsupported) } /// Make token transferred back to Bifrost chain account. @@ -1071,11 +960,8 @@ impl MoonbeamAgent { XcmOperation::Bond | XcmOperation::BondExtra | XcmOperation::Unbond | - XcmOperation::Chill | XcmOperation::Rebond | XcmOperation::Undelegate | - XcmOperation::CancelLeave | - XcmOperation::ExecuteLeave | XcmOperation::Liquidize => T::SubstrateResponseManager::create_query_record( &responder, Some(Pallet::::confirm_delegator_ledger_call()), @@ -1674,7 +1560,7 @@ impl MoonbeamAgent { fn get_report_transact_status_instruct(query_id: QueryId, max_weight: Weight) -> Instruction { ReportTransactStatus(QueryResponseInfo { - destination: MultiLocation::from(X1(Parachain(u32::from(T::ParachainId::get())))), + destination: MultiLocation::new(1, X1(Parachain(u32::from(T::ParachainId::get())))), query_id, max_weight, }) diff --git a/pallets/slp/src/agents/moonbeam_agent/types.rs b/pallets/slp/src/agents/moonbeam_agent/types.rs index 2b4bc691c6..ac5e8d6936 100644 --- a/pallets/slp/src/agents/moonbeam_agent/types.rs +++ b/pallets/slp/src/agents/moonbeam_agent/types.rs @@ -20,6 +20,7 @@ use crate::{BalanceOf, Config}; use codec::{Decode, Encode}; use frame_support::RuntimeDebug; use scale_info::TypeInfo; +use sp_arithmetic::Percent; use sp_core::H160; use sp_runtime::traits::{IdentityLookup, StaticLookup}; use sp_std::{boxed::Box, vec::Vec}; @@ -56,12 +57,15 @@ pub enum MoonbeamUtilityCall { #[derive(Encode, Decode, RuntimeDebug, Clone)] pub enum MoonbeamParachainStakingCall { - #[codec(index = 17)] - Delegate(H160, BalanceOf, u32, u32), + #[codec(index = 18)] + DelegateWithAutoCompound(H160, BalanceOf, Percent, u32, u32, u32), + // schedule_revoke_delegation #[codec(index = 19)] ScheduleLeaveDelegators, + // execute_delegation_request #[codec(index = 20)] ExecuteLeaveDelegators(H160, u32), + // cancel_delegation_request #[codec(index = 21)] CancelLeaveDelegators, #[codec(index = 22)] diff --git a/pallets/slp/src/lib.rs b/pallets/slp/src/lib.rs index 89f53aea9e..cdeb0df0a2 100644 --- a/pallets/slp/src/lib.rs +++ b/pallets/slp/src/lib.rs @@ -2069,7 +2069,7 @@ pub mod pallet { if let Response::DispatchResult(MaybeErrorCode::Success) = response { Self::get_ledger_update_agent_then_process(query_id, true)?; } else { - Self::do_fail_validators_by_delegator_query_response(query_id)?; + Self::do_fail_delegator_ledger_query_response(query_id)?; } Ok(()) } diff --git a/pallets/slp/src/tests/moonriver_tests.rs b/pallets/slp/src/tests/moonriver_tests.rs index 6a686f6837..04c3d3b57e 100644 --- a/pallets/slp/src/tests/moonriver_tests.rs +++ b/pallets/slp/src/tests/moonriver_tests.rs @@ -419,53 +419,6 @@ fn moonriver_unbond_works() { }); } -#[test] -fn moonriver_unbond_all_works() { - let bifrost_parachain_account_id_20: [u8; 20] = Sibling::from(2001).into_account_truncating(); - - let subaccount_0_account_id_20: [u8; 20] = - Slp::derivative_account_id_20(bifrost_parachain_account_id_20, 0).into(); - - let subaccount_0_location = MultiLocation { - parents: 1, - interior: X2( - Parachain(2023), - Junction::AccountKey20 { network: None, key: subaccount_0_account_id_20 }, - ), - }; - - ExtBuilder::default().build().execute_with(|| { - // environment setup - moonriver_setup(); - - let mut delegation_set: BTreeMap> = BTreeMap::new(); - delegation_set.insert(VALIDATOR_0_LOCATION, 8_000_000_000_000_000_000); - let request_briefs_set: BTreeMap)> = - BTreeMap::new(); - - // set delegator_0 ledger - let moonriver_ledger = OneToManyLedger { - account: subaccount_0_location, - total: 8_000_000_000_000_000_000, - less_total: 0, - delegations: delegation_set, - requests: vec![], - request_briefs: request_briefs_set, - status: OneToManyDelegatorStatus::Active, - }; - - let ledger = Ledger::Moonbeam(moonriver_ledger); - - // Set delegator ledger - DelegatorLedgers::::insert(MOVR, subaccount_0_location, ledger); - - assert_noop!( - Slp::unbond_all(RuntimeOrigin::signed(ALICE), MOVR, Box::new(subaccount_0_location),), - Error::::XcmFailure - ); - }); -} - #[test] fn moonriver_rebond_works() { let bifrost_parachain_account_id_20: [u8; 20] = Sibling::from(2001).into_account_truncating(); @@ -582,68 +535,6 @@ fn moonriver_undelegate_works() { }); } -#[test] -fn moonriver_redelegate_works() { - let bifrost_parachain_account_id_20: [u8; 20] = Sibling::from(2001).into_account_truncating(); - - let subaccount_0_account_id_20: [u8; 20] = - Slp::derivative_account_id_20(bifrost_parachain_account_id_20, 0).into(); - - let subaccount_0_location = MultiLocation { - parents: 1, - interior: X2( - Parachain(2023), - Junction::AccountKey20 { network: None, key: subaccount_0_account_id_20 }, - ), - }; - - ExtBuilder::default().build().execute_with(|| { - // environment setup - moonriver_setup(); - - let mut delegation_set: BTreeMap> = BTreeMap::new(); - delegation_set.insert(VALIDATOR_0_LOCATION, 8_000_000_000_000_000_000); - - let request = OneToManyScheduledRequest { - validator: VALIDATOR_0_LOCATION, - when_executable: TimeUnit::Round(24), - action: OneToManyDelegationAction::Revoke(8_000_000_000_000_000_000), - }; - let mut request_list = Vec::new(); - request_list.push(request); - let mut request_briefs_set: BTreeMap)> = - BTreeMap::new(); - request_briefs_set - .insert(VALIDATOR_0_LOCATION, (TimeUnit::Round(24), 8_000_000_000_000_000_000)); - - // set delegator_0 ledger - let moonriver_ledger = OneToManyLedger { - account: subaccount_0_location, - total: 8_000_000_000_000_000_000, - less_total: 8_000_000_000_000_000_000, - delegations: delegation_set, - requests: request_list, - request_briefs: request_briefs_set, - status: OneToManyDelegatorStatus::Leaving(TimeUnit::Round(24)), - }; - - let ledger = Ledger::Moonbeam(moonriver_ledger); - - // Set delegator ledger - DelegatorLedgers::::insert(MOVR, Box::new(subaccount_0_location), ledger); - - assert_noop!( - Slp::redelegate( - RuntimeOrigin::signed(ALICE), - MOVR, - Box::new(subaccount_0_location), - None - ), - Error::::XcmFailure - ); - }); -} - #[test] fn moonriver_liquidize_works() { let bifrost_parachain_account_id_20: [u8; 20] = Sibling::from(2001).into_account_truncating(); @@ -1514,113 +1405,6 @@ fn moonriver_undelegate_confirm_works() { }); } -#[test] -fn moonriver_redelegate_confirm_works() { - let bifrost_parachain_account_id_20: [u8; 20] = Sibling::from(2001).into_account_truncating(); - - let subaccount_0_account_id_20: [u8; 20] = - Slp::derivative_account_id_20(bifrost_parachain_account_id_20, 0).into(); - - let subaccount_0_location = MultiLocation { - parents: 1, - interior: X2( - Parachain(2023), - Junction::AccountKey20 { network: None, key: subaccount_0_account_id_20 }, - ), - }; - - ExtBuilder::default().build().execute_with(|| { - // environment setup - moonriver_setup(); - - let mut delegation_set: BTreeMap> = BTreeMap::new(); - delegation_set.insert(VALIDATOR_0_LOCATION, 8_000_000_000_000_000_000); - - let request = OneToManyScheduledRequest { - validator: VALIDATOR_0_LOCATION, - when_executable: TimeUnit::Round(24), - action: OneToManyDelegationAction::Revoke(8_000_000_000_000_000_000), - }; - let mut request_list = Vec::new(); - request_list.push(request); - let mut request_briefs_set: BTreeMap)> = - BTreeMap::new(); - request_briefs_set - .insert(VALIDATOR_0_LOCATION, (TimeUnit::Round(24), 8_000_000_000_000_000_000)); - - // set delegator_0 ledger - let moonriver_ledger = OneToManyLedger { - account: subaccount_0_location, - total: 8_000_000_000_000_000_000, - less_total: 8_000_000_000_000_000_000, - delegations: delegation_set, - requests: request_list, - request_briefs: request_briefs_set, - status: OneToManyDelegatorStatus::Leaving(TimeUnit::Round(24)), - }; - - let ledger = Ledger::Moonbeam(moonriver_ledger); - - // Set delegator ledger - DelegatorLedgers::::insert(MOVR, Box::new(subaccount_0_location), ledger); - - assert_noop!( - Slp::redelegate( - RuntimeOrigin::signed(ALICE), - MOVR, - Box::new(subaccount_0_location), - None - ), - Error::::XcmFailure - ); - - let query_id = 8; - let update_entry_8 = LedgerUpdateEntry::Moonbeam(MoonbeamLedgerUpdateEntry { - currency_id: MOVR, - delegator_id: subaccount_0_location, - validator_id: None, - update_operation: MoonbeamLedgerUpdateOperation::CancelLeave, - amount: 0, - unlock_time: None, - }); - - DelegatorLedgerXcmUpdateQueue::::insert(query_id, (update_entry_8.clone(), 1000)); - - assert_eq!( - DelegatorLedgerXcmUpdateQueue::::get(query_id), - Some((update_entry_8.clone(), 1000)) - ); - - assert_ok!(Slp::confirm_delegator_ledger_query_response( - RuntimeOrigin::signed(ALICE), - MOVR, - query_id - )); - - assert_eq!(DelegatorLedgerXcmUpdateQueue::::get(query_id), None); - - let mut delegation_set: BTreeMap> = BTreeMap::new(); - delegation_set.insert(VALIDATOR_0_LOCATION, 8_000_000_000_000_000_000); - let request_briefs_set: BTreeMap)> = - BTreeMap::new(); - - // set delegator_0 ledger - let moonriver_ledger = OneToManyLedger { - account: subaccount_0_location, - total: 8_000_000_000_000_000_000, - less_total: 0, - delegations: delegation_set, - requests: vec![], - request_briefs: request_briefs_set, - status: OneToManyDelegatorStatus::Active, - }; - - let ledger = Ledger::Moonbeam(moonriver_ledger); - - assert_eq!(DelegatorLedgers::::get(MOVR, subaccount_0_location), Some(ledger)); - }); -} - #[test] fn moonriver_transfer_back_works() { let bifrost_parachain_account_id_20: [u8; 20] = Sibling::from(2001).into_account_truncating(); diff --git a/runtime/bifrost-kusama/Cargo.toml b/runtime/bifrost-kusama/Cargo.toml index cf25fe2444..7fece04c77 100644 --- a/runtime/bifrost-kusama/Cargo.toml +++ b/runtime/bifrost-kusama/Cargo.toml @@ -364,4 +364,4 @@ disable-runtime-api = [] on-chain-release-build = ["sp-api/disable-logging"] # Set timing constants (e.g. session period) to faster versions to speed up testing. -fast-runtime = [] \ No newline at end of file +fast-runtime = []