tendermint/v0_38/abci/
response.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
pub use crate::abci::response::{
    ApplySnapshotChunk, BeginBlock, CheckTx, Commit, DeliverTx, Echo, EndBlock, Exception,
    ExtendVote, FinalizeBlock, Info, InitChain, ListSnapshots, LoadSnapshotChunk, OfferSnapshot,
    PrepareProposal, ProcessProposal, Query, VerifyVoteExtension,
};
use crate::Error;

/// All possible ABCI responses for this protocol version.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Response {
    #[doc = include_str!("../../abci/doc/response-exception.md")]
    Exception(Exception),
    #[doc = include_str!("../../abci/doc/response-echo.md")]
    Echo(Echo),
    #[doc = include_str!("../../abci/doc/response-flush.md")]
    Flush,
    #[doc = include_str!("../../abci/doc/response-info.md")]
    Info(Info),
    #[doc = include_str!("../../abci/doc/response-initchain.md")]
    InitChain(InitChain),
    #[doc = include_str!("../../abci/doc/response-query.md")]
    Query(Query),
    #[doc = include_str!("../../abci/doc/response-checktx.md")]
    CheckTx(CheckTx),
    #[doc = include_str!("../../abci/doc/response-commit.md")]
    Commit(Commit),
    #[doc = include_str!("../../abci/doc/response-listsnapshots.md")]
    ListSnapshots(ListSnapshots),
    #[doc = include_str!("../../abci/doc/response-offersnapshot.md")]
    OfferSnapshot(OfferSnapshot),
    #[doc = include_str!("../../abci/doc/response-loadsnapshotchunk.md")]
    LoadSnapshotChunk(LoadSnapshotChunk),
    #[doc = include_str!("../../abci/doc/response-applysnapshotchunk.md")]
    ApplySnapshotChunk(ApplySnapshotChunk),
    #[doc = include_str!("../../abci/doc/response-prepareproposal.md")]
    PrepareProposal(PrepareProposal),
    #[doc = include_str!("../../abci/doc/response-processproposal.md")]
    ProcessProposal(ProcessProposal),
    #[doc = include_str!("../../abci/doc/response-extendvote.md")]
    ExtendVote(ExtendVote),
    #[doc = include_str!("../../abci/doc/response-verifyvoteextension.md")]
    VerifyVoteExtension(VerifyVoteExtension),
    #[doc = include_str!("../../abci/doc/response-finalizeblock.md")]
    FinalizeBlock(FinalizeBlock),
}

/// The consensus category of ABCI responses.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ConsensusResponse {
    #[doc = include_str!("../../abci/doc/response-initchain.md")]
    InitChain(InitChain),
    #[doc = include_str!("../../abci/doc/response-prepareproposal.md")]
    PrepareProposal(PrepareProposal),
    #[doc = include_str!("../../abci/doc/response-processproposal.md")]
    ProcessProposal(ProcessProposal),
    #[doc = include_str!("../../abci/doc/response-commit.md")]
    Commit(Commit),
    #[doc = include_str!("../../abci/doc/response-extendvote.md")]
    ExtendVote(ExtendVote),
    #[doc = include_str!("../../abci/doc/response-verifyvoteextension.md")]
    VerifyVoteExtension(VerifyVoteExtension),
    #[doc = include_str!("../../abci/doc/response-finalizeblock.md")]
    FinalizeBlock(FinalizeBlock),
}

/// The mempool category of ABCI responses.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum MempoolResponse {
    #[doc = include_str!("../../abci/doc/response-checktx.md")]
    CheckTx(CheckTx),
}

/// The info category of ABCI responses.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum InfoResponse {
    #[doc = include_str!("../../abci/doc/response-echo.md")]
    Echo(Echo),
    #[doc = include_str!("../../abci/doc/response-info.md")]
    Info(Info),
    #[doc = include_str!("../../abci/doc/response-query.md")]
    Query(Query),
}

/// The snapshot category of ABCI responses.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum SnapshotResponse {
    #[doc = include_str!("../../abci/doc/response-listsnapshots.md")]
    ListSnapshots(ListSnapshots),
    #[doc = include_str!("../../abci/doc/response-offersnapshot.md")]
    OfferSnapshot(OfferSnapshot),
    #[doc = include_str!("../../abci/doc/response-loadsnapshotchunk.md")]
    LoadSnapshotChunk(LoadSnapshotChunk),
    #[doc = include_str!("../../abci/doc/response-applysnapshotchunk.md")]
    ApplySnapshotChunk(ApplySnapshotChunk),
}

impl From<ConsensusResponse> for Response {
    fn from(req: ConsensusResponse) -> Self {
        match req {
            ConsensusResponse::InitChain(x) => Self::InitChain(x),
            ConsensusResponse::PrepareProposal(x) => Self::PrepareProposal(x),
            ConsensusResponse::ProcessProposal(x) => Self::ProcessProposal(x),
            ConsensusResponse::Commit(x) => Self::Commit(x),
            ConsensusResponse::ExtendVote(x) => Self::ExtendVote(x),
            ConsensusResponse::VerifyVoteExtension(x) => Self::VerifyVoteExtension(x),
            ConsensusResponse::FinalizeBlock(x) => Self::FinalizeBlock(x),
        }
    }
}

impl TryFrom<Response> for ConsensusResponse {
    type Error = Error;
    fn try_from(req: Response) -> Result<Self, Self::Error> {
        match req {
            Response::InitChain(x) => Ok(Self::InitChain(x)),
            Response::PrepareProposal(x) => Ok(Self::PrepareProposal(x)),
            Response::ProcessProposal(x) => Ok(Self::ProcessProposal(x)),
            Response::Commit(x) => Ok(Self::Commit(x)),
            Response::ExtendVote(x) => Ok(Self::ExtendVote(x)),
            Response::VerifyVoteExtension(x) => Ok(Self::VerifyVoteExtension(x)),
            Response::FinalizeBlock(x) => Ok(Self::FinalizeBlock(x)),
            _ => Err(Error::invalid_abci_response_type()),
        }
    }
}

impl From<MempoolResponse> for Response {
    fn from(req: MempoolResponse) -> Self {
        match req {
            MempoolResponse::CheckTx(x) => Self::CheckTx(x),
        }
    }
}

impl TryFrom<Response> for MempoolResponse {
    type Error = Error;
    fn try_from(req: Response) -> Result<Self, Self::Error> {
        match req {
            Response::CheckTx(x) => Ok(Self::CheckTx(x)),
            _ => Err(Error::invalid_abci_response_type()),
        }
    }
}

impl From<InfoResponse> for Response {
    fn from(req: InfoResponse) -> Self {
        match req {
            InfoResponse::Echo(x) => Self::Echo(x),
            InfoResponse::Info(x) => Self::Info(x),
            InfoResponse::Query(x) => Self::Query(x),
        }
    }
}

impl TryFrom<Response> for InfoResponse {
    type Error = Error;
    fn try_from(req: Response) -> Result<Self, Self::Error> {
        match req {
            Response::Echo(x) => Ok(Self::Echo(x)),
            Response::Info(x) => Ok(Self::Info(x)),
            Response::Query(x) => Ok(Self::Query(x)),
            _ => Err(Error::invalid_abci_response_type()),
        }
    }
}

impl From<SnapshotResponse> for Response {
    fn from(req: SnapshotResponse) -> Self {
        match req {
            SnapshotResponse::ListSnapshots(x) => Self::ListSnapshots(x),
            SnapshotResponse::OfferSnapshot(x) => Self::OfferSnapshot(x),
            SnapshotResponse::LoadSnapshotChunk(x) => Self::LoadSnapshotChunk(x),
            SnapshotResponse::ApplySnapshotChunk(x) => Self::ApplySnapshotChunk(x),
        }
    }
}

impl TryFrom<Response> for SnapshotResponse {
    type Error = Error;
    fn try_from(req: Response) -> Result<Self, Self::Error> {
        match req {
            Response::ListSnapshots(x) => Ok(Self::ListSnapshots(x)),
            Response::OfferSnapshot(x) => Ok(Self::OfferSnapshot(x)),
            Response::LoadSnapshotChunk(x) => Ok(Self::LoadSnapshotChunk(x)),
            Response::ApplySnapshotChunk(x) => Ok(Self::ApplySnapshotChunk(x)),
            _ => Err(Error::invalid_abci_response_type()),
        }
    }
}

// =============================================================================
// Protobuf conversions
// =============================================================================

use tendermint_proto::v0_38::abci as pb;
use tendermint_proto::Protobuf;

impl From<Response> for pb::Response {
    fn from(response: Response) -> pb::Response {
        use pb::response::Value;
        let value = match response {
            Response::Exception(x) => Some(Value::Exception(x.into())),
            Response::Echo(x) => Some(Value::Echo(x.into())),
            Response::Flush => Some(Value::Flush(Default::default())),
            Response::Info(x) => Some(Value::Info(x.into())),
            Response::InitChain(x) => Some(Value::InitChain(x.into())),
            Response::Query(x) => Some(Value::Query(x.into())),
            Response::CheckTx(x) => Some(Value::CheckTx(x.into())),
            Response::Commit(x) => Some(Value::Commit(x.into())),
            Response::ListSnapshots(x) => Some(Value::ListSnapshots(x.into())),
            Response::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())),
            Response::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())),
            Response::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())),
            Response::PrepareProposal(x) => Some(Value::PrepareProposal(x.into())),
            Response::ProcessProposal(x) => Some(Value::ProcessProposal(x.into())),
            Response::ExtendVote(x) => Some(Value::ExtendVote(x.into())),
            Response::VerifyVoteExtension(x) => Some(Value::VerifyVoteExtension(x.into())),
            Response::FinalizeBlock(x) => Some(Value::FinalizeBlock(x.into())),
        };
        pb::Response { value }
    }
}

impl TryFrom<pb::Response> for Response {
    type Error = Error;

    fn try_from(response: pb::Response) -> Result<Self, Self::Error> {
        use pb::response::Value;

        let value = response.value.ok_or_else(Error::missing_data)?;

        let response = match value {
            Value::Exception(x) => Response::Exception(x.try_into()?),
            Value::Echo(x) => Response::Echo(x.try_into()?),
            Value::Flush(_) => Response::Flush,
            Value::Info(x) => Response::Info(x.try_into()?),
            Value::InitChain(x) => Response::InitChain(x.try_into()?),
            Value::Query(x) => Response::Query(x.try_into()?),
            Value::CheckTx(x) => Response::CheckTx(x.try_into()?),
            Value::Commit(x) => Response::Commit(x.try_into()?),
            Value::ListSnapshots(x) => Response::ListSnapshots(x.try_into()?),
            Value::OfferSnapshot(x) => Response::OfferSnapshot(x.try_into()?),
            Value::LoadSnapshotChunk(x) => Response::LoadSnapshotChunk(x.try_into()?),
            Value::ApplySnapshotChunk(x) => Response::ApplySnapshotChunk(x.try_into()?),
            Value::PrepareProposal(x) => Response::PrepareProposal(x.try_into()?),
            Value::ProcessProposal(x) => Response::ProcessProposal(x.try_into()?),
            Value::ExtendVote(x) => Response::ExtendVote(x.try_into()?),
            Value::VerifyVoteExtension(x) => Response::VerifyVoteExtension(x.try_into()?),
            Value::FinalizeBlock(x) => Response::FinalizeBlock(x.try_into()?),
        };
        Ok(response)
    }
}

impl Protobuf<pb::Response> for Response {}