1use std::{
6 collections::{BTreeMap, HashMap},
7 fs::File,
8 io::{BufReader, BufWriter},
9 path::Path,
10};
11
12use anyhow::{Context, Result};
13use fastcrypto::{
14 encoding::{Base64, Encoding},
15 hash::HashFunction,
16};
17use iota_types::{
18 GENESIS_IOTA_BRIDGE_OBJECT_ID, IOTA_RANDOMNESS_STATE_OBJECT_ID,
19 authenticator_state::{AuthenticatorStateInner, get_authenticator_state},
20 base_types::{IotaAddress, ObjectID},
21 clock::Clock,
22 committee::{Committee, CommitteeWithNetworkMetadata, EpochId, ProtocolVersion},
23 crypto::DefaultHash,
24 deny_list_v1::get_deny_list_root_object,
25 effects::{TransactionEffects, TransactionEvents},
26 error::IotaResult,
27 iota_system_state::{
28 IotaSystemState, IotaSystemStateTrait, IotaSystemStateWrapper, IotaValidatorGenesis,
29 get_iota_system_state, get_iota_system_state_wrapper,
30 },
31 messages_checkpoint::{
32 CertifiedCheckpointSummary, CheckpointContents, CheckpointSummary, VerifiedCheckpoint,
33 },
34 object::Object,
35 storage::ObjectStore,
36 transaction::Transaction,
37};
38use serde::{Deserialize, Deserializer, Serialize, Serializer};
39use tracing::trace;
40
41#[derive(Clone, Debug)]
42pub struct Genesis {
43 checkpoint: CertifiedCheckpointSummary,
44 checkpoint_contents: CheckpointContents,
45 transaction: Transaction,
46 effects: TransactionEffects,
47 events: TransactionEvents,
48 objects: Vec<Object>,
49}
50
51#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Debug)]
52pub struct UnsignedGenesis {
53 pub checkpoint: CheckpointSummary,
54 pub checkpoint_contents: CheckpointContents,
55 pub transaction: Transaction,
56 pub effects: TransactionEffects,
57 pub events: TransactionEvents,
58 pub objects: Vec<Object>,
59}
60
61impl PartialEq for Genesis {
64 fn eq(&self, other: &Self) -> bool {
65 self.checkpoint.data() == other.checkpoint.data()
66 && {
67 let this = self.checkpoint.auth_sig();
68 let other = other.checkpoint.auth_sig();
69
70 this.epoch == other.epoch
71 && this.signature.as_ref() == other.signature.as_ref()
72 && this.signers_map == other.signers_map
73 }
74 && self.checkpoint_contents == other.checkpoint_contents
75 && self.transaction == other.transaction
76 && self.effects == other.effects
77 && self.objects == other.objects
78 }
79}
80
81impl Eq for Genesis {}
82
83impl Genesis {
84 pub fn new(
85 checkpoint: CertifiedCheckpointSummary,
86 checkpoint_contents: CheckpointContents,
87 transaction: Transaction,
88 effects: TransactionEffects,
89 events: TransactionEvents,
90 objects: Vec<Object>,
91 ) -> Self {
92 Self {
93 checkpoint,
94 checkpoint_contents,
95 transaction,
96 effects,
97 events,
98 objects,
99 }
100 }
101
102 pub fn into_objects(self) -> Vec<Object> {
103 self.objects
104 }
105
106 pub fn objects(&self) -> &[Object] {
107 &self.objects
108 }
109
110 pub fn object(&self, id: ObjectID) -> Option<Object> {
111 self.objects.iter().find(|o| o.id() == id).cloned()
112 }
113
114 pub fn transaction(&self) -> &Transaction {
115 &self.transaction
116 }
117
118 pub fn effects(&self) -> &TransactionEffects {
119 &self.effects
120 }
121 pub fn events(&self) -> &TransactionEvents {
122 &self.events
123 }
124
125 pub fn checkpoint(&self) -> VerifiedCheckpoint {
126 self.checkpoint
127 .clone()
128 .try_into_verified(&self.committee().unwrap())
129 .unwrap()
130 }
131
132 pub fn checkpoint_contents(&self) -> &CheckpointContents {
133 &self.checkpoint_contents
134 }
135
136 pub fn epoch(&self) -> EpochId {
137 0
138 }
139
140 pub fn validator_set_for_tooling(&self) -> Vec<IotaValidatorGenesis> {
141 self.iota_system_object()
142 .into_genesis_version_for_tooling()
143 .validators
144 .active_validators
145 }
146
147 pub fn committee_with_network(&self) -> CommitteeWithNetworkMetadata {
148 self.iota_system_object().get_current_epoch_committee()
149 }
150
151 pub fn reference_gas_price(&self) -> u64 {
152 self.iota_system_object().reference_gas_price()
153 }
154
155 pub fn committee(&self) -> IotaResult<Committee> {
157 Ok(self.committee_with_network().committee().clone())
158 }
159
160 pub fn iota_system_wrapper_object(&self) -> IotaSystemStateWrapper {
161 get_iota_system_state_wrapper(&self.objects())
162 .expect("IOTA System State Wrapper object must always exist")
163 }
164
165 pub fn contains_migrations(&self) -> bool {
166 self.checkpoint_contents.size() > 1
167 }
168
169 pub fn iota_system_object(&self) -> IotaSystemState {
170 get_iota_system_state(&self.objects()).expect("IOTA System State object must always exist")
171 }
172
173 pub fn clock(&self) -> Clock {
174 let clock = self
175 .objects()
176 .iter()
177 .find(|o| o.id() == iota_types::IOTA_CLOCK_OBJECT_ID)
178 .expect("clock must always exist")
179 .data
180 .try_as_move()
181 .expect("clock must be a Move object");
182 bcs::from_bytes::<Clock>(clock.contents())
183 .expect("clock object deserialization cannot fail")
184 }
185
186 pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, anyhow::Error> {
187 let path = path.as_ref();
188 trace!("reading Genesis from {}", path.display());
189 let read = File::open(path)
190 .with_context(|| format!("unable to load Genesis from {}", path.display()))?;
191 bcs::from_reader(BufReader::new(read))
192 .with_context(|| format!("unable to parse Genesis from {}", path.display()))
193 }
194
195 pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), anyhow::Error> {
196 let path = path.as_ref();
197 trace!("writing Genesis to {}", path.display());
198 let mut write = BufWriter::new(File::create(path)?);
199 bcs::serialize_into(&mut write, &self)
200 .with_context(|| format!("unable to save Genesis to {}", path.display()))?;
201 Ok(())
202 }
203
204 pub fn to_bytes(&self) -> Vec<u8> {
205 bcs::to_bytes(self).expect("failed to serialize genesis")
206 }
207
208 pub fn hash(&self) -> [u8; 32] {
209 use std::io::Write;
210
211 let mut digest = DefaultHash::default();
212 digest.write_all(&self.to_bytes()).unwrap();
213 let hash = digest.finalize();
214 hash.into()
215 }
216}
217
218impl Serialize for Genesis {
219 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
220 where
221 S: Serializer,
222 {
223 use serde::ser::Error;
224
225 #[derive(Serialize)]
226 struct RawGenesis<'a> {
227 checkpoint: &'a CertifiedCheckpointSummary,
228 checkpoint_contents: &'a CheckpointContents,
229 transaction: &'a Transaction,
230 effects: &'a TransactionEffects,
231 events: &'a TransactionEvents,
232 objects: &'a [Object],
233 }
234
235 let raw_genesis = RawGenesis {
236 checkpoint: &self.checkpoint,
237 checkpoint_contents: &self.checkpoint_contents,
238 transaction: &self.transaction,
239 effects: &self.effects,
240 events: &self.events,
241 objects: &self.objects,
242 };
243
244 if serializer.is_human_readable() {
245 let bytes = bcs::to_bytes(&raw_genesis).map_err(|e| Error::custom(e.to_string()))?;
246 let s = Base64::encode(bytes);
247 serializer.serialize_str(&s)
248 } else {
249 raw_genesis.serialize(serializer)
250 }
251 }
252}
253
254impl<'de> Deserialize<'de> for Genesis {
255 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
256 where
257 D: Deserializer<'de>,
258 {
259 use serde::de::Error;
260
261 #[derive(Deserialize)]
262 struct RawGenesis {
263 checkpoint: CertifiedCheckpointSummary,
264 checkpoint_contents: CheckpointContents,
265 transaction: Transaction,
266 effects: TransactionEffects,
267 events: TransactionEvents,
268 objects: Vec<Object>,
269 }
270
271 let raw_genesis = if deserializer.is_human_readable() {
272 let s = String::deserialize(deserializer)?;
273 let bytes = Base64::decode(&s).map_err(|e| Error::custom(e.to_string()))?;
274 bcs::from_bytes(&bytes).map_err(|e| Error::custom(e.to_string()))?
275 } else {
276 RawGenesis::deserialize(deserializer)?
277 };
278
279 Ok(Genesis {
280 checkpoint: raw_genesis.checkpoint,
281 checkpoint_contents: raw_genesis.checkpoint_contents,
282 transaction: raw_genesis.transaction,
283 effects: raw_genesis.effects,
284 events: raw_genesis.events,
285 objects: raw_genesis.objects,
286 })
287 }
288}
289
290impl UnsignedGenesis {
291 pub fn objects(&self) -> &[Object] {
292 &self.objects
293 }
294
295 pub fn object(&self, id: ObjectID) -> Option<Object> {
296 self.objects.iter().find(|o| o.id() == id).cloned()
297 }
298
299 pub fn transaction(&self) -> &Transaction {
300 &self.transaction
301 }
302
303 pub fn effects(&self) -> &TransactionEffects {
304 &self.effects
305 }
306 pub fn events(&self) -> &TransactionEvents {
307 &self.events
308 }
309
310 pub fn checkpoint(&self) -> &CheckpointSummary {
311 &self.checkpoint
312 }
313
314 pub fn checkpoint_contents(&self) -> &CheckpointContents {
315 &self.checkpoint_contents
316 }
317
318 pub fn epoch(&self) -> EpochId {
319 0
320 }
321
322 pub fn iota_system_wrapper_object(&self) -> IotaSystemStateWrapper {
323 get_iota_system_state_wrapper(&self.objects())
324 .expect("IOTA System State Wrapper object must always exist")
325 }
326
327 pub fn iota_system_object(&self) -> IotaSystemState {
328 get_iota_system_state(&self.objects()).expect("IOTA System State object must always exist")
329 }
330
331 pub fn authenticator_state_object(&self) -> Option<AuthenticatorStateInner> {
332 get_authenticator_state(self.objects()).expect("read from genesis cannot fail")
333 }
334
335 pub fn has_randomness_state_object(&self) -> bool {
336 self.objects()
337 .get_object(&IOTA_RANDOMNESS_STATE_OBJECT_ID)
338 .is_some()
339 }
340
341 pub fn has_bridge_object(&self) -> bool {
342 self.objects()
343 .get_object(&GENESIS_IOTA_BRIDGE_OBJECT_ID)
344 .is_some()
345 }
346
347 pub fn has_coin_deny_list_object(&self) -> bool {
348 get_deny_list_root_object(&self.objects()).is_some()
349 }
350}
351
352#[derive(Clone, Debug, Serialize, Deserialize)]
353#[serde(rename_all = "kebab-case")]
354pub struct GenesisChainParameters {
355 pub protocol_version: u64,
356 pub chain_start_timestamp_ms: u64,
357 pub epoch_duration_ms: u64,
358
359 pub max_validator_count: u64,
361 pub min_validator_joining_stake: u64,
362 pub validator_low_stake_threshold: u64,
363 pub validator_very_low_stake_threshold: u64,
364 pub validator_low_stake_grace_period: u64,
365}
366
367#[derive(Serialize, Deserialize)]
369pub struct GenesisCeremonyParameters {
370 #[serde(default = "GenesisCeremonyParameters::default_timestamp_ms")]
371 pub chain_start_timestamp_ms: u64,
372
373 #[serde(default = "ProtocolVersion::max")]
375 pub protocol_version: ProtocolVersion,
376
377 #[serde(default = "GenesisCeremonyParameters::default_allow_insertion_of_extra_objects")]
378 pub allow_insertion_of_extra_objects: bool,
379
380 #[serde(default = "GenesisCeremonyParameters::default_epoch_duration_ms")]
382 pub epoch_duration_ms: u64,
383}
384
385impl GenesisCeremonyParameters {
386 pub fn new() -> Self {
387 Self {
388 chain_start_timestamp_ms: Self::default_timestamp_ms(),
389 protocol_version: ProtocolVersion::MAX,
390 allow_insertion_of_extra_objects: true,
391 epoch_duration_ms: Self::default_epoch_duration_ms(),
392 }
393 }
394
395 fn default_timestamp_ms() -> u64 {
396 std::time::SystemTime::now()
397 .duration_since(std::time::UNIX_EPOCH)
398 .unwrap()
399 .as_millis() as u64
400 }
401
402 fn default_allow_insertion_of_extra_objects() -> bool {
403 true
404 }
405
406 fn default_epoch_duration_ms() -> u64 {
407 24 * 60 * 60 * 1000
409 }
410
411 pub fn to_genesis_chain_parameters(&self) -> GenesisChainParameters {
412 GenesisChainParameters {
413 protocol_version: self.protocol_version.as_u64(),
414 chain_start_timestamp_ms: self.chain_start_timestamp_ms,
415 epoch_duration_ms: self.epoch_duration_ms,
416 max_validator_count: iota_types::governance::MAX_VALIDATOR_COUNT,
417 min_validator_joining_stake: iota_types::governance::MIN_VALIDATOR_JOINING_STAKE_NANOS,
418 validator_low_stake_threshold:
419 iota_types::governance::VALIDATOR_LOW_STAKE_THRESHOLD_NANOS,
420 validator_very_low_stake_threshold:
421 iota_types::governance::VALIDATOR_VERY_LOW_STAKE_THRESHOLD_NANOS,
422 validator_low_stake_grace_period:
423 iota_types::governance::VALIDATOR_LOW_STAKE_GRACE_PERIOD,
424 }
425 }
426}
427
428impl Default for GenesisCeremonyParameters {
429 fn default() -> Self {
430 Self::new()
431 }
432}
433
434#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
435#[serde(rename_all = "kebab-case")]
436pub struct TokenDistributionSchedule {
437 pub pre_minted_supply: u64,
438 pub allocations: Vec<TokenAllocation>,
439}
440
441impl TokenDistributionSchedule {
442 pub fn contains_timelocked_stake(&self) -> bool {
443 self.allocations
444 .iter()
445 .find_map(|allocation| allocation.staked_with_timelock_expiration)
446 .is_some()
447 }
448
449 pub fn validate(&self) {
450 let mut total_nanos = self.pre_minted_supply;
451
452 for allocation in &self.allocations {
453 total_nanos = total_nanos
454 .checked_add(allocation.amount_nanos)
455 .expect("TokenDistributionSchedule allocates more than the maximum supply which equals u64::MAX");
456 }
457 }
458
459 pub fn check_minimum_stake_for_validators<I: IntoIterator<Item = IotaAddress>>(
460 &self,
461 validators: I,
462 ) -> Result<()> {
463 let mut validators: HashMap<IotaAddress, u64> =
464 validators.into_iter().map(|a| (a, 0)).collect();
465
466 for allocation in &self.allocations {
469 if let Some(staked_with_validator) = &allocation.staked_with_validator {
470 *validators
471 .get_mut(staked_with_validator)
472 .expect("allocation must be staked with valid validator") +=
473 allocation.amount_nanos;
474 }
475 }
476
477 let minimum_required_stake = iota_types::governance::VALIDATOR_LOW_STAKE_THRESHOLD_NANOS;
480 for (validator, stake) in validators {
481 if stake < minimum_required_stake {
482 anyhow::bail!(
483 "validator {validator} has '{stake}' stake and does not meet the minimum required stake threshold of '{minimum_required_stake}'"
484 );
485 }
486 }
487 Ok(())
488 }
489
490 pub fn new_for_validators_with_default_allocation<I: IntoIterator<Item = IotaAddress>>(
491 validators: I,
492 ) -> Self {
493 let default_allocation = iota_types::governance::VALIDATOR_LOW_STAKE_THRESHOLD_NANOS;
494
495 let allocations = validators
496 .into_iter()
497 .map(|a| TokenAllocation {
498 recipient_address: a,
499 amount_nanos: default_allocation,
500 staked_with_validator: Some(a),
501 staked_with_timelock_expiration: None,
502 })
503 .collect();
504
505 let schedule = Self {
506 pre_minted_supply: 0,
507 allocations,
508 };
509
510 schedule.validate();
511 schedule
512 }
513
514 pub fn from_csv<R: std::io::Read>(reader: R) -> Result<Self> {
522 let mut reader = csv_reader_with_comments(reader);
523 let mut allocations: Vec<TokenAllocation> =
524 reader.deserialize().collect::<Result<_, _>>()?;
525
526 let pre_minted_supply = allocations.pop().unwrap();
527 assert_eq!(
528 IotaAddress::default(),
529 pre_minted_supply.recipient_address,
530 "final allocation must be for the pre-minted supply amount",
531 );
532 assert!(
533 pre_minted_supply.staked_with_validator.is_none(),
534 "cannot stake the pre-minted supply amount",
535 );
536
537 let schedule = Self {
538 pre_minted_supply: pre_minted_supply.amount_nanos,
539 allocations,
540 };
541
542 schedule.validate();
543 Ok(schedule)
544 }
545
546 pub fn to_csv<W: std::io::Write>(&self, writer: W) -> Result<()> {
547 let mut writer = csv::Writer::from_writer(writer);
548
549 for allocation in &self.allocations {
550 writer.serialize(allocation)?;
551 }
552
553 writer.serialize(TokenAllocation {
554 recipient_address: IotaAddress::default(),
555 amount_nanos: self.pre_minted_supply,
556 staked_with_validator: None,
557 staked_with_timelock_expiration: None,
558 })?;
559
560 Ok(())
561 }
562}
563
564#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
565#[serde(rename_all = "kebab-case")]
566pub struct TokenAllocation {
567 pub recipient_address: IotaAddress,
573 pub amount_nanos: u64,
579
580 pub staked_with_validator: Option<IotaAddress>,
583 pub staked_with_timelock_expiration: Option<u64>,
586}
587
588#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
589pub struct TokenDistributionScheduleBuilder {
590 pre_minted_supply: u64,
591 allocations: Vec<TokenAllocation>,
592}
593
594impl TokenDistributionScheduleBuilder {
595 #[expect(clippy::new_without_default)]
596 pub fn new() -> Self {
597 Self {
598 pre_minted_supply: 0,
599 allocations: vec![],
600 }
601 }
602
603 pub fn set_pre_minted_supply(&mut self, pre_minted_supply: u64) {
604 self.pre_minted_supply = pre_minted_supply;
605 }
606
607 pub fn default_allocation_for_validators<I: IntoIterator<Item = IotaAddress>>(
608 &mut self,
609 validators: I,
610 ) {
611 let default_allocation = iota_types::governance::VALIDATOR_LOW_STAKE_THRESHOLD_NANOS;
612
613 for validator in validators {
614 self.add_allocation(TokenAllocation {
615 recipient_address: validator,
616 amount_nanos: default_allocation,
617 staked_with_validator: Some(validator),
618 staked_with_timelock_expiration: None,
619 });
620 }
621 }
622
623 pub fn add_allocation(&mut self, allocation: TokenAllocation) {
624 self.allocations.push(allocation);
625 }
626
627 pub fn build(&self) -> TokenDistributionSchedule {
628 let schedule = TokenDistributionSchedule {
629 pre_minted_supply: self.pre_minted_supply,
630 allocations: self.allocations.clone(),
631 };
632
633 schedule.validate();
634 schedule
635 }
636}
637
638#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
640#[serde(rename_all = "kebab-case")]
641pub struct ValidatorAllocation {
642 pub validator: IotaAddress,
644 pub amount_nanos_to_stake: u64,
646 pub amount_nanos_to_pay_gas: u64,
648}
649
650#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
654#[serde(rename_all = "kebab-case")]
655pub struct Delegation {
656 pub delegator: IotaAddress,
658 #[serde(flatten)]
660 pub validator_allocation: ValidatorAllocation,
661}
662
663#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
670#[serde(rename_all = "kebab-case")]
671pub struct Delegations {
672 pub allocations: BTreeMap<IotaAddress, Vec<ValidatorAllocation>>,
673}
674
675impl Delegations {
676 pub fn new_for_validators_with_default_allocation(
677 validators: impl IntoIterator<Item = IotaAddress>,
678 delegator: IotaAddress,
679 ) -> Self {
680 let validator_allocations = validators
681 .into_iter()
682 .map(|address| ValidatorAllocation {
683 validator: address,
684 amount_nanos_to_stake: iota_types::governance::MIN_VALIDATOR_JOINING_STAKE_NANOS,
685 amount_nanos_to_pay_gas: 0,
686 })
687 .collect();
688
689 let mut allocations = BTreeMap::new();
690 allocations.insert(delegator, validator_allocations);
691
692 Self { allocations }
693 }
694
695 pub fn from_csv<R: std::io::Read>(reader: R) -> Result<Self> {
708 let mut reader = csv_reader_with_comments(reader);
709
710 let mut delegations = Self::default();
711 for delegation in reader.deserialize::<Delegation>() {
712 let delegation = delegation?;
713 delegations
714 .allocations
715 .entry(delegation.delegator)
716 .or_default()
717 .push(delegation.validator_allocation);
718 }
719
720 Ok(delegations)
721 }
722
723 pub fn to_csv<W: std::io::Write>(&self, writer: W) -> Result<()> {
731 let mut writer = csv::Writer::from_writer(writer);
732
733 writer.write_record([
734 "delegator",
735 "validator",
736 "amount-nanos-to-stake",
737 "amount-nanos-to-pay-gas",
738 ])?;
739
740 for (&delegator, validator_allocations) in &self.allocations {
741 for validator_allocation in validator_allocations {
742 writer.write_record(&[
743 delegator.to_string(),
744 validator_allocation.validator.to_string(),
745 validator_allocation.amount_nanos_to_stake.to_string(),
746 validator_allocation.amount_nanos_to_pay_gas.to_string(),
747 ])?;
748 }
749 }
750
751 Ok(())
752 }
753}
754
755pub fn csv_reader_with_comments<R: std::io::Read>(reader: R) -> csv::Reader<R> {
758 csv::ReaderBuilder::new()
759 .comment(Some(b'#'))
760 .from_reader(reader)
761}