mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Add validator sets RPC functions necessary for the coordinator
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use std::{sync::Arc, ops::Deref, convert::AsRef, collections::HashSet};
|
||||
use core::{ops::Deref, convert::AsRef, str::FromStr};
|
||||
use std::{sync::Arc, collections::HashSet};
|
||||
|
||||
use rand_core::{RngCore, OsRng};
|
||||
|
||||
@@ -111,5 +112,74 @@ pub(crate) fn module<
|
||||
Ok(key_pair.map(|key_pair| hex::encode(borsh::to_vec(&key_pair).unwrap())))
|
||||
});
|
||||
|
||||
module.register_method(
|
||||
"validator-sets/current_validators",
|
||||
|params, client, _ext| -> Result<_, Error> {
|
||||
let Some(block_hash) = block_hash(&**client, ¶ms)? else {
|
||||
Err(Error::InvalidStateReference)?
|
||||
};
|
||||
let network = network(¶ms)?;
|
||||
let Ok(validators) = client.runtime_api().current_validators(block_hash, network) else {
|
||||
Err(Error::Internal("couldn't fetch the current validators for the requested network"))?
|
||||
};
|
||||
Ok(
|
||||
validators.map(|validators| validators.iter().map(ToString::to_string).collect::<Vec<_>>()),
|
||||
)
|
||||
},
|
||||
);
|
||||
|
||||
module.register_method(
|
||||
"validator-sets/pending_slash_report",
|
||||
|params, client, _ext| -> Result<_, Error> {
|
||||
let Some(block_hash) = block_hash(&**client, ¶ms)? else {
|
||||
Err(Error::InvalidStateReference)?
|
||||
};
|
||||
let Ok(network) = ExternalNetworkId::try_from(network(¶ms)?) else {
|
||||
Err(Error::InvalidRequest(
|
||||
"asking if a non-external validator set has a pending slash report",
|
||||
))?
|
||||
};
|
||||
client
|
||||
.runtime_api()
|
||||
.pending_slash_report(block_hash, network)
|
||||
.map_err(|_| Error::Internal("couldn't fetch if this network has a pending slash report"))
|
||||
},
|
||||
);
|
||||
|
||||
module.register_method(
|
||||
"validator-sets/embedded_elliptic_curve_keys",
|
||||
|params, client, _ext| -> Result<_, Error> {
|
||||
let Some(block_hash) = block_hash(&**client, ¶ms)? else {
|
||||
Err(Error::InvalidStateReference)?
|
||||
};
|
||||
|
||||
#[derive(sp_core::serde::Deserialize)]
|
||||
#[serde(crate = "sp_core::serde")]
|
||||
struct Validator {
|
||||
validator: String,
|
||||
}
|
||||
let Ok(validator) = params.parse::<Validator>() else {
|
||||
Err(Error::InvalidRequest(r#"missing `string` "validator" field"#))?
|
||||
};
|
||||
let Ok(validator) = SeraiAddress::from_str(&validator.validator) else {
|
||||
Err(Error::InvalidRequest(r#"validator had an invalid address"#))?
|
||||
};
|
||||
|
||||
let Ok(network) = ExternalNetworkId::try_from(network(¶ms)?) else {
|
||||
Err(Error::InvalidRequest(
|
||||
"asking for the embedded elliptic curve keys for a non-external network",
|
||||
))?
|
||||
};
|
||||
let Ok(embedded_elliptic_curve_keys) =
|
||||
client.runtime_api().embedded_elliptic_curve_keys(block_hash, validator, network)
|
||||
else {
|
||||
Err(Error::Internal("couldn't fetch the keys for the requested validator set"))?
|
||||
};
|
||||
Ok(embedded_elliptic_curve_keys.map(|embedded_elliptic_curve_keys| {
|
||||
hex::encode(borsh::to_vec(&embedded_elliptic_curve_keys).unwrap())
|
||||
}))
|
||||
},
|
||||
);
|
||||
|
||||
module
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user