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

@@ -1,8 +1,8 @@
use sp_core::sr25519::Signature;
use sp_core::sr25519::{Public, Signature};
use serai_runtime::{validator_sets, ValidatorSets, Runtime};
pub use validator_sets::primitives;
use primitives::{ValidatorSet, ValidatorSetData, KeyPair};
use primitives::{ValidatorSet, KeyPair};
use subxt::utils::Encoded;
@@ -31,39 +31,29 @@ impl Serai {
.await
}
pub async fn get_validator_set(
pub async fn get_validator_set_participants(
&self,
set: ValidatorSet,
) -> Result<Option<ValidatorSetData>, SeraiError> {
self
.storage(
PALLET,
"ValidatorSets",
Some(vec![scale_value(set)]),
self.get_latest_block_hash().await?,
)
.await
network: NetworkId,
at_hash: [u8; 32],
) -> Result<Option<Vec<Public>>, SeraiError> {
self.storage(PALLET, "Participants", Some(vec![scale_value(network)]), at_hash).await
}
pub async fn get_validator_set_musig_key(
&self,
set: ValidatorSet,
at_hash: [u8; 32],
) -> Result<Option<[u8; 32]>, SeraiError> {
self
.storage(
PALLET,
"MuSigKeys",
Some(vec![scale_value(set)]),
self.get_latest_block_hash().await?,
)
.await
self.storage(PALLET, "MuSigKeys", Some(vec![scale_value(set)]), at_hash).await
}
// TODO: Store these separately since we almost never need both at once?
pub async fn get_keys(&self, set: ValidatorSet) -> Result<Option<KeyPair>, SeraiError> {
self
.storage(PALLET, "Keys", Some(vec![scale_value(set)]), self.get_latest_block_hash().await?)
.await
pub async fn get_keys(
&self,
set: ValidatorSet,
at_hash: [u8; 32],
) -> Result<Option<KeyPair>, SeraiError> {
self.storage(PALLET, "Keys", Some(vec![scale_value(set)]), at_hash).await
}
pub fn set_validator_set_keys(