web3-proxy/web3_proxy/src/rpcs/synced_connections.rs

60 lines
1.8 KiB
Rust
Raw Normal View History

2022-12-03 08:31:03 +03:00
use super::blockchain::SavedBlock;
2022-08-24 03:11:49 +03:00
use super::connection::Web3Connection;
use super::connections::Web3Connections;
2022-08-24 02:13:56 +03:00
use ethers::prelude::{H256, U64};
use serde::Serialize;
use std::fmt;
2022-08-24 02:13:56 +03:00
use std::sync::Arc;
/// A collection of Web3Connections that are on the same block.
/// Serialize is so we can print it on our debug endpoint
#[derive(Clone, Default, Serialize)]
pub struct SyncedConnections {
2022-08-30 23:01:42 +03:00
// TODO: store ArcBlock instead?
2022-12-03 08:31:03 +03:00
pub(super) head_block_id: Option<SavedBlock>,
2022-08-24 02:13:56 +03:00
// TODO: this should be able to serialize, but it isn't
#[serde(skip_serializing)]
2022-09-05 06:40:13 +03:00
pub(super) conns: Vec<Arc<Web3Connection>>,
2022-08-24 02:13:56 +03:00
}
impl fmt::Debug for SyncedConnections {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO: the default formatter takes forever to write. this is too quiet though
// TODO: print the actual conns?
f.debug_struct("SyncedConnections")
2022-09-01 08:58:55 +03:00
.field("head_block_id", &self.head_block_id)
.field("num_conns", &self.conns.len())
.finish_non_exhaustive()
}
}
impl Web3Connections {
2022-12-03 08:31:03 +03:00
pub fn head_block_id(&self) -> Option<SavedBlock> {
2022-09-01 08:58:55 +03:00
self.synced_connections.load().head_block_id.clone()
}
2022-09-01 08:58:55 +03:00
pub fn head_block_hash(&self) -> Option<H256> {
self.synced_connections
.load()
.head_block_id
.as_ref()
2022-12-03 08:31:03 +03:00
.map(|head_block_id| head_block_id.hash())
}
2022-09-01 08:58:55 +03:00
pub fn head_block_num(&self) -> Option<U64> {
self.synced_connections
.load()
.head_block_id
.as_ref()
2022-12-03 08:31:03 +03:00
.map(|head_block_id| head_block_id.number())
}
pub fn synced(&self) -> bool {
2022-08-27 02:44:25 +03:00
!self.synced_connections.load().conns.is_empty()
}
pub fn num_synced_rpcs(&self) -> usize {
self.synced_connections.load().conns.len()
}
}