penumbra_governance/component/
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::pin::Pin;
use std::str::FromStr;

use anyhow::Context;
use async_stream::try_stream;
use cnidarium::Storage;
use futures::{StreamExt, TryStreamExt};
use penumbra_num::Amount;
use penumbra_proto::core::component::governance::v1::AllTalliedDelegatorVotesForProposalRequest;
use penumbra_proto::core::component::governance::v1::AllTalliedDelegatorVotesForProposalResponse;
use penumbra_proto::core::component::governance::v1::NextProposalIdRequest;
use penumbra_proto::core::component::governance::v1::NextProposalIdResponse;
use penumbra_proto::core::component::governance::v1::VotingPowerAtProposalStartRequest;
use penumbra_proto::core::component::governance::v1::VotingPowerAtProposalStartResponse;
use penumbra_proto::{
    core::component::governance::v1::{
        query_service_server::QueryService, ProposalDataRequest, ProposalDataResponse,
        ProposalInfoRequest, ProposalInfoResponse, ProposalListRequest, ProposalListResponse,
        ProposalRateDataRequest, ProposalRateDataResponse, ValidatorVotesRequest,
        ValidatorVotesResponse,
    },
    StateReadProto,
};
use penumbra_stake::rate::RateData;
use penumbra_stake::IdentityKey;
use tonic::Status;
use tracing::instrument;

use crate::state_key;
use crate::Tally;
use crate::Vote;

use super::StateReadExt;

// TODO: Hide this and only expose a Router?
pub struct Server {
    storage: Storage,
}

impl Server {
    pub fn new(storage: Storage) -> Self {
        Self { storage }
    }
}

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

        let start_block_height = state
            .proposal_voting_start(proposal_id)
            .await
            .map_err(|e| tonic::Status::internal(e.to_string()))?
            .ok_or_else(|| tonic::Status::not_found(format!("proposal {proposal_id} not found")))?;

        let start_position = state
            .proposal_voting_start_position(proposal_id)
            .await
            .map_err(|e| tonic::Status::internal(e.to_string()))?
            .ok_or_else(|| tonic::Status::not_found(format!("proposal {proposal_id} not found")))?;

        Ok(tonic::Response::new(ProposalInfoResponse {
            start_block_height,
            start_position: start_position.into(),
        }))
    }

    #[instrument(skip(self, _request))]
    async fn next_proposal_id(
        &self,
        _request: tonic::Request<NextProposalIdRequest>,
    ) -> Result<tonic::Response<NextProposalIdResponse>, Status> {
        let state = self.storage.latest_snapshot();

        let next_proposal_id: u64 = state
            .get_proto(state_key::next_proposal_id())
            .await
            .map_err(|e| tonic::Status::internal(format!("unable to fetch next proposal id: {e}")))?
            .ok_or_else(|| tonic::Status::not_found("there are no proposals yet".to_string()))?;

        Ok(tonic::Response::new(NextProposalIdResponse {
            next_proposal_id,
        }))
    }

    #[instrument(skip(self, request))]
    async fn proposal_data(
        &self,
        request: tonic::Request<ProposalDataRequest>,
    ) -> Result<tonic::Response<ProposalDataResponse>, Status> {
        let state = self.storage.latest_snapshot();
        let proposal_id = request.into_inner().proposal_id;

        let start_block_height = state
            .proposal_voting_start(proposal_id)
            .await
            .map_err(|e| tonic::Status::internal(e.to_string()))?
            .ok_or_else(|| tonic::Status::not_found(format!("proposal {proposal_id} not found")))?;

        let end_block_height = state
            .proposal_voting_end(proposal_id)
            .await
            .map_err(|e| tonic::Status::internal(e.to_string()))?
            .ok_or_else(|| tonic::Status::not_found(format!("proposal {proposal_id} not found")))?;

        let start_position = state
            .proposal_voting_start_position(proposal_id)
            .await
            .map_err(|e| tonic::Status::internal(e.to_string()))?
            .ok_or_else(|| tonic::Status::not_found(format!("proposal {proposal_id} not found")))?;

        let proposal = state
            .proposal_definition(proposal_id)
            .await
            .map_err(|e| tonic::Status::internal(format!("unable to fetch proposal: {e}")))?
            .ok_or_else(|| {
                tonic::Status::not_found(format!("proposal {} not found", proposal_id))
            })?;

        let proposal_state = state
            .proposal_state(proposal_id)
            .await
            .map_err(|e| tonic::Status::internal(format!("unable to fetch proposal state: {e}")))?
            .ok_or_else(|| {
                tonic::Status::not_found(format!("proposal {} state not found", proposal_id))
            })?;

        let proposal_deposit_amount: Amount = state
            .get(&state_key::proposal_deposit_amount(proposal_id))
            .await
            .map_err(|e| {
                tonic::Status::internal(format!("unable to fetch proposal deposit amount: {e}"))
            })?
            .ok_or_else(|| {
                tonic::Status::not_found(format!(
                    "deposit amount for proposal {} was not found",
                    proposal_id
                ))
            })?;

        Ok(tonic::Response::new(ProposalDataResponse {
            start_block_height,
            end_block_height,
            start_position: start_position.into(),
            state: Some(proposal_state.into()),
            proposal: Some(proposal.into()),
            proposal_deposit_amount: Some(proposal_deposit_amount.into()),
        }))
    }

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

    #[instrument(skip(self, request))]
    async fn proposal_rate_data(
        &self,
        request: tonic::Request<ProposalRateDataRequest>,
    ) -> Result<tonic::Response<Self::ProposalRateDataStream>, Status> {
        let state = self.storage.latest_snapshot();
        let proposal_id = request.into_inner().proposal_id;

        let s = state.prefix(&state_key::all_rate_data_at_proposal_start(proposal_id));
        Ok(tonic::Response::new(
            s.map_ok(|i: (String, RateData)| {
                let (_key, rate_data) = i;
                ProposalRateDataResponse {
                    rate_data: Some(rate_data.into()),
                }
            })
            .map_err(|e: anyhow::Error| {
                tonic::Status::unavailable(format!("error getting prefix value from storage: {e}"))
            })
            // TODO: how do we instrument a Stream
            //.instrument(Span::current())
            .boxed(),
        ))
    }

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

    #[instrument(skip(self, request))]
    async fn proposal_list(
        &self,
        request: tonic::Request<ProposalListRequest>,
    ) -> Result<tonic::Response<Self::ProposalListStream>, Status> {
        let state = self.storage.latest_snapshot();

        let proposal_id_list: Vec<u64> = if request.into_inner().inactive {
            let next = state.next_proposal_id().await.map_err(|e| {
                tonic::Status::internal(format!("unable to get next proposal id: {e}"))
            })?;

            (0..next).collect()
        } else {
            state
                .unfinished_proposals()
                .await
                .map_err(|e| {
                    tonic::Status::internal(format!("unable to fetch unfinished proposals: {e}"))
                })?
                .into_iter()
                .collect::<Vec<_>>()
        };

        let s = try_stream! {
            for proposal_id in proposal_id_list {
            let proposal = state
                .proposal_definition(proposal_id)
                .await
                .map_err(|e| tonic::Status::internal(format!("unable to fetch proposal: {e}")))?
                .ok_or_else(|| {
                    tonic::Status::not_found(format!("proposal {} not found", proposal_id))
                })?;

            let proposal_state = state
                .proposal_state(proposal_id)
                .await
                .map_err(|e| tonic::Status::internal(format!("unable to fetch proposal state: {e}")))?
                .ok_or_else(|| {
                    tonic::Status::not_found(format!("proposal {} state not found", proposal_id))
                })?;

            let proposal_voting_start_position = state
                .proposal_voting_start_position(proposal_id)
                .await
                .map_err(|e| {
                    tonic::Status::internal(format!(
                        "unable to fetch proposal voting start position: {e}"
                    ))
                })?
                .ok_or_else(|| {
                    tonic::Status::not_found(format!(
                        "voting start position for proposal {} not found",
                        proposal_id
                    ))
                })?;

            let start_block_height = state
                .proposal_voting_start(proposal_id)
                .await
                .map_err(|e| {
                    tonic::Status::internal(format!(
                        "unable to fetch proposal voting start block: {e}"
                    ))
                })?
                .ok_or_else(|| {
                    tonic::Status::not_found(format!(
                        "voting start block for proposal {} not found",
                        proposal_id
                    ))
                })?;

            let end_block_height = state
                .proposal_voting_end(proposal_id)
                .await
                .map_err(|e| tonic::Status::internal(e.to_string()))?
                .ok_or_else(|| tonic::Status::not_found(format!("proposal {proposal_id} not found")))?;

            yield ProposalListResponse {
                proposal: Some(proposal.into()),
                start_block_height,
                end_block_height,
                start_position: proposal_voting_start_position.into(),
                state: Some(proposal_state.into()),
            }
        }};

        Ok(tonic::Response::new(
            s.map_err(|e: anyhow::Error| {
                tonic::Status::unavailable(format!(
                    "error getting position value from storage: {e}"
                ))
            })
            // TODO: how do we instrument a Stream
            //.instrument(Span::current())
            .boxed(),
        ))
    }

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

    #[instrument(skip(self, request))]
    async fn validator_votes(
        &self,
        request: tonic::Request<ValidatorVotesRequest>,
    ) -> Result<tonic::Response<Self::ValidatorVotesStream>, Status> {
        let state = self.storage.latest_snapshot();

        let proposal_id = request.into_inner().proposal_id;

        let s = state
            .prefix::<Vote>(&state_key::all_validator_votes_for_proposal(proposal_id))
            .and_then(|r| async move {
                Ok((
                    IdentityKey::from_str(r.0.rsplit('/').next().context("invalid key")?)?,
                    r.1,
                ))
            })
            .map_ok(|i: (IdentityKey, Vote)| ValidatorVotesResponse {
                vote: Some(i.1.into()),
                identity_key: Some(i.0.into()),
            });

        Ok(tonic::Response::new(
            s.map_err(|e: anyhow::Error| {
                tonic::Status::unavailable(format!(
                    "error getting validator votes from storage: {e}"
                ))
            })
            // TODO: how do we instrument a Stream
            //.instrument(Span::current())
            .boxed(),
        ))
    }

    #[instrument(skip(self, request))]
    async fn voting_power_at_proposal_start(
        &self,
        request: tonic::Request<VotingPowerAtProposalStartRequest>,
    ) -> Result<tonic::Response<VotingPowerAtProposalStartResponse>, Status> {
        let state = self.storage.latest_snapshot();
        let request = request.into_inner();
        let proposal_id = request.proposal_id;
        if let Some(identity_key) = request.identity_key {
            // If the query is for a specific validator, return their voting power at the start of
            // the proposal
            let identity_key = identity_key.try_into().map_err(|_| {
                tonic::Status::invalid_argument(
                    "identity key in request was bad protobuf".to_string(),
                )
            })?;

            let voting_power = state
                .get_proto::<u64>(&state_key::voting_power_at_proposal_start(
                    proposal_id,
                    identity_key,
                ))
                .await
                .map_err(|e| tonic::Status::internal(format!("error accessing storage: {}", e)))?;

            if voting_power.is_none() {
                return Err(tonic::Status::not_found(format!(
                    "validator did not exist at proposal creation: {}",
                    identity_key
                )));
            }

            Ok(tonic::Response::new(VotingPowerAtProposalStartResponse {
                voting_power: voting_power.expect("voting power should be set"),
            }))
        } else {
            // If the query is for the total voting power at the start of the proposal, return that
            let total_voting_power = state
                .total_voting_power_at_proposal_start(proposal_id)
                .await
                .map_err(|e| tonic::Status::internal(format!("error accessing storage: {}", e)))?;

            Ok(tonic::Response::new(VotingPowerAtProposalStartResponse {
                voting_power: total_voting_power,
            }))
        }
    }

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

    #[instrument(skip(self, request))]
    async fn all_tallied_delegator_votes_for_proposal(
        &self,
        request: tonic::Request<AllTalliedDelegatorVotesForProposalRequest>,
    ) -> Result<tonic::Response<Self::AllTalliedDelegatorVotesForProposalStream>, Status> {
        let state = self.storage.latest_snapshot();
        let proposal_id = request.into_inner().proposal_id;

        let s = state.prefix::<Tally>(&state_key::all_tallied_delegator_votes_for_proposal(
            proposal_id,
        ));
        Ok(tonic::Response::new(
            s.and_then(|r| async move {
                Ok((
                    IdentityKey::from_str(r.0.rsplit('/').next().context("invalid key")?)?,
                    r.1,
                ))
            })
            .map_err(|e| {
                tonic::Status::internal(format!("unable to retrieve tallied delegator votes: {e}"))
            })
            .map_ok(
                |i: (IdentityKey, Tally)| AllTalliedDelegatorVotesForProposalResponse {
                    tally: Some(i.1.into()),
                    identity_key: Some(i.0.into()),
                },
            )
            // TODO: how do we instrument a Stream
            //.instrument(Span::current())
            .boxed(),
        ))
    }
}