Skip to content

Commit

Permalink
External Index handling as sent by external chain (#836)
Browse files Browse the repository at this point in the history
* epoch

* doc

* migration code

* fmt

* some fixes here and there

* fix

* types

* fix test

* missing test

* fix

* tests for migration

* try-runtime tests

* add on era hook timestamp

* fmt

* change everything to index id

* dancelight

* improve doc

* keep renaming

* keep renaming

* fixes with respect to e2e

* ts-api

* yet more fixes

* final changes
  • Loading branch information
girazoki authored Jan 31, 2025
1 parent bf6339c commit 8ca9120
Show file tree
Hide file tree
Showing 39 changed files with 896 additions and 623 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions chains/orchestrator-paras/runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ scale-info = { workspace = true, features = [ "derive" ] }
# Own
pallet-configuration = { workspace = true }
pallet-data-preservers = { workspace = true }
pallet-external-validator-slashes = { workspace = true }
pallet-external-validators = { workspace = true }
pallet-foreign-asset-creator = { workspace = true }
pallet-invulnerables = { workspace = true }
Expand Down Expand Up @@ -50,6 +51,7 @@ staging-xcm = { workspace = true }

sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-staking = { workspace = true }
sp-std = { workspace = true }

# Cumulus
Expand All @@ -70,6 +72,7 @@ std = [
"pallet-beefy-mmr/std",
"pallet-configuration/std",
"pallet-data-preservers/std",
"pallet-external-validator-slashes/std",
"pallet-external-validators/std",
"pallet-foreign-asset-creator/std",
"pallet-invulnerables/std",
Expand All @@ -84,6 +87,7 @@ std = [
"scale-info/std",
"sp-core/std",
"sp-runtime/std",
"sp-staking/std",
"sp-std/std",
"staging-xcm/std",
]
Expand All @@ -97,6 +101,7 @@ runtime-benchmarks = [
"pallet-beefy-mmr/runtime-benchmarks",
"pallet-configuration/runtime-benchmarks",
"pallet-data-preservers/runtime-benchmarks",
"pallet-external-validator-slashes/runtime-benchmarks",
"pallet-external-validators/runtime-benchmarks",
"pallet-foreign-asset-creator/runtime-benchmarks",
"pallet-invulnerables/runtime-benchmarks",
Expand All @@ -107,6 +112,7 @@ runtime-benchmarks = [
"pallet-treasury/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
]

try-runtime = [
Expand All @@ -118,6 +124,7 @@ try-runtime = [
"pallet-beefy-mmr/try-runtime",
"pallet-configuration/try-runtime",
"pallet-data-preservers/try-runtime",
"pallet-external-validator-slashes/try-runtime",
"pallet-external-validators/try-runtime",
"pallet-foreign-asset-creator/try-runtime",
"pallet-invulnerables/try-runtime",
Expand Down
142 changes: 103 additions & 39 deletions chains/orchestrator-paras/runtime/common/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ where
/// Run a standard pre-runtime test. This works the same way as in a normal runtime upgrade.
#[cfg(feature = "try-runtime")]
fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
log::info!(
"hashed key {:?}",
pallet_configuration::ActiveConfig::<T>::hashed_key()
);
let old_config_bytes = frame_support::storage::unhashed::get_raw(
&pallet_configuration::ActiveConfig::<T>::hashed_key(),
)
Expand Down Expand Up @@ -837,6 +841,96 @@ where
}
}

pub struct MigrateMMRLeafPallet<T>(pub PhantomData<T>);

impl<T: frame_system::Config> Migration for MigrateMMRLeafPallet<T> {
fn friendly_name(&self) -> &str {
"SM_MigrateMMRLeafPallet"
}

fn migrate(&self, available_weight: Weight) -> Weight {
let new_name =
<<T as frame_system::Config>::PalletInfo as frame_support::traits::PalletInfo>::name::<
pallet_beefy_mmr::Pallet<T>,
>()
.expect("pallet_beefy_mmr must be part of dancelight before this migration");
move_pallet(Self::old_pallet_name().as_bytes(), new_name.as_bytes());
available_weight
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
Ok(vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(&self, _state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
Ok(())
}
}

impl<T> MigrateMMRLeafPallet<T> {
pub fn old_pallet_name() -> &'static str {
"MMRLeaf"
}
}

pub struct BondedErasTimestampMigration<Runtime>(pub PhantomData<Runtime>);

impl<Runtime> Migration for BondedErasTimestampMigration<Runtime>
where
Runtime: pallet_external_validator_slashes::Config,
{
fn friendly_name(&self) -> &str {
"TM_ExternalValidatorSlashesBondedErasTimestampMigration"
}

fn migrate(&self, _available_weight: Weight) -> Weight {
use frame_support::pallet_prelude::*;

let bonded_eras: Vec<(sp_staking::EraIndex, sp_staking::SessionIndex)> =
frame_support::storage::unhashed::get(
&pallet_external_validator_slashes::BondedEras::<Runtime>::hashed_key(),
)
.unwrap_or_default();
let new_eras = bonded_eras
.iter()
.map(|(era, session)| (*era, *session, 0u64))
.collect();
pallet_external_validator_slashes::BondedEras::<Runtime>::set(new_eras);

// One db read and one db write per element, plus the on-chain storage
Runtime::DbWeight::get().reads_writes(1, 1)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
use frame_support::pallet_prelude::*;

let previous_bonded_eras: Vec<(sp_staking::EraIndex, sp_staking::SessionIndex)> =
frame_support::storage::unhashed::get(
&pallet_external_validator_slashes::BondedEras::<Runtime>::hashed_key(),
)
.unwrap_or_default();

Ok(previous_bonded_eras.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(&self, state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
use parity_scale_codec::Decode;
let previous_bonded_eras: Vec<(sp_staking::EraIndex, sp_staking::SessionIndex)> =
Decode::decode(&mut &state[..]).expect("state to be decoded properly");
let new_eras = pallet_external_validator_slashes::BondedEras::<Runtime>::get();
for (i, bonded) in new_eras.iter().enumerate() {
assert_eq!(previous_bonded_eras[i].0, bonded.0);
assert_eq!(previous_bonded_eras[i].1, bonded.1);
assert_eq!(bonded.2, 0u64);
}
Ok(())
}
}

pub struct FlashboxMigrations<Runtime>(PhantomData<Runtime>);

impl<Runtime> GetMigrations for FlashboxMigrations<Runtime>
Expand Down Expand Up @@ -990,40 +1084,6 @@ where
}
}

pub struct MigrateMMRLeafPallet<T>(pub PhantomData<T>);

impl<T: frame_system::Config> Migration for MigrateMMRLeafPallet<T> {
fn friendly_name(&self) -> &str {
"SM_MigrateMMRLeafPallet"
}

fn migrate(&self, available_weight: Weight) -> Weight {
let new_name =
<<T as frame_system::Config>::PalletInfo as frame_support::traits::PalletInfo>::name::<
pallet_beefy_mmr::Pallet<T>,
>()
.expect("pallet_beefy_mmr must be part of dancelight before this migration");
move_pallet(Self::old_pallet_name().as_bytes(), new_name.as_bytes());
available_weight
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade(&self) -> Result<Vec<u8>, sp_runtime::DispatchError> {
Ok(vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(&self, _state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
Ok(())
}
}

impl<T> MigrateMMRLeafPallet<T> {
pub fn old_pallet_name() -> &'static str {
"MMRLeaf"
}
}

pub struct DancelightMigrations<Runtime>(PhantomData<Runtime>);

impl<Runtime> GetMigrations for DancelightMigrations<Runtime>
Expand All @@ -1034,18 +1094,22 @@ where
Runtime: pallet_session::Config<
ValidatorId = <Runtime as pallet_external_validators::Config>::ValidatorId,
>,
Runtime: pallet_external_validator_slashes::Config,
{
fn get_migrations() -> Vec<Box<dyn Migration>> {
let migrate_mmr_leaf_pallet = MigrateMMRLeafPallet::<Runtime>(Default::default());
let migrate_external_validators =
ExternalValidatorsInitialMigration::<Runtime>(Default::default());
let migrate_config_full_rotation_mode =
MigrateConfigurationAddFullRotationMode::<Runtime>(Default::default());

let external_validator_slashes_bonded_eras_timestamp =
BondedErasTimestampMigration::<Runtime>(Default::default());

vec![
Box::new(migrate_mmr_leaf_pallet),
Box::new(migrate_external_validators),
// Applied in runtime 1000
//Box::new(migrate_mmr_leaf_pallet),
// Applied in runtime 900
//Box::new(migrate_external_validators),
Box::new(migrate_config_full_rotation_mode),
Box::new(external_validator_slashes_bonded_eras_timestamp),
]
}
}
4 changes: 2 additions & 2 deletions chains/orchestrator-relays/runtime/dancelight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ impl pallet_external_validators_rewards::Config for Runtime {
// Will likely be through InflationRewards.

type EraInflationProvider = ExternalRewardsEraInflationProvider;
type TimestampProvider = TimestampProvider;
type ExternalIndexProvider = ExternalValidators;
type GetWhitelistedValidators = GetWhitelistedValidators;
type Hashing = Keccak256;
type ValidateMessage = tp_bridge::MessageValidator<Runtime>;
Expand All @@ -1482,7 +1482,7 @@ impl pallet_external_validator_slashes::Config for Runtime {
type InvulnerablesProvider = ExternalValidators;
type ValidateMessage = tp_bridge::MessageValidator<Runtime>;
type OutboundQueue = tp_bridge::CustomSendMessage<Runtime, GetAggregateMessageOriginTanssi>;
type TimestampProvider = TimestampProvider;
type ExternalIndexProvider = ExternalValidators;
type QueuedSlashesProcessedPerBlock = ConstU32<10>;
type WeightInfo = weights::pallet_external_validator_slashes::SubstrateWeight<Runtime>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn receive_msg_from_eth_validators_are_updated() {
magic_bytes: MAGIC_BYTES,
message: SymbioticMessage::V1(InboundCommand::<Runtime>::ReceiveValidators {
validators: payload_validators.clone(),
timestamp: 0u64,
external_index: 0u64,
}),
};

Expand Down
Loading

0 comments on commit 8ca9120

Please sign in to comment.