use fastcrypto::encoding::Base64;
use iota_types::{
base_types::TransactionDigest,
committee::EpochId,
crypto::AggregateAuthoritySignature,
digests::CheckpointDigest,
gas::GasCostSummary,
iota_serde::BigInt,
message_envelope::Message,
messages_checkpoint::{
CheckpointCommitment, CheckpointContents, CheckpointSequenceNumber, CheckpointSummary,
CheckpointTimestamp, EndOfEpochData,
},
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use crate::Page;
pub type CheckpointPage = Page<Checkpoint, BigInt<u64>>;
#[serde_as]
#[derive(Clone, Debug, JsonSchema, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Checkpoint {
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub epoch: EpochId,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub sequence_number: CheckpointSequenceNumber,
pub digest: CheckpointDigest,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub network_total_transactions: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub previous_digest: Option<CheckpointDigest>,
pub epoch_rolling_gas_cost_summary: GasCostSummary,
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
pub timestamp_ms: CheckpointTimestamp,
#[serde(skip_serializing_if = "Option::is_none")]
pub end_of_epoch_data: Option<EndOfEpochData>,
pub transactions: Vec<TransactionDigest>,
pub checkpoint_commitments: Vec<CheckpointCommitment>,
#[schemars(with = "Base64")]
pub validator_signature: AggregateAuthoritySignature,
}
impl
From<(
CheckpointSummary,
CheckpointContents,
AggregateAuthoritySignature,
)> for Checkpoint
{
fn from(
(summary, contents, signature): (
CheckpointSummary,
CheckpointContents,
AggregateAuthoritySignature,
),
) -> Self {
let digest = summary.digest();
let CheckpointSummary {
epoch,
sequence_number,
network_total_transactions,
previous_digest,
epoch_rolling_gas_cost_summary,
timestamp_ms,
end_of_epoch_data,
..
} = summary;
Checkpoint {
epoch,
sequence_number,
digest,
network_total_transactions,
previous_digest,
epoch_rolling_gas_cost_summary,
timestamp_ms,
end_of_epoch_data,
transactions: contents.iter().map(|digest| digest.transaction).collect(),
checkpoint_commitments: Default::default(),
validator_signature: signature,
}
}
}
#[serde_as]
#[derive(Clone, Copy, Debug, JsonSchema, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CheckpointId {
SequenceNumber(
#[schemars(with = "BigInt<u64>")]
#[serde_as(as = "BigInt<u64>")]
CheckpointSequenceNumber,
),
Digest(CheckpointDigest),
}
impl From<CheckpointSequenceNumber> for CheckpointId {
fn from(seq: CheckpointSequenceNumber) -> Self {
Self::SequenceNumber(seq)
}
}
impl From<CheckpointDigest> for CheckpointId {
fn from(digest: CheckpointDigest) -> Self {
Self::Digest(digest)
}
}