espresso_node/request_response/
request.rs

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