Skip to content

Commit

Permalink
Integrate session pallet into validator-sets pallet (#440)
Browse files Browse the repository at this point in the history
* remove pallet-session

* Store key shares in InSet

* integrate grandpa to vs-pallet

* integrate pallet babe

* remove pallet-session & authority discovery from runtime

* update the grandpa pallet path

* cargo update grandpa

* cargo update substrate

* Misc tweaks

Sets validators for BABE/GRANDPA in chain_spec, per Akil's realization that was
the missing piece.

* fix pr comments

* bug fix & tidy up

---------

Co-authored-by: Luke Parker <[email protected]>
  • Loading branch information
akildemir and kayabaNerve authored Nov 22, 2023
1 parent 07c6573 commit fcfdadc
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 253 deletions.
232 changes: 108 additions & 124 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 5 additions & 16 deletions coordinator/src/substrate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn in_set(
return Ok(None);
};
let key = (Ristretto::generator() * key.deref()).to_bytes();
Ok(Some(participants.iter().any(|participant| participant.0 == key)))
Ok(Some(participants.iter().any(|(participant, _)| participant.0 == key)))
}

async fn handle_new_set<D: Db>(
Expand All @@ -67,21 +67,10 @@ async fn handle_new_set<D: Db>(
let set_participants =
serai.participants(set.network).await?.expect("NewSet for set which doesn't exist");

let allocation_per_key_share = serai
.allocation_per_key_share(set.network)
.await?
.expect("NewSet for set which didn't have an allocation per key share")
.0;

let mut set_data = vec![];
for participant in set_participants {
let allocation = serai
.allocation(set.network, participant)
.await?
.expect("validator selected for set yet didn't have an allocation")
.0;
set_data.push((participant, u16::try_from(allocation / allocation_per_key_share).unwrap()));
}
let mut set_data = set_participants
.into_iter()
.map(|(k, w)| (k, u16::try_from(w).unwrap()))
.collect::<Vec<_>>();
amortize_excess_key_shares(&mut set_data);
set_data
};
Expand Down
5 changes: 4 additions & 1 deletion substrate/client/src/serai/validator_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ impl<'a> SeraiValidatorSets<'a> {
self.0.storage(PALLET, "CurrentSession", Some(vec![scale_value(network)])).await
}

pub async fn participants(&self, network: NetworkId) -> Result<Option<Vec<Public>>, SeraiError> {
pub async fn participants(
&self,
network: NetworkId,
) -> Result<Option<Vec<(Public, u64)>>, SeraiError> {
self.0.storage(PALLET, "Participants", Some(vec![scale_value(network)])).await
}

Expand Down
7 changes: 6 additions & 1 deletion substrate/client/tests/validator_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ serai_test!(

{
let vs_serai = serai.with_current_latest_block().await.unwrap().validator_sets();
let participants = vs_serai.participants(set.network).await.unwrap().unwrap();
let participants = vs_serai.participants(set.network).await
.unwrap()
.unwrap()
.into_iter()
.map(|(k, _)| k)
.collect::<Vec<_>>();
let participants_ref: &[_] = participants.as_ref();
assert_eq!(participants_ref, [public].as_ref());
assert_eq!(vs_serai.musig_key(set).await.unwrap().unwrap(), musig_key(set, &[public]).0);
Expand Down
28 changes: 9 additions & 19 deletions substrate/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use sp_core::Pair as PairTrait;
use sc_service::ChainType;

use serai_runtime::{
primitives::*, WASM_BINARY, opaque::SessionKeys, BABE_GENESIS_EPOCH_CONFIG, RuntimeGenesisConfig,
SystemConfig, CoinsConfig, DexConfig, ValidatorSetsConfig, SessionConfig, BabeConfig,
GrandpaConfig, AuthorityDiscoveryConfig,
primitives::*, WASM_BINARY, BABE_GENESIS_EPOCH_CONFIG, RuntimeGenesisConfig, SystemConfig,
CoinsConfig, DexConfig, ValidatorSetsConfig, BabeConfig, GrandpaConfig,
};

pub type ChainSpec = sc_service::GenericChainSpec<RuntimeGenesisConfig>;
Expand All @@ -21,16 +20,7 @@ fn testnet_genesis(
validators: &[&'static str],
endowed_accounts: Vec<PublicKey>,
) -> RuntimeGenesisConfig {
let session_key = |name| {
let key = account_from_name(name);
(
key,
key,
// TODO: Properly diversify these?
SessionKeys { babe: key.into(), grandpa: key.into(), authority_discovery: key.into() },
)
};

let validators = validators.iter().map(|name| account_from_name(name)).collect::<Vec<_>>();
RuntimeGenesisConfig {
system: SystemConfig { code: wasm_binary.to_vec(), _config: PhantomData },

Expand Down Expand Up @@ -59,17 +49,17 @@ fn testnet_genesis(
NetworkId::Monero => (NetworkId::Monero, Amount(100_000 * 10_u64.pow(8))),
})
.collect(),
participants: validators.iter().map(|name| account_from_name(name)).collect(),
participants: validators.clone(),
},
session: SessionConfig { keys: validators.iter().map(|name| session_key(*name)).collect() },
babe: BabeConfig {
authorities: vec![],
authorities: validators.iter().map(|validator| ((*validator).into(), 1)).collect(),
epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
_config: PhantomData,
},
grandpa: GrandpaConfig { authorities: vec![], _config: PhantomData },

authority_discovery: AuthorityDiscoveryConfig { keys: vec![], _config: PhantomData },
grandpa: GrandpaConfig {
authorities: validators.into_iter().map(|validator| (validator.into(), 1)).collect(),
_config: PhantomData,
},
}
}

Expand Down
6 changes: 0 additions & 6 deletions substrate/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ coins-pallet = { package = "serai-coins-pallet", path = "../coins/pallet", defau
dex-pallet = { package = "serai-dex-pallet", path = "../dex/pallet", default-features = false }

validator-sets-pallet = { package = "serai-validator-sets-pallet", path = "../validator-sets/pallet", default-features = false }
pallet-session = { git = "https://github.com/serai-dex/substrate", default-features = false }

in-instructions-pallet = { package = "serai-in-instructions-pallet", path = "../in-instructions/pallet", default-features = false }

Expand All @@ -59,8 +58,6 @@ signals-pallet = { package = "serai-signals-pallet", path = "../signals/pallet",
pallet-babe = { git = "https://github.com/serai-dex/substrate", default-features = false }
pallet-grandpa = { git = "https://github.com/serai-dex/substrate", default-features = false }

pallet-authority-discovery = { git = "https://github.com/serai-dex/substrate", default-features = false }

frame-system-rpc-runtime-api = { git = "https://github.com/serai-dex/substrate", default-features = false }
pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/serai-dex/substrate", default-features = false }

Expand Down Expand Up @@ -105,7 +102,6 @@ std = [
"dex-pallet/std",

"validator-sets-pallet/std",
"pallet-session/std",

"in-instructions-pallet/std",

Expand All @@ -114,8 +110,6 @@ std = [
"pallet-babe/std",
"pallet-grandpa/std",

"pallet-authority-discovery/std",

"frame-system-rpc-runtime-api/std",
"pallet-transaction-payment-rpc-runtime-api/std",
]
Expand Down
42 changes: 8 additions & 34 deletions substrate/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub use coins_pallet as coins;
pub use dex_pallet as dex;

pub use validator_sets_pallet as validator_sets;
pub use pallet_session as session;

pub use in_instructions_pallet as in_instructions;

Expand All @@ -29,8 +28,6 @@ pub use signals_pallet as signals;
pub use pallet_babe as babe;
pub use pallet_grandpa as grandpa;

pub use pallet_authority_discovery as authority_discovery;

// Actually used by the runtime
use sp_core::OpaqueMetadata;
use sp_std::prelude::*;
Expand All @@ -41,7 +38,7 @@ use sp_version::NativeVersion;

use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys, KeyTypeId,
traits::{Convert, OpaqueKeys, BlakeTwo256, Block as BlockT},
traits::{Convert, BlakeTwo256, Block as BlockT},
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, Perbill,
};
Expand Down Expand Up @@ -83,13 +80,10 @@ pub mod opaque {
pub struct SessionKeys {
pub babe: Babe,
pub grandpa: Grandpa,
pub authority_discovery: AuthorityDiscovery,
}
}
}

use opaque::SessionKeys;

#[sp_version::runtime_version]
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("serai"),
Expand Down Expand Up @@ -167,12 +161,6 @@ impl Contains<RuntimeCall> for CallFilter {
RuntimeCall::InInstructions(call) => !matches!(call, in_instructions::Call::__Ignore(_, _)),
RuntimeCall::Signals(call) => !matches!(call, signals::Call::__Ignore(_, _)),

RuntimeCall::Session(call) => match call {
session::Call::set_keys { .. } => true,
session::Call::purge_keys { .. } => false,
session::Call::__Ignore(_, _) => false,
},

RuntimeCall::Babe(call) => match call {
babe::Call::report_equivocation { .. } => true,
babe::Call::report_equivocation_unsigned { .. } => true,
Expand Down Expand Up @@ -256,6 +244,8 @@ impl dex::Config for Runtime {

impl validator_sets::Config for Runtime {
type RuntimeEvent = RuntimeEvent;

type ShouldEndSession = Babe;
}

pub struct IdentityValidatorIdOf;
Expand All @@ -265,18 +255,6 @@ impl Convert<PublicKey, Option<PublicKey>> for IdentityValidatorIdOf {
}
}

impl session::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type ValidatorId = PublicKey;
type ValidatorIdOf = IdentityValidatorIdOf;
type ShouldEndSession = Babe;
type NextSessionRotation = Babe;
type SessionManager = ValidatorSets;
type SessionHandler = <SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
type Keys = SessionKeys;
type WeightInfo = session::weights::SubstrateWeight<Runtime>;
}

impl signals::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
// 1 week
Expand All @@ -294,7 +272,7 @@ impl babe::Config for Runtime {
type EpochDuration = ConstU64<{ 1 * DAYS }>;
type ExpectedBlockTime = ConstU64<{ TARGET_BLOCK_TIME * 1000 }>;
type EpochChangeTrigger = pallet_babe::ExternalTrigger;
type DisabledValidators = Session;
type DisabledValidators = ValidatorSets;

type WeightInfo = ();

Expand All @@ -317,10 +295,6 @@ impl grandpa::Config for Runtime {
type EquivocationReportSystem = ();
}

impl authority_discovery::Config for Runtime {
type MaxAuthorities = MaxAuthorities;
}

pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type SignedExtra = (
Expand Down Expand Up @@ -357,16 +331,13 @@ construct_runtime!(
Dex: dex,

ValidatorSets: validator_sets,
Session: session,

InInstructions: in_instructions,

Signals: signals,

Babe: babe,
Grandpa: grandpa,

AuthorityDiscovery: authority_discovery,
}
);

Expand Down Expand Up @@ -569,7 +540,10 @@ sp_api::impl_runtime_apis! {

impl sp_authority_discovery::AuthorityDiscoveryApi<Block> for Runtime {
fn authorities() -> Vec<AuthorityDiscoveryId> {
AuthorityDiscovery::authorities()
Babe::authorities()
.into_iter()
.map(|(id, _)| AuthorityDiscoveryId::from(id.into_inner()))
.collect()
}
}
}
8 changes: 6 additions & 2 deletions substrate/validator-sets/pallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ sp-io = { git = "https://github.com/serai-dex/substrate", default-features = fal
sp-std = { git = "https://github.com/serai-dex/substrate", default-features = false }
sp-application-crypto = { git = "https://github.com/serai-dex/substrate", default-features = false }
sp-runtime = { git = "https://github.com/serai-dex/substrate", default-features = false }
sp-session = { git = "https://github.com/serai-dex/substrate", default-features = false }

frame-system = { git = "https://github.com/serai-dex/substrate", default-features = false }
frame-support = { git = "https://github.com/serai-dex/substrate", default-features = false }

pallet-session = { git = "https://github.com/serai-dex/substrate", default-features = false }
pallet-babe = { git = "https://github.com/serai-dex/substrate", default-features = false }
pallet-grandpa = { git = "https://github.com/serai-dex/substrate", default-features = false }

serai-primitives = { path = "../../primitives", default-features = false }
validator-sets-primitives = { package = "serai-validator-sets-primitives", path = "../primitives", default-features = false }
Expand All @@ -42,11 +44,13 @@ std = [
"sp-std/std",
"sp-application-crypto/std",
"sp-runtime/std",
"sp-session/std",

"frame-system/std",
"frame-support/std",

"pallet-session/std",
"pallet-babe/std",
"pallet-grandpa/std",

"serai-primitives/std",
"validator-sets-primitives/std",
Expand Down
Loading

0 comments on commit fcfdadc

Please sign in to comment.