Skip to main content

espresso_node/request_response/
request.rs

1use anyhow::{Context, Result};
2use committable::Commitment;
3use espresso_types::{
4    Certificate2, FeeAccount, FeeMerkleTree, Leaf2,
5    v0_3::{ChainConfig, RewardAccountV1, RewardMerkleTreeV1},
6    v0_4::{RewardAccountV2, RewardMerkleTreeV2},
7};
8use hotshot_types::{data::VidShare, simple_certificate::LightClientStateUpdateCertificateV2};
9use request_response::{Serializable, request::Request as RequestTrait};
10use serde::{Deserialize, Serialize};
11
12use crate::{SeqTypes, api::BlocksFrontier};
13
14// Some type aliases for readability
15type Height = u64;
16type ViewNumber = u64;
17type RequestId = u64;
18
19/// The outermost request type. This an enum that contains all the possible requests that the
20/// sequencer can make.
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub enum Request {
23    /// A request for the accounts at a given height and view
24    Accounts(Height, ViewNumber, Vec<FeeAccount>),
25    /// A request for the leaf chain at a given height
26    Leaf(Height),
27    /// A request for a chain config with a particular commitment
28    ChainConfig(Commitment<ChainConfig>),
29    /// A request for the blocks frontier
30    BlocksFrontier(Height, ViewNumber),
31    /// A request for the reward accounts at a given height and view
32    RewardAccountsV2(Height, ViewNumber, Vec<RewardAccountV2>),
33    /// A request for the v1 reward accounts at a given height and view
34    RewardAccountsV1(Height, ViewNumber, Vec<RewardAccountV1>),
35    /// A request for the VID share at the given block height
36    VidShare(Height, RequestId),
37    /// A request for the state certificate at a given epoch
38    StateCert(u64),
39    /// A request for data to reconstruct the reward merkle tree at a given height
40    RewardMerkleTreeV2(u64, ViewNumber),
41    /// A request for the cert2 at or above the given height
42    Cert2(Height),
43}
44
45/// The outermost response type. This an enum that contains all the possible responses that the
46/// sequencer can make.
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub enum Response {
49    /// A response for the accounts at a given height and view
50    Accounts(FeeMerkleTree),
51    /// A request for the leaf chain at a given height
52    Leaf(Vec<Leaf2>),
53    /// A response for a chain config with a particular commitment
54    ChainConfig(ChainConfig),
55    /// A response for the blocks frontier
56    BlocksFrontier(BlocksFrontier),
57    /// A response for the reward accounts at a given height and view
58    RewardAccountsV2(RewardMerkleTreeV2),
59    /// A response for the v1 reward accounts at a given height and view
60    RewardAccountsV1(RewardMerkleTreeV1),
61    /// A response for a VID share at the given block height
62    VidShare(VidShare),
63    /// A response for a state certificate at a given epoch
64    StateCert(LightClientStateUpdateCertificateV2<SeqTypes>),
65    /// A response with data to reconstruct the reward merkle tree at a given height
66    RewardMerkleTreeV2(Vec<u8>),
67    /// A response with the earliest cert2 (fast finality protocol)
68    Cert2(Certificate2<SeqTypes>),
69}
70
71/// Implement the `RequestTrait` trait for the `Request` type. This tells the request response
72/// protocol how to validate the request and what the response type is.
73impl RequestTrait for Request {
74    type Response = Response;
75
76    fn validate(&self) -> Result<()> {
77        // Right now, all requests are valid
78        Ok(())
79    }
80}
81
82/// Implement the `Serializable` trait for the `Request` type. This tells the request response
83/// protocol how to serialize and deserialize the request
84impl Serializable for Request {
85    fn to_bytes(&self) -> Result<Vec<u8>> {
86        bincode::serialize(&self).with_context(|| "failed to serialize")
87    }
88
89    fn from_bytes(bytes: &[u8]) -> Result<Self> {
90        bincode::deserialize(bytes).with_context(|| "failed to deserialize")
91    }
92}
93
94/// Implement the `Serializable` trait for the `Response` type. This tells the request response
95/// protocol how to serialize and deserialize the response.
96impl Serializable for Response {
97    fn to_bytes(&self) -> Result<Vec<u8>> {
98        bincode::serialize(self).with_context(|| "failed to serialize")
99    }
100
101    fn from_bytes(bytes: &[u8]) -> Result<Self> {
102        bincode::deserialize(bytes).with_context(|| "failed to deserialize")
103    }
104}