iota_types/
full_checkpoint_content.rs1use std::collections::BTreeMap;
6
7use serde::{Deserialize, Serialize};
8use tap::Pipe;
9
10use crate::{
11 base_types::{ObjectID, ObjectRef},
12 effects::{
13 TransactionEffects, TransactionEffectsAPI, TransactionEffectsExt, TransactionEvents,
14 },
15 iota_system_state::{IotaSystemStateTrait, get_iota_system_state},
16 messages_checkpoint::{CertifiedCheckpointSummary, CheckpointContents},
17 object::Object,
18 storage::{BackingPackageStore, EpochInfo, error::Error as StorageError},
19 transaction::{Transaction, TransactionDataAPI, TransactionKind},
20};
21
22#[derive(Clone, Debug, Serialize, Deserialize)]
23pub struct CheckpointData {
24 pub checkpoint_summary: CertifiedCheckpointSummary,
25 pub checkpoint_contents: CheckpointContents,
26 pub transactions: Vec<CheckpointTransaction>,
27}
28
29impl CheckpointData {
30 pub fn latest_live_output_objects(&self) -> Vec<&Object> {
33 let mut latest_live_objects = BTreeMap::new();
34 for tx in self.transactions.iter() {
35 for obj in tx.output_objects.iter() {
36 latest_live_objects.insert(obj.id(), obj);
37 }
38 for obj_ref in tx.removed_object_refs_post_version() {
39 latest_live_objects.remove(&obj_ref.object_id);
40 }
41 }
42 latest_live_objects.into_values().collect()
43 }
44
45 pub fn eventually_removed_object_refs_post_version(&self) -> Vec<ObjectRef> {
48 let mut eventually_removed_object_refs = BTreeMap::new();
49 for tx in self.transactions.iter() {
50 for obj_ref in tx.removed_object_refs_post_version() {
51 eventually_removed_object_refs.insert(obj_ref.object_id, obj_ref);
52 }
53 for obj in tx.output_objects.iter() {
54 eventually_removed_object_refs.remove(&(obj.id()));
55 }
56 }
57 eventually_removed_object_refs.into_values().collect()
58 }
59
60 pub fn all_objects(&self) -> Vec<&Object> {
61 self.transactions
62 .iter()
63 .flat_map(|tx| &tx.input_objects)
64 .chain(self.transactions.iter().flat_map(|tx| &tx.output_objects))
65 .collect()
66 }
67
68 pub fn epoch_info(&self) -> Result<Option<EpochInfo>, StorageError> {
69 if self.checkpoint_summary.end_of_epoch_data.is_none()
71 && self.checkpoint_summary.sequence_number != 0
72 {
73 return Ok(None);
74 }
75
76 let (start_checkpoint, transaction) = if self.checkpoint_summary.sequence_number != 0 {
77 let Some(transaction) = self.transactions.iter().find(|tx| {
78 matches!(
79 tx.transaction.intent_message().value.kind(),
80 TransactionKind::EndOfEpoch(_)
81 )
82 }) else {
83 return Err(StorageError::custom(format!(
84 "Failed to get end of epoch transaction in checkpoint {} with EndOfEpochData",
85 self.checkpoint_summary.sequence_number,
86 )));
87 };
88 (self.checkpoint_summary.sequence_number + 1, transaction)
89 } else {
90 let Some(transaction) = self.transactions.iter().find(|tx| {
92 matches!(
93 tx.transaction.intent_message().value.kind(),
94 TransactionKind::Genesis(_)
95 )
96 }) else {
97 return Err(StorageError::custom(format!(
98 "Failed to get genesis transaction in checkpoint {}",
99 self.checkpoint_summary.sequence_number,
100 )));
101 };
102 (0, transaction)
103 };
104
105 let system_state =
106 get_iota_system_state(&transaction.output_objects.as_slice()).map_err(|e| {
107 StorageError::custom(format!(
108 "Failed to find system state object output from end of epoch or genesis transaction: {e}"
109 ))
110 })?;
111
112 Ok(Some(EpochInfo {
113 epoch: system_state.epoch(),
114 protocol_version: system_state.protocol_version(),
115 start_timestamp_ms: system_state.epoch_start_timestamp_ms(),
116 end_timestamp_ms: None,
117 start_checkpoint,
118 end_checkpoint: None,
119 reference_gas_price: system_state.reference_gas_price(),
120 system_state,
121 }))
122 }
123}
124
125#[derive(Clone, Debug, Serialize, Deserialize)]
126pub struct CheckpointTransaction {
127 pub transaction: Transaction,
129 pub effects: TransactionEffects,
131 pub events: Option<TransactionEvents>,
133 pub input_objects: Vec<Object>,
136 pub output_objects: Vec<Object>,
139}
140
141impl CheckpointTransaction {
142 pub fn removed_objects_pre_version(&self) -> impl Iterator<Item = &Object> {
144 self.effects
148 .all_removed_objects()
149 .into_iter() .map(|(object_ref, _)| {
151 self.input_objects
152 .iter()
153 .find(|o| o.id() == object_ref.object_id)
154 .expect("all removed objects should show up in input objects")
155 })
156 }
157
158 pub fn removed_object_refs_post_version(&self) -> impl Iterator<Item = ObjectRef> {
159 let deleted = self.effects.deleted().into_iter();
160 let wrapped = self.effects.wrapped().into_iter();
161 let unwrapped_then_deleted = self.effects.unwrapped_then_deleted().into_iter();
162 deleted.chain(wrapped).chain(unwrapped_then_deleted)
163 }
164
165 pub fn changed_objects(&self) -> impl Iterator<Item = (&Object, Option<&Object>)> {
166 self.effects
167 .all_changed_objects()
168 .into_iter()
169 .map(|(object_ref, ..)| {
170 let object = self
171 .output_objects
172 .iter()
173 .find(|o| o.id() == object_ref.object_id)
174 .expect("changed objects should show up in output objects");
175
176 let old_object = self
177 .input_objects
178 .iter()
179 .find(|o| o.id() == object_ref.object_id);
180
181 (object, old_object)
182 })
183 }
184
185 pub fn created_objects(&self) -> impl Iterator<Item = &Object> {
186 self.effects
188 .created()
189 .into_iter()
190 .map(|(object_ref, _)| {
192 self.output_objects
193 .iter()
194 .find(|o| o.id() == object_ref.object_id && o.version() == object_ref.version)
195 .expect("created objects should show up in output objects")
196 })
197 }
198}
199
200impl BackingPackageStore for CheckpointData {
201 fn get_package_object(
202 &self,
203 package_id: &ObjectID,
204 ) -> crate::error::IotaResult<Option<crate::storage::PackageObject>> {
205 self.transactions
206 .iter()
207 .flat_map(|transaction| transaction.output_objects.iter())
208 .find(|object| object.is_package() && &object.id() == package_id)
209 .cloned()
210 .map(crate::storage::PackageObject::new)
211 .pipe(Ok)
212 }
213}