sequencer/request_response/
request.rs

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