cargo fmt, move ScannerFeed from String to the RPC error

This commit is contained in:
Luke Parker 2024-09-18 18:35:31 -04:00
parent a717ae9ea7
commit 9e628d217f
2 changed files with 33 additions and 16 deletions

View file

@ -26,7 +26,9 @@ impl TransactionPublisher {
}
// This will always return Ok(Some(_)) or Err(_), never Ok(None)
async fn router(&self) -> Result<RwLockReadGuard<'_, Option<Router>>, RpcError<TransportErrorKind>> {
async fn router(
&self,
) -> Result<RwLockReadGuard<'_, Option<Router>>, RpcError<TransportErrorKind>> {
let router = self.router.read().await;
// If the router is None, find it on-chain
@ -35,9 +37,14 @@ impl TransactionPublisher {
let mut router = self.router.write().await;
// Check again if it's None in case a different task already did this
if router.is_none() {
let Some(router_actual) = Router::new(self.rpc.clone(), &self.initial_serai_key).await? else {
Err(TransportErrorKind::Custom("publishing transaction yet couldn't find router on chain. was our node reset?".to_string().into()))?
};
let Some(router_actual) = Router::new(self.rpc.clone(), &self.initial_serai_key).await?
else {
Err(TransportErrorKind::Custom(
"publishing transaction yet couldn't find router on chain. was our node reset?"
.to_string()
.into(),
))?
};
*router = Some(router_actual);
}
return Ok(router.downgrade());
@ -60,7 +67,9 @@ impl signers::TransactionPublisher<Transaction> for TransactionPublisher {
let router = router.as_ref().unwrap();
let tx = match tx.0 {
Action::SetKey { chain_id: _, nonce: _, key } => router.update_serai_key(&key, &tx.1),
Action::Batch { chain_id: _, nonce: _, outs } => router.execute(OutInstructions::from(outs.as_ref()), &tx.1),
Action::Batch { chain_id: _, nonce: _, outs } => {
router.execute(OutInstructions::from(outs.as_ref()), &tx.1)
}
};
/*

View file

@ -2,6 +2,7 @@ use core::future::Future;
use std::sync::Arc;
use alloy_rpc_types_eth::{BlockTransactionsKind, BlockNumberOrTag};
use alloy_transport::{RpcError, TransportErrorKind};
use alloy_simple_request_transport::SimpleRequest;
use alloy_provider::{Provider, RootProvider};
@ -28,7 +29,7 @@ impl ScannerFeed for Rpc {
type Block = FullEpoch;
type EphemeralError = String;
type EphemeralError = RpcError<TransportErrorKind>;
fn latest_finalized_block_number(
&self,
@ -37,14 +38,17 @@ impl ScannerFeed for Rpc {
let actual_number = self
.provider
.get_block(BlockNumberOrTag::Finalized.into(), BlockTransactionsKind::Hashes)
.await
.map_err(|e| format!("couldn't get the latest finalized block: {e:?}"))?
.ok_or_else(|| "there was no finalized block".to_string())?
.await?
.ok_or_else(|| {
TransportErrorKind::Custom("there was no finalized block".to_string().into())
})?
.header
.number;
// Error if there hasn't been a full epoch yet
if actual_number < 32 {
Err("there has not been a completed epoch yet".to_string())?
Err(TransportErrorKind::Custom(
"there has not been a completed epoch yet".to_string().into(),
))?
}
// The divison by 32 returns the amount of completed epochs
// Converting from amount of completed epochs to the latest completed epoch requires
@ -75,10 +79,12 @@ impl ScannerFeed for Rpc {
self
.provider
.get_block((start - 1).into(), BlockTransactionsKind::Hashes)
.await
.map_err(|e| format!("couldn't get block: {e:?}"))?
.await?
.ok_or_else(|| {
format!("ethereum node didn't have requested block: {number:?}. did we reorg?")
TransportErrorKind::Custom(
format!("ethereum node didn't have requested block: {number:?}. was the node reset?")
.into(),
)
})?
.header
.hash
@ -88,10 +94,12 @@ impl ScannerFeed for Rpc {
let end_header = self
.provider
.get_block((start + 31).into(), BlockTransactionsKind::Hashes)
.await
.map_err(|e| format!("couldn't get block: {e:?}"))?
.await?
.ok_or_else(|| {
format!("ethereum node didn't have requested block: {number:?}. did we reorg?")
TransportErrorKind::Custom(
format!("ethereum node didn't have requested block: {number:?}. was the node reset?")
.into(),
)
})?
.header;