cnidarium/
rpc.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
256
// Autogen code isn't clippy clean:
#[allow(clippy::unwrap_used)]
pub mod proto {
    pub mod v1 {
        include!("gen/penumbra.cnidarium.v1.rs");
        include!("gen/penumbra.cnidarium.v1.serde.rs");
    }

    // https://github.com/penumbra-zone/penumbra/issues/3038#issuecomment-1722534133
    pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!("gen/proto_descriptor.bin.no_lfs");
}

pub struct Server {
    storage: Storage,
}

impl Server {
    pub fn new(storage: Storage) -> Self {
        Self { storage }
    }
}
use std::pin::Pin;

use crate::read::StateRead;
use crate::rpc::proto::v1::{
    key_value_response::Value as JMTValue, non_verifiable_key_value_response::Value as NVValue,
    query_service_server::QueryService, watch_response as wr, KeyValueRequest, KeyValueResponse,
    NonVerifiableKeyValueRequest, NonVerifiableKeyValueResponse, PrefixValueRequest,
    PrefixValueResponse, WatchRequest, WatchResponse,
};
use futures::{StreamExt, TryStreamExt};
use regex::Regex;
use tokio_stream::wrappers::ReceiverStream;
use tonic::Status;
use tracing::instrument;

use crate::Storage;

#[tonic::async_trait]
impl QueryService for Server {
    #[instrument(skip(self, request))]
    async fn non_verifiable_key_value(
        &self,
        request: tonic::Request<NonVerifiableKeyValueRequest>,
    ) -> Result<tonic::Response<NonVerifiableKeyValueResponse>, Status> {
        let state = self.storage.latest_snapshot();
        let request = request.into_inner();

        if request.key.is_none() || request.key.as_ref().expect("key is Some").inner.is_empty() {
            return Err(Status::invalid_argument("key is empty"));
        }

        let key = request.key.expect("key is Some").inner;
        let some_value = state
            .nonverifiable_get_raw(&key)
            .await
            .map_err(|e| tonic::Status::internal(e.to_string()))?;

        Ok(tonic::Response::new(NonVerifiableKeyValueResponse {
            value: some_value.map(|value| NVValue { value }),
        }))
    }

    #[instrument(skip(self, request))]
    async fn key_value(
        &self,
        request: tonic::Request<KeyValueRequest>,
    ) -> Result<tonic::Response<KeyValueResponse>, Status> {
        let state = self.storage.latest_snapshot();
        // We map the error here to avoid including `tonic` as a dependency
        // in the `chain` crate, to support its compilation to wasm.
        let request = request.into_inner();
        tracing::debug!(?request, "processing key_value request");

        if request.key.is_empty() {
            return Err(Status::invalid_argument("key is empty"));
        }

        let (some_value, proof) = {
            // Don't generate the proof if the request doesn't ask for it.
            let (v, p) = if request.proof {
                let (v, p) = state
                    .get_with_proof(request.key.into_bytes())
                    .await
                    .map_err(|e| tonic::Status::internal(e.to_string()))?;
                (v, Some(p))
            } else {
                (
                    state
                        .get_raw(&request.key)
                        .await
                        .map_err(|e| tonic::Status::internal(e.to_string()))?,
                    None,
                )
            };
            (v, p)
        };

        Ok(tonic::Response::new(KeyValueResponse {
            value: some_value.map(|value| JMTValue { value }),
            proof: if request.proof {
                Some(ibc_proto::ibc::core::commitment::v1::MerkleProof {
                    proofs: proof
                        .expect("proof should be present")
                        .proofs
                        .into_iter()
                        .map(|p| {
                            let mut encoded = Vec::new();
                            prost::Message::encode(&p, &mut encoded).expect("able to encode proof");
                            prost::Message::decode(&*encoded).expect("able to decode proof")
                        })
                        .collect(),
                })
            } else {
                None
            },
        }))
    }

    type PrefixValueStream =
        Pin<Box<dyn futures::Stream<Item = Result<PrefixValueResponse, tonic::Status>> + Send>>;

    #[instrument(skip(self, request))]
    async fn prefix_value(
        &self,
        request: tonic::Request<PrefixValueRequest>,
    ) -> Result<tonic::Response<Self::PrefixValueStream>, Status> {
        let state = self.storage.latest_snapshot();
        let request = request.into_inner();
        tracing::debug!(?request);

        if request.prefix.is_empty() {
            return Err(Status::invalid_argument("prefix is empty"));
        }

        Ok(tonic::Response::new(
            state
                .prefix_raw(&request.prefix)
                .map_ok(|i: (String, Vec<u8>)| {
                    let (key, value) = i;
                    PrefixValueResponse { key, value }
                })
                .map_err(|e: anyhow::Error| {
                    tonic::Status::unavailable(format!(
                        "error getting prefix value from storage: {e}"
                    ))
                })
                .boxed(),
        ))
    }

    type WatchStream = ReceiverStream<Result<WatchResponse, tonic::Status>>;

    #[instrument(skip(self, request))]
    async fn watch(
        &self,
        request: tonic::Request<WatchRequest>,
    ) -> Result<tonic::Response<Self::WatchStream>, Status> {
        let request = request.into_inner();
        tracing::debug!(?request);

        const MAX_REGEX_LEN: usize = 1024;

        let key_regex = match request.key_regex.as_str() {
            "" => None,
            _ => Some(
                regex::RegexBuilder::new(&request.key_regex)
                    .size_limit(MAX_REGEX_LEN)
                    .build()
                    .map_err(|e| Status::invalid_argument(format!("invalid key_regex: {}", e)))?,
            ),
        };

        // Use the `bytes` regex to allow matching byte strings.
        let nv_key_regex = match request.nv_key_regex.as_str() {
            "" => None,
            _ => Some(
                regex::bytes::RegexBuilder::new(&request.nv_key_regex)
                    .size_limit(MAX_REGEX_LEN)
                    .unicode(false)
                    .build()
                    .map_err(|e| {
                        Status::invalid_argument(format!("invalid nv_key_regex: {}", e))
                    })?,
            ),
        };

        let (tx, rx) = tokio::sync::mpsc::channel::<Result<WatchResponse, tonic::Status>>(10);

        tokio::spawn(watch_changes(
            self.storage.clone(),
            key_regex,
            nv_key_regex,
            tx,
        ));

        Ok(tonic::Response::new(ReceiverStream::new(rx)))
    }
}

async fn watch_changes(
    storage: Storage,
    key_regex: Option<regex::Regex>,
    nv_key_regex: Option<regex::bytes::Regex>,
    tx: tokio::sync::mpsc::Sender<Result<WatchResponse, tonic::Status>>,
) -> anyhow::Result<()> {
    let mut changes_rx = storage.subscribe_changes();
    while !tx.is_closed() {
        // Wait for a new set of changes, reporting an error if we don't get one.
        if let Err(e) = changes_rx.changed().await {
            tx.send(Err(tonic::Status::internal(e.to_string()))).await?;
        }
        let (version, changes) = changes_rx.borrow_and_update().clone();

        if key_regex.is_some() || nv_key_regex.is_none() {
            for (key, value) in changes.unwritten_changes().iter() {
                if key_regex
                    .as_ref()
                    .unwrap_or(&Regex::new(r"").expect("empty regex ok"))
                    .is_match(key)
                {
                    tx.send(Ok(WatchResponse {
                        version,
                        entry: Some(wr::Entry::Kv(wr::KeyValue {
                            key: key.clone(),
                            value: value.as_ref().cloned().unwrap_or_default(),
                            deleted: value.is_none(),
                        })),
                    }))
                    .await?;
                }
            }
        }

        if nv_key_regex.is_some() || key_regex.is_none() {
            for (key, value) in changes.nonverifiable_changes().iter() {
                if nv_key_regex
                    .as_ref()
                    .unwrap_or(&regex::bytes::Regex::new(r"").expect("empty regex ok"))
                    .is_match(key)
                {
                    tx.send(Ok(WatchResponse {
                        version,
                        entry: Some(wr::Entry::NvKv(wr::NvKeyValue {
                            key: key.clone(),
                            value: value.as_ref().cloned().unwrap_or_default(),
                            deleted: value.is_none(),
                        })),
                    }))
                    .await?;
                }
            }
        }
    }
    return Ok(());
}