Skip to content

Commit f7b0396

Browse files
kianenigmaAnk4ngithub-actions[bot]
authored
[AHM/Staking/VMP] Paginated Offence Reports + Retries for Validator Set (#9619)
* Please see the full design do [here](https://docs.google.com/document/d/1l2COWct1f-gC8nM0tq7Xs8pBWeAP6pX0LociWC6enUg/edit?tab=t.0) * closes paritytech/srlabs_findings#520 This PR makes the following changes: #### Common * `SendToRelayChain` and `SendToAssetHub` traits now return a result, allowing the caller to know if the underlying XCM was sent or not. * Adds a number of testing facilities to `pallet-root-offences`, and `staking-async/papi-tests`. Both of which can be ignored in the review. #### Offences * `SendToAssetHub::relay_new_offence` is removed. Instead, we use the new `relay_new_offence_paged` which is a vector of self-contained offences, not requiring us to group offences per session in each message. * Offences are not sent immediately anymore. * Instead, they are stored in a paginated `OffenceSendQueue`. * `on-init`, we grab one page of this storage map, and sent it. #### Session Report * Session reports now also have a retry mechanism. * Upon each failure, we emit an `UnexpectedEvent` * If our retries run out and we still can't send the session report, we will emit a different `UnexpectedEvent`. We also retore the validator points that we meant to send, and merge them back, so that they are sent in the next session report. #### Validator Set * Similar to offences, they are not sent immediately anymore. * Instead, they are stored in a storage item, and are sent on subsequent on-inits. * A maximum retry count is added. ### Review notes As noted above, ignore all changes in * `staking-async/runtimes` * `staking-async/runtimes/papi-tests` * `root-offences` As they are only related to testing. --------- Co-authored-by: Ankan <[email protected]> Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent ae7177e commit f7b0396

40 files changed

Lines changed: 2729 additions & 970 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ workspace = true
1414
[dependencies]
1515
codec = { features = ["derive", "max-encoded-len"], workspace = true }
1616
hex-literal = { workspace = true, default-features = true }
17+
log = { workspace = true }
1718
scale-info = { features = ["derive"], workspace = true }
1819
serde_json = { features = ["alloc"], workspace = true }
1920
tracing = { workspace = true }
@@ -137,6 +138,7 @@ alloy-core = { workspace = true, features = ["sol-types"] }
137138
asset-test-utils = { workspace = true, default-features = true }
138139
pallet-revive-fixtures = { workspace = true, default-features = true }
139140
parachains-runtimes-test-utils = { workspace = true, default-features = true }
141+
sp-tracing = { workspace = true, default-features = true }
140142

141143
[build-dependencies]
142144
substrate-wasm-builder = { optional = true, workspace = true, default-features = true }
@@ -301,6 +303,7 @@ std = [
301303
"frame-system-rpc-runtime-api/std",
302304
"frame-system/std",
303305
"frame-try-runtime?/std",
306+
"log/std",
304307
"pallet-ah-ops/std",
305308
"pallet-asset-conversion-ops/std",
306309
"pallet-asset-conversion-tx-payment/std",

cumulus/parachains/runtimes/assets/asset-hub-westend/src/staking.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ impl pallet_staking_async_rc_client::Config for Runtime {
304304
type RelayChainOrigin = EnsureRoot<AccountId>;
305305
type AHStakingInterface = Staking;
306306
type SendToRelayChain = StakingXcmToRelayChain;
307+
type MaxValidatorSetRetries = ConstU32<64>;
307308
}
308309

309310
#[derive(Encode, Decode)]
@@ -350,13 +351,13 @@ pub struct StakingXcmToRelayChain;
350351

351352
impl rc_client::SendToRelayChain for StakingXcmToRelayChain {
352353
type AccountId = AccountId;
353-
fn validator_set(report: rc_client::ValidatorSetReport<Self::AccountId>) {
354+
fn validator_set(report: rc_client::ValidatorSetReport<Self::AccountId>) -> Result<(), ()> {
354355
rc_client::XCMSender::<
355356
xcm_config::XcmRouter,
356357
RelayLocation,
357358
rc_client::ValidatorSetReport<Self::AccountId>,
358359
ValidatorSetToXcm,
359-
>::split_then_send(report, Some(8));
360+
>::send(report)
360361
}
361362
}
362363

polkadot/runtime/westend/src/lib.rs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ enum RcClientCalls<AccountId> {
793793
#[codec(index = 0)]
794794
RelaySessionReport(rc_client::SessionReport<AccountId>),
795795
#[codec(index = 1)]
796-
RelayNewOffence(SessionIndex, Vec<rc_client::Offence<AccountId>>),
796+
RelayNewOffencePaged(Vec<(SessionIndex, rc_client::Offence<AccountId>)>),
797797
}
798798

799799
pub struct AssetHubLocation;
@@ -842,42 +842,53 @@ impl sp_runtime::traits::Convert<rc_client::SessionReport<AccountId>, Xcm<()>>
842842
}
843843
}
844844

845-
pub struct StakingXcmToAssetHub;
846-
impl ah_client::SendToAssetHub for StakingXcmToAssetHub {
847-
type AccountId = AccountId;
848-
849-
fn relay_session_report(session_report: rc_client::SessionReport<Self::AccountId>) {
850-
rc_client::XCMSender::<
851-
xcm_config::XcmRouter,
852-
AssetHubLocation,
853-
rc_client::SessionReport<AccountId>,
854-
SessionReportToXcm,
855-
>::split_then_send(session_report, Some(8));
856-
}
857-
858-
fn relay_new_offence(
859-
session_index: SessionIndex,
860-
offences: Vec<rc_client::Offence<Self::AccountId>>,
861-
) {
862-
let message = Xcm(vec![
845+
pub struct QueuedOffenceToXcm;
846+
impl sp_runtime::traits::Convert<Vec<ah_client::QueuedOffenceOf<Runtime>>, Xcm<()>>
847+
for QueuedOffenceToXcm
848+
{
849+
fn convert(offences: Vec<ah_client::QueuedOffenceOf<Runtime>>) -> Xcm<()> {
850+
Xcm(vec![
863851
Instruction::UnpaidExecution {
864852
weight_limit: WeightLimit::Unlimited,
865853
check_origin: None,
866854
},
867855
Instruction::Transact {
868856
origin_kind: OriginKind::Superuser,
869857
fallback_max_weight: None,
870-
call: AssetHubRuntimePallets::RcClient(RcClientCalls::RelayNewOffence(
871-
session_index,
858+
call: AssetHubRuntimePallets::RcClient(RcClientCalls::RelayNewOffencePaged(
872859
offences,
873860
))
874861
.encode()
875862
.into(),
876863
},
877-
]);
878-
if let Err(err) = send_xcm::<xcm_config::XcmRouter>(AssetHubLocation::get(), message) {
879-
log::error!(target: "runtime::ah-client", "Failed to send relay offence message: {:?}", err);
880-
}
864+
])
865+
}
866+
}
867+
868+
pub struct StakingXcmToAssetHub;
869+
impl ah_client::SendToAssetHub for StakingXcmToAssetHub {
870+
type AccountId = AccountId;
871+
872+
fn relay_session_report(
873+
session_report: rc_client::SessionReport<Self::AccountId>,
874+
) -> Result<(), ()> {
875+
rc_client::XCMSender::<
876+
xcm_config::XcmRouter,
877+
AssetHubLocation,
878+
rc_client::SessionReport<AccountId>,
879+
SessionReportToXcm,
880+
>::send(session_report)
881+
}
882+
883+
fn relay_new_offence_paged(
884+
offences: Vec<ah_client::QueuedOffenceOf<Runtime>>,
885+
) -> Result<(), ()> {
886+
rc_client::XCMSender::<
887+
xcm_config::XcmRouter,
888+
AssetHubLocation,
889+
Vec<ah_client::QueuedOffenceOf<Runtime>>,
890+
QueuedOffenceToXcm,
891+
>::send(offences)
881892
}
882893
}
883894

@@ -893,7 +904,8 @@ impl ah_client::Config for Runtime {
893904
type PointsPerBlock = ConstU32<20>;
894905
type MaxOffenceBatchSize = ConstU32<50>;
895906
type Fallback = Staking;
896-
type WeightInfo = ah_client::weights::SubstrateWeight<Runtime>;
907+
type MaximumValidatorsWithPoints = ConstU32<{ MaxActiveValidators::get() * 4 }>;
908+
type MaxSessionReportRetries = ConstU32<5>;
897909
}
898910

899911
impl pallet_fast_unstake::Config for Runtime {
@@ -2143,7 +2155,6 @@ mod benches {
21432155
[pallet_scheduler, Scheduler]
21442156
[pallet_session, SessionBench::<Runtime>]
21452157
[pallet_staking, Staking]
2146-
[pallet_staking_async_ah_client, StakingAhClient]
21472158
[pallet_sudo, Sudo]
21482159
[frame_system, SystemBench::<Runtime>]
21492160
[frame_system_extensions, SystemExtensionsBench::<Runtime>]

polkadot/runtime/westend/src/weights/pallet_staking_async_ah_client.rs

Lines changed: 0 additions & 78 deletions
This file was deleted.

prdoc/pr_9619.prdoc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
title: '[AHM/Staking/VMP] Paginated Offence Reports + Retries for Validator Set'
2+
doc:
3+
- audience: Runtime Dev
4+
description: |-
5+
This PR makes the following changes:
6+
7+
#### Common
8+
9+
* `SendToRelayChain` and `SendToAssetHub` traits now return a result, allowing the caller to know if the underlying XCM was sent or not.
10+
11+
#### Offences
12+
13+
* `SendToAssetHub::relay_new_offence` is removed. Instead, we use the new `relay_new_offence_paged` which is a vector of self-contained offences, not requiring us to group offences per session in each message.
14+
* Offences are not sent immediately anymore.
15+
* Instead, they are stored in a paginated `OffenceSendQueue`.
16+
* `on-init`, we grab one page of this storage map, and sent it.
17+
18+
#### Session Report
19+
* Session reports now also have a retry mechanism.
20+
* Upon each failure, we emit an `UnexpectedEvent`
21+
* If our retries run out and we still can't send the session report, we will emit a different `UnexpectedEvent`. We also retore the validator points that we meant to send, and merge them back, so that they are sent in the next session report.
22+
23+
#### Validator Set
24+
* Similar to offences, they are not sent immediately anymore.
25+
* Instead, they are stored in a storage item, and are sent on subsequent on-inits.
26+
* A maximum retry count is added.
27+
crates:
28+
- name: pallet-offences
29+
bump: patch
30+
- name: pallet-root-offences
31+
bump: major
32+
- name: pallet-staking-async-ah-client
33+
bump: major
34+
- name: pallet-staking-async
35+
bump: patch
36+
- name: westend-runtime
37+
bump: minor
38+
- name: pallet-staking-async-rc-client
39+
bump: major
40+
- name: pallet-staking-async-rc-runtime-constants
41+
bump: patch
42+
- name: asset-hub-westend-runtime
43+
bump: minor
44+
- name: pallet-election-provider-multi-block
45+
bump: patch

substrate/frame/election-provider-multi-block/src/types.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ impl<T: crate::Config> Phase<T> {
303303

304304
fn are_we_done() -> Self {
305305
let query = T::AreWeDone::get();
306-
log!(debug, "Are we done? {:?}", query);
307306
query
308307
}
309308

0 commit comments

Comments
 (0)