iota_json_rpc_api/
read.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use iota_json_rpc_types::{
6    Checkpoint, CheckpointId, CheckpointPage, IotaEvent, IotaGetPastObjectRequest,
7    IotaObjectDataOptions, IotaObjectResponse, IotaPastObjectResponse,
8    IotaTransactionBlockResponse, IotaTransactionBlockResponseOptions, ProtocolConfigResponse,
9};
10use iota_open_rpc_macros::open_rpc;
11use iota_types::{
12    base_types::{ObjectID, SequenceNumber, TransactionDigest},
13    iota_serde::BigInt,
14};
15use jsonrpsee::{core::RpcResult, proc_macros::rpc};
16
17/// Provides methods for reading transaction related data such as transaction
18/// blocks, checkpoints, and protocol configuration. The trait further provides
19/// methods for reading the ledger (current objects) as well its history (past
20/// objects).
21#[open_rpc(namespace = "iota", tag = "Read API")]
22#[rpc(server, client, namespace = "iota")]
23pub trait ReadApi {
24    /// Return the transaction response object.
25    #[rustfmt::skip]
26    #[method(name = "getTransactionBlock")]
27    async fn get_transaction_block(
28        &self,
29        /// the digest of the queried transaction
30        digest: TransactionDigest,
31        /// options for specifying the content to be returned
32        options: Option<IotaTransactionBlockResponseOptions>,
33    ) -> RpcResult<IotaTransactionBlockResponse>;
34
35    /// Returns an ordered list of transaction responses
36    /// The method will throw an error if the input contains any duplicate or
37    /// the input size exceeds QUERY_MAX_RESULT_LIMIT
38    #[rustfmt::skip]
39    #[method(name = "multiGetTransactionBlocks")]
40    async fn multi_get_transaction_blocks(
41        &self,
42        /// A list of transaction digests.
43        digests: Vec<TransactionDigest>,
44        /// config options to control which fields to fetch
45        options: Option<IotaTransactionBlockResponseOptions>,
46    ) -> RpcResult<Vec<IotaTransactionBlockResponse>>;
47
48    /// Return the object information for a specified object
49    #[rustfmt::skip]
50    #[method(name = "getObject")]
51    async fn get_object(
52        &self,
53        /// the ID of the queried object
54        object_id: ObjectID,
55        /// options for specifying the content to be returned
56        options: Option<IotaObjectDataOptions>,
57    ) -> RpcResult<IotaObjectResponse>;
58
59    /// Return the object data for a list of objects
60    #[rustfmt::skip]
61    #[method(name = "multiGetObjects")]
62    async fn multi_get_objects(
63        &self,
64        /// the IDs of the queried objects
65        object_ids: Vec<ObjectID>,
66        /// options for specifying the content to be returned
67        options: Option<IotaObjectDataOptions>,
68    ) -> RpcResult<Vec<IotaObjectResponse>>;
69
70    /// Note there is no software-level guarantee/SLA that objects with past
71    /// versions can be retrieved by this API, even if the object and version
72    /// exists/existed. The result may vary across nodes depending on their
73    /// pruning policies. Return the object information for a specified version
74    #[rustfmt::skip]
75    #[method(name = "tryGetPastObject")]
76    async fn try_get_past_object(
77        &self,
78        /// the ID of the queried object
79        object_id: ObjectID,
80        /// the version of the queried object. If None, default to the latest known version
81        version: SequenceNumber,
82        /// options for specifying the content to be returned
83        options: Option<IotaObjectDataOptions>,
84    ) -> RpcResult<IotaPastObjectResponse>;
85
86    /// Note there is no software-level guarantee/SLA that objects with past
87    /// versions can be retrieved by this API, even if the object and
88    /// version exists/existed. The result may vary across nodes depending
89    /// on their pruning policies. Returns the latest object information
90    /// with a version less than or equal to the given version
91    // Note that this endpoint is used by iota replay tool. Also the
92    // implementation in `iota-json-rpc` uses internally the
93    // `AuthorityState::find_object_lt_or_eq_version` method, which has
94    // underlying utility, e.g., `RemoteFetcher::get_child_object` uses
95    // `try_get_object_before_version` to get the object with the versions <=
96    // the given version. We have the `deprecated` flag here to not expose it in
97    // the generated spec file, and it should be only for internal usage.
98    #[method(name = "tryGetObjectBeforeVersion", deprecated = "true")]
99    async fn try_get_object_before_version(
100        &self,
101        /// the ID of the queried object
102        object_id: ObjectID,
103        /// the version of the queried object
104        version: SequenceNumber,
105    ) -> RpcResult<IotaPastObjectResponse>;
106
107    /// Note there is no software-level guarantee/SLA that objects with past
108    /// versions can be retrieved by this API, even if the object and version
109    /// exists/existed. The result may vary across nodes depending on their
110    /// pruning policies. Return the object information for a specified version
111    #[rustfmt::skip]
112    #[method(name = "tryMultiGetPastObjects")]
113    async fn try_multi_get_past_objects(
114        &self,
115        /// a vector of object and versions to be queried
116        past_objects: Vec<IotaGetPastObjectRequest>,
117        /// options for specifying the content to be returned
118        options: Option<IotaObjectDataOptions>,
119    ) -> RpcResult<Vec<IotaPastObjectResponse>>;
120
121    /// Return a checkpoint
122    #[rustfmt::skip]
123    #[method(name = "getCheckpoint")]
124    async fn get_checkpoint(
125        &self,
126        /// Checkpoint identifier, can use either checkpoint digest, or checkpoint sequence number as input.
127        id: CheckpointId,
128    ) -> RpcResult<Checkpoint>;
129
130    /// Return paginated list of checkpoints
131    #[rustfmt::skip]
132    #[method(name = "getCheckpoints")]
133    async fn get_checkpoints(
134        &self,
135        /// An optional paging cursor. If provided, the query will start from the next item after the specified cursor. Default to start from the first item if not specified.
136        cursor: Option<BigInt<u64>>,
137        /// Maximum item returned per page, default to [QUERY_MAX_RESULT_LIMIT_CHECKPOINTS] if not specified.
138        limit: Option<usize>,
139        /// query result ordering, default to false (ascending order), oldest record first.
140        descending_order: bool,
141    ) -> RpcResult<CheckpointPage>;
142
143    /// Return transaction events.
144    #[method(name = "getEvents")]
145    async fn get_events(
146        &self,
147        /// the event query criteria.
148        transaction_digest: TransactionDigest,
149    ) -> RpcResult<Vec<IotaEvent>>;
150
151    /// Return the total number of transaction blocks known to the server.
152    #[method(name = "getTotalTransactionBlocks")]
153    async fn get_total_transaction_blocks(&self) -> RpcResult<BigInt<u64>>;
154
155    /// Return the sequence number of the latest checkpoint that has been
156    /// executed
157    #[method(name = "getLatestCheckpointSequenceNumber")]
158    async fn get_latest_checkpoint_sequence_number(&self) -> RpcResult<BigInt<u64>>;
159
160    /// Return the protocol config table for the given version number.
161    /// If the version number is not specified, If none is specified, the node uses
162    /// the version of the latest epoch it has processed.
163    #[rustfmt::skip]
164    #[method(name = "getProtocolConfig")]
165    async fn get_protocol_config(
166        &self,
167        /// An optional protocol version specifier. If omitted, the latest protocol config table for the node will be returned.
168        version: Option<BigInt<u64>>,
169    ) -> RpcResult<ProtocolConfigResponse>;
170
171    /// Return the first four bytes of the chain's genesis checkpoint digest.
172    #[method(name = "getChainIdentifier")]
173    async fn get_chain_identifier(&self) -> RpcResult<String>;
174}