mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Expand validator sets API with the rest of the events and some getters
We could've added a storage API, and fetched fields that way, except we want the storage to be opaque. That meant we needed to add the RPC routes to the node, which also simplifies other people writing RPC code and fetching these fields. Then the node could've used the storage API, except a lot of the storage in validator-sets is marked opaque and to only be read via functions, so extending the runtime made the most sense.
This commit is contained in:
@@ -6,9 +6,10 @@ extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
use serai_abi::{
|
||||
primitives::{
|
||||
crypto::{Public, SignedEmbeddedEllipticCurveKeys},
|
||||
crypto::{Public, SignedEmbeddedEllipticCurveKeys, KeyPair},
|
||||
network_id::NetworkId,
|
||||
balance::Balance,
|
||||
validator_sets::{Session, ExternalValidatorSet, ValidatorSet},
|
||||
balance::{Amount, Balance},
|
||||
},
|
||||
Event,
|
||||
};
|
||||
@@ -34,7 +35,10 @@ sp_api::decl_runtime_apis! {
|
||||
}
|
||||
pub trait SeraiApi {
|
||||
fn events() -> Vec<Vec<u8>>;
|
||||
fn validators(network_id: NetworkId) -> Vec<Public>;
|
||||
fn validators(network: NetworkId) -> Vec<Public>;
|
||||
fn current_session(network: NetworkId) -> Option<Session>;
|
||||
fn current_stake(network: NetworkId) -> Option<Amount>;
|
||||
fn keys(set: ExternalValidatorSet) -> Option<KeyPair>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +48,8 @@ mod apis {
|
||||
use alloc::borrow::Cow;
|
||||
use serai_abi::{SubstrateHeader as Header, SubstrateBlock as Block};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[sp_version::runtime_version]
|
||||
pub const VERSION: sp_version::RuntimeVersion = sp_version::RuntimeVersion {
|
||||
spec_name: Cow::Borrowed("serai"),
|
||||
@@ -181,6 +187,15 @@ mod apis {
|
||||
) -> Vec<serai_abi::primitives::crypto::Public> {
|
||||
unimplemented!("runtime is only implemented when WASM")
|
||||
}
|
||||
fn current_session(network: NetworkId) -> Option<Session> {
|
||||
unimplemented!("runtime is only implemented when WASM")
|
||||
}
|
||||
fn current_stake(network: NetworkId) -> Option<Amount> {
|
||||
unimplemented!("runtime is only implemented when WASM")
|
||||
}
|
||||
fn keys(set: ExternalValidatorSet) -> Option<KeyPair> {
|
||||
unimplemented!("runtime is only implemented when WASM")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use serai_abi::{
|
||||
primitives::{
|
||||
network_id::{ExternalNetworkId, NetworkId},
|
||||
balance::{Amount, ExternalBalance},
|
||||
validator_sets::ValidatorSet,
|
||||
validator_sets::{Session, ExternalValidatorSet, ValidatorSet},
|
||||
address::SeraiAddress,
|
||||
},
|
||||
SubstrateHeader as Header, SubstrateBlock,
|
||||
@@ -522,6 +522,21 @@ sp_api::impl_runtime_apis! {
|
||||
.map(|validator| validator.0.into())
|
||||
.collect()
|
||||
}
|
||||
fn current_session(network: NetworkId) -> Option<Session> {
|
||||
ValidatorSets::current_session(network)
|
||||
}
|
||||
fn current_stake(network: NetworkId) -> Option<Amount> {
|
||||
ValidatorSets::stake_for_current_validator_set(network)
|
||||
}
|
||||
fn keys(set: ExternalValidatorSet) -> Option<serai_abi::primitives::crypto::KeyPair> {
|
||||
ValidatorSets::oraclization_key(set)
|
||||
.and_then(|oraclization_key| {
|
||||
ValidatorSets::external_key(set)
|
||||
.map(|external_key| {
|
||||
serai_abi::primitives::crypto::KeyPair(oraclization_key.into(), external_key)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user