Staking pallet (#373)

* initial staking pallet

* add staking pallet to runtime

* support session rotation for serai

* optimizations & cleaning

* fix deny

* add serai network to initial networks

* a few tweaks & comments

* fix some pr comments

* Rewrite validator-sets with logarithmic algorithms

Uses the fact the underlying DB is sorted to achieve sorting of potential
validators by stake.

Removes release of deallocated stake for now.

---------

Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
akildemir
2023-10-10 13:53:24 +03:00
committed by GitHub
parent 2f45bba2d4
commit 98190b7b83
25 changed files with 635 additions and 149 deletions

View File

@@ -50,7 +50,9 @@ pallet-transaction-payment = { git = "https://github.com/serai-dex/substrate", d
tokens-pallet = { package = "serai-tokens-pallet", path = "../tokens/pallet", default-features = false }
in-instructions-pallet = { package = "serai-in-instructions-pallet", path = "../in-instructions/pallet", default-features = false }
staking-pallet = { package = "serai-staking-pallet", path = "../staking/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 }
pallet-babe = { git = "https://github.com/serai-dex/substrate", default-features = false }
pallet-grandpa = { git = "https://github.com/serai-dex/substrate", default-features = false }
@@ -102,7 +104,9 @@ std = [
"tokens-pallet/std",
"in-instructions-pallet/std",
"staking-pallet/std",
"validator-sets-pallet/std",
"pallet-session/std",
"pallet-babe/std",
"pallet-grandpa/std",

View File

@@ -21,6 +21,7 @@ pub use pallet_assets as assets;
pub use tokens_pallet as tokens;
pub use in_instructions_pallet as in_instructions;
pub use staking_pallet as staking;
pub use validator_sets_pallet as validator_sets;
pub use pallet_session as session;
@@ -142,7 +143,7 @@ parameter_types! {
NORMAL_DISPATCH_RATIO,
);
pub const MaxAuthorities: u32 = 100;
pub const MaxAuthorities: u32 = validator_sets::primitives::MAX_VALIDATORS_PER_SET;
}
pub struct CallFilter;
@@ -172,10 +173,24 @@ impl Contains<RuntimeCall> for CallFilter {
return matches!(call, in_instructions::Call::execute_batch { .. });
}
if let RuntimeCall::Staking(call) = call {
return matches!(
call,
staking::Call::stake { .. } |
staking::Call::unstake { .. } |
staking::Call::allocate { .. } |
staking::Call::deallocate { .. }
);
}
if let RuntimeCall::ValidatorSets(call) = call {
return matches!(call, validator_sets::Call::set_keys { .. });
}
if let RuntimeCall::Session(call) = call {
return matches!(call, session::Call::set_keys { .. });
}
false
}
}
@@ -300,6 +315,10 @@ impl in_instructions::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}
impl staking::Config for Runtime {
type Currency = Balances;
}
impl validator_sets::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}
@@ -317,7 +336,7 @@ impl session::Config for Runtime {
type ValidatorIdOf = IdentityValidatorIdOf;
type ShouldEndSession = Babe;
type NextSessionRotation = Babe;
type SessionManager = (); // TODO?
type SessionManager = Staking;
type SessionHandler = <SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
type Keys = SessionKeys;
type WeightInfo = session::weights::SubstrateWeight<Runtime>;
@@ -393,6 +412,8 @@ construct_runtime!(
ValidatorSets: validator_sets,
Staking: staking,
Session: session,
Babe: babe,
Grandpa: grandpa,