Skip to content

Commit

Permalink
integrate module homa-validator-list
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjj9219 committed Apr 8, 2024
1 parent 6e05383 commit 4fba768
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ module-evm-bridge = { path = "modules/evm-bridge", default-features = false }
module-evm-rpc-runtime-api = { path = "modules/evm/rpc/runtime-api", default-features = false }
module-evm-utility = { path = "modules/evm-utility", default-features = false }
module-homa = { path = "modules/homa", default-features = false }
module-homa-validator-list = { path = "modules/homa-validator-list", default-features = false }
module-honzon = { path = "modules/honzon", default-features = false }
module-honzon-bridge = { path = "modules/honzon-bridge", default-features = false }
module-idle-scheduler = { path = "modules/idle-scheduler", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions modules/homa-validator-list/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl ExchangeRateProvider for MockLiquidStakingExchangeProvider {

parameter_types! {
pub static MockBlockNumberProvider: u64 = 0;
pub ActiveSubAccountsIndexList: Vec<u16> = vec![0, 1, 2];
}

impl BlockNumberProvider for MockBlockNumberProvider {
Expand Down Expand Up @@ -190,6 +191,8 @@ impl Config for Runtime {
type OnIncreaseGuarantee = MockOnIncreaseGuarantee;
type OnDecreaseGuarantee = MockOnDecreaseGuarantee;
type BlockNumberProvider = MockBlockNumberProvider;
type MaxNominations = ConstU32<24>;
type ActiveSubAccountsIndexList = ActiveSubAccountsIndexList;
}

type Block = frame_system::mocking::MockBlock<Runtime>;
Expand Down
2 changes: 0 additions & 2 deletions modules/homa-validator-list/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,6 @@ fn contains_work() {
.total_insurance,
100
);
assert!(!HomaValidatorListModule::contains(&VALIDATOR_1));

assert_ok!(HomaValidatorListModule::bond(
RuntimeOrigin::signed(ALICE),
Expand All @@ -874,6 +873,5 @@ fn contains_work() {
.total_insurance,
200
);
assert!(HomaValidatorListModule::contains(&VALIDATOR_1));
});
}
26 changes: 11 additions & 15 deletions modules/homa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ use primitives::{Balance, CurrencyId, EraIndex};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{
AccountIdConversion, BlockNumberProvider, Bounded, CheckedDiv, CheckedSub, MaybeDisplay, One, Saturating,
AccountIdConversion, BlockNumberProvider, Bounded, CheckedDiv, CheckedSub, One, Saturating,
UniqueSaturatedInto, Zero,
},
ArithmeticError, FixedPointNumber,
};
use sp_std::{cmp::Ordering, convert::From, fmt::Debug, prelude::*, vec, vec::Vec};
use sp_std::{cmp::Ordering, convert::From, prelude::*, vec, vec::Vec};

pub use module::*;
pub use weights::WeightInfo;
Expand All @@ -47,6 +47,11 @@ pub mod weights;
pub mod module {
use super::*;

pub type RelayChainAccountIdOf<T> = <<T as Config>::XcmInterface as HomaSubAccountXcm<
<T as frame_system::Config>::AccountId,
Balance,
>>::RelayChainAccountId;

/// The subaccount's staking ledger which kept by Homa protocol
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, Default)]
pub struct StakingLedger {
Expand Down Expand Up @@ -147,21 +152,12 @@ pub mod module {
type RelayChainBlockNumber: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;

/// The XcmInterface to manage the staking of sub-account on relaychain.
type XcmInterface: HomaSubAccountXcm<Self::AccountId, Balance, Self::RelayChainAccountId>;
type XcmInterface: HomaSubAccountXcm<Self::AccountId, Balance>;

/// Weight information for the extrinsics in this module.
type WeightInfo: WeightInfo;

/// The AccountId of a relay chain account.
type RelayChainAccountId: Parameter
+ Member
+ MaybeSerializeDeserialize
+ Debug
+ MaybeDisplay
+ Ord
+ MaxEncodedLen;

type GetNominations: Get<Vec<(u16, Vec<Self::RelayChainAccountId>)>>;
type NominationsProvider: Get<Vec<(u16, Vec<RelayChainAccountIdOf<Self>>)>>;
}

#[pallet::error]
Expand Down Expand Up @@ -263,7 +259,7 @@ pub mod module {
/// Nominate validators on RelayChain
HomaNominate {
sub_account_index: u16,
nominations: Vec<T::RelayChainAccountId>,
nominations: Vec<RelayChainAccountIdOf<T>>,
},
}

Expand Down Expand Up @@ -1144,7 +1140,7 @@ pub mod module {
let nominate_interval_era = NominateIntervalEra::<T>::get();
if !nominate_interval_era.is_zero() && new_era % nominate_interval_era == 0 {
let active_sub_accounts_list = T::ActiveSubAccountsIndexList::get();
for (sub_account_index, nominations) in T::GetNominations::get() {
for (sub_account_index, nominations) in T::NominationsProvider::get() {
if active_sub_accounts_list.contains(&sub_account_index) && !nominations.is_empty() {
T::XcmInterface::nominate_on_sub_account(sub_account_index, nominations.clone())?;

Expand Down
9 changes: 5 additions & 4 deletions modules/homa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ pub const VALIDATOR_D: AccountId = AccountId32::new([203u8; 32]);

/// mock XCM transfer.
pub struct MockHomaSubAccountXcm;
impl HomaSubAccountXcm<AccountId, Balance, AccountId> for MockHomaSubAccountXcm {
impl HomaSubAccountXcm<AccountId, Balance> for MockHomaSubAccountXcm {
type RelayChainAccountId = AccountId;

fn transfer_staking_to_sub_account(sender: &AccountId, _: u16, amount: Balance) -> DispatchResult {
Currencies::withdraw(StakingCurrencyId::get(), sender, amount)
}
Expand All @@ -72,7 +74,7 @@ impl HomaSubAccountXcm<AccountId, Balance, AccountId> for MockHomaSubAccountXcm
Ok(())
}

fn nominate_on_sub_account(_: u16, _: Vec<AccountId>) -> DispatchResult {
fn nominate_on_sub_account(_: u16, _: Vec<Self::RelayChainAccountId>) -> DispatchResult {
Ok(())
}

Expand Down Expand Up @@ -198,8 +200,7 @@ impl Config for Runtime {
type RelayChainBlockNumber = MockRelayBlockNumberProvider;
type XcmInterface = MockHomaSubAccountXcm;
type WeightInfo = ();
type RelayChainAccountId = AccountId;
type GetNominations = GetNominations;
type NominationsProvider = GetNominations;
}

type Block = frame_system::mocking::MockBlock<Runtime>;
Expand Down
6 changes: 4 additions & 2 deletions modules/support/src/homa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

use crate::{ExchangeRate, Rate};
use sp_runtime::DispatchResult;
use sp_std::fmt::Debug;
use xcm::v3::prelude::*;

pub trait HomaSubAccountXcm<AccountId, Balance, RelayChainAccountId> {
pub trait HomaSubAccountXcm<AccountId, Balance> {
type RelayChainAccountId: Debug + Clone + Ord;
/// Cross-chain transfer staking currency to sub account on relaychain.
fn transfer_staking_to_sub_account(sender: &AccountId, sub_account_index: u16, amount: Balance) -> DispatchResult;
/// Send XCM message to the relaychain for sub account to withdraw_unbonded staking currency and
Expand All @@ -31,7 +33,7 @@ pub trait HomaSubAccountXcm<AccountId, Balance, RelayChainAccountId> {
/// Send XCM message to the relaychain for sub account to unbond.
fn unbond_on_sub_account(sub_account_index: u16, amount: Balance) -> DispatchResult;
/// Send XCM message to the relaychain for sub account to nominate.
fn nominate_on_sub_account(sub_account_index: u16, targets: Vec<RelayChainAccountId>) -> DispatchResult;
fn nominate_on_sub_account(sub_account_index: u16, targets: Vec<Self::RelayChainAccountId>) -> DispatchResult;
/// The fee of cross-chain transfer is deducted from the recipient.
fn get_xcm_transfer_fee() -> Balance;
/// The fee of parachain
Expand Down
6 changes: 4 additions & 2 deletions modules/xcm-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ pub mod module {
}
}

impl<T: Config> HomaSubAccountXcm<T::AccountId, Balance, T::AccountId> for Pallet<T> {
impl<T: Config> HomaSubAccountXcm<T::AccountId, Balance> for Pallet<T> {
type RelayChainAccountId = T::AccountId;

/// Cross-chain transfer staking currency to sub account on relaychain.
fn transfer_staking_to_sub_account(
sender: &T::AccountId,
Expand Down Expand Up @@ -307,7 +309,7 @@ pub mod module {
}

/// Send XCM message to the relaychain for sub account to nominate.
fn nominate_on_sub_account(sub_account_index: u16, targets: Vec<T::AccountId>) -> DispatchResult {
fn nominate_on_sub_account(sub_account_index: u16, targets: Vec<Self::RelayChainAccountId>) -> DispatchResult {
let (xcm_dest_weight, xcm_fee) = Self::xcm_dest_weight_and_fee(XcmInterfaceOperation::HomaNominate);
let xcm_message = T::RelayChainCallBuilder::finalize_call_into_xcm_message(
T::RelayChainCallBuilder::utility_as_derivative_call(
Expand Down
3 changes: 3 additions & 0 deletions runtime/acala/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ module-evm-accounts = { workspace = true }
module-evm-bridge = { workspace = true }
module-evm-rpc-runtime-api = { workspace = true }
module-homa = { workspace = true }
module-homa-validator-list = { workspace = true }
module-honzon = { workspace = true }
module-idle-scheduler = { workspace = true }
module-incentives = { workspace = true }
Expand Down Expand Up @@ -243,6 +244,7 @@ std = [
"module-evm-rpc-runtime-api/std",
"module-evm/std",
"module-homa/std",
"module-homa-validator-list/std",
"module-honzon/std",
"module-idle-scheduler/std",
"module-incentives/std",
Expand Down Expand Up @@ -382,6 +384,7 @@ try-runtime = [
"module-evm-bridge/try-runtime",
"module-evm/try-runtime",
"module-homa/try-runtime",
"module-homa-validator-list/try-runtime",
"module-honzon/try-runtime",
"module-idle-scheduler/try-runtime",
"module-incentives/try-runtime",
Expand Down
26 changes: 26 additions & 0 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,31 @@ impl module_homa::Config for Runtime {
type RelayChainBlockNumber = RelaychainDataProvider<Runtime>;
type XcmInterface = XcmInterface;
type WeightInfo = weights::module_homa::WeightInfo<Runtime>;
type NominationsProvider = HomaValidatorList;
}

parameter_types! {
pub MinBondAmount: Balance = 10 * dollar(LDOT);
pub const ValidatorBackingBondingDuration: BlockNumber = 28 * DAYS;
pub ValidatorInsuranceThreshold: Balance = 100_000 * dollar(LDOT);
}

impl module_homa_validator_list::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RelayChainAccountId = AccountId;
type LiquidTokenCurrency = module_currencies::Currency<Runtime, GetLiquidCurrencyId>;
type MinBondAmount = MinBondAmount;
type BondingDuration = ValidatorBackingBondingDuration;
type ValidatorInsuranceThreshold = ValidatorInsuranceThreshold;
type GovernanceOrigin = EnsureRootOrHalfGeneralCouncil;
type OnSlash = ();
type LiquidStakingExchangeRateProvider = Homa;
type WeightInfo = ();
type OnIncreaseGuarantee = ();
type OnDecreaseGuarantee = ();
type BlockNumberProvider = RelaychainDataProvider<Runtime>;
type MaxNominations = ConstU32<24>;
type ActiveSubAccountsIndexList = ActiveSubAccountsIndexList;
}

pub fn create_x2_parachain_multilocation(index: u16) -> MultiLocation {
Expand Down Expand Up @@ -1842,6 +1867,7 @@ construct_runtime!(
// Homa
Homa: module_homa = 116,
XcmInterface: module_xcm_interface = 117,
HomaValidatorList: module_homa_validator_list = 118,

// Acala Other
Incentives: module_incentives = 120,
Expand Down
5 changes: 5 additions & 0 deletions runtime/common/src/precompile/homa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ mod tests {
Some(FixedU128::saturating_from_rational(1, 10)),
Some(FixedU128::saturating_from_rational(1, 10)),
Some(FixedU128::saturating_from_rational(1, 10)),
None,
));

assert_ok!(Currencies::update_balance(
Expand Down Expand Up @@ -275,6 +276,7 @@ mod tests {
Some(FixedU128::saturating_from_rational(1, 10)),
Some(FixedU128::saturating_from_rational(1, 10)),
Some(FixedU128::saturating_from_rational(1, 10)),
None,
));

assert_ok!(Currencies::update_balance(
Expand Down Expand Up @@ -353,6 +355,7 @@ mod tests {
Some(FixedU128::saturating_from_rational(1, 10)),
None,
None,
None,
));

// getEstimatedRewardRate() -> 0xd313f77e
Expand Down Expand Up @@ -384,6 +387,7 @@ mod tests {
None,
Some(FixedU128::saturating_from_rational(1, 10)),
None,
None,
));

// getCommissionRate() => 0x3e4eb36c
Expand Down Expand Up @@ -413,6 +417,7 @@ mod tests {
None,
None,
Some(FixedU128::saturating_from_rational(1, 10)),
None,
));

// getFastMatchFee() => 0xc18290dd
Expand Down
2 changes: 2 additions & 0 deletions runtime/common/src/precompile/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ parameter_types! {
pub const BondingDuration: EraIndex = 28;
pub const MintThreshold: Balance = 0;
pub const RedeemThreshold: Balance = 0;
pub VoidNominationsProvider: Vec<(u16, Vec<AccountId>)> = vec![];
}

impl module_homa::Config for Test {
Expand All @@ -724,6 +725,7 @@ impl module_homa::Config for Test {
type RelayChainBlockNumber = MockRelayBlockNumberProvider;
type XcmInterface = MockHomaSubAccountXcm;
type WeightInfo = ();
type NominationsProvider = VoidNominationsProvider;
}

impl orml_rewards::Config for Test {
Expand Down
3 changes: 3 additions & 0 deletions runtime/karura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ module-evm-accounts = { workspace = true }
module-evm-bridge = { workspace = true }
module-evm-rpc-runtime-api = { workspace = true }
module-homa = { workspace = true }
module-homa-validator-list = { workspace = true }
module-honzon = { workspace = true }
module-honzon-bridge = { workspace = true }
module-idle-scheduler = { workspace = true }
Expand Down Expand Up @@ -244,6 +245,7 @@ std = [
"module-evm-rpc-runtime-api/std",
"module-evm/std",
"module-homa/std",
"module-homa-validator-list/std",
"module-honzon-bridge/std",
"module-honzon/std",
"module-idle-scheduler/std",
Expand Down Expand Up @@ -384,6 +386,7 @@ try-runtime = [
"module-evm-bridge/try-runtime",
"module-evm/try-runtime",
"module-homa/try-runtime",
"module-homa-validator-list/try-runtime",
"module-honzon-bridge/try-runtime",
"module-honzon/try-runtime",
"module-idle-scheduler/try-runtime",
Expand Down
26 changes: 26 additions & 0 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,31 @@ impl module_homa::Config for Runtime {
type RelayChainBlockNumber = RelaychainDataProvider<Runtime>;
type XcmInterface = XcmInterface;
type WeightInfo = weights::module_homa::WeightInfo<Runtime>;
type NominationsProvider = HomaValidatorList;
}

parameter_types! {
pub MinBondAmount: Balance = 10 * dollar(LKSM);
pub const ValidatorBackingBondingDuration: BlockNumber = 7 * DAYS;
pub ValidatorInsuranceThreshold: Balance = 10_000 * dollar(LKSM);
}

impl module_homa_validator_list::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RelayChainAccountId = AccountId;
type LiquidTokenCurrency = module_currencies::Currency<Runtime, GetLiquidCurrencyId>;
type MinBondAmount = MinBondAmount;
type BondingDuration = ValidatorBackingBondingDuration;
type ValidatorInsuranceThreshold = ValidatorInsuranceThreshold;
type GovernanceOrigin = EnsureRootOrHalfGeneralCouncil;
type OnSlash = ();
type LiquidStakingExchangeRateProvider = Homa;
type WeightInfo = ();
type OnIncreaseGuarantee = ();
type OnDecreaseGuarantee = ();
type BlockNumberProvider = RelaychainDataProvider<Runtime>;
type MaxNominations = ConstU32<24>;
type ActiveSubAccountsIndexList = ActiveSubAccountsIndexList;
}

pub fn create_x2_parachain_multilocation(index: u16) -> MultiLocation {
Expand Down Expand Up @@ -1846,6 +1871,7 @@ construct_runtime!(
// Homa
Homa: module_homa = 116,
XcmInterface: module_xcm_interface = 117,
HomaValidatorList: module_homa_validator_list = 118,

// Karura Other
Incentives: module_incentives = 120,
Expand Down
Loading

0 comments on commit 4fba768

Please sign in to comment.