celestia_core/v0_34/abci/
request.rs

1use celestia_core_proto::v0_34::abci as pb;
2use celestia_core_proto::Protobuf;
3
4use crate::abci::request::{ConsensusRequest, InfoRequest, MempoolRequest, SnapshotRequest};
5use crate::abci::MethodKind;
6use crate::Error;
7
8pub use crate::abci::request::{
9    ApplySnapshotChunk, BeginBlock, CheckTx, CheckTxKind, DeliverTx, Echo, EndBlock, Info,
10    InitChain, LoadSnapshotChunk, OfferSnapshot, PrepareProposal, ProcessProposal, Query,
11    SetOption,
12};
13
14/// All possible ABCI requests.
15#[allow(clippy::large_enum_variant)]
16#[derive(Clone, PartialEq, Eq, Debug)]
17pub enum Request {
18    #[doc = include_str!("../../abci/doc/request-echo.md")]
19    Echo(Echo),
20    #[doc = include_str!("../../abci/doc/request-flush.md")]
21    Flush,
22    #[doc = include_str!("../../abci/doc/request-info.md")]
23    Info(Info),
24    #[doc = include_str!("../../abci/doc/request-setoption.md")]
25    SetOption(SetOption),
26    #[doc = include_str!("../../abci/doc/request-initchain.md")]
27    InitChain(InitChain),
28    #[doc = include_str!("../../abci/doc/request-query.md")]
29    Query(Query),
30    #[doc = include_str!("../../abci/doc/request-beginblock.md")]
31    BeginBlock(BeginBlock),
32    #[doc = include_str!("../../abci/doc/request-checktx.md")]
33    CheckTx(CheckTx),
34    #[doc = include_str!("../../abci/doc/request-delivertx.md")]
35    DeliverTx(DeliverTx),
36    #[doc = include_str!("../../abci/doc/request-endblock.md")]
37    EndBlock(EndBlock),
38    #[doc = include_str!("../../abci/doc/request-commit.md")]
39    Commit,
40    #[doc = include_str!("../../abci/doc/request-listsnapshots.md")]
41    ListSnapshots,
42    #[doc = include_str!("../../abci/doc/request-offersnapshot.md")]
43    OfferSnapshot(OfferSnapshot),
44    #[doc = include_str!("../../abci/doc/request-loadsnapshotchunk.md")]
45    LoadSnapshotChunk(LoadSnapshotChunk),
46    #[doc = include_str!("../../abci/doc/request-applysnapshotchunk.md")]
47    ApplySnapshotChunk(ApplySnapshotChunk),
48    PrepareProposal(PrepareProposal),
49    ProcessProposal(ProcessProposal),
50}
51
52impl Request {
53    /// Get the method kind for this request.
54    pub fn kind(&self) -> MethodKind {
55        use Request::*;
56        match self {
57            Flush => MethodKind::Flush,
58            InitChain(_) => MethodKind::Consensus,
59            PrepareProposal(_) => MethodKind::Consensus,
60            ProcessProposal(_) => MethodKind::Consensus,
61            BeginBlock(_) => MethodKind::Consensus,
62            DeliverTx(_) => MethodKind::Consensus,
63            EndBlock(_) => MethodKind::Consensus,
64            Commit => MethodKind::Consensus,
65            CheckTx(_) => MethodKind::Mempool,
66            ListSnapshots => MethodKind::Snapshot,
67            OfferSnapshot(_) => MethodKind::Snapshot,
68            LoadSnapshotChunk(_) => MethodKind::Snapshot,
69            ApplySnapshotChunk(_) => MethodKind::Snapshot,
70            Info(_) => MethodKind::Info,
71            Query(_) => MethodKind::Info,
72            Echo(_) => MethodKind::Info,
73            SetOption(_) => MethodKind::Info,
74        }
75    }
76}
77
78impl From<ConsensusRequest> for Request {
79    fn from(req: ConsensusRequest) -> Self {
80        match req {
81            ConsensusRequest::InitChain(x) => Self::InitChain(x),
82            ConsensusRequest::PrepareProposal(_) => {
83                panic!("Cannot convert PrepareProposal into a v0.34 Request")
84            }
85            ConsensusRequest::ProcessProposal(_) => {
86                panic!("Cannot convert ProcessProposal into a v0.34 Request")
87            }
88            ConsensusRequest::BeginBlock(x) => Self::BeginBlock(x),
89            ConsensusRequest::DeliverTx(x) => Self::DeliverTx(x),
90            ConsensusRequest::EndBlock(x) => Self::EndBlock(x),
91            ConsensusRequest::Commit => Self::Commit,
92        }
93    }
94}
95
96impl TryFrom<Request> for ConsensusRequest {
97    type Error = Error;
98    fn try_from(req: Request) -> Result<Self, Self::Error> {
99        match req {
100            Request::InitChain(x) => Ok(Self::InitChain(x)),
101            Request::BeginBlock(x) => Ok(Self::BeginBlock(x)),
102            Request::DeliverTx(x) => Ok(Self::DeliverTx(x)),
103            Request::EndBlock(x) => Ok(Self::EndBlock(x)),
104            Request::Commit => Ok(Self::Commit),
105            _ => Err(Error::invalid_abci_request_type()),
106        }
107    }
108}
109
110impl From<MempoolRequest> for Request {
111    fn from(req: MempoolRequest) -> Self {
112        match req {
113            MempoolRequest::CheckTx(x) => Self::CheckTx(x),
114        }
115    }
116}
117
118impl TryFrom<Request> for MempoolRequest {
119    type Error = Error;
120    fn try_from(req: Request) -> Result<Self, Self::Error> {
121        match req {
122            Request::CheckTx(x) => Ok(Self::CheckTx(x)),
123            _ => Err(Error::invalid_abci_request_type()),
124        }
125    }
126}
127
128impl From<InfoRequest> for Request {
129    fn from(req: InfoRequest) -> Self {
130        match req {
131            InfoRequest::Info(x) => Self::Info(x),
132            InfoRequest::Query(x) => Self::Query(x),
133            InfoRequest::Echo(x) => Self::Echo(x),
134            InfoRequest::SetOption(x) => Self::SetOption(x),
135        }
136    }
137}
138
139impl TryFrom<Request> for InfoRequest {
140    type Error = Error;
141    fn try_from(req: Request) -> Result<Self, Self::Error> {
142        match req {
143            Request::Info(x) => Ok(Self::Info(x)),
144            Request::Query(x) => Ok(Self::Query(x)),
145            Request::Echo(x) => Ok(Self::Echo(x)),
146            Request::SetOption(x) => Ok(Self::SetOption(x)),
147            _ => Err(Error::invalid_abci_request_type()),
148        }
149    }
150}
151
152impl From<SnapshotRequest> for Request {
153    fn from(req: SnapshotRequest) -> Self {
154        match req {
155            SnapshotRequest::ListSnapshots => Self::ListSnapshots,
156            SnapshotRequest::OfferSnapshot(x) => Self::OfferSnapshot(x),
157            SnapshotRequest::LoadSnapshotChunk(x) => Self::LoadSnapshotChunk(x),
158            SnapshotRequest::ApplySnapshotChunk(x) => Self::ApplySnapshotChunk(x),
159        }
160    }
161}
162
163impl TryFrom<Request> for SnapshotRequest {
164    type Error = Error;
165    fn try_from(req: Request) -> Result<Self, Self::Error> {
166        match req {
167            Request::ListSnapshots => Ok(Self::ListSnapshots),
168            Request::OfferSnapshot(x) => Ok(Self::OfferSnapshot(x)),
169            Request::LoadSnapshotChunk(x) => Ok(Self::LoadSnapshotChunk(x)),
170            Request::ApplySnapshotChunk(x) => Ok(Self::ApplySnapshotChunk(x)),
171            _ => Err(Error::invalid_abci_request_type()),
172        }
173    }
174}
175
176// =============================================================================
177// Protobuf conversions
178// =============================================================================
179
180impl From<Request> for pb::Request {
181    fn from(request: Request) -> pb::Request {
182        use pb::request::Value;
183        let value = match request {
184            Request::Echo(x) => Some(Value::Echo(x.into())),
185            Request::Flush => Some(Value::Flush(Default::default())),
186            Request::Info(x) => Some(Value::Info(x.into())),
187            Request::SetOption(x) => Some(Value::SetOption(x.into())),
188            Request::InitChain(x) => Some(Value::InitChain(x.into())),
189            Request::Query(x) => Some(Value::Query(x.into())),
190            Request::BeginBlock(x) => Some(Value::BeginBlock(x.into())),
191            Request::CheckTx(x) => Some(Value::CheckTx(x.into())),
192            Request::DeliverTx(x) => Some(Value::DeliverTx(x.into())),
193            Request::EndBlock(x) => Some(Value::EndBlock(x.into())),
194            Request::Commit => Some(Value::Commit(Default::default())),
195            Request::ListSnapshots => Some(Value::ListSnapshots(Default::default())),
196            Request::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())),
197            Request::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())),
198            Request::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())),
199            Request::PrepareProposal(x) => Some(Value::PrepareProposal(x.into())),
200            Request::ProcessProposal(x) => Some(Value::ProcessProposal(x.into())),
201        };
202        pb::Request { value }
203    }
204}
205
206impl TryFrom<pb::Request> for Request {
207    type Error = Error;
208
209    fn try_from(request: pb::Request) -> Result<Self, Self::Error> {
210        use pb::request::Value;
211        match request.value {
212            Some(Value::Echo(x)) => Ok(Request::Echo(x.try_into()?)),
213            Some(Value::Flush(pb::RequestFlush {})) => Ok(Request::Flush),
214            Some(Value::Info(x)) => Ok(Request::Info(x.try_into()?)),
215            Some(Value::SetOption(x)) => Ok(Request::SetOption(x.try_into()?)),
216            Some(Value::InitChain(x)) => Ok(Request::InitChain(x.try_into()?)),
217            Some(Value::Query(x)) => Ok(Request::Query(x.try_into()?)),
218            Some(Value::BeginBlock(x)) => Ok(Request::BeginBlock(x.try_into()?)),
219            Some(Value::CheckTx(x)) => Ok(Request::CheckTx(x.try_into()?)),
220            Some(Value::DeliverTx(x)) => Ok(Request::DeliverTx(x.try_into()?)),
221            Some(Value::EndBlock(x)) => Ok(Request::EndBlock(x.try_into()?)),
222            Some(Value::Commit(pb::RequestCommit {})) => Ok(Request::Commit),
223            Some(Value::ListSnapshots(pb::RequestListSnapshots {})) => Ok(Request::ListSnapshots),
224            Some(Value::OfferSnapshot(x)) => Ok(Request::OfferSnapshot(x.try_into()?)),
225            Some(Value::LoadSnapshotChunk(x)) => Ok(Request::LoadSnapshotChunk(x.try_into()?)),
226            Some(Value::ApplySnapshotChunk(x)) => Ok(Request::ApplySnapshotChunk(x.try_into()?)),
227            Some(Value::PrepareProposal(x)) => Ok(Request::PrepareProposal(x.try_into()?)),
228            Some(Value::ProcessProposal(x)) => Ok(Request::ProcessProposal(x.try_into()?)),
229            None => Err(crate::Error::missing_data()),
230        }
231    }
232}
233
234impl Protobuf<pb::Request> for Request {}