Update all of serai-coordinator to compile with the new serai-client-serai

This commit is contained in:
Luke Parker
2025-11-16 11:50:24 -05:00
parent 9891ccade8
commit 7a314baa9f
37 changed files with 1014 additions and 1081 deletions

View File

@@ -24,7 +24,9 @@ simple-request = { path = "../../../common/request", version = "0.3" }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
borsh = { version = "1", default-features = false, features = ["std"] }
serai-abi = { path = "../../abi", version = "0.1" }
bitvec = { version = "1", default-features = false, features = ["alloc", "std"] }
serai-abi = { path = "../../abi", version = "0.1", default-features = false, features = ["std"] }
async-lock = "3"

View File

@@ -0,0 +1,52 @@
pub use serai_abi::{
primitives::instructions::SignedBatch,
in_instructions::{Call, Event},
UnsignedCall, Transaction,
};
use crate::{RpcError, TemporalSerai};
/// A `TemporalSerai` scoped to the in instructions module.
#[derive(Clone)]
pub struct InInstructions<'serai>(pub(super) &'serai TemporalSerai<'serai>);
impl<'serai> InInstructions<'serai> {
/// The events from the in instructions module.
pub async fn events(&self) -> Result<Vec<Event>, RpcError> {
Ok(
self
.0
.events_borrowed()
.await?
.as_ref()
.expect("`TemporalSerai::events` returned None")
.iter()
.flat_map(IntoIterator::into_iter)
.filter_map(|event| match event {
serai_abi::Event::InInstructions(event) => Some(event.clone()),
_ => None,
})
.collect(),
)
}
/// The `Batch` events from the in instructions module.
pub async fn batch_events(&self) -> Result<Vec<Event>, RpcError> {
Ok(
self
.events()
.await?
.into_iter()
.filter(|event| matches!(event, Event::Batch { .. }))
.collect(),
)
}
/// Create a transaction to execute a batch.
pub fn execute_batch(batch: SignedBatch) -> Transaction {
Transaction::Unsigned {
call: UnsignedCall::try_from(serai_abi::Call::from(Call::execute_batch { batch }))
.expect("`execute_batch` wasn't an unsigned call?"),
}
}
}

View File

@@ -25,6 +25,9 @@ pub use coins::Coins;
mod validator_sets;
pub use validator_sets::ValidatorSets;
mod in_instructions;
pub use in_instructions::InInstructions;
/// An error from the RPC.
#[derive(Debug, Error)]
pub enum RpcError {
@@ -58,8 +61,8 @@ pub struct Serai {
/// from this block will be cached within this. This allows future calls for events to be done
/// cheaply.
#[derive(Clone)]
pub struct TemporalSerai<'a> {
serai: &'a Serai,
pub struct TemporalSerai<'serai> {
serai: &'serai Serai,
block: BlockHash,
events: Arc<RwLock<Option<Vec<Vec<Event>>>>>,
}
@@ -176,7 +179,7 @@ impl Serai {
///
/// This will yield an error if the block chosen isn't finalized. This ensures, given an honest
/// node, that this scope will be available for the lifetime of this object.
pub async fn as_of<'a>(&'a self, block: BlockHash) -> Result<TemporalSerai<'a>, RpcError> {
pub async fn as_of(&self, block: BlockHash) -> Result<TemporalSerai<'_>, RpcError> {
if !self.finalized(block).await? {
Err(RpcError::NotFinalized)?;
}
@@ -184,10 +187,7 @@ impl Serai {
}
/// Scope this RPC client to the state as of the latest finalized block.
pub async fn as_of_latest_finalized_block<'a>(
&'a self,
block: BlockHash,
) -> Result<TemporalSerai<'a>, RpcError> {
pub async fn as_of_latest_finalized_block(&self) -> Result<TemporalSerai<'_>, RpcError> {
let block = self
.block_by_number(self.latest_finalized_block_number().await?)
.await?
@@ -215,7 +215,7 @@ impl Serai {
}
}
impl<'a> TemporalSerai<'a> {
impl<'serai> TemporalSerai<'serai> {
async fn call<ResponseValue: Default + JsonDeserialize>(
&self,
method: &str,
@@ -282,4 +282,9 @@ impl<'a> TemporalSerai<'a> {
pub fn validator_sets(&self) -> ValidatorSets<'_> {
ValidatorSets(self)
}
/// Scope to the in instructions module.
pub fn in_instructions(&self) -> InInstructions<'_> {
InInstructions(self)
}
}

View File

@@ -2,12 +2,14 @@ use borsh::BorshDeserialize;
pub use serai_abi::{
primitives::{
crypto::KeyPair,
crypto::{Signature, KeyPair, EmbeddedEllipticCurveKeys},
network_id::{ExternalNetworkId, NetworkId},
validator_sets::{Session, ExternalValidatorSet, ValidatorSet},
validator_sets::{Session, ExternalValidatorSet, ValidatorSet, SlashReport},
balance::Amount,
address::SeraiAddress,
},
validator_sets::Event,
validator_sets::{Call, Event},
UnsignedCall, Transaction,
};
use crate::{RpcError, TemporalSerai};
@@ -24,9 +26,9 @@ fn rpc_network(network: impl Into<NetworkId>) -> Result<&'static str, RpcError>
/// A `TemporalSerai` scoped to the validator sets module.
#[derive(Clone)]
pub struct ValidatorSets<'a>(pub(super) &'a TemporalSerai<'a>);
pub struct ValidatorSets<'serai>(pub(super) &'serai TemporalSerai<'serai>);
impl<'a> ValidatorSets<'a> {
impl<'serai> ValidatorSets<'serai> {
/// The events from the validator sets module.
pub async fn events(&self) -> Result<Vec<Event>, RpcError> {
Ok(
@@ -108,7 +110,7 @@ impl<'a> ValidatorSets<'a> {
)
}
/// The stake for the current validators for specified network.
/// The stake for the current validators for the specified network.
pub async fn current_stake(&self, network: NetworkId) -> Result<Option<Amount>, RpcError> {
Ok(
self
@@ -142,4 +144,38 @@ impl<'a> ValidatorSets<'a> {
.map(Some)
.map_err(|_| RpcError::InvalidNode("validator set's keys weren't a valid key pair".to_string()))
}
/// Create a transaction to set a validator set's keys.
pub fn set_keys(
network: ExternalNetworkId,
key_pair: KeyPair,
signature_participants: bitvec::vec::BitVec<u8, bitvec::order::Lsb0>,
signature: Signature,
) -> Transaction {
Transaction::Unsigned {
call: UnsignedCall::try_from(serai_abi::Call::from(Call::set_keys {
network,
key_pair,
signature_participants,
signature,
}))
.expect("`set_keys` wasn't an unsigned call?"),
}
}
/// Create a transaction to report the slashes for a validator set.
pub fn report_slashes(
network: ExternalNetworkId,
slashes: SlashReport,
signature: Signature,
) -> Transaction {
Transaction::Unsigned {
call: UnsignedCall::try_from(serai_abi::Call::from(Call::report_slashes {
network,
slashes,
signature,
}))
.expect("`report_slashes` wasn't an unsigned call?"),
}
}
}