From 2292d2d2afd2a133d41f1d4e4e26e5cf742092f2 Mon Sep 17 00:00:00 2001 From: akildemir Date: Mon, 19 Feb 2024 18:32:55 +0300 Subject: [PATCH] fix pr comments --- substrate/client/src/serai/mod.rs | 9 +++------ substrate/client/src/serai/validator_sets.rs | 7 +++++++ substrate/client/tests/validator_sets.rs | 17 +++++++++-------- substrate/validator-sets/pallet/src/lib.rs | 2 +- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/substrate/client/src/serai/mod.rs b/substrate/client/src/serai/mod.rs index e44e05070..1347fc058 100644 --- a/substrate/client/src/serai/mod.rs +++ b/substrate/client/src/serai/mod.rs @@ -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::{ @@ -195,15 +195,12 @@ impl Serai { Ok(()) } - pub async fn active_network_validators( - &self, - network: NetworkId, - ) -> Result, SeraiError> { + async fn active_network_validators(&self, network: NetworkId) -> Result, 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::>(bytes.into()) + let r = Vec::::decode(&mut bytes.as_slice()) .map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?; Ok(r) } diff --git a/substrate/client/src/serai/validator_sets.rs b/substrate/client/src/serai/validator_sets.rs index 7459fe9ee..c4e296441 100644 --- a/substrate/client/src/serai/validator_sets.rs +++ b/substrate/client/src/serai/validator_sets.rs @@ -159,6 +159,13 @@ impl<'a> SeraiValidatorSets<'a> { .await } + pub async fn active_network_validators( + &self, + network: NetworkId, + ) -> Result, 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, SeraiError> { self.0.storage(PALLET, "Keys", (sp_core::hashing::twox_64(&set.encode()), set)).await diff --git a/substrate/client/tests/validator_sets.rs b/substrate/client/tests/validator_sets.rs index b366046e4..2ab8c4238 100644 --- a/substrate/client/tests/validator_sets.rs +++ b/substrate/client/tests/validator_sets.rs @@ -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(); @@ -176,7 +176,7 @@ 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 = @@ -184,7 +184,7 @@ async fn validator_set_rotation() { .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, @@ -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 @@ -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, @@ -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); @@ -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 { diff --git a/substrate/validator-sets/pallet/src/lib.rs b/substrate/validator-sets/pallet/src/lib.rs index b72bc0a45..aa747a5a6 100644 --- a/substrate/validator-sets/pallet/src/lib.rs +++ b/substrate/validator-sets/pallet/src/lib.rs @@ -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::::get(set.network).is_some() { Self::deposit_event(Event::SetRetired { set: ValidatorSet { network: set.network, session: Session(set.session.0 - 1) },