Add validator sets RPC functions necessary for the coordinator

This commit is contained in:
Luke Parker
2025-11-16 17:27:58 -05:00
parent 0ea16f9e01
commit fa0ed4b180
6 changed files with 219 additions and 8 deletions

View File

@@ -6,10 +6,11 @@ extern crate alloc;
use alloc::vec::Vec;
use serai_abi::{
primitives::{
crypto::{Public, SignedEmbeddedEllipticCurveKeys, KeyPair},
network_id::NetworkId,
crypto::{Public, EmbeddedEllipticCurveKeys, SignedEmbeddedEllipticCurveKeys, KeyPair},
network_id::{ExternalNetworkId, NetworkId},
validator_sets::{Session, ExternalValidatorSet, ValidatorSet},
balance::{Amount, Balance},
address::SeraiAddress,
},
Event,
};
@@ -39,6 +40,12 @@ sp_api::decl_runtime_apis! {
fn current_session(network: NetworkId) -> Option<Session>;
fn current_stake(network: NetworkId) -> Option<Amount>;
fn keys(set: ExternalValidatorSet) -> Option<KeyPair>;
fn current_validators(network: NetworkId) -> Option<Vec<SeraiAddress>>;
fn pending_slash_report(network: ExternalNetworkId) -> bool;
fn embedded_elliptic_curve_keys(
validator: SeraiAddress,
network: ExternalNetworkId,
) -> Option<EmbeddedEllipticCurveKeys>;
}
}
@@ -183,7 +190,7 @@ mod apis {
unimplemented!("runtime is only implemented when WASM")
}
fn validators(
network: serai_abi::primitives::network_id::NetworkId
network: NetworkId
) -> Vec<serai_abi::primitives::crypto::Public> {
unimplemented!("runtime is only implemented when WASM")
}
@@ -196,6 +203,18 @@ mod apis {
fn keys(set: ExternalValidatorSet) -> Option<KeyPair> {
unimplemented!("runtime is only implemented when WASM")
}
fn current_validators(network: NetworkId) -> Option<Vec<SeraiAddress>> {
unimplemented!("runtime is only implemented when WASM")
}
fn pending_slash_report(network: ExternalNetworkId) -> bool {
unimplemented!("runtime is only implemented when WASM")
}
fn embedded_elliptic_curve_keys(
validator: SeraiAddress,
network: ExternalNetworkId,
) -> Option<EmbeddedEllipticCurveKeys> {
unimplemented!("runtime is only implemented when WASM")
}
}
}
}

View File

@@ -10,6 +10,7 @@ use sp_version::RuntimeVersion;
use serai_abi::{
primitives::{
crypto::EmbeddedEllipticCurveKeys,
network_id::{ExternalNetworkId, NetworkId},
balance::{Amount, ExternalBalance},
validator_sets::{Session, ExternalValidatorSet, ValidatorSet},
@@ -582,6 +583,23 @@ sp_api::impl_runtime_apis! {
})
})
}
fn current_validators(network: NetworkId) -> Option<Vec<SeraiAddress>> {
let session = ValidatorSets::current_session(network)?;
Some(
ValidatorSets::selected_validators(ValidatorSet { network, session })
.map(|(key, _key_shares)| SeraiAddress::from(key))
.collect()
)
}
fn pending_slash_report(network: ExternalNetworkId) -> bool {
ValidatorSets::pending_slash_report(network)
}
fn embedded_elliptic_curve_keys(
validator: SeraiAddress,
network: ExternalNetworkId,
) -> Option<EmbeddedEllipticCurveKeys> {
ValidatorSets::embedded_elliptic_curve_keys(validator.into(), network)
}
}
}