penumbra_sdk_app/rpc/
query.rs

1use {
2    crate::app::StateReadExt as _,
3    cnidarium::Storage,
4    penumbra_sdk_proto::core::app::v1::{
5        query_service_server::QueryService, AppParametersRequest, AppParametersResponse,
6        TransactionsByHeightRequest, TransactionsByHeightResponse,
7    },
8    tonic::Status,
9    tracing::instrument,
10};
11
12pub(super) struct AppQueryServer {
13    storage: Storage,
14}
15
16impl AppQueryServer {
17    pub fn new(storage: Storage) -> Self {
18        Self { storage }
19    }
20}
21
22#[tonic::async_trait]
23impl QueryService for AppQueryServer {
24    #[instrument(skip(self, request))]
25    async fn transactions_by_height(
26        &self,
27        request: tonic::Request<TransactionsByHeightRequest>,
28    ) -> Result<tonic::Response<TransactionsByHeightResponse>, Status> {
29        let state = self.storage.latest_snapshot();
30        let request_inner = request.into_inner();
31        let block_height = request_inner.block_height;
32
33        let tx_response = state
34            .transactions_by_height(block_height)
35            .await
36            .map_err(|e| tonic::Status::internal(format!("transaction response bad: {e}")))?;
37
38        Ok(tonic::Response::new(tx_response))
39    }
40
41    #[instrument(skip(self, _request))]
42    async fn app_parameters(
43        &self,
44        _request: tonic::Request<AppParametersRequest>,
45    ) -> Result<tonic::Response<AppParametersResponse>, Status> {
46        let state = self.storage.latest_snapshot();
47        // We map the error here to avoid including `tonic` as a dependency
48        // in the `chain` crate, to support its compilation to wasm.
49
50        let app_parameters = state.get_app_params().await.map_err(|e| {
51            tonic::Status::unavailable(format!("error getting app parameters: {e}"))
52        })?;
53
54        Ok(tonic::Response::new(AppParametersResponse {
55            app_parameters: Some(app_parameters.into()),
56        }))
57    }
58}