Skip to content

Commit

Permalink
fix pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Feb 19, 2024
1 parent c04afa0 commit 2292d2d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
9 changes: 3 additions & 6 deletions substrate/client/src/serai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use thiserror::Error;
use async_lock::RwLock;
use simple_request::{hyper, Request, Client};

use scale::{decode_from_bytes, Compact, Decode, Encode};
use scale::{Compact, Decode, Encode};
use serde::{Serialize, Deserialize, de::DeserializeOwned};

pub use sp_core::{
Expand Down Expand Up @@ -195,15 +195,12 @@ impl Serai {
Ok(())
}

pub async fn active_network_validators(
&self,
network: NetworkId,
) -> Result<Vec<Public>, SeraiError> {
async fn active_network_validators(&self, network: NetworkId) -> Result<Vec<Public>, SeraiError> {
let hash: String = self
.call("state_call", ["SeraiRuntimeApi_validators".to_string(), hex::encode(network.encode())])
.await?;
let bytes = Self::hex_decode(hash)?;
let r = decode_from_bytes::<Vec<Public>>(bytes.into())
let r = Vec::<Public>::decode(&mut bytes.as_slice())
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
Ok(r)
}
Expand Down
7 changes: 7 additions & 0 deletions substrate/client/src/serai/validator_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ impl<'a> SeraiValidatorSets<'a> {
.await
}

pub async fn active_network_validators(
&self,
network: NetworkId,
) -> Result<Vec<Public>, SeraiError> {
self.0.serai.active_network_validators(network).await
}

// TODO: Store these separately since we almost never need both at once?
pub async fn keys(&self, set: ValidatorSet) -> Result<Option<KeyPair>, SeraiError> {
self.0.storage(PALLET, "Keys", (sp_core::hashing::twox_64(&set.encode()), set)).await
Expand Down
17 changes: 9 additions & 8 deletions substrate/client/tests/validator_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async fn validator_set_rotation() {
let alice_rpc = ops.handle(&alice).host_port(9944).unwrap();
let alice_rpc = format!("http://{}:{}", alice_rpc.0, alice_rpc.1);

// Sleep for a minute
// Sleep for some time
tokio::time::sleep(core::time::Duration::from_secs(20)).await;
let serai = Serai::new(alice_rpc.clone()).await.unwrap();

Expand Down Expand Up @@ -176,15 +176,15 @@ async fn validator_set_rotation() {

// we start the chain with 4 default participants that has a single key share each
participants.sort();
verfiy_session_and_active_validators(&serai, network, 0, &participants).await;
verify_session_and_active_validators(&serai, network, 0, &participants).await;

// add 1 participant & verify
let hash =
allocate_stake(&serai, network, key_shares[&network], &pair5, i.try_into().unwrap())
.await;
participants.push(pair5.public());
participants.sort();
verfiy_session_and_active_validators(
verify_session_and_active_validators(
&serai,
network,
get_active_session(&serai, network, hash).await,
Expand All @@ -199,7 +199,7 @@ async fn validator_set_rotation() {
participants.swap_remove(participants.iter().position(|k| *k == pair2.public()).unwrap());
let active_session = get_active_session(&serai, network, hash).await;
participants.sort();
verfiy_session_and_active_validators(&serai, network, active_session, &participants).await;
verify_session_and_active_validators(&serai, network, active_session, &participants).await;

// check pending deallocations
let pending = serai
Expand All @@ -220,7 +220,7 @@ async fn validator_set_rotation() {
.await;
}

async fn verfiy_session_and_active_validators(
async fn verify_session_and_active_validators(
serai: &Serai,
network: NetworkId,
session: u64,
Expand All @@ -235,12 +235,13 @@ async fn verfiy_session_and_active_validators(
let serai_for_block =
serai.as_of(serai.finalized_block_by_number(epoch_block).await.unwrap().unwrap().hash());

// verfiy session
// verify session
let s = serai_for_block.validator_sets().session(network).await.unwrap().unwrap();
assert_eq!(u64::from(s.0), session);

// verify participants
let mut validators = serai.active_network_validators(network).await.unwrap();
let mut validators =
serai_for_block.validator_sets().active_network_validators(network).await.unwrap();
validators.sort();
assert_eq!(validators, participants);

Expand All @@ -255,7 +256,7 @@ async fn verfiy_session_and_active_validators(
.await
.unwrap();

// TODO: verfiy key shares as well?
// TODO: verify key shares as well?
}

async fn get_active_session(serai: &Serai, network: NetworkId, hash: [u8; 32]) -> u64 {
Expand Down
2 changes: 1 addition & 1 deletion substrate/validator-sets/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ pub mod pallet {
}

pub fn retire_set(set: ValidatorSet) {
// If the prior set didn't report, emit they're retired now
// If the prior prior set didn't report, emit they're retired now
if PendingSlashReport::<T>::get(set.network).is_some() {
Self::deposit_event(Event::SetRetired {
set: ValidatorSet { network: set.network, session: Session(set.session.0 - 1) },
Expand Down

0 comments on commit 2292d2d

Please sign in to comment.