sequencer/state_signature/relay_server/
lcv3_relay.rs

1use std::{
2    collections::{hash_map::Entry, BTreeSet, HashMap},
3    sync::Arc,
4};
5
6use alloy::primitives::U256;
7use hotshot_task_impls::helpers::derive_signed_state_digest;
8use hotshot_types::{
9    light_client::{
10        LCV3StateSignatureRequestBody, LCV3StateSignaturesBundle, LightClientState, StateVerKey,
11    },
12    traits::signature_key::LCV3StateSignatureKey,
13};
14use tide_disco::{error::ServerError, Error, StatusCode};
15
16use super::stake_table_tracker::StakeTableTracker;
17
18#[async_trait::async_trait]
19pub trait LCV3StateRelayServerDataSource {
20    /// Get the latest available signatures bundle.
21    /// # Errors
22    /// Errors if there's no available signatures bundle.
23    fn get_latest_signature_bundle(&self) -> Result<LCV3StateSignaturesBundle, ServerError>;
24
25    /// Post a signature to the relay server
26    /// # Errors
27    /// Errors if the signature is invalid, already posted, or no longer needed.
28    async fn post_signature(
29        &mut self,
30        req: LCV3StateSignatureRequestBody,
31    ) -> Result<(), ServerError>;
32}
33
34/// Server state that tracks the light client V3 state and signatures
35pub struct LCV3StateRelayServerState {
36    /// Bundles for light client V3
37    bundles: HashMap<u64, HashMap<LightClientState, LCV3StateSignaturesBundle>>,
38
39    /// The latest state signatures bundle for legacy light client
40    latest_available_bundle: Option<LCV3StateSignaturesBundle>,
41    /// The block height of the latest available legacy state signature bundle
42    latest_block_height: Option<u64>,
43
44    /// A ordered queue of block heights for legacy light client state, used for garbage collection.
45    gc_queue: BTreeSet<u64>,
46
47    /// Stake table tracker
48    stake_table_tracker: Arc<StakeTableTracker>,
49}
50
51#[async_trait::async_trait]
52impl LCV3StateRelayServerDataSource for LCV3StateRelayServerState {
53    fn get_latest_signature_bundle(&self) -> Result<LCV3StateSignaturesBundle, ServerError> {
54        self.latest_available_bundle
55            .clone()
56            .ok_or(ServerError::catch_all(
57                StatusCode::NOT_FOUND,
58                "The light client V3 state signatures are not ready.".to_owned(),
59            ))
60    }
61
62    async fn post_signature(
63        &mut self,
64        req: LCV3StateSignatureRequestBody,
65    ) -> Result<(), ServerError> {
66        let block_height = req.state.block_height;
67        if block_height <= self.latest_block_height.unwrap_or(0) {
68            // This signature is no longer needed
69            return Ok(());
70        }
71        let stake_table = self
72            .stake_table_tracker
73            .stake_table_info_for_block(block_height)
74            .await
75            .map_err(|e| {
76                ServerError::catch_all(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
77            })?;
78        let Some(weight) = stake_table.known_nodes.get(&req.key) else {
79            tracing::warn!(
80                "Received invalid legacy signature from unknown node: {:?}",
81                req
82            );
83            return Err(ServerError::catch_all(
84                StatusCode::UNAUTHORIZED,
85                "Legacy signature posted by nodes not on the stake table".to_owned(),
86            ));
87        };
88
89        // sanity check the signature validity first before adding in
90        let signed_state_digest =
91            derive_signed_state_digest(&req.state, &req.next_stake, &req.auth_root);
92        if !<StateVerKey as LCV3StateSignatureKey>::verify_state_sig(
93            &req.key,
94            &req.signature,
95            signed_state_digest,
96        ) {
97            tracing::warn!("Received invalid legacy signature: {:?}", req);
98            return Err(ServerError::catch_all(
99                StatusCode::BAD_REQUEST,
100                "The posted legacy signature is not valid.".to_owned(),
101            ));
102        }
103
104        let bundles_at_height = self.bundles.entry(block_height).or_default();
105        self.gc_queue.insert(block_height);
106
107        let bundle = bundles_at_height
108            .entry(req.state)
109            .or_insert(LCV3StateSignaturesBundle {
110                state: req.state,
111                next_stake: req.next_stake,
112                auth_root: req.auth_root,
113                signatures: Default::default(),
114                accumulated_weight: U256::from(0),
115            });
116        tracing::debug!(
117            "Accepting new legacy signature for block height {} from {}.",
118            block_height,
119            req.key
120        );
121        match bundle.signatures.entry(req.key) {
122            Entry::Occupied(_) => {
123                // A signature is already posted for this key with this state
124                return Err(ServerError::catch_all(
125                    StatusCode::BAD_REQUEST,
126                    "A legacy signature of this light client state is already posted at this \
127                     block height for this key."
128                        .to_owned(),
129                ));
130            },
131            Entry::Vacant(entry) => {
132                entry.insert(req.signature);
133                bundle.accumulated_weight += *weight;
134            },
135        }
136
137        if bundle.accumulated_weight >= stake_table.threshold {
138            tracing::info!(
139                "Light client V3 state signature bundle at block height {} is ready to serve.",
140                block_height
141            );
142            self.latest_block_height = Some(block_height);
143            self.latest_available_bundle = Some(bundle.clone());
144
145            // garbage collect
146            self.prune(block_height);
147        }
148
149        Ok(())
150    }
151}
152
153impl LCV3StateRelayServerState {
154    /// Centralizing all garbage-collection logic, won't panic, won't error, simply do nothing if nothing to prune.
155    /// `until_height` is inclusive, meaning that would also be pruned.
156    pub fn prune(&mut self, until_height: u64) {
157        while let Some(&height) = self.gc_queue.first() {
158            if height > until_height {
159                return;
160            }
161            self.bundles.remove(&height);
162            self.gc_queue.pop_first();
163            tracing::debug!(%height, "garbage collected for ");
164        }
165    }
166
167    pub fn new(stake_table_tracker: Arc<StakeTableTracker>) -> Self {
168        Self {
169            bundles: HashMap::new(),
170            latest_available_bundle: None,
171            latest_block_height: None,
172            gc_queue: BTreeSet::new(),
173            stake_table_tracker,
174        }
175    }
176}