tendermint/v0_37/abci/
request.rs

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