penumbra_app/server/
info.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! Logic for enabling `pd` to interact with chain state.
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
    vec,
};

use crate::PenumbraHost;
use anyhow::Context as _;
use cnidarium::Storage;
use futures::FutureExt;
use ibc_proto::ibc::core::{
    channel::v1::{
        PacketState, QueryChannelsRequest, QueryChannelsResponse,
        QueryPacketAcknowledgementsRequest, QueryPacketAcknowledgementsResponse,
        QueryPacketCommitmentsRequest, QueryPacketCommitmentsResponse, QueryUnreceivedAcksRequest,
        QueryUnreceivedAcksResponse, QueryUnreceivedPacketsRequest, QueryUnreceivedPacketsResponse,
    },
    client::v1::{IdentifiedClientState, QueryClientStatesRequest, QueryClientStatesResponse},
};
use ibc_proto::ibc::core::{
    channel::v1::{QueryConnectionChannelsRequest, QueryConnectionChannelsResponse},
    connection::v1::{QueryConnectionsRequest, QueryConnectionsResponse},
};
use ibc_types::core::channel::IdentifiedChannelEnd;
use ibc_types::core::channel::{ChannelId, PortId};
use ibc_types::core::client::ClientId;
use ibc_types::core::connection::ConnectionId;
use ibc_types::core::connection::IdentifiedConnectionEnd;
use penumbra_ibc::component::ChannelStateReadExt as _;
use penumbra_ibc::component::ClientStateReadExt as _;
use penumbra_ibc::component::ConnectionStateReadExt as _;
use penumbra_ibc::component::HostInterface;
use prost::Message;
use std::str::FromStr;
use tendermint::v0_37::abci::{
    request,
    response::{self, Echo},
    InfoRequest, InfoResponse,
};
use tower_abci::BoxError;
use tracing::Instrument;

use penumbra_tower_trace::v037::RequestExt;

const ABCI_INFO_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Implements service traits for Tonic gRPC services.
///
/// The fields of this struct are the configuration and data
/// necessary to the gRPC services.
#[derive(Clone, Debug)]
pub struct Info {
    /// Storage interface for retrieving chain state.
    storage: Storage,
    // height_rx: watch::Receiver<block::Height>,
}

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

    async fn info(&self, info: request::Info) -> anyhow::Result<response::Info> {
        let state = self.storage.latest_snapshot();
        // Previously, we used the latest version of the JMT state to
        // report the current block height. This worked well because
        // the JMT version and the current height were always aligned.
        // However, adding support for genesis migrations breaks this
        // invariant because there is a pregenesis (aka. phantom)
        // block that occurs immediately after an upgrade. This block
        // has height 0. To support this case, we report back the height
        // that is stored in the state store.
        let last_block_height = PenumbraHost::get_block_height(&state)
            .await
            // if we can't get the block height, we're in pregenesis
            // in that case we want to report 0 to cometbft.
            .unwrap_or_default()
            .try_into()?;

        let app_version = crate::APP_VERSION;

        tracing::info!(?info, state_version = ?state.version(), last_block_height = ?last_block_height, ?app_version,"reporting height in info query");

        let last_block_app_hash = state.root_hash().await?.0.to_vec().try_into()?;

        Ok(response::Info {
            data: "penumbra".to_string(),
            version: ABCI_INFO_VERSION.to_string(),
            app_version,
            last_block_height,
            last_block_app_hash,
        })
    }

    async fn get_snapshot_for_height(
        &self,
        height: u64,
    ) -> anyhow::Result<(cnidarium::Snapshot, u64)> {
        match height {
            // ABCI docs say:
            //
            // The default `0` returns data for the latest committed block. Note that
            // this is the height of the block containing the application's Merkle root
            // hash, which represents the state as it was after committing the block at
            // `height - 1`.
            //
            // Try to do this behavior by querying at height = latest-1.
            0 => {
                let height = self.storage.latest_snapshot().version().saturating_sub(1);
                let snapshot = self
                    .storage
                    .snapshot(height)
                    .ok_or_else(|| anyhow::anyhow!("no snapshot of height {height}"))?;

                Ok((snapshot, height))
            }
            height => {
                let snapshot = self
                    .storage
                    .snapshot(height)
                    .ok_or_else(|| anyhow::anyhow!("no snapshot of height {height}"))?;

                Ok((snapshot, height))
            }
        }
    }

    async fn query(&self, query: request::Query) -> anyhow::Result<response::Query> {
        // The other query params are already in the span, so we just need to emit an event.
        tracing::debug!("got query");

        match query.path.as_str() {
            "state/key" => {
                let (snapshot, height) = self
                    .get_snapshot_for_height(u64::from(query.height))
                    .await?;

                let key = hex::decode(&query.data).unwrap_or_else(|_| query.data.to_vec());

                let (value, proof) =
                    snapshot
                        .get_with_proof(key.clone())
                        .await
                        .with_context(|| {
                            format!("failed to get key {}", String::from_utf8_lossy(&key))
                        })?;

                let mut ops = vec![];
                for commitment_proof in proof.proofs {
                    match commitment_proof
                        .clone()
                        .proof
                        .expect("should have non empty commitment proofs")
                    {
                        ics23::commitment_proof::Proof::Exist(x_proof) => {
                            let proof_op = tendermint::merkle::proof::ProofOp {
                                field_type: "jmt:v".to_string(),
                                key: x_proof.key,
                                data: commitment_proof.encode_to_vec(),
                            };
                            ops.push(proof_op);
                        }
                        ics23::commitment_proof::Proof::Nonexist(nx_proof) => {
                            let proof_op = tendermint::merkle::proof::ProofOp {
                                field_type: "jmt:v".to_string(),
                                key: nx_proof.key,
                                data: commitment_proof.encode_to_vec(),
                            };
                            ops.push(proof_op);
                        }
                        ics23::commitment_proof::Proof::Batch(_) => {
                            anyhow::bail!("batch proofs not supported in abci query")
                        }
                        ics23::commitment_proof::Proof::Compressed(_) => {
                            anyhow::bail!("compressed proofs not supported in abci query")
                        }
                    }
                }
                let proof_ops = tendermint::merkle::proof::ProofOps { ops };
                let value = value.unwrap_or_else(Vec::new);

                Ok(response::Query {
                    code: 0.into(),
                    key: query.data,
                    log: "".to_string(),
                    value: value.into(),
                    proof: Some(proof_ops),
                    height: height.try_into().context("failed to convert height")?,
                    codespace: "".to_string(),
                    info: "".to_string(),
                    index: 0,
                })
            }
            "/ibc.core.connection.v1.Query/Connections" => {
                let (snapshot, height) = self
                    .get_snapshot_for_height(u64::from(query.height))
                    .await?;

                // TODO: handle request.pagination
                let _request = QueryConnectionsRequest::decode(query.data.clone())
                    .context("failed to decode QueryConnectionsRequest")?;

                let connection_counter = snapshot.get_connection_counter().await?;

                let mut connections = vec![];
                for conn_idx in 0..connection_counter.0 {
                    let conn_id = ConnectionId(format!("connection-{}", conn_idx));
                    let connection = snapshot
                        .get_connection(&conn_id)
                        .await?
                        .context("couldn't find connection")?;
                    let id_conn = IdentifiedConnectionEnd {
                        connection_id: conn_id,
                        connection_end: connection,
                    };
                    connections.push(id_conn.into());
                }

                let res_value = QueryConnectionsResponse {
                    connections,
                    pagination: None,
                    height: None,
                }
                .encode_to_vec();

                Ok(response::Query {
                    code: 0.into(),
                    key: query.data,
                    log: "".to_string(),
                    value: res_value.into(),
                    proof: None,
                    height: height.try_into().context("failed to convert height")?,
                    codespace: "".to_string(),
                    info: "".to_string(),
                    index: 0,
                })
            }
            "/ibc.core.channel.v1.Query/Channels" => {
                let (snapshot, height) = self
                    .get_snapshot_for_height(u64::from(query.height))
                    .await?;

                // TODO: handle request.pagination
                let _request = QueryChannelsRequest::decode(query.data.clone())
                    .context("failed to decode QueryConnectionsRequest")?;

                let channel_counter = snapshot.get_channel_counter().await?;

                let mut channels = vec![];
                for chan_idx in 0..channel_counter {
                    let chan_id = ChannelId(format!("channel-{}", chan_idx));
                    let channel = snapshot
                        .get_channel(&chan_id, &PortId::transfer())
                        .await?
                        .context("couldn't find channel")?;
                    let id_chan = IdentifiedChannelEnd {
                        channel_id: chan_id,
                        port_id: PortId::transfer(),
                        channel_end: channel,
                    };
                    channels.push(id_chan.into());
                }

                let res_value = QueryChannelsResponse {
                    channels,
                    pagination: None,
                    height: None,
                }
                .encode_to_vec();

                Ok(response::Query {
                    code: 0.into(),
                    key: query.data,
                    log: "".to_string(),
                    value: res_value.into(),
                    proof: None,
                    height: height.try_into().context("failed to convert height")?,
                    codespace: "".to_string(),
                    info: "".to_string(),
                    index: 0,
                })
            }
            "/ibc.core.channel.v1.Query/ConnectionChannels" => {
                let (snapshot, height) = self
                    .get_snapshot_for_height(u64::from(query.height))
                    .await?;

                let request = QueryConnectionChannelsRequest::decode(query.data.clone())
                    .context("failed to decode QueryConnectionChannelsRequest")?;

                let connection_id: ConnectionId = ConnectionId::from_str(&request.connection)
                    .context("couldn't decode connection id from request")?;

                // look up all of the channels for this connection
                let channel_counter = snapshot.get_channel_counter().await?;

                let mut channels = vec![];
                for chan_idx in 0..channel_counter {
                    let chan_id = ChannelId(format!("channel-{}", chan_idx));
                    let channel = snapshot
                        .get_channel(&chan_id, &PortId::transfer())
                        .await?
                        .context("couldn't find channel")?;
                    if channel.connection_hops.contains(&connection_id) {
                        let id_chan = IdentifiedChannelEnd {
                            channel_id: chan_id,
                            port_id: PortId::transfer(),
                            channel_end: channel,
                        };
                        channels.push(id_chan.into());
                    }
                }

                let res_value = QueryConnectionChannelsResponse {
                    channels,
                    pagination: None,
                    height: None,
                }
                .encode_to_vec();

                Ok(response::Query {
                    code: 0.into(),
                    key: query.data,
                    log: "".to_string(),
                    value: res_value.into(),
                    proof: None,
                    height: height.try_into().context("failed to convert height")?,
                    codespace: "".to_string(),
                    info: "".to_string(),
                    index: 0,
                })
            }
            "/ibc.core.client.v1.Query/ClientStates" => {
                let (snapshot, height) = self
                    .get_snapshot_for_height(u64::from(query.height))
                    .await?;

                // TODO; handle request.pagination
                let _request = QueryClientStatesRequest::decode(query.data.clone())
                    .context("failed to decode QueryClientStatesRequest")?;

                let client_counter = snapshot.client_counter().await?.0;

                let mut client_states = vec![];
                for client_idx in 0..client_counter {
                    // NOTE: currently, we only look up tendermint clients, because we only support tendermint clients.
                    let client_id =
                        ClientId::from_str(format!("07-tendermint-{}", client_idx).as_str())?;
                    let client_state = snapshot.get_client_state(&client_id).await;
                    let id_client = IdentifiedClientState {
                        client_id: client_id.to_string(),
                        client_state: client_state.ok().map(|state| state.into()), // send None if we couldn't find the client state
                    };
                    client_states.push(id_client);
                }

                let res_value = QueryClientStatesResponse {
                    client_states,
                    pagination: None,
                }
                .encode_to_vec();

                Ok(response::Query {
                    code: 0.into(),
                    key: query.data,
                    log: "".to_string(),
                    value: res_value.into(),
                    proof: None,
                    height: height.try_into().context("failed to convert height")?,
                    codespace: "".to_string(),
                    info: "".to_string(),
                    index: 0,
                })
            }
            "/ibc.core.channel.v1.Query/PacketCommitments" => {
                let (snapshot, height) = self
                    .get_snapshot_for_height(u64::from(query.height))
                    .await?;

                let request = QueryPacketCommitmentsRequest::decode(query.data.clone())
                    .context("failed to decode QueryPacketCommitmentsRequest")?;

                let chan_id: ChannelId =
                    ChannelId::from_str(&request.channel_id).context("invalid channel id")?;
                let port_id: PortId =
                    PortId::from_str(&request.port_id).context("invalid port id")?;

                let mut commitment_states = vec![];
                let commitment_counter = snapshot.get_send_sequence(&chan_id, &port_id).await?;

                // this starts at 1; the first commitment index is 1 (from ibc spec)
                for commitment_idx in 1..commitment_counter {
                    let commitment = snapshot
                        .get_packet_commitment_by_id(&chan_id, &port_id, commitment_idx)
                        .await?;
                    if commitment.is_none() {
                        continue;
                    }
                    let commitment = commitment.expect("commitment is Some");

                    let commitment_state = PacketState {
                        port_id: request.port_id.clone(),
                        channel_id: request.channel_id.clone(),
                        sequence: commitment_idx,
                        data: commitment.clone(),
                    };

                    commitment_states.push(commitment_state);
                }

                let res_value = QueryPacketCommitmentsResponse {
                    commitments: commitment_states,
                    pagination: None,
                    height: None,
                }
                .encode_to_vec();

                Ok(response::Query {
                    code: 0.into(),
                    key: query.data,
                    log: "".to_string(),
                    value: res_value.into(),
                    proof: None,
                    height: height.try_into().context("failed to convert height")?,
                    codespace: "".to_string(),
                    info: "".to_string(),
                    index: 0,
                })
            }
            "/ibc.core.channel.v1.Query/PacketAcknowledgements" => {
                let (snapshot, height) = self
                    .get_snapshot_for_height(u64::from(query.height))
                    .await?;

                let request = QueryPacketAcknowledgementsRequest::decode(query.data.clone())
                    .context("failed to decode QueryPacketAcknowledgementsRequest")?;

                let chan_id: ChannelId =
                    ChannelId::from_str(&request.channel_id).context("invalid channel id")?;
                let port_id: PortId =
                    PortId::from_str(&request.port_id).context("invalid port id")?;

                let ack_counter = snapshot.get_ack_sequence(&chan_id, &port_id).await?;

                let mut acks = vec![];
                for ack_idx in 0..ack_counter {
                    let ack = snapshot
                        .get_packet_acknowledgement(&port_id, &chan_id, ack_idx)
                        .await?
                        .ok_or_else(|| anyhow::anyhow!("couldn't find ack"))?;

                    let ack_state = PacketState {
                        port_id: request.port_id.clone(),
                        channel_id: request.channel_id.clone(),
                        sequence: ack_idx,
                        data: ack.clone(),
                    };

                    acks.push(ack_state);
                }

                let res_value = QueryPacketAcknowledgementsResponse {
                    acknowledgements: acks,
                    pagination: None,
                    height: None,
                }
                .encode_to_vec();

                Ok(response::Query {
                    code: 0.into(),
                    key: query.data,
                    log: "".to_string(),
                    value: res_value.into(),
                    proof: None,
                    height: height.try_into().context("failed to convert height")?,
                    codespace: "".to_string(),
                    info: "".to_string(),
                    index: 0,
                })
            }

            // docstring from ibc-go:
            //
            // UnreceivedPackets implements the Query/UnreceivedPackets gRPC method. Given
            // a list of counterparty packet commitments, the querier checks if the packet
            // has already been received by checking if a receipt exists on this
            // chain for the packet sequence. All packets that haven't been received yet
            // are returned in the response
            // Usage: To use this method correctly, first query all packet commitments on
            // the sending chain using the Query/PacketCommitments gRPC method.
            // Then input the returned sequences into the QueryUnreceivedPacketsRequest
            // and send the request to this Query/UnreceivedPackets on the **receiving**
            // chain. This gRPC method will then return the list of packet sequences that
            // are yet to be received on the receiving chain.
            //
            // NOTE: The querier makes the assumption that the provided list of packet
            // commitments is correct and will not function properly if the list
            // is not up to date. Ideally the query height should equal the latest height
            // on the counterparty's client which represents this chain
            "/ibc.core.channel.v1.Query/UnreceivedPackets" => {
                let (snapshot, height) = self
                    .get_snapshot_for_height(u64::from(query.height))
                    .await?;

                let request = QueryUnreceivedPacketsRequest::decode(query.data.clone())
                    .context("failed to decode QueryUnreceivedPacketsRequest")?;

                let chan_id: ChannelId =
                    ChannelId::from_str(&request.channel_id).context("invalid channel id")?;
                let port_id: PortId =
                    PortId::from_str(&request.port_id).context("invalid port id")?;

                let mut unreceived_seqs = vec![];

                for seq in request.packet_commitment_sequences {
                    if seq == 0 {
                        anyhow::bail!("packet sequence {} cannot be 0", seq);
                    }

                    if !snapshot
                        .seen_packet_by_channel(&chan_id, &port_id, seq)
                        .await?
                    {
                        unreceived_seqs.push(seq);
                    }
                }

                let res_value = QueryUnreceivedPacketsResponse {
                    sequences: unreceived_seqs,
                    height: None,
                }
                .encode_to_vec();

                Ok(response::Query {
                    code: 0.into(),
                    key: query.data,
                    log: "".to_string(),
                    value: res_value.into(),
                    proof: None,
                    height: height.try_into().context("failed to convert height")?,
                    codespace: "".to_string(),
                    info: "".to_string(),
                    index: 0,
                })
            }

            "/ibc.core.channel.v1.Query/UnreceivedAcks" => {
                let (snapshot, height) = self
                    .get_snapshot_for_height(u64::from(query.height))
                    .await?;

                let request = QueryUnreceivedAcksRequest::decode(query.data.clone())
                    .context("failed to decode QueryUnreceivedAcksRequest")?;

                let chan_id: ChannelId =
                    ChannelId::from_str(&request.channel_id).context("invalid channel id")?;
                let port_id: PortId =
                    PortId::from_str(&request.port_id).context("invalid port id")?;

                let mut unreceived_seqs = vec![];

                for seq in request.packet_ack_sequences {
                    if seq == 0 {
                        anyhow::bail!("packet sequence {} cannot be 0", seq);
                    }

                    if snapshot
                        .get_packet_commitment_by_id(&chan_id, &port_id, seq)
                        .await?
                        .is_some()
                    {
                        unreceived_seqs.push(seq);
                    }
                }

                let res_value = QueryUnreceivedAcksResponse {
                    sequences: unreceived_seqs,
                    height: None,
                }
                .encode_to_vec();

                Ok(response::Query {
                    code: 0.into(),
                    key: query.data,
                    log: "".to_string(),
                    value: res_value.into(),
                    proof: None,
                    height: height.try_into().context("failed to convert height")?,
                    codespace: "".to_string(),
                    info: "".to_string(),
                    index: 0,
                })
            }

            _ => Err(anyhow::anyhow!(
                "requested unrecognized path in ABCI query: {}",
                query.path
            )),
        }
    }
}

impl tower_service::Service<InfoRequest> for Info {
    type Response = InfoResponse;
    type Error = BoxError;
    type Future = Pin<Box<dyn Future<Output = Result<InfoResponse, BoxError>> + Send + 'static>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: InfoRequest) -> Self::Future {
        let span = req.create_span();
        let self2 = self.clone();

        async move {
            match req {
                InfoRequest::Info(info) => self2
                    .info(info)
                    .await
                    .map(InfoResponse::Info)
                    .map_err(Into::into),
                InfoRequest::Query(query) => match self2.query(query).await {
                    Ok(rsp) => {
                        tracing::debug!(value = ?rsp.value);
                        Ok(InfoResponse::Query(rsp))
                    }
                    Err(e) => {
                        tracing::debug!(error = ?e);
                        Ok(InfoResponse::Query(response::Query {
                            code: 1.into(),
                            log: format!("{:#}", e),
                            ..Default::default()
                        }))
                    }
                },
                InfoRequest::Echo(echo) => Ok(InfoResponse::Echo(Echo {
                    message: echo.message,
                })),
            }
        }
        .instrument(span)
        .boxed()
    }
}