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
use std::sync::Arc;

#[cfg(feature = "use-async-std")]
use async_std::sync::Mutex;
#[cfg(feature = "use-smol")]
use smol::lock::Mutex;
use tendermint_proto::abci::{
    request::Value as RequestValue, response::Value as ResponseValue, Response, ResponseException,
};
#[cfg(feature = "use-tokio")]
use tokio::sync::Mutex;
use tracing::{debug, instrument};

use crate::{
    async_api::{Consensus, Info, Mempool, Snapshot},
    state::ConsensusStateValidator,
    types::ResponseEcho,
};

#[instrument]
pub fn handle_unknown_request(request_value: RequestValue) -> Response {
    let response_value = match request_value {
        RequestValue::Echo(request) => ResponseValue::Echo(ResponseEcho {
            message: request.message,
        }),
        RequestValue::Flush(_) => ResponseValue::Flush(Default::default()),
        _ => unreachable!("handle_unknown_request cannot handle known requests"),
    };

    Response {
        value: Some(response_value),
    }
}

#[instrument(skip(consensus))]
pub async fn handle_consensus_request<C: Consensus>(
    consensus: &C,
    validator: Arc<Mutex<ConsensusStateValidator>>,
    request_value: RequestValue,
) -> Response {
    let response_value = match request_value {
        RequestValue::Echo(request) => ResponseValue::Echo(consensus.echo(request).await),
        RequestValue::Flush(request) => ResponseValue::Flush(consensus.flush(request).await),
        RequestValue::InitChain(request) => match validator.lock().await.on_init_chain_request() {
            Ok(_) => ResponseValue::InitChain(consensus.init_chain(request).await),
            Err(error) => ResponseValue::Exception(ResponseException { error }),
        },
        RequestValue::BeginBlock(request) => {
            match validator.lock().await.on_begin_block_request(&request) {
                Ok(_) => ResponseValue::BeginBlock(consensus.begin_block(request).await),
                Err(error) => ResponseValue::Exception(ResponseException { error }),
            }
        }
        RequestValue::DeliverTx(request) => match validator.lock().await.on_deliver_tx_request() {
            Ok(_) => ResponseValue::DeliverTx(consensus.deliver_tx(request).await),
            Err(error) => ResponseValue::Exception(ResponseException { error }),
        },
        RequestValue::EndBlock(request) => {
            match validator.lock().await.on_end_block_request(&request) {
                Ok(_) => ResponseValue::EndBlock(consensus.end_block(request).await),
                Err(error) => ResponseValue::Exception(ResponseException { error }),
            }
        }
        RequestValue::Commit(request) => {
            let mut validator_locked = validator.lock().await;

            match validator_locked.on_commit_request() {
                Ok(_) => {
                    let response = consensus.commit(request).await;

                    match validator_locked.on_commit_response(&response) {
                        Ok(_) => ResponseValue::Commit(response),
                        Err(error) => ResponseValue::Exception(ResponseException { error }),
                    }
                }
                Err(error) => ResponseValue::Exception(ResponseException { error }),
            }
        }
        _ => ResponseValue::Exception(ResponseException {
            error: "Non-consensus request on consensus connection".to_string(),
        }),
    };

    let response = Response {
        value: Some(response_value),
    };

    debug!(message = "Sending response", ?response);

    response
}

#[instrument(skip(mempool))]
pub async fn handle_mempool_request<M: Mempool>(
    mempool: &M,
    request_value: RequestValue,
) -> Response {
    let response_value = match request_value {
        RequestValue::Echo(request) => ResponseValue::Echo(mempool.echo(request).await),
        RequestValue::Flush(request) => ResponseValue::Flush(mempool.flush(request).await),
        RequestValue::CheckTx(request) => ResponseValue::CheckTx(mempool.check_tx(request).await),
        _ => ResponseValue::Exception(ResponseException {
            error: "Non-mempool request on mempool connection".to_string(),
        }),
    };

    let response = Response {
        value: Some(response_value),
    };

    debug!(message = "Sending response", ?response);

    response
}

#[instrument(skip(info))]
pub async fn handle_info_request<I: Info>(
    info: &I,
    validator: Arc<Mutex<ConsensusStateValidator>>,
    request_value: RequestValue,
) -> Response {
    let response_value = match request_value {
        RequestValue::Echo(request) => ResponseValue::Echo(info.echo(request).await),
        RequestValue::Flush(request) => ResponseValue::Flush(info.flush(request).await),
        RequestValue::Info(request) => {
            let info_response = info.info(request).await;
            validator.lock().await.on_info_response(&info_response);
            ResponseValue::Info(info_response)
        }
        RequestValue::SetOption(request) => {
            ResponseValue::SetOption(info.set_option(request).await)
        }
        RequestValue::Query(request) => ResponseValue::Query(info.query(request).await),
        _ => ResponseValue::Exception(ResponseException {
            error: "Non-info request on info connection".to_string(),
        }),
    };

    let response = Response {
        value: Some(response_value),
    };

    debug!(message = "Sending response", ?response);

    response
}

#[instrument(skip(snapshot))]
pub async fn handle_snapshot_request<S: Snapshot>(
    snapshot: &S,
    request_value: RequestValue,
) -> Response {
    let response_value = match request_value {
        RequestValue::Echo(request) => ResponseValue::Echo(snapshot.echo(request).await),
        RequestValue::Flush(request) => ResponseValue::Flush(snapshot.flush(request).await),
        RequestValue::ListSnapshots(request) => {
            ResponseValue::ListSnapshots(snapshot.list_snapshots(request).await)
        }
        RequestValue::OfferSnapshot(request) => {
            ResponseValue::OfferSnapshot(snapshot.offer_snapshot(request).await)
        }
        RequestValue::LoadSnapshotChunk(request) => {
            ResponseValue::LoadSnapshotChunk(snapshot.load_snapshot_chunk(request).await)
        }
        RequestValue::ApplySnapshotChunk(request) => {
            ResponseValue::ApplySnapshotChunk(snapshot.apply_snapshot_chunk(request).await)
        }
        _ => ResponseValue::Exception(ResponseException {
            error: "Non-snapshot request on snapshot connection".to_string(),
        }),
    };

    let response = Response {
        value: Some(response_value),
    };

    debug!(message = "Sending response", ?response);

    response
}