Add a NewSet event to validator-sets

Updates to the latest serai-dex/substrate due to depending on
10ccaca0eb498a2316bbf627d419b29b1a75933a.
This commit is contained in:
Luke Parker
2023-04-15 00:40:33 -04:00
parent 2e2bc59703
commit 124b994c23
5 changed files with 165 additions and 131 deletions

View File

@@ -11,6 +11,15 @@ const PALLET: &str = "ValidatorSets";
pub type ValidatorSetsEvent = validator_sets::Event<Runtime>;
impl Serai {
pub async fn get_new_set_events(
&self,
block: [u8; 32],
) -> Result<Vec<ValidatorSetsEvent>, SeraiError> {
self
.events::<ValidatorSets, _>(block, |event| matches!(event, ValidatorSetsEvent::NewSet { .. }))
.await
}
pub async fn get_vote_events(
&self,
block: [u8; 32],

View File

@@ -3,11 +3,14 @@ use rand_core::{RngCore, OsRng};
use sp_core::{sr25519::Public, Pair};
use serai_client::{
primitives::{BITCOIN_NET_ID, BITCOIN_NET, insecure_pair_from_name},
primitives::{
BITCOIN_NET_ID, ETHEREUM_NET_ID, MONERO_NET_ID, BITCOIN_NET, insecure_pair_from_name,
},
validator_sets::{
primitives::{Session, ValidatorSet},
ValidatorSetsEvent,
},
subxt::config::Header,
Serai,
};
@@ -33,6 +36,22 @@ serai_test!(
let serai = serai().await;
// Make sure the genesis is as expected
assert_eq!(
serai
.get_new_set_events(
serai.get_block_by_number(0).await.unwrap().unwrap().header.hash().into()
)
.await
.unwrap(),
[BITCOIN_NET_ID, ETHEREUM_NET_ID, MONERO_NET_ID]
.iter()
.copied()
.map(|network| ValidatorSetsEvent::NewSet {
set: ValidatorSet { session: Session(0), network }
})
.collect::<Vec<_>>(),
);
let set_data = serai.get_validator_set(set).await.unwrap().unwrap();
assert_eq!(set_data.network, *BITCOIN_NET);
let participants_ref: &[_] = set_data.participants.as_ref();

View File

@@ -64,27 +64,12 @@ pub mod pallet {
pub type VoteCount<T: Config> =
StorageMap<_, Blake2_128Concat, (ValidatorSet, KeyPair), u16, ValueQuery>;
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
let mut participants = Vec::new();
for participant in self.participants.clone() {
participants.push((participant, self.bond));
}
let participants = BoundedVec::try_from(participants).unwrap();
for (id, network) in self.networks.clone() {
ValidatorSets::<T>::set(
ValidatorSet { session: Session(0), network: id },
Some(ValidatorSetData { bond: self.bond, network, participants: participants.clone() }),
);
}
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
NewSet {
set: ValidatorSet,
},
Vote {
voter: T::AccountId,
set: ValidatorSet,
@@ -98,6 +83,26 @@ pub mod pallet {
},
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
let mut participants = Vec::new();
for participant in self.participants.clone() {
participants.push((participant, self.bond));
}
let participants = BoundedVec::try_from(participants).unwrap();
for (id, network) in self.networks.clone() {
let set = ValidatorSet { session: Session(0), network: id };
ValidatorSets::<T>::set(
set,
Some(ValidatorSetData { bond: self.bond, network, participants: participants.clone() }),
);
Pallet::<T>::deposit_event(Event::NewSet { set })
}
}
}
#[pallet::error]
pub enum Error<T> {
/// Validator Set doesn't exist.