Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kiz e2e staking #7853

Merged
merged 7 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl<T: SendXcm> rc_client::SendToRelayChain for XcmToRelayChain<T> {
check_origin: None,
},
Instruction::Transact {
origin_kind: OriginKind::Superuser, // TODO: double check
origin_kind: OriginKind::Native,
fallback_max_weight: None,
call: RelayChainRuntimePallets::AhClient(AhClientCalls::ValidatorSet(report)).encode().into(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rpc_port = 9944
name = "bob"
validator = true
rpc_port = 9955
args = [ "-lruntime::system=debug,runtime::session=trace,runtime::staking::ah-client=trace"]
args = [ "-lruntime::system=debug,runtime::session=trace,runtime::staking::ah-client=trace,xcm=trace"]

[[parachains]]
id = 1100
Expand Down
2 changes: 1 addition & 1 deletion polkadot/parachain/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub struct BlockData(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec
Ord,
PartialEq,
PartialOrd,
RuntimeDebug,
Debug,
serde::Serialize,
serde::Deserialize,
TypeInfo,
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/parachains/src/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub mod pallet {
Encode,
Decode,
DecodeWithMemTracking,
sp_core::RuntimeDebug,
Debug,
scale_info::TypeInfo,
MaxEncodedLen,
)]
Expand Down
16 changes: 15 additions & 1 deletion polkadot/runtime/westend-next/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,24 @@ impl<T: SendXcm, AssetHubId: Get<u32>> XcmToAssetHub<T, AssetHubId> {
}
}

// TODO: is this not equal to `EnsureXcm<Equals<AssetHubNextLocation>>`?
pub struct EnsureAssetHub;
impl frame_support::traits::EnsureOrigin<RuntimeOrigin> for EnsureAssetHub {
type Success = ();
fn try_origin(o: RuntimeOrigin) -> Result<Self::Success, RuntimeOrigin> {
match <RuntimeOrigin as Into<Result<parachains_origin::Origin, RuntimeOrigin>>>::into(o.clone()) {
Ok(parachains_origin::Origin::Parachain(id)) if id == 1100.into() => Ok(()),
_ => Err(o)
}
}
}

impl pallet_staking_ah_client::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AssetHubOrigin = frame_support::traits::EitherOfDiverse<
EnsureRoot<AccountId>,
EnsureXcm<Equals<AssetHubNextLocation>>,
EnsureAssetHub
// EnsureXcm<Equals<AssetHubNextLocation>>,
>;
type SendToAssetHub = XcmToAssetHub<crate::xcm_config::XcmRouter, AssetHubId>;
type MinimumValidatorSetSize = ConstU32<333>;
Expand Down
73 changes: 58 additions & 15 deletions substrate/frame/staking/ah-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ pub mod pallet {

#[pallet::config]
pub trait Config: frame_system::Config {
/// Overarching runtime event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// An origin type that ensures an incoming message is from asset hub.
type AssetHubOrigin: EnsureOrigin<Self::RuntimeOrigin>;

Expand Down Expand Up @@ -167,6 +170,20 @@ pub mod pallet {
MinimumValidatorSetSize,
}

#[pallet::event]
#[pallet::generate_deposit(fn deposit_event)]
pub enum Event<T: Config> {
/// A new validator set has been received.
ValidatorSetReceived {
id: u32,
new_validator_set_count: u32,
prune_up_to: u32,
leftover: bool,
},
/// We could not merge, and therefore dropped a buffered message.
ValidatorSetDropped,
}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
Expand All @@ -176,26 +193,52 @@ pub mod pallet {
report: rc_client::ValidatorSetReport<T::AccountId>,
) -> DispatchResult {
// Ensure the origin is one of Root or whatever is representing AssetHub.
T::AssetHubOrigin::ensure_origin_or_root(origin)?;
log!(info, "Received new validator set report {:?}", report);
let rc_client::ValidatorSetReport { id, leftover, mut new_validator_set, prune_up_to } =
report;
debug_assert!(!leftover);
T::AssetHubOrigin::ensure_origin_or_root(origin)?;

// TODO: buffer in `IncompleteValidatorSetReport` if incomplete, similar to how
// rc-client does it.
let maybe_new_validator_set_report = match IncompleteValidatorSetReport::<T>::take() {
Some(old) => old.merge(report.clone()),
None => Ok(report),
};

// ensure the validator set, deduplicated, is not too big.
new_validator_set.sort();
new_validator_set.dedup();
if let Err(_) = maybe_new_validator_set_report {
Self::deposit_event(Event::ValidatorSetDropped);
// note -- if we return error the storage ops are reverted, so we do this instead.
return Ok(());
}

ensure!(
new_validator_set.len() as u32 >= T::MinimumValidatorSetSize::get(),
Error::<T>::MinimumValidatorSetSize
);
let new_validator_set_report =
maybe_new_validator_set_report.expect("checked above; qed");

if new_validator_set_report.leftover {
// buffer it, and nothing further to do.
IncompleteValidatorSetReport::<T>::put(new_validator_set_report);
} else {
let rc_client::ValidatorSetReport {
id,
leftover,
mut new_validator_set,
prune_up_to,
} = new_validator_set_report;

// ensure the validator set, deduplicated, is not too big.
new_validator_set.sort();
new_validator_set.dedup();

ensure!(
new_validator_set.len() as u32 >= T::MinimumValidatorSetSize::get(),
Error::<T>::MinimumValidatorSetSize
);

// Save the validator set.
ValidatorSet::<T>::put((id, new_validator_set));
// Save the validator set.
Self::deposit_event(Event::ValidatorSetReceived {
id,
new_validator_set_count: new_validator_set.len() as u32,
prune_up_to,
leftover,
});
ValidatorSet::<T>::put((id, new_validator_set));
}

Ok(())
}
Expand Down
9 changes: 6 additions & 3 deletions substrate/frame/staking/ahm-test/src/ah/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use crate::shared;
use frame::testing_prelude::*;
use frame_election_provider_support::{
bounds::{ElectionBounds, ElectionBoundsBuilder},
ElectionProvider, SequentialPhragmen,
SequentialPhragmen,
};
use frame_support::sp_runtime::testing::TestXt;
use pallet_election_provider_multi_block as multi_block;
use pallet_staking::{ActiveEra, ActiveEraInfo};
use sp_staking::SessionIndex;

construct_runtime! {
Expand Down Expand Up @@ -206,6 +205,7 @@ impl multi_block::signed::Config for Runtime {

type Currency = Balances;

type EjectGraceRatio = ();
type BailoutGraceRatio = ();
type DepositBase = DepositBase;
type DepositPerPage = DepositPerPage;
Expand All @@ -223,6 +223,7 @@ parameter_types! {
}

impl pallet_staking::Config for Runtime {
type Filter = ();
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = RuntimeHoldReason;

Expand Down Expand Up @@ -269,6 +270,7 @@ impl pallet_staking::Config for Runtime {
}

impl pallet_staking_rc_client::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AHStakingInterface = Staking;
type SendToRelayChain = DeliverToRelay;
type RelayChainOrigin = EnsureRoot<AccountId>;
Expand Down Expand Up @@ -376,7 +378,8 @@ impl ExtBuilder {
let mut state: TestState = t.into();

state.execute_with(|| {
// todo: remove this
// so events can be deposited.
frame_system::Pallet::<Runtime>::set_block_number(1);
});

state
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/staking/ahm-test/src/ah/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pallet_staking_rc_client as rc_client;
fn on_receive_session_report() {
ExtBuilder::default().local_queue().build().execute_with(|| {
// GIVEN genesis state of ah
assert_eq!(System::block_number(), 0);
assert_eq!(System::block_number(), 1);
assert_eq!(CurrentPlannedSession::<T>::get(), 0);
assert_eq!(CurrentEra::<T>::get(), Some(0));
assert_eq!(pallet_staking::ErasStartSessionIndex::<T>::get(0), Some(0));
Expand Down
5 changes: 2 additions & 3 deletions substrate/frame/staking/ahm-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod shared;
#[cfg(test)]
mod tests {
use super::*;
use codec::Decode;
use frame::testing_prelude::*;
use pallet_election_provider_multi_block as multi_block;
use pallet_staking::{ActiveEra, ActiveEraInfo};
Expand All @@ -23,7 +22,7 @@ mod tests {

// initial state of ah
shared::in_ah(|| {
assert_eq!(frame_system::Pallet::<ah::Runtime>::block_number(), 0);
assert_eq!(frame_system::Pallet::<ah::Runtime>::block_number(), 1);
assert_eq!(pallet_staking::CurrentPlannedSession::<ah::Runtime>::get(), 0);
assert_eq!(pallet_staking::CurrentEra::<ah::Runtime>::get(), Some(0));
assert_eq!(
Expand Down Expand Up @@ -57,7 +56,7 @@ mod tests {

shared::in_ah(|| {
// ah's rc-client has also progressed some blocks, equal to 4 sessions
assert_eq!(frame_system::Pallet::<ah::Runtime>::block_number(), 120);
assert_eq!(frame_system::Pallet::<ah::Runtime>::block_number(), 121);
assert_eq!(pallet_staking::CurrentPlannedSession::<ah::Runtime>::get(), 5);
// election is ongoing, and has just started
assert!(matches!(
Expand Down
11 changes: 8 additions & 3 deletions substrate/frame/staking/ahm-test/src/rc/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use frame::{deps::sp_runtime::testing::UintAuthorityId, testing_prelude::*};
use pallet_staking::NullIdentity;
use pallet_staking_ah_client as ah_client;
use sp_staking::SessionIndex;

Expand Down Expand Up @@ -147,6 +146,7 @@ parameter_types! {
}

impl ah_client::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type SendToAssetHub = DeliverToAH;
type AssetHubOrigin = EnsureSigned<AccountId>;
type UnixTime = Timestamp;
Expand Down Expand Up @@ -217,7 +217,12 @@ impl ExtBuilder {

pub fn build(self) -> TestState {
let _ = sp_tracing::try_init_simple();
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
t.into()
let t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
let mut state: TestState = t.into();
state.execute_with(|| {
// so events can be deposited.
frame_system::Pallet::<Runtime>::set_block_number(1);
});
state
}
}
Loading
Loading