iota_config/
genesis.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use 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    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
61// Hand implement PartialEq in order to get around the fact that AuthSigs don't
62// impl Eq
63impl 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    // TODO: No need to return IotaResult. Also consider return &.
156    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            .expect("read from genesis cannot fail")
339            .is_some()
340    }
341
342    pub fn has_bridge_object(&self) -> bool {
343        self.objects()
344            .get_object(&IOTA_BRIDGE_OBJECT_ID)
345            .expect("read from genesis cannot fail")
346            .is_some()
347    }
348
349    pub fn has_coin_deny_list_object(&self) -> bool {
350        get_deny_list_root_object(&self.objects()).is_ok()
351    }
352}
353
354#[derive(Clone, Debug, Serialize, Deserialize)]
355#[serde(rename_all = "kebab-case")]
356pub struct GenesisChainParameters {
357    pub protocol_version: u64,
358    pub chain_start_timestamp_ms: u64,
359    pub epoch_duration_ms: u64,
360
361    // Validator committee parameters
362    pub max_validator_count: u64,
363    pub min_validator_joining_stake: u64,
364    pub validator_low_stake_threshold: u64,
365    pub validator_very_low_stake_threshold: u64,
366    pub validator_low_stake_grace_period: u64,
367}
368
369/// Initial set of parameters for a chain.
370#[derive(Serialize, Deserialize)]
371pub struct GenesisCeremonyParameters {
372    #[serde(default = "GenesisCeremonyParameters::default_timestamp_ms")]
373    pub chain_start_timestamp_ms: u64,
374
375    /// protocol version that the chain starts at.
376    #[serde(default = "ProtocolVersion::max")]
377    pub protocol_version: ProtocolVersion,
378
379    #[serde(default = "GenesisCeremonyParameters::default_allow_insertion_of_extra_objects")]
380    pub allow_insertion_of_extra_objects: bool,
381
382    /// The duration of an epoch, in milliseconds.
383    #[serde(default = "GenesisCeremonyParameters::default_epoch_duration_ms")]
384    pub epoch_duration_ms: u64,
385}
386
387impl GenesisCeremonyParameters {
388    pub fn new() -> Self {
389        Self {
390            chain_start_timestamp_ms: Self::default_timestamp_ms(),
391            protocol_version: ProtocolVersion::MAX,
392            allow_insertion_of_extra_objects: true,
393            epoch_duration_ms: Self::default_epoch_duration_ms(),
394        }
395    }
396
397    fn default_timestamp_ms() -> u64 {
398        std::time::SystemTime::now()
399            .duration_since(std::time::UNIX_EPOCH)
400            .unwrap()
401            .as_millis() as u64
402    }
403
404    fn default_allow_insertion_of_extra_objects() -> bool {
405        true
406    }
407
408    fn default_epoch_duration_ms() -> u64 {
409        // 24 hrs
410        24 * 60 * 60 * 1000
411    }
412
413    pub fn to_genesis_chain_parameters(&self) -> GenesisChainParameters {
414        GenesisChainParameters {
415            protocol_version: self.protocol_version.as_u64(),
416            chain_start_timestamp_ms: self.chain_start_timestamp_ms,
417            epoch_duration_ms: self.epoch_duration_ms,
418            max_validator_count: iota_types::governance::MAX_VALIDATOR_COUNT,
419            min_validator_joining_stake: iota_types::governance::MIN_VALIDATOR_JOINING_STAKE_NANOS,
420            validator_low_stake_threshold:
421                iota_types::governance::VALIDATOR_LOW_STAKE_THRESHOLD_NANOS,
422            validator_very_low_stake_threshold:
423                iota_types::governance::VALIDATOR_VERY_LOW_STAKE_THRESHOLD_NANOS,
424            validator_low_stake_grace_period:
425                iota_types::governance::VALIDATOR_LOW_STAKE_GRACE_PERIOD,
426        }
427    }
428}
429
430impl Default for GenesisCeremonyParameters {
431    fn default() -> Self {
432        Self::new()
433    }
434}
435
436#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
437#[serde(rename_all = "kebab-case")]
438pub struct TokenDistributionSchedule {
439    pub pre_minted_supply: u64,
440    pub allocations: Vec<TokenAllocation>,
441}
442
443impl TokenDistributionSchedule {
444    pub fn contains_timelocked_stake(&self) -> bool {
445        self.allocations
446            .iter()
447            .find_map(|allocation| allocation.staked_with_timelock_expiration)
448            .is_some()
449    }
450
451    pub fn validate(&self) {
452        let mut total_nanos = self.pre_minted_supply;
453
454        for allocation in &self.allocations {
455            total_nanos = total_nanos
456                .checked_add(allocation.amount_nanos)
457                .expect("TokenDistributionSchedule allocates more than the maximum supply which equals u64::MAX");
458        }
459    }
460
461    pub fn check_minimum_stake_for_validators<I: IntoIterator<Item = IotaAddress>>(
462        &self,
463        validators: I,
464    ) -> Result<()> {
465        let mut validators: HashMap<IotaAddress, u64> =
466            validators.into_iter().map(|a| (a, 0)).collect();
467
468        // Check that all allocations are for valid validators, while summing up all
469        // allocations for each validator
470        for allocation in &self.allocations {
471            if let Some(staked_with_validator) = &allocation.staked_with_validator {
472                *validators
473                    .get_mut(staked_with_validator)
474                    .expect("allocation must be staked with valid validator") +=
475                    allocation.amount_nanos;
476            }
477        }
478
479        // Check that all validators have sufficient stake allocated to ensure they meet
480        // the minimum stake threshold
481        let minimum_required_stake = iota_types::governance::VALIDATOR_LOW_STAKE_THRESHOLD_NANOS;
482        for (validator, stake) in validators {
483            if stake < minimum_required_stake {
484                anyhow::bail!(
485                    "validator {validator} has '{stake}' stake and does not meet the minimum required stake threshold of '{minimum_required_stake}'"
486                );
487            }
488        }
489        Ok(())
490    }
491
492    pub fn new_for_validators_with_default_allocation<I: IntoIterator<Item = IotaAddress>>(
493        validators: I,
494    ) -> Self {
495        let default_allocation = iota_types::governance::VALIDATOR_LOW_STAKE_THRESHOLD_NANOS;
496
497        let allocations = validators
498            .into_iter()
499            .map(|a| TokenAllocation {
500                recipient_address: a,
501                amount_nanos: default_allocation,
502                staked_with_validator: Some(a),
503                staked_with_timelock_expiration: None,
504            })
505            .collect();
506
507        let schedule = Self {
508            pre_minted_supply: 0,
509            allocations,
510        };
511
512        schedule.validate();
513        schedule
514    }
515
516    /// Helper to read a TokenDistributionSchedule from a csv file.
517    ///
518    /// The file is encoded such that the final entry in the CSV file is used to
519    /// denote the allocation to the stake subsidy fund.
520    ///
521    /// Comments are optional, and start with a `#` character.
522    /// Only entries that start with this character are treated as comments.
523    pub fn from_csv<R: std::io::Read>(reader: R) -> Result<Self> {
524        let mut reader = csv_reader_with_comments(reader);
525        let mut allocations: Vec<TokenAllocation> =
526            reader.deserialize().collect::<Result<_, _>>()?;
527
528        let pre_minted_supply = allocations.pop().unwrap();
529        assert_eq!(
530            IotaAddress::default(),
531            pre_minted_supply.recipient_address,
532            "final allocation must be for the pre-minted supply amount",
533        );
534        assert!(
535            pre_minted_supply.staked_with_validator.is_none(),
536            "cannot stake the pre-minted supply amount",
537        );
538
539        let schedule = Self {
540            pre_minted_supply: pre_minted_supply.amount_nanos,
541            allocations,
542        };
543
544        schedule.validate();
545        Ok(schedule)
546    }
547
548    pub fn to_csv<W: std::io::Write>(&self, writer: W) -> Result<()> {
549        let mut writer = csv::Writer::from_writer(writer);
550
551        for allocation in &self.allocations {
552            writer.serialize(allocation)?;
553        }
554
555        writer.serialize(TokenAllocation {
556            recipient_address: IotaAddress::default(),
557            amount_nanos: self.pre_minted_supply,
558            staked_with_validator: None,
559            staked_with_timelock_expiration: None,
560        })?;
561
562        Ok(())
563    }
564}
565
566#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
567#[serde(rename_all = "kebab-case")]
568pub struct TokenAllocation {
569    /// Indicates the address that owns the tokens. It means that this
570    /// `TokenAllocation` can serve to stake some funds to the
571    /// `staked_with_validator` during genesis, but it's the `recipient_address`
572    /// which will receive the associated StakedIota (or TimelockedStakedIota)
573    /// object.
574    pub recipient_address: IotaAddress,
575    /// Indicates an amount of nanos that is:
576    /// - minted for the `recipient_address` and staked to a validator, only in
577    ///   the case `staked_with_validator` is Some
578    /// - minted for the `recipient_address` and transferred that address,
579    ///   otherwise.
580    pub amount_nanos: u64,
581
582    /// Indicates if this allocation should be staked at genesis and with which
583    /// validator
584    pub staked_with_validator: Option<IotaAddress>,
585    /// Indicates if this allocation should be staked with timelock at genesis
586    /// and contains its timelock_expiration
587    pub staked_with_timelock_expiration: Option<u64>,
588}
589
590#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
591pub struct TokenDistributionScheduleBuilder {
592    pre_minted_supply: u64,
593    allocations: Vec<TokenAllocation>,
594}
595
596impl TokenDistributionScheduleBuilder {
597    #[expect(clippy::new_without_default)]
598    pub fn new() -> Self {
599        Self {
600            pre_minted_supply: 0,
601            allocations: vec![],
602        }
603    }
604
605    pub fn set_pre_minted_supply(&mut self, pre_minted_supply: u64) {
606        self.pre_minted_supply = pre_minted_supply;
607    }
608
609    pub fn default_allocation_for_validators<I: IntoIterator<Item = IotaAddress>>(
610        &mut self,
611        validators: I,
612    ) {
613        let default_allocation = iota_types::governance::VALIDATOR_LOW_STAKE_THRESHOLD_NANOS;
614
615        for validator in validators {
616            self.add_allocation(TokenAllocation {
617                recipient_address: validator,
618                amount_nanos: default_allocation,
619                staked_with_validator: Some(validator),
620                staked_with_timelock_expiration: None,
621            });
622        }
623    }
624
625    pub fn add_allocation(&mut self, allocation: TokenAllocation) {
626        self.allocations.push(allocation);
627    }
628
629    pub fn build(&self) -> TokenDistributionSchedule {
630        let schedule = TokenDistributionSchedule {
631            pre_minted_supply: self.pre_minted_supply,
632            allocations: self.allocations.clone(),
633        };
634
635        schedule.validate();
636        schedule
637    }
638}
639
640/// Represents the allocation of stake and gas payment to a validator.
641#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
642#[serde(rename_all = "kebab-case")]
643pub struct ValidatorAllocation {
644    /// The validator address receiving the stake and/or gas payment
645    pub validator: IotaAddress,
646    /// The amount of nanos to stake to the validator
647    pub amount_nanos_to_stake: u64,
648    /// The amount of nanos to transfer as gas payment to the validator
649    pub amount_nanos_to_pay_gas: u64,
650}
651
652/// Represents a delegation of stake and gas payment to a validator,
653/// coming from a delegator. This struct is used to serialize and deserialize
654/// delegations to and from a csv file.
655#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
656#[serde(rename_all = "kebab-case")]
657pub struct Delegation {
658    /// The address from which to take the nanos for staking/gas
659    pub delegator: IotaAddress,
660    /// The allocation to a validator receiving a stake and/or a gas payment
661    #[serde(flatten)]
662    pub validator_allocation: ValidatorAllocation,
663}
664
665/// Represents genesis delegations to validators.
666///
667/// This struct maps a delegator address to a list of validators and their
668/// stake and gas allocations. Each ValidatorAllocation contains the address of
669/// a validator that will receive an amount of nanos to stake and an amount as
670/// gas payment.
671#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
672#[serde(rename_all = "kebab-case")]
673pub struct Delegations {
674    pub allocations: BTreeMap<IotaAddress, Vec<ValidatorAllocation>>,
675}
676
677impl Delegations {
678    pub fn new_for_validators_with_default_allocation(
679        validators: impl IntoIterator<Item = IotaAddress>,
680        delegator: IotaAddress,
681    ) -> Self {
682        let validator_allocations = validators
683            .into_iter()
684            .map(|address| ValidatorAllocation {
685                validator: address,
686                amount_nanos_to_stake: iota_types::governance::MIN_VALIDATOR_JOINING_STAKE_NANOS,
687                amount_nanos_to_pay_gas: 0,
688            })
689            .collect();
690
691        let mut allocations = BTreeMap::new();
692        allocations.insert(delegator, validator_allocations);
693
694        Self { allocations }
695    }
696
697    /// Helper to read a Delegations struct from a csv file.
698    ///
699    /// The file is encoded such that the final entry in the CSV file is used to
700    /// denote the allocation coming from a delegator. It must be in the
701    /// following format:
702    /// `delegator,validator,amount-nanos-to-stake,amount-nanos-to-pay-gas
703    /// <delegator1-address>,<validator-1-address>,2000000000000000,5000000000
704    /// <delegator1-address>,<validator-2-address>,3000000000000000,5000000000
705    /// <delegator2-address>,<validator-3-address>,4500000000000000,5000000000`
706    ///
707    /// Comments are optional, and start with a `#` character.
708    /// Only entries that start with this character are treated as comments.
709    pub fn from_csv<R: std::io::Read>(reader: R) -> Result<Self> {
710        let mut reader = csv_reader_with_comments(reader);
711
712        let mut delegations = Self::default();
713        for delegation in reader.deserialize::<Delegation>() {
714            let delegation = delegation?;
715            delegations
716                .allocations
717                .entry(delegation.delegator)
718                .or_default()
719                .push(delegation.validator_allocation);
720        }
721
722        Ok(delegations)
723    }
724
725    /// Helper to write a Delegations struct into a csv file.
726    ///
727    /// It writes in the following format:
728    /// `delegator,validator,amount-nanos-to-stake,amount-nanos-to-pay-gas
729    /// <delegator1-address>,<validator-1-address>,2000000000000000,5000000000
730    /// <delegator1-address>,<validator-2-address>,3000000000000000,5000000000
731    /// <delegator2-address>,<validator-3-address>,4500000000000000,5000000000`
732    pub fn to_csv<W: std::io::Write>(&self, writer: W) -> Result<()> {
733        let mut writer = csv::Writer::from_writer(writer);
734
735        writer.write_record([
736            "delegator",
737            "validator",
738            "amount-nanos-to-stake",
739            "amount-nanos-to-pay-gas",
740        ])?;
741
742        for (&delegator, validator_allocations) in &self.allocations {
743            for validator_allocation in validator_allocations {
744                writer.write_record(&[
745                    delegator.to_string(),
746                    validator_allocation.validator.to_string(),
747                    validator_allocation.amount_nanos_to_stake.to_string(),
748                    validator_allocation.amount_nanos_to_pay_gas.to_string(),
749                ])?;
750            }
751        }
752
753        Ok(())
754    }
755}
756
757/// Helper function to create a CSV reader with custom settings.
758/// In this case, it sets the comment character to `#`.
759pub fn csv_reader_with_comments<R: std::io::Read>(reader: R) -> csv::Reader<R> {
760    csv::ReaderBuilder::new()
761        .comment(Some(b'#'))
762        .from_reader(reader)
763}