iota_json_rpc_types/
iota_extended.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::collections::BTreeMap;
6
7use fastcrypto::traits::ToFromBytes;
8use iota_types::{
9    base_types::{AuthorityName, EpochId},
10    committee::Committee,
11    iota_serde::BigInt,
12    iota_system_state::iota_system_state_summary::IotaValidatorSummary,
13    messages_checkpoint::CheckpointSequenceNumber,
14};
15use schemars::JsonSchema;
16use serde::{Deserialize, Serialize};
17use serde_with::{DisplayFromStr, serde_as};
18
19use crate::{
20    MoveFunctionName, Page,
21    iota_system_state_summary::IotaValidatorSummary as IotaValidatorSummarySchema,
22};
23
24pub type EpochPage = Page<EpochInfo, BigInt<u64>>;
25pub type EpochMetricsPage = Page<EpochMetrics, BigInt<u64>>;
26
27#[serde_as]
28#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
29#[serde(rename_all = "camelCase")]
30pub struct EpochInfo {
31    /// Epoch number
32    #[schemars(with = "String")]
33    #[serde_as(as = "DisplayFromStr")]
34    pub epoch: EpochId,
35    /// List of validators included in epoch
36    #[schemars(with = "Vec<IotaValidatorSummarySchema>")]
37    pub validators: Vec<IotaValidatorSummary>,
38    /// Count of tx in epoch
39    #[schemars(with = "String")]
40    #[serde_as(as = "DisplayFromStr")]
41    pub epoch_total_transactions: u64,
42    /// First, last checkpoint sequence numbers
43    #[schemars(with = "String")]
44    #[serde_as(as = "DisplayFromStr")]
45    pub first_checkpoint_id: CheckpointSequenceNumber,
46    /// The timestamp when the epoch started.
47    #[schemars(with = "String")]
48    #[serde_as(as = "DisplayFromStr")]
49    pub epoch_start_timestamp: u64,
50    /// The end of epoch information.
51    pub end_of_epoch_info: Option<EndOfEpochInfo>,
52    /// The reference gas price for the given epoch.
53    pub reference_gas_price: Option<u64>,
54    /// Committee validators. Each element is an index
55    /// pointing to `validators`.
56    #[schemars(with = "Vec<String>")]
57    #[serde_as(as = "Vec<DisplayFromStr>")]
58    #[serde(skip_serializing_if = "Vec::is_empty")]
59    #[serde(default)]
60    pub committee_members: Vec<u64>,
61}
62
63impl EpochInfo {
64    pub fn committee(&self) -> Result<Committee, fastcrypto::error::FastCryptoError> {
65        let mut voting_rights = BTreeMap::new();
66        for &i in &self.committee_members {
67            let validator = self
68                .validators
69                .get(i as usize)
70                .expect("validators should include committee members");
71            let name = AuthorityName::from_bytes(&validator.authority_pubkey_bytes)?;
72            voting_rights.insert(name, validator.voting_power);
73        }
74        Ok(Committee::new(self.epoch, voting_rights))
75    }
76}
77
78/// A light-weight version of `EpochInfo` for faster loading
79#[serde_as]
80#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
81#[serde(rename_all = "camelCase")]
82pub struct EpochMetrics {
83    /// The current epoch ID.
84    #[schemars(with = "String")]
85    #[serde_as(as = "DisplayFromStr")]
86    pub epoch: EpochId,
87    /// The total number of transactions in the epoch.
88    #[schemars(with = "String")]
89    #[serde_as(as = "DisplayFromStr")]
90    pub epoch_total_transactions: u64,
91    /// The first checkpoint ID of the epoch.
92    #[schemars(with = "String")]
93    #[serde_as(as = "DisplayFromStr")]
94    pub first_checkpoint_id: CheckpointSequenceNumber,
95    /// The timestamp when the epoch started.
96    #[schemars(with = "String")]
97    #[serde_as(as = "DisplayFromStr")]
98    pub epoch_start_timestamp: u64,
99    /// The end of epoch information.
100    pub end_of_epoch_info: Option<EndOfEpochInfo>,
101}
102
103#[serde_as]
104#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
105#[serde(rename_all = "camelCase")]
106pub struct EndOfEpochInfo {
107    #[schemars(with = "String")]
108    #[serde_as(as = "DisplayFromStr")]
109    pub last_checkpoint_id: CheckpointSequenceNumber,
110    #[schemars(with = "String")]
111    #[serde_as(as = "DisplayFromStr")]
112    pub epoch_end_timestamp: u64,
113    /// existing fields from `SystemEpochInfoEventV1` (without epoch)
114    #[schemars(with = "String")]
115    #[serde_as(as = "DisplayFromStr")]
116    pub protocol_version: u64,
117    #[schemars(with = "String")]
118    #[serde_as(as = "DisplayFromStr")]
119    pub reference_gas_price: u64,
120    #[schemars(with = "String")]
121    #[serde_as(as = "DisplayFromStr")]
122    pub total_stake: u64,
123    #[schemars(with = "String")]
124    #[serde_as(as = "DisplayFromStr")]
125    pub storage_charge: u64,
126    #[schemars(with = "String")]
127    #[serde_as(as = "DisplayFromStr")]
128    pub storage_rebate: u64,
129    #[schemars(with = "String")]
130    #[serde_as(as = "DisplayFromStr")]
131    pub storage_fund_balance: u64,
132    #[schemars(with = "String")]
133    #[serde_as(as = "DisplayFromStr")]
134    pub total_gas_fees: u64,
135    #[schemars(with = "String")]
136    #[serde_as(as = "DisplayFromStr")]
137    pub total_stake_rewards_distributed: u64,
138    #[schemars(with = "String")]
139    #[serde_as(as = "DisplayFromStr")]
140    pub burnt_tokens_amount: u64,
141    #[schemars(with = "String")]
142    #[serde_as(as = "DisplayFromStr")]
143    pub minted_tokens_amount: u64,
144}
145
146#[serde_as]
147#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
148#[serde(rename_all = "camelCase")]
149pub struct NetworkMetrics {
150    /// Current TPS - Transaction Blocks per Second.
151    pub current_tps: f64,
152    /// Peak TPS in the past 30 days
153    pub tps_30_days: f64,
154    /// Total number of packages published in the network
155    #[schemars(with = "String")]
156    #[serde_as(as = "DisplayFromStr")]
157    pub total_packages: u64,
158    /// Total number of addresses seen in the network
159    #[schemars(with = "String")]
160    #[serde_as(as = "DisplayFromStr")]
161    pub total_addresses: u64,
162    /// Total number of live objects in the network
163    #[schemars(with = "String")]
164    #[serde_as(as = "DisplayFromStr")]
165    pub total_objects: u64,
166    /// Current epoch number
167    #[schemars(with = "String")]
168    #[serde_as(as = "DisplayFromStr")]
169    pub current_epoch: u64,
170    /// Current checkpoint number
171    #[schemars(with = "String")]
172    #[serde_as(as = "DisplayFromStr")]
173    pub current_checkpoint: u64,
174}
175
176#[serde_as]
177#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
178#[serde(rename_all = "camelCase")]
179pub struct MoveCallMetrics {
180    /// The count of calls of each function in the last 3 days.
181    #[schemars(with = "Vec<(MoveFunctionName, String)>")]
182    #[serde_as(as = "Vec<(_, DisplayFromStr)>")]
183    pub rank_3_days: Vec<(MoveFunctionName, usize)>,
184    /// The count of calls of each function in the last 7 days.
185    #[schemars(with = "Vec<(MoveFunctionName, String)>")]
186    #[serde_as(as = "Vec<(_, DisplayFromStr)>")]
187    pub rank_7_days: Vec<(MoveFunctionName, usize)>,
188    /// The count of calls of each function in the last 30 days.
189    #[schemars(with = "Vec<(MoveFunctionName, String)>")]
190    #[serde_as(as = "Vec<(_, DisplayFromStr)>")]
191    pub rank_30_days: Vec<(MoveFunctionName, usize)>,
192}
193
194/// Provides metrics about the addresses.
195#[serde_as]
196#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
197#[serde(rename_all = "camelCase")]
198pub struct AddressMetrics {
199    /// The checkpoint sequence number at which the metrics were computed.
200    pub checkpoint: u64,
201    /// The epoch to which the checkpoint is assigned.
202    pub epoch: u64,
203    /// The checkpoint timestamp.
204    pub timestamp_ms: u64,
205    /// The count of sender and recipient addresses.
206    pub cumulative_addresses: u64,
207    /// The count of sender addresses.
208    pub cumulative_active_addresses: u64,
209    /// The count of daily unique sender addresses.
210    pub daily_active_addresses: u64,
211}
212
213/// Provides metrics about the participation in the network.
214#[serde_as]
215#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
216#[serde(rename_all = "camelCase")]
217pub struct ParticipationMetrics {
218    /// The count of distinct addresses with delegated stake.
219    pub total_addresses: u64,
220}