Skip to content

Commit be7f8b3

Browse files
authored
change: Automatic key registration (#1026)
1 parent 6efd6dc commit be7f8b3

File tree

25 files changed

+621
-607
lines changed

25 files changed

+621
-607
lines changed

Cargo.lock

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

demo/node/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ time-source = { workspace = true, features = ["mock"] }
135135
assert_cmd = { workspace = true }
136136
pallet-sidechain-rpc = { workspace = true }
137137
authority-selection-inherents = { workspace = true, features = ["mock"] }
138+
pretty_assertions = { workspace = true }
138139

139140
[features]
140141
default = ["sidechain-domain/std"]

demo/node/src/chain_spec.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use partner_chains_cli::CreateChainSpecConfig;
22
use partner_chains_demo_runtime::{
3-
AccountId, CrossChainPublic, Signature, WASM_BINARY, opaque::SessionKeys,
3+
AccountId, CrossChainPublic, SessionConfig, Signature, WASM_BINARY, opaque::SessionKeys,
44
};
55
use sc_service::ChainType;
66
use sidechain_slots::SlotsPerEpoch;
@@ -46,9 +46,21 @@ pub fn pc_create_chain_spec(config: &CreateChainSpecConfig<SessionKeys>) -> serd
4646
grandpa: partner_chains_demo_runtime::GrandpaConfig::default(),
4747
sudo: partner_chains_demo_runtime::SudoConfig::default(),
4848
transaction_payment: Default::default(),
49-
session: config.pallet_partner_chains_session_config(),
49+
session: SessionConfig {
50+
keys: config
51+
.initial_permissioned_candidates_parsed
52+
.iter()
53+
.map(|authority_keys| {
54+
(
55+
authority_keys.account_id_32().into(),
56+
authority_keys.account_id_32().into(),
57+
authority_keys.keys.clone(),
58+
)
59+
})
60+
.collect(),
61+
non_authority_keys: vec![],
62+
},
5063
sidechain: config.pallet_sidechain_config(SlotsPerEpoch::default()),
51-
pallet_session: Default::default(),
5264
session_committee_management: config.pallet_session_validator_management_config(),
5365
governed_map: config.governed_map_config(),
5466
test_helper_pallet: partner_chains_demo_runtime::TestHelperPalletConfig {

demo/node/src/staging.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,19 @@ pub fn staging_genesis(
131131
},
132132
transaction_payment: Default::default(),
133133
session: SessionConfig {
134-
initial_validators: initial_authorities
134+
keys: initial_authorities
135135
.iter()
136136
.map(|authority_keys| {
137-
(authority_keys.cross_chain.clone().into(), authority_keys.session.clone())
137+
(
138+
authority_keys.cross_chain.clone().into(),
139+
authority_keys.cross_chain.clone().into(),
140+
authority_keys.session.clone(),
141+
)
138142
})
139143
.collect(),
144+
non_authority_keys: Default::default(),
140145
},
141146
sidechain: SidechainConfig { genesis_utxo, ..Default::default() },
142-
pallet_session: Default::default(),
143147
session_committee_management: SessionCommitteeManagementConfig {
144148
initial_authorities: initial_authorities
145149
.into_iter()

demo/node/src/template_chain_spec.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ pub fn chain_spec() -> Result<ChainSpec, envy::Error> {
2727
},
2828
transaction_payment: Default::default(),
2929
session: SessionConfig {
30-
// Initial validators are meant to be updated in the chain spec file, so it is empty here.
31-
initial_validators: vec![],
30+
// Keys are meant to be updated in the chain spec file, so it is empty here.
31+
keys: vec![],
32+
non_authority_keys: Default::default(),
3233
},
3334
sidechain: SidechainConfig { genesis_utxo, ..Default::default() },
34-
pallet_session: Default::default(),
3535
session_committee_management: SessionCommitteeManagementConfig {
3636
// Same as SessionConfig
3737
initial_authorities: vec![],

demo/node/src/testnet.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,23 @@ pub fn testnet_genesis(
182182
},
183183
transaction_payment: Default::default(),
184184
session: SessionConfig {
185-
initial_validators: initial_authorities
185+
keys: initial_authorities
186186
.iter()
187187
.map(|authority_keys| {
188-
(authority_keys.cross_chain.clone().into(), authority_keys.session.clone())
188+
(
189+
authority_keys.cross_chain.clone().into(),
190+
authority_keys.cross_chain.clone().into(),
191+
authority_keys.session.clone(),
192+
)
189193
})
190194
.collect(),
195+
non_authority_keys: Default::default(),
191196
},
192197
sidechain: SidechainConfig {
193198
genesis_utxo,
194199
slots_per_epoch: SlotsPerEpoch::read_from_env()?,
195200
..Default::default()
196201
},
197-
pallet_session: Default::default(),
198202
session_committee_management: SessionCommitteeManagementConfig {
199203
initial_authorities: initial_authorities
200204
.into_iter()

demo/node/src/tests/chain_spec.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::chain_spec::pc_create_chain_spec;
22
use partner_chains_cli::{CreateChainSpecConfig, ParsedPermissionedCandidatesKeys};
33
use partner_chains_demo_runtime::opaque::SessionKeys;
4+
use pretty_assertions::assert_eq;
45
use sidechain_domain::{AssetName, MainchainAddress, PolicyId, UtxoId};
56
use sp_core::{ecdsa, ed25519, sr25519};
67
use std::str::FromStr;
@@ -92,15 +93,17 @@ fn pc_create_chain_spec_test() {
9293
assert_eq!(
9394
config_obj.get("session").unwrap(),
9495
&serde_json::json!({
95-
"initialValidators": [
96+
"keys": [
9697
[
98+
"5CUUBrDiVEKVa655Bsm8sYc5An5Jqi52PetteUpMY2JFbuRF",
9799
"5CUUBrDiVEKVa655Bsm8sYc5An5Jqi52PetteUpMY2JFbuRF",
98100
{
99101
"aura": "5CLW1ZaVdZdj6bf7nmvJfba6GbvxueXzV6Dw5fnPaKTiSARx",
100102
"grandpa": "5CMpMdu3LbHuj2TqX4RAUzXCHCqmNj8Fce43wAbcqSFZuNfp"
101103
}
102104
]
103-
]
105+
],
106+
"nonAuthorityKeys": []
104107
})
105108
);
106109
}

demo/runtime/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ sp-block-production-log = { workspace = true }
7575
pallet-block-production-log = { workspace = true }
7676
sp-sidechain = { workspace = true }
7777
pallet-sidechain = { workspace = true }
78-
pallet-session-validator-management = { workspace = true, features = [
79-
"pallet-session-compat",
80-
] }
78+
pallet-session-validator-management = { workspace = true }
8179
sp-session-validator-management = { workspace = true, features = ["serde"] }
8280
pallet-session-validator-management-benchmarking = { workspace = true, optional = true }
8381
sidechain-domain = { workspace = true, features = ["serde"] }

demo/runtime/src/genesis_config_presets.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ fn testnet_genesis(
5858
transaction_payment: Default::default(),
5959
sidechain: Default::default(),
6060
session_committee_management: Default::default(),
61-
pallet_session: Default::default(),
6261
session: Default::default(),
6362
governed_map: GovernedMapConfig {
6463
main_chain_scripts: Some(sp_governed_map::MainChainScriptsV1::default()),

demo/runtime/src/lib.rs

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use frame_system::EnsureRoot;
2727
use opaque::SessionKeys;
2828
use pallet_block_producer_metadata;
2929
use pallet_grandpa::AuthorityId as GrandpaId;
30-
use pallet_session_validator_management::session_manager::ValidatorManagementSessionManager;
30+
use pallet_session_validator_management::pallet_session_support::PalletSessionSupport;
3131
use pallet_transaction_payment::{ConstFeeMultiplier, FungibleAdapter, Multiplier};
3232
use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
3333
use scale_info::TypeInfo;
@@ -47,6 +47,7 @@ use sp_core::{OpaqueMetadata, crypto::KeyTypeId};
4747
use sp_governed_map::MainChainScriptsV1;
4848
use sp_inherents::InherentIdentifier;
4949
use sp_partner_chains_bridge::{BridgeDataCheckpoint, MainChainScripts as BridgeMainChainScripts};
50+
use sp_runtime::traits::ConvertInto;
5051
use sp_runtime::{
5152
ApplyExtrinsicResult, MultiSignature, Perbill, generic, impl_opaque_keys,
5253
traits::{
@@ -304,7 +305,19 @@ impl pallet_aura::Config for Runtime {
304305
type SlotDuration = ConstU64<SLOT_DURATION>;
305306
}
306307

307-
pallet_partner_chains_session::impl_pallet_session_config!(Runtime);
308+
impl pallet_session::Config for Runtime {
309+
type RuntimeEvent = RuntimeEvent;
310+
type ValidatorId = AccountId;
311+
type ValidatorIdOf = ConvertInto;
312+
type ShouldEndSession = PalletSessionSupport<Runtime>;
313+
type NextSessionRotation = ();
314+
type SessionManager = PalletSessionSupport<Runtime>;
315+
type SessionHandler = <SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
316+
type Keys = SessionKeys;
317+
type DisablingStrategy = pallet_session::disabling::UpToLimitWithReEnablingDisablingStrategy;
318+
319+
type WeightInfo = pallet_session::weights::SubstrateWeight<Runtime>;
320+
}
308321

309322
impl pallet_grandpa::Config for Runtime {
310323
type RuntimeEvent = RuntimeEvent;
@@ -368,15 +381,6 @@ impl pallet_sudo::Config for Runtime {
368381
type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
369382
}
370383

371-
impl pallet_partner_chains_session::Config for Runtime {
372-
type ValidatorId = <Self as frame_system::Config>::AccountId;
373-
type ShouldEndSession = ValidatorManagementSessionManager<Runtime>;
374-
type NextSessionRotation = ();
375-
type SessionManager = ValidatorManagementSessionManager<Runtime>;
376-
type SessionHandler = <opaque::SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
377-
type Keys = opaque::SessionKeys;
378-
}
379-
380384
parameter_types! {
381385
pub const MaxValidators: u32 = 1024;
382386
}
@@ -708,13 +712,11 @@ construct_runtime!(
708712
BlockProducerMetadata: pallet_block_producer_metadata,
709713
BlockProductionLog: pallet_block_production_log,
710714
BlockParticipation: pallet_block_participation,
711-
// pallet_grandpa reads pallet_session::pallet::CurrentIndex storage.
712-
// Only stub implementation of pallet_session should be wired.
713-
// Partner Chains session_manager ValidatorManagementSessionManager writes to pallet_session::pallet::CurrentIndex.
714-
// ValidatorManagementSessionManager is wired in by pallet_partner_chains_session.
715-
PalletSession: pallet_session,
716-
// The order matters!! pallet_partner_chains_session needs to come last for correct initialization order
717-
Session: pallet_partner_chains_session,
715+
// We exclude pallet's extrinsics to make them unavailable to users to submit.
716+
// This is to ensure that registrations on Cardano coming in throught the
717+
// `SessionCommitteeManagement` pallet are the only source of truth about keys
718+
// and accounts of block producers.
719+
Session: pallet_session exclude_parts { Call },
718720
GovernedMap: pallet_governed_map,
719721
Bridge: pallet_partner_chains_bridge,
720722
TestHelperPallet: crate::test_helper_pallet,
@@ -910,7 +912,6 @@ impl_runtime_apis! {
910912
}
911913
}
912914

913-
914915
impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Nonce> for Runtime {
915916
fn account_nonce(account: AccountId) -> Nonce {
916917
System::account_nonce(account)
@@ -1160,6 +1161,7 @@ mod tests {
11601161
inherent::ProvideInherent,
11611162
traits::{UnfilteredDispatchable, WhitelistedStorageKeys},
11621163
};
1164+
use pretty_assertions::assert_eq;
11631165
use sp_core::{Pair, hexdisplay::HexDisplay};
11641166
use sp_inherents::InherentData;
11651167
use std::collections::HashSet;
@@ -1199,6 +1201,8 @@ mod tests {
11991201
new_test_ext().execute_with(|| {
12001202
// Needs to be run to initialize first slot and epoch numbers;
12011203
advance_block();
1204+
1205+
// Scheduled committee goes into effect after a 2-epoch delay
12021206
set_committee_through_inherent_data(&[alice()]);
12031207
until_epoch_after_finalizing(1, &|| {
12041208
assert_current_epoch!(0);
@@ -1210,30 +1214,37 @@ mod tests {
12101214
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
12111215
assert_current_epoch!(1);
12121216
assert_grandpa_weights();
1213-
assert_grandpa_authorities!([alice()]);
1217+
assert_grandpa_authorities!([alice(), bob()]);
12141218
});
1215-
1219+
set_committee_through_inherent_data(&[alice()]);
12161220
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
12171221
assert_current_epoch!(2);
12181222
assert_grandpa_weights();
1219-
assert_grandpa_authorities!([bob()]);
1223+
assert_grandpa_authorities!([alice()]);
12201224
});
1221-
1222-
// Authorities can be set as late as in the first block of new epoch, but it makes session last 1 block longer
1223-
set_committee_through_inherent_data(&[alice()]);
1224-
advance_block();
1225-
assert_current_epoch!(3);
1226-
assert_grandpa_authorities!([bob()]);
12271225
set_committee_through_inherent_data(&[alice(), bob()]);
1228-
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH - 1, &|| {
1226+
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
12291227
assert_current_epoch!(3);
12301228
assert_grandpa_weights();
1229+
assert_grandpa_authorities!([bob()]);
1230+
});
1231+
set_committee_through_inherent_data(&[bob(), alice()]);
1232+
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
1233+
assert_current_epoch!(4);
1234+
assert_grandpa_weights();
12311235
assert_grandpa_authorities!([alice()]);
12321236
});
1237+
set_committee_through_inherent_data(&[alice()]);
1238+
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
1239+
assert_current_epoch!(5);
1240+
assert_grandpa_weights();
1241+
assert_grandpa_authorities!([alice(), bob()]);
1242+
});
12331243

1244+
// When there's no new committees being scheduled, the last committee stays in power
12341245
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH * 3, &|| {
12351246
assert_grandpa_weights();
1236-
assert_grandpa_authorities!([alice(), bob()]);
1247+
assert_grandpa_authorities!([bob(), alice()]);
12371248
});
12381249
});
12391250

@@ -1248,33 +1259,46 @@ mod tests {
12481259
#[test]
12491260
fn check_aura_authorities_rotation() {
12501261
new_test_ext().execute_with(|| {
1262+
// Needs to be run to initialize first slot and epoch numbers;
12511263
advance_block();
1264+
1265+
// Scheduled committee goes into effect after a 2-epoch delay
12521266
set_committee_through_inherent_data(&[alice()]);
1253-
until_epoch(1, &|| {
1267+
until_epoch_after_finalizing(1, &|| {
12541268
assert_current_epoch!(0);
12551269
assert_aura_authorities!([alice(), bob()]);
12561270
});
12571271

1258-
for_next_n_blocks(SLOTS_PER_EPOCH, &|| {
1272+
set_committee_through_inherent_data(&[bob()]);
1273+
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
12591274
assert_current_epoch!(1);
1275+
assert_aura_authorities!([alice(), bob()]);
1276+
});
1277+
set_committee_through_inherent_data(&[alice()]);
1278+
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
1279+
assert_current_epoch!(2);
12601280
assert_aura_authorities!([alice()]);
12611281
});
1262-
1263-
// Authorities can be set as late as in the first block of new epoch, but it makes session last 1 block longer
1264-
set_committee_through_inherent_data(&[bob()]);
1265-
assert_current_epoch!(2);
1266-
assert_aura_authorities!([alice()]);
1267-
advance_block();
12681282
set_committee_through_inherent_data(&[alice(), bob()]);
1269-
for_next_n_blocks(SLOTS_PER_EPOCH - 1, &|| {
1270-
assert_current_epoch!(2);
1283+
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
1284+
assert_current_epoch!(3);
12711285
assert_aura_authorities!([bob()]);
12721286
});
1273-
1274-
set_committee_through_inherent_data(&[alice(), bob()]);
1275-
for_next_n_blocks(SLOTS_PER_EPOCH * 3, &|| {
1287+
set_committee_through_inherent_data(&[bob(), alice()]);
1288+
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
1289+
assert_current_epoch!(4);
1290+
assert_aura_authorities!([alice()]);
1291+
});
1292+
set_committee_through_inherent_data(&[alice()]);
1293+
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH, &|| {
1294+
assert_current_epoch!(5);
12761295
assert_aura_authorities!([alice(), bob()]);
12771296
});
1297+
1298+
// When there's no new committees being scheduled, the last committee stays in power
1299+
for_next_n_blocks_after_finalizing(SLOTS_PER_EPOCH * 3, &|| {
1300+
assert_aura_authorities!([bob(), alice()]);
1301+
});
12781302
});
12791303
}
12801304

0 commit comments

Comments
 (0)