mirror of
https://github.com/serai-dex/serai.git
synced 2025-02-04 12:16:37 +00:00
cargo fmt, move ScannerFeed from String to the RPC error
This commit is contained in:
parent
a717ae9ea7
commit
9e628d217f
2 changed files with 33 additions and 16 deletions
|
@ -26,7 +26,9 @@ impl TransactionPublisher {
|
||||||
}
|
}
|
||||||
|
|
||||||
// This will always return Ok(Some(_)) or Err(_), never Ok(None)
|
// 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;
|
let router = self.router.read().await;
|
||||||
|
|
||||||
// If the router is None, find it on-chain
|
// If the router is None, find it on-chain
|
||||||
|
@ -35,8 +37,13 @@ impl TransactionPublisher {
|
||||||
let mut router = self.router.write().await;
|
let mut router = self.router.write().await;
|
||||||
// Check again if it's None in case a different task already did this
|
// Check again if it's None in case a different task already did this
|
||||||
if router.is_none() {
|
if router.is_none() {
|
||||||
let Some(router_actual) = Router::new(self.rpc.clone(), &self.initial_serai_key).await? else {
|
let Some(router_actual) = Router::new(self.rpc.clone(), &self.initial_serai_key).await?
|
||||||
Err(TransportErrorKind::Custom("publishing transaction yet couldn't find router on chain. was our node reset?".to_string().into()))?
|
else {
|
||||||
|
Err(TransportErrorKind::Custom(
|
||||||
|
"publishing transaction yet couldn't find router on chain. was our node reset?"
|
||||||
|
.to_string()
|
||||||
|
.into(),
|
||||||
|
))?
|
||||||
};
|
};
|
||||||
*router = Some(router_actual);
|
*router = Some(router_actual);
|
||||||
}
|
}
|
||||||
|
@ -60,7 +67,9 @@ impl signers::TransactionPublisher<Transaction> for TransactionPublisher {
|
||||||
let router = router.as_ref().unwrap();
|
let router = router.as_ref().unwrap();
|
||||||
let tx = match tx.0 {
|
let tx = match tx.0 {
|
||||||
Action::SetKey { chain_id: _, nonce: _, key } => router.update_serai_key(&key, &tx.1),
|
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)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -2,6 +2,7 @@ use core::future::Future;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use alloy_rpc_types_eth::{BlockTransactionsKind, BlockNumberOrTag};
|
use alloy_rpc_types_eth::{BlockTransactionsKind, BlockNumberOrTag};
|
||||||
|
use alloy_transport::{RpcError, TransportErrorKind};
|
||||||
use alloy_simple_request_transport::SimpleRequest;
|
use alloy_simple_request_transport::SimpleRequest;
|
||||||
use alloy_provider::{Provider, RootProvider};
|
use alloy_provider::{Provider, RootProvider};
|
||||||
|
|
||||||
|
@ -28,7 +29,7 @@ impl ScannerFeed for Rpc {
|
||||||
|
|
||||||
type Block = FullEpoch;
|
type Block = FullEpoch;
|
||||||
|
|
||||||
type EphemeralError = String;
|
type EphemeralError = RpcError<TransportErrorKind>;
|
||||||
|
|
||||||
fn latest_finalized_block_number(
|
fn latest_finalized_block_number(
|
||||||
&self,
|
&self,
|
||||||
|
@ -37,14 +38,17 @@ impl ScannerFeed for Rpc {
|
||||||
let actual_number = self
|
let actual_number = self
|
||||||
.provider
|
.provider
|
||||||
.get_block(BlockNumberOrTag::Finalized.into(), BlockTransactionsKind::Hashes)
|
.get_block(BlockNumberOrTag::Finalized.into(), BlockTransactionsKind::Hashes)
|
||||||
.await
|
.await?
|
||||||
.map_err(|e| format!("couldn't get the latest finalized block: {e:?}"))?
|
.ok_or_else(|| {
|
||||||
.ok_or_else(|| "there was no finalized block".to_string())?
|
TransportErrorKind::Custom("there was no finalized block".to_string().into())
|
||||||
|
})?
|
||||||
.header
|
.header
|
||||||
.number;
|
.number;
|
||||||
// Error if there hasn't been a full epoch yet
|
// Error if there hasn't been a full epoch yet
|
||||||
if actual_number < 32 {
|
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
|
// The divison by 32 returns the amount of completed epochs
|
||||||
// Converting from amount of completed epochs to the latest completed epoch requires
|
// Converting from amount of completed epochs to the latest completed epoch requires
|
||||||
|
@ -75,10 +79,12 @@ impl ScannerFeed for Rpc {
|
||||||
self
|
self
|
||||||
.provider
|
.provider
|
||||||
.get_block((start - 1).into(), BlockTransactionsKind::Hashes)
|
.get_block((start - 1).into(), BlockTransactionsKind::Hashes)
|
||||||
.await
|
.await?
|
||||||
.map_err(|e| format!("couldn't get block: {e:?}"))?
|
|
||||||
.ok_or_else(|| {
|
.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
|
.header
|
||||||
.hash
|
.hash
|
||||||
|
@ -88,10 +94,12 @@ impl ScannerFeed for Rpc {
|
||||||
let end_header = self
|
let end_header = self
|
||||||
.provider
|
.provider
|
||||||
.get_block((start + 31).into(), BlockTransactionsKind::Hashes)
|
.get_block((start + 31).into(), BlockTransactionsKind::Hashes)
|
||||||
.await
|
.await?
|
||||||
.map_err(|e| format!("couldn't get block: {e:?}"))?
|
|
||||||
.ok_or_else(|| {
|
.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;
|
.header;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue