Skip to main content

celestia_types/state/
abci.rs

1use celestia_proto::cosmos::base::tendermint::v1beta1::{
2    AbciQueryResponse as RawAbciQueryResponse, ProofOps,
3};
4use serde::{Deserialize, Serialize};
5#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
6use wasm_bindgen::prelude::*;
7
8use crate::state::ErrorCode;
9use crate::{Error, Height, validation_error};
10
11/// Response to a tx query
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
14#[cfg_attr(
15    all(target_arch = "wasm32", feature = "wasm-bindgen"),
16    wasm_bindgen(getter_with_clone)
17)]
18pub struct AbciQueryResponse {
19    /// The block height from which data was derived.
20    ///
21    /// Note that this is the height of the block containing the application's Merkle root hash,
22    /// which represents the state as it was after committing the block at height - 1.
23    #[cfg_attr(
24        all(target_arch = "wasm32", feature = "wasm-bindgen"),
25        wasm_bindgen(skip)
26    )]
27    pub height: Height,
28
29    /// Response code.
30    pub code: ErrorCode,
31
32    /// Namespace for the Code.
33    pub codespace: String,
34
35    /// The index of the key in the tree.
36    pub index: u64,
37
38    /// The key of the matching data.
39    pub key: Vec<u8>,
40
41    /// The value of the matching data.
42    pub value: Vec<u8>,
43
44    /// Serialized proof for the value data, if requested,
45    /// to be verified against the [`AppHash`] for the given [`Height`].
46    ///
47    /// [`AppHash`]: tendermint::hash::AppHash
48    pub proof_ops: Option<ProofOps>,
49
50    /// The output of the application's logger (raw string). May be
51    /// non-deterministic.
52    pub log: String,
53
54    /// Additional information. May be non-deterministic.
55    pub info: String,
56}
57
58#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
59#[wasm_bindgen]
60impl AbciQueryResponse {
61    /// The block height from which data was derived.
62    ///
63    /// Note that this is the height of the block containing the application's Merkle root hash,
64    /// which represents the state as it was after committing the block at height - 1.
65    #[wasm_bindgen(getter)]
66    pub fn height(&self) -> u64 {
67        self.height.value()
68    }
69}
70
71impl TryFrom<RawAbciQueryResponse> for AbciQueryResponse {
72    type Error = Error;
73
74    fn try_from(response: RawAbciQueryResponse) -> Result<AbciQueryResponse, Self::Error> {
75        Ok(AbciQueryResponse {
76            height: response.height.try_into()?,
77            code: response.code.try_into()?,
78            codespace: response.codespace,
79            index: response
80                .index
81                .try_into()
82                .map_err(|_| validation_error!("Negative index"))?,
83            key: response.key,
84            value: response.value,
85            proof_ops: response.proof_ops,
86            log: response.log,
87            info: response.info,
88        })
89    }
90}