Skip to main content

espresso_api/v1/
status.rs

1//! V1 status API.
2//!
3//! Mirrors the endpoints of the legacy `hotshot-query-service` status API (its tide-disco
4//! `status.toml`, since removed).
5
6use alloy::primitives::Address;
7use async_trait::async_trait;
8use hotshot_types::{addr::NetAddr, light_client::StateVerKey, signature_key::BLSPubKey, x25519};
9use serde::{Deserialize, Serialize};
10
11#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
12pub struct NodePublicKeys {
13    pub eth_account: Option<Address>,
14    pub consensus_key: BLSPubKey,
15    pub state_ver_key: StateVerKey,
16    #[serde(with = "x25519_tagged")]
17    pub x25519_key: Option<x25519::PublicKey>,
18    /// Cliquenet address peers dial: the configured advertise address, `None` when there is none.
19    pub p2p_addr: Option<NetAddr>,
20}
21
22mod x25519_tagged {
23    use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as _};
24
25    use super::x25519;
26
27    pub fn serialize<S: Serializer>(
28        key: &Option<x25519::PublicKey>,
29        s: S,
30    ) -> Result<S::Ok, S::Error> {
31        key.as_ref().map(ToString::to_string).serialize(s)
32    }
33
34    pub fn deserialize<'de, D: Deserializer<'de>>(
35        d: D,
36    ) -> Result<Option<x25519::PublicKey>, D::Error> {
37        Option::<String>::deserialize(d)?
38            .map(|s| s.parse().map_err(D::Error::custom))
39            .transpose()
40    }
41}
42
43#[async_trait]
44pub trait StatusApi {
45    async fn block_height(&self) -> anyhow::Result<u64>;
46    async fn success_rate(&self) -> anyhow::Result<f64>;
47    async fn time_since_last_decide(&self) -> anyhow::Result<u64>;
48
49    async fn metrics(&self) -> anyhow::Result<String>;
50
51    async fn keys(&self) -> anyhow::Result<NodePublicKeys>;
52}