Start defining the coordinator

This commit is contained in:
Luke Parker
2023-04-15 17:38:47 -04:00
parent 51bf51ae1e
commit eafd054296
7 changed files with 135 additions and 19 deletions

View File

@@ -16,7 +16,7 @@ use subxt::{
extrinsic_params::{BaseExtrinsicParams, BaseExtrinsicParamsBuilder},
},
tx::{Signer, Payload, TxClient},
rpc::types::ChainBlock,
rpc::types::{ChainBlock, ChainBlockExtrinsic},
Config as SubxtConfig, OnlineClient,
};
@@ -56,7 +56,32 @@ impl SubxtConfig for SeraiConfig {
type ExtrinsicParams = BaseExtrinsicParams<SeraiConfig, Tip>;
}
pub type Block = ChainBlock<SeraiConfig>;
#[derive(Debug)]
pub struct Block(ChainBlock<SeraiConfig>);
impl Block {
pub fn hash(&self) -> [u8; 32] {
self.0.header.hash().into()
}
pub fn number(&self) -> u64 {
self.0.header.number
}
pub fn header(&self) -> &Header {
&self.0.header
}
pub fn transactions(&self) -> &[ChainBlockExtrinsic] {
&self.0.extrinsics
}
}
impl Clone for Block {
fn clone(&self) -> Block {
Block(ChainBlock::<SeraiConfig> {
header: self.0.header.clone(),
extrinsics: self.0.extrinsics.clone(),
})
}
}
#[derive(Error, Debug)]
pub enum SeraiError {
@@ -120,6 +145,19 @@ impl Serai {
Ok(self.0.rpc().finalized_head().await.map_err(SeraiError::RpcError)?.into())
}
pub async fn get_latest_block(&self) -> Result<Block, SeraiError> {
Ok(Block(
self
.0
.rpc()
.block(Some(self.0.rpc().finalized_head().await.map_err(SeraiError::RpcError)?))
.await
.map_err(SeraiError::RpcError)?
.ok_or(SeraiError::InvalidNode)?
.block,
))
}
// There is no provided method for this
// TODO: Add one to Serai
pub async fn is_finalized(&self, header: &Header) -> Result<Option<bool>, SeraiError> {
@@ -169,7 +207,7 @@ impl Serai {
return Ok(None);
}
Ok(Some(res.block))
Ok(Some(Block(res.block)))
}
// Ideally, this would be get_block_hash, not get_block_by_number

View File

@@ -2,7 +2,7 @@ use core::time::Duration;
use tokio::time::sleep;
use serai_client::subxt::{config::Header, utils::Encoded};
use serai_client::subxt::utils::Encoded;
use crate::common::serai;
@@ -10,13 +10,8 @@ use crate::common::serai;
pub async fn publish_tx(tx: &Encoded) -> [u8; 32] {
let serai = serai().await;
let mut latest = serai
.get_block(serai.get_latest_block_hash().await.unwrap())
.await
.unwrap()
.unwrap()
.header
.number();
let mut latest =
serai.get_block(serai.get_latest_block_hash().await.unwrap()).await.unwrap().unwrap().number();
serai.publish(tx).await.unwrap();
@@ -42,9 +37,9 @@ pub async fn publish_tx(tx: &Encoded) -> [u8; 32] {
block.unwrap()
};
for extrinsic in block.extrinsics {
if extrinsic.0 == tx.0[2 ..] {
return block.header.hash().into();
for transaction in block.transactions() {
if transaction.0 == tx.0[2 ..] {
return block.hash();
}
}
}

View File

@@ -10,7 +10,6 @@ use serai_client::{
primitives::{Session, ValidatorSet},
ValidatorSetsEvent,
},
subxt::config::Header,
Serai,
};
@@ -38,9 +37,7 @@ serai_test!(
// Make sure the genesis is as expected
assert_eq!(
serai
.get_new_set_events(
serai.get_block_by_number(0).await.unwrap().unwrap().header.hash().into()
)
.get_new_set_events(serai.get_block_by_number(0).await.unwrap().unwrap().hash())
.await
.unwrap(),
[BITCOIN_NET_ID, ETHEREUM_NET_ID, MONERO_NET_ID]