Skip to content

Commit a8f1972

Browse files
authored
feat: integrate parachain collator pallet with session and cumulus aura (#113)
* chore(deps): update substrate cumulus polkadot deps * chore(deps): use latest polkadot release branch * fix: breaking changes from latest relase branch * use chainlink master * update polkadot deps * use compact Cargo.toml fromat everywhere * update to master * update tests * add sp-runtime as optional dependency * update chainlink * switch to polkadot v0.9.4 branches * update runtime * revert new pallet balances types * revert config name change * add license header * revert naming conventions * feat: add collator selection pallet * configure session authorship collator selection * configure cumulus aura * chore: rustfmt * use cumulus collator selection * use correct keys
1 parent a390b69 commit a8f1972

File tree

6 files changed

+150
-23
lines changed

6 files changed

+150
-23
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ polkadot-node-core-pvf = { git = "https://github.com/paritytech//polkadot", rev
170170
cumulus-primitives-core = { git = "https://github.com/paritytech//cumulus", rev = "08b7bcfe21b2ce2a26dcdaa664bf8c016f7c93ae" }
171171
cumulus-pallet-parachain-system = { git = "https://github.com/paritytech//cumulus", rev = "08b7bcfe21b2ce2a26dcdaa664bf8c016f7c93ae" }
172172
cumulus-pallet-aura-ext = { git = "https://github.com/paritytech//cumulus", rev = "08b7bcfe21b2ce2a26dcdaa664bf8c016f7c93ae" }
173+
pallet-collator-selection = { git = "https://github.com/paritytech//cumulus", rev = "08b7bcfe21b2ce2a26dcdaa664bf8c016f7c93ae" }
173174
parachain-info = { git = "https://github.com/paritytech//cumulus", rev = "08b7bcfe21b2ce2a26dcdaa664bf8c016f7c93ae" }
174175
cumulus-client-cli = { git = "https://github.com/paritytech//cumulus", rev = "08b7bcfe21b2ce2a26dcdaa664bf8c016f7c93ae" }
175176
cumulus-client-collator = { git = "https://github.com/paritytech//cumulus", rev = "08b7bcfe21b2ce2a26dcdaa664bf8c016f7c93ae" }

node/src/chain_spec.rs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
77
use sc_service::ChainType;
88
use serde::{Deserialize, Serialize};
99
use sp_core::{sr25519, Pair, Public};
10-
use sp_runtime::traits::{IdentifyAccount, Verify};
10+
use sp_runtime::traits::{IdentifyAccount, Verify, Zero};
1111

1212
/// Specialized `ChainSpec` for the normal parachain runtime.
1313
pub type ChainSpec = sc_service::GenericChainSpec<parachain_runtime::GenesisConfig, Extensions>;
@@ -19,6 +19,13 @@ pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Pu
1919
.public()
2020
}
2121

22+
/// Generate collator keys from seed.
23+
///
24+
/// This function's return type must always match the session keys of the chain in tuple format.
25+
pub fn get_collator_keys_from_seed(seed: &str) -> AuraId {
26+
get_from_seed::<AuraId>(seed)
27+
}
28+
2229
/// The extensions for the [`ChainSpec`].
2330
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]
2431
#[serde(deny_unknown_fields)]
@@ -56,10 +63,11 @@ pub fn pint_development_config(id: ParaId) -> ChainSpec {
5663
move || {
5764
pint_testnet_genesis(
5865
get_account_id_from_seed::<sr25519::Public>("Alice"),
59-
vec![
60-
get_from_seed::<AuraId>("Alice"),
61-
get_from_seed::<AuraId>("Bob"),
62-
],
66+
// initial collators.
67+
vec![(
68+
get_account_id_from_seed::<sr25519::Public>("Alice"),
69+
get_collator_keys_from_seed("Alice"),
70+
)],
6371
vec![
6472
get_account_id_from_seed::<sr25519::Public>("Alice"),
6573
get_account_id_from_seed::<sr25519::Public>("Bob"),
@@ -96,9 +104,16 @@ pub fn pint_local_config(id: ParaId) -> ChainSpec {
96104
move || {
97105
pint_testnet_genesis(
98106
get_account_id_from_seed::<sr25519::Public>("Alice"),
107+
// initial collators.
99108
vec![
100-
get_from_seed::<AuraId>("Alice"),
101-
get_from_seed::<AuraId>("Bob"),
109+
(
110+
get_account_id_from_seed::<sr25519::Public>("Alice"),
111+
get_collator_keys_from_seed("Alice"),
112+
),
113+
(
114+
get_account_id_from_seed::<sr25519::Public>("Bob"),
115+
get_collator_keys_from_seed("Bob"),
116+
),
102117
],
103118
vec![
104119
get_account_id_from_seed::<sr25519::Public>("Alice"),
@@ -136,7 +151,7 @@ pub fn pint_local_config(id: ParaId) -> ChainSpec {
136151

137152
fn pint_testnet_genesis(
138153
root_key: AccountId,
139-
_initial_authorities: Vec<AuraId>,
154+
initial_authorities: Vec<(AccountId, AuraId)>,
140155
endowed_accounts: Vec<AccountId>,
141156
council_members: Vec<AccountId>,
142157
id: ParaId,
@@ -161,6 +176,28 @@ fn pint_testnet_genesis(
161176
},
162177
pallet_sudo: parachain_runtime::SudoConfig { key: root_key },
163178
parachain_info: parachain_runtime::ParachainInfoConfig { parachain_id: id },
179+
pallet_collator_selection: parachain_runtime::CollatorSelectionConfig {
180+
invulnerables: initial_authorities
181+
.iter()
182+
.cloned()
183+
.map(|(acc, _)| acc)
184+
.collect(),
185+
candidacy_bond: Zero::zero(),
186+
..Default::default()
187+
},
188+
pallet_session: parachain_runtime::SessionConfig {
189+
keys: initial_authorities
190+
.iter()
191+
.cloned()
192+
.map(|(acc, aura)| {
193+
(
194+
acc.clone(), // account id
195+
acc, // validator id
196+
parachain_runtime::opaque::SessionKeys { aura }, // session keys
197+
)
198+
})
199+
.collect(),
200+
},
164201
// no need to pass anything to aura, in fact it will panic if we do. Session will take care
165202
// of this.
166203
pallet_aura: Default::default(),

node/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub struct ExportGenesisStateCommand {
5454
/// Id of the parachain this state is for.
5555
///
5656
/// Default: 100
57-
#[structopt(long, conflicts_with = "chain")]
57+
#[structopt(long)]
5858
pub parachain_id: Option<u32>,
5959

6060
/// Write output in binary. Default is to write in hex.

runtime/Cargo.toml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkad
3232
sp-session = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
3333
sp-std = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
3434
sp-transaction-pool = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
35+
frame-try-runtime = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false, optional = true }
3536
sp-version = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
3637

3738
## Substrate FRAME Dependencies
@@ -44,14 +45,17 @@ frame-system-rpc-runtime-api = { git = 'https://github.com/paritytech/substrate'
4445

4546
## Substrate Pallet Dependencies
4647
pallet-aura = { git = 'https://github.com/paritytech/substrate', default-features = false, branch = 'master' }
48+
pallet-authorship = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false}
4749
pallet-balances = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
4850
pallet-randomness-collective-flip = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
51+
pallet-session = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false, features = ['historical'] }
4952
pallet-sudo = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
5053
pallet-timestamp = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
5154
pallet-transaction-payment = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
5255
pallet-transaction-payment-rpc-runtime-api = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.4', default-features = false }
5356

5457
# Cumulus Dependencies
58+
pallet-collator-selection = { git = 'https://github.com/paritytech/cumulus', branch = 'polkadot-v0.9.4', default-features = false }
5559
cumulus-pallet-aura-ext = { git = 'https://github.com/paritytech/cumulus', branch = 'polkadot-v0.9.4', default-features = false }
5660
cumulus-pallet-parachain-system = { git = 'https://github.com/paritytech/cumulus', branch = 'polkadot-v0.9.4', default-features = false }
5761
cumulus-pallet-dmp-queue = { git = 'https://github.com/paritytech/cumulus', branch = 'polkadot-v0.9.4', default-features = false }
@@ -81,8 +85,8 @@ xcm-calls = {path = '../primitives/xcm-calls', default-features = false }
8185
pallet-chainlink-feed = { git = 'https://github.com/ChainSafe/chainlink-polkadot', branch = 'upgrade-substrate-master', default-features = false }
8286

8387
[dev-dependencies]
84-
hex-literal = "0.3.1"
85-
hex = "0.4.3"
88+
hex-literal = '0.3.1'
89+
hex = '0.4.3'
8690

8791
[features]
8892
default = ['std']
@@ -94,6 +98,7 @@ runtime-benchmarks = [
9498
'frame-system-benchmarking',
9599
'frame-system/runtime-benchmarks',
96100
'xcm-builder/runtime-benchmarks',
101+
'pallet-collator-selection/runtime-benchmarks',
97102
'pallet-balances/runtime-benchmarks',
98103
'pallet-timestamp/runtime-benchmarks',
99104
'pallet-asset-depository/runtime-benchmarks',
@@ -111,9 +116,16 @@ std = [
111116
'codec/std',
112117
'serde',
113118
'log/std',
119+
120+
'frame-support/std',
121+
'frame-executive/std',
122+
'frame-system/std',
123+
'frame-try-runtime/std',
124+
114125
'sp-api/std',
115126
'sp-std/std',
116127
'sp-io/std',
128+
'sp-consensus-aura/std',
117129
'sp-core/std',
118130
'sp-runtime/std',
119131
'sp-version/std',
@@ -122,13 +134,12 @@ std = [
122134
'sp-block-builder/std',
123135
'sp-transaction-pool/std',
124136
'sp-inherents/std',
125-
'frame-support/std',
126-
'frame-executive/std',
127-
'frame-system/std',
137+
'pallet-authorship/std',
128138
'pallet-balances/std',
129139
'pallet-randomness-collective-flip/std',
130140
'pallet-timestamp/std',
131141
'pallet-sudo/std',
142+
'pallet-session/std',
132143
'pallet-transaction-payment/std',
133144
'parachain-info/std',
134145
'cumulus-pallet-aura-ext/std',
@@ -141,9 +152,9 @@ std = [
141152
'xcm-builder/std',
142153
'xcm-executor/std',
143154
'pallet-aura/std',
144-
'sp-consensus-aura/std',
145155
'pallet-asset-depository/std',
146156
'pallet-asset-index/std',
157+
"pallet-collator-selection/std",
147158
'pallet-committee/std',
148159
'pallet-local-treasury/std',
149160
'pallet-price-feed/std',

runtime/src/lib.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,56 @@ impl pallet_aura::Config for Runtime {
467467
type AuthorityId = AuraId;
468468
}
469469

470+
parameter_types! {
471+
pub const UncleGenerations: u32 = 0;
472+
}
473+
474+
impl pallet_authorship::Config for Runtime {
475+
type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Aura>;
476+
type UncleGenerations = UncleGenerations;
477+
type FilterUncle = ();
478+
type EventHandler = CollatorSelection;
479+
}
480+
481+
parameter_types! {
482+
pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33);
483+
pub const Period: u32 = 6 * HOURS;
484+
pub const Offset: BlockNumber = 0;
485+
}
486+
487+
impl pallet_session::Config for Runtime {
488+
type Event = Event;
489+
type ValidatorId = <Self as frame_system::Config>::AccountId;
490+
// we don't have stash and controller, thus we don't need the convert as well.
491+
type ValidatorIdOf = pallet_collator_selection::IdentityCollator;
492+
type ShouldEndSession = pallet_session::PeriodicSessions<Period, Offset>;
493+
type NextSessionRotation = pallet_session::PeriodicSessions<Period, Offset>;
494+
type SessionManager = CollatorSelection;
495+
// Essentially just Aura, but lets be pedantic.
496+
type SessionHandler =
497+
<opaque::SessionKeys as sp_runtime::traits::OpaqueKeys>::KeyTypeIdProviders;
498+
type Keys = opaque::SessionKeys;
499+
type DisabledValidatorsThreshold = DisabledValidatorsThreshold;
500+
type WeightInfo = ();
501+
}
502+
503+
parameter_types! {
504+
pub const PotId: PalletId = PalletId(*b"PotStake");
505+
pub const MaxCandidates: u32 = 200;
506+
pub const MaxInvulnerables: u32 = 50;
507+
}
508+
509+
impl pallet_collator_selection::Config for Runtime {
510+
type Event = Event;
511+
type Currency = Balances;
512+
type UpdateOrigin = EnsureApprovedByCommittee;
513+
type PotId = PotId;
514+
type MaxCandidates = MaxCandidates;
515+
type MaxInvulnerables = MaxInvulnerables;
516+
type KickThreshold = Period;
517+
type WeightInfo = ();
518+
}
519+
470520
parameter_types! {
471521
pub const TreasuryPalletId: PalletId = PalletId(*b"12345678");
472522
}
@@ -726,7 +776,9 @@ construct_runtime!(
726776
ParachainInfo: parachain_info::{Pallet, Storage, Config},
727777

728778
// Collator. The order of the 4 below are important and shall not change.
729-
// TODO authorship, collatorselection
779+
Authorship: pallet_authorship::{Pallet, Call, Storage},
780+
CollatorSelection: pallet_collator_selection::{Pallet, Call, Storage, Event<T>, Config<T>},
781+
Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>},
730782
Aura: pallet_aura::{Pallet, Config<T>},
731783
AuraExt: cumulus_pallet_aura_ext::{Pallet, Config},
732784

@@ -779,6 +831,7 @@ pub type Executive = frame_executive::Executive<
779831
frame_system::ChainContext<Runtime>,
780832
Runtime,
781833
AllPallets,
834+
(),
782835
>;
783836

784837
impl_runtime_apis! {
@@ -929,4 +982,7 @@ impl_runtime_apis! {
929982
}
930983
}
931984

932-
cumulus_pallet_parachain_system::register_validate_block!(Runtime, Executive);
985+
cumulus_pallet_parachain_system::register_validate_block!(
986+
Runtime,
987+
cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>
988+
);

0 commit comments

Comments
 (0)