iota_json_rpc_types/
iota_checkpoint.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use fastcrypto::encoding::Base64;
6use iota_types::{
7    base_types::TransactionDigest,
8    committee::EpochId,
9    crypto::AggregateAuthoritySignature,
10    digests::CheckpointDigest,
11    gas::GasCostSummary,
12    iota_serde::BigInt,
13    message_envelope::Message,
14    messages_checkpoint::{
15        CheckpointCommitment, CheckpointContents, CheckpointSequenceNumber, CheckpointSummary,
16        CheckpointTimestamp, EndOfEpochData,
17    },
18};
19use schemars::JsonSchema;
20use serde::{Deserialize, Serialize};
21use serde_with::serde_as;
22
23use crate::Page;
24pub type CheckpointPage = Page<Checkpoint, BigInt<u64>>;
25
26#[serde_as]
27#[derive(Clone, Debug, JsonSchema, Serialize, Deserialize, PartialEq, Eq)]
28#[serde(rename_all = "camelCase")]
29pub struct Checkpoint {
30    /// Checkpoint's epoch ID
31    #[schemars(with = "BigInt<u64>")]
32    #[serde_as(as = "BigInt<u64>")]
33    pub epoch: EpochId,
34    /// Checkpoint sequence number
35    #[schemars(with = "BigInt<u64>")]
36    #[serde_as(as = "BigInt<u64>")]
37    pub sequence_number: CheckpointSequenceNumber,
38    /// Checkpoint digest
39    pub digest: CheckpointDigest,
40    /// Total number of transactions committed since genesis, including those in
41    /// this checkpoint.
42    #[schemars(with = "BigInt<u64>")]
43    #[serde_as(as = "BigInt<u64>")]
44    pub network_total_transactions: u64,
45    /// Digest of the previous checkpoint
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub previous_digest: Option<CheckpointDigest>,
48    /// The running total gas costs of all transactions included in the current
49    /// epoch so far until this checkpoint.
50    pub epoch_rolling_gas_cost_summary: GasCostSummary,
51    /// Timestamp of the checkpoint - number of milliseconds from the Unix epoch
52    /// Checkpoint timestamps are monotonic, but not strongly monotonic -
53    /// subsequent checkpoints can have same timestamp if they originate
54    /// from the same underlining consensus commit
55    #[schemars(with = "BigInt<u64>")]
56    #[serde_as(as = "BigInt<u64>")]
57    pub timestamp_ms: CheckpointTimestamp,
58    /// Present only on the final checkpoint of the epoch.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub end_of_epoch_data: Option<EndOfEpochData>,
61    /// Transaction digests
62    pub transactions: Vec<TransactionDigest>,
63
64    /// Commitments to checkpoint state
65    pub checkpoint_commitments: Vec<CheckpointCommitment>,
66    /// Validator Signature
67    #[schemars(with = "Base64")]
68    //#[serde_as(as = "Readable<Base64, Bytes>")]
69    pub validator_signature: AggregateAuthoritySignature,
70}
71
72impl
73    From<(
74        CheckpointSummary,
75        CheckpointContents,
76        AggregateAuthoritySignature,
77    )> for Checkpoint
78{
79    fn from(
80        (summary, contents, signature): (
81            CheckpointSummary,
82            CheckpointContents,
83            AggregateAuthoritySignature,
84        ),
85    ) -> Self {
86        let digest = summary.digest();
87        let CheckpointSummary {
88            epoch,
89            sequence_number,
90            network_total_transactions,
91            previous_digest,
92            epoch_rolling_gas_cost_summary,
93            timestamp_ms,
94            end_of_epoch_data,
95            ..
96        } = summary;
97
98        Checkpoint {
99            epoch,
100            sequence_number,
101            digest,
102            network_total_transactions,
103            previous_digest,
104            epoch_rolling_gas_cost_summary,
105            timestamp_ms,
106            end_of_epoch_data,
107            transactions: contents.iter().map(|digest| digest.transaction).collect(),
108            // TODO: populate commitment for rpc clients. Most likely, rpc clients don't need this
109            // info (if they need it, they need to get signed BCS data anyway in order to trust
110            // it).
111            checkpoint_commitments: Default::default(),
112            validator_signature: signature,
113        }
114    }
115}
116
117#[serde_as]
118#[derive(Clone, Copy, Debug, JsonSchema, Serialize, Deserialize)]
119#[serde(untagged)]
120pub enum CheckpointId {
121    SequenceNumber(
122        #[schemars(with = "BigInt<u64>")]
123        #[serde_as(as = "BigInt<u64>")]
124        CheckpointSequenceNumber,
125    ),
126    Digest(CheckpointDigest),
127}
128
129impl From<CheckpointSequenceNumber> for CheckpointId {
130    fn from(seq: CheckpointSequenceNumber) -> Self {
131        Self::SequenceNumber(seq)
132    }
133}
134
135impl From<CheckpointDigest> for CheckpointId {
136    fn from(digest: CheckpointDigest) -> Self {
137        Self::Digest(digest)
138    }
139}