snarkos-aot 0.1.0

Ahead of time utilities for SnarkVM, and a wrapper around the SnarkOS node for more options
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use std::{fs, path::PathBuf, str::FromStr};

use aleo_std::StorageMode;
use anyhow::{anyhow, ensure, Result};
use clap::Parser;
use colored::Colorize;
use indexmap::IndexMap;
use rand::{CryptoRng, Rng, SeedableRng};
use rand_chacha::ChaChaRng;
use serde::{de::DeserializeOwned, Serialize};
use snarkvm::{
    ledger::{
        committee::MIN_VALIDATOR_STAKE,
        store::{helpers::memory::ConsensusMemory, ConsensusStore},
        Header, Ratify, Solutions,
    },
    synthesizer::program::FinalizeGlobalState,
    utilities::ToBytes,
};

use crate::{
    ledger::util::public_transaction, Address, Block, CTRecord, Committee, DbLedger, MemVM,
    Network, NetworkId, PTRecord, PrivateKey, Transaction, ViewKey,
};

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct AddressMap<N: Network, T: DeserializeOwned>(IndexMap<Address<N>, T>);
impl<N: Network, T: DeserializeOwned> FromStr for AddressMap<N, T> {
    type Err = serde_json::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(AddressMap(serde_json::from_str(s)?))
    }
}

/// This command helps generate a custom genesis block given an initial private
/// key, seed, and committee size.
#[derive(Debug, Clone, Parser)]
pub struct Genesis<N: Network> {
    /// The private key to use when generating the genesis block. Generates one
    /// randomly if not passed.
    #[clap(short, long)]
    pub genesis_key: Option<PrivateKey<N>>,

    /// Where to write the genesis block to.
    #[clap(short, long, default_value = "genesis.block")]
    pub output: PathBuf,

    /// The committee size. Not used if --bonded-balances is set.
    #[clap(long, default_value_t = 4)]
    pub committee_size: u16,

    /// A place to optionally write out the generated committee private keys
    /// JSON.
    #[clap(long)]
    pub committee_output: Option<PathBuf>,

    /// Additional number of accounts that aren't validators to add balances to.
    #[clap(long, default_value_t = 0)]
    pub additional_accounts: u16,

    /// The balance to add to the number of accounts specified by
    /// additional-accounts.
    #[clap(
        name = "additional-accounts-balance",
        long,
        default_value_t = 100_000_000
    )]
    pub additional_accounts_balance: u64,

    /// If --additional-accounts is passed you can additionally add an amount to
    /// give them in a record.
    #[clap(long)]
    pub additional_accounts_record_balance: Option<u64>,

    /// A place to write out the additionally generated accounts by
    /// --additional-accounts.
    #[clap(long)]
    pub additional_accounts_output: Option<PathBuf>,

    /// The seed to use when generating committee private keys and the genesis
    /// block. If unpassed, uses DEVELOPMENT_MODE_RNG_SEED (1234567890u64).
    #[clap(long)]
    pub seed: Option<u64>,

    /// The bonded balance each bonded address receives. Not used if
    /// `--bonded-balances` is passed.
    #[clap(long, default_value_t = 10_000_000_000_000)]
    pub bonded_balance: u64,

    /// An optional map from address to bonded balance. Overrides
    /// `--bonded-balance` and `--committee-size`.
    #[clap(long)]
    pub bonded_balances: Option<AddressMap<N, u64>>,

    /// An optional to specify withdrawal addresses for the genesis committee.
    #[clap(long)]
    pub bonded_withdrawal: Option<AddressMap<N, Address<N>>>,

    /// The bonded commission each bonded address uses. Not used if
    /// `--bonded-commissions` is passed. Defaults to 0. Must be 100 or less.
    #[clap(long, default_value_t = 0)]
    pub bonded_commission: u8,

    /// An optional map from address to bonded commission. Overrides
    /// `--bonded-commission`.
    /// Defaults to 0. Must be 100 or less.
    #[clap(long)]
    pub bonded_commissions: Option<AddressMap<N, u8>>,

    /// Optionally initialize a ledger as well.
    #[clap(long)]
    pub ledger: Option<PathBuf>,
}

/// Returns a new genesis block for a quorum chain.
pub fn genesis_quorum<R: Rng + CryptoRng, N: Network>(
    vm: &MemVM<N>,
    private_key: &PrivateKey<N>,
    committee: Committee<N>,
    public_balances: IndexMap<Address<N>, u64>,
    bonded_balances: IndexMap<Address<N>, (Address<N>, Address<N>, u64)>,
    transactions: Vec<Transaction<N>>,
    rng: &mut R,
) -> Result<Block<N>> {
    // Retrieve the total stake.
    let total_stake = committee.total_stake();
    // Compute the account supply.
    let account_supply = public_balances.values().try_fold(0u64, |acc, x| {
        acc.checked_add(*x).ok_or(anyhow!("Invalid account supply"))
    })?;
    // Compute the total supply.
    let total_supply = total_stake
        .checked_add(account_supply)
        .ok_or_else(|| anyhow!("Invalid total supply"))?;
    // Ensure the total supply matches.
    ensure!(
        total_supply == N::STARTING_SUPPLY,
        "Invalid total supply. Found {total_supply}, expected {}",
        N::STARTING_SUPPLY
    );

    // Prepare the ratifications.
    let ratifications = vec![Ratify::Genesis(
        Box::new(committee),
        Box::new(public_balances),
        Box::new(bonded_balances),
    )];
    // Prepare the solutions.
    let solutions = Solutions::from(None);
    // The genesis block
    // Prepare the aborted solution IDs.
    let aborted_solution_ids = vec![];

    // Construct the finalize state.
    let state = FinalizeGlobalState::new_genesis::<N>()?;
    // Speculate on the ratifications, solutions, and transactions.
    let (ratifications, transactions, aborted_transaction_ids, ratified_finalize_operations) = vm
        .speculate(
        state,
        None,
        ratifications,
        &solutions,
        transactions.iter(),
        rng,
    )?;
    ensure!(
        aborted_transaction_ids.is_empty(),
        "Failed to initialize a genesis block - found aborted transactionIDs"
    );

    // Prepare the block header.
    let header = Header::genesis(&ratifications, &transactions, ratified_finalize_operations)?; // Prepare the previous block hash.
    let previous_hash = N::BlockHash::default();

    // Construct the block.
    let block = Block::new_beacon(
        private_key,
        previous_hash,
        header,
        ratifications,
        solutions,
        aborted_solution_ids,
        transactions,
        aborted_transaction_ids,
        rng,
    )?;

    Ok(block)
}

impl<N: Network> Genesis<N> {
    pub fn parse(self) -> Result<()> {
        let mut rng = ChaChaRng::seed_from_u64(self.seed.unwrap_or(1234567890u64));

        tracing::trace!(
            "Generating genesis block for network {}",
            NetworkId::from_network::<N>()
        );

        // Generate a genesis key if one was not passed.
        let genesis_key = match self.genesis_key {
            Some(genesis_key) => genesis_key,
            None => PrivateKey::new(&mut rng)?,
        };

        let genesis_addr = Address::try_from(&genesis_key)?;

        // Lookup the commission for a given address.
        let get_commission = |addr| {
            self.bonded_commissions
                .as_ref()
                .and_then(|commissions| commissions.0.get(&addr))
                .copied()
                .unwrap_or(self.bonded_commission)
                .clamp(0, 100)
        };

        // Lookup the withdrawal address for a given address.
        let get_withdrawal = |addr| {
            self.bonded_withdrawal
                .as_ref()
                .and_then(|withdrawals| withdrawals.0.get(&addr))
                .copied()
                .unwrap_or(addr)
        };

        let (mut committee_members, bonded_balances, members, mut public_balances) = match self
            .bonded_balances
        {
            Some(balances) => {
                ensure!(
                    balances.0.contains_key(&genesis_addr),
                    "The genesis address should be present in the passed-in bonded balances."
                );

                let mut bonded_balances = IndexMap::with_capacity(self.committee_size as usize);
                let mut members = IndexMap::with_capacity(self.committee_size as usize);

                for (addr, balance) in &balances.0 {
                    ensure!(
                        balance >= &MIN_VALIDATOR_STAKE,
                        "Validator stake is too low: {balance} < {MIN_VALIDATOR_STAKE}",
                    );

                    bonded_balances.insert(*addr, (*addr, get_withdrawal(*addr), *balance));
                    members.insert(*addr, (*balance, true, get_commission(*addr)));
                }

                (None, bonded_balances, members, balances.0)
            }

            None => {
                ensure!(
                    self.bonded_balance >= MIN_VALIDATOR_STAKE,
                    "Validator stake is too low: {} < {MIN_VALIDATOR_STAKE}",
                    self.bonded_balance
                );

                let mut committee_members = IndexMap::with_capacity(self.committee_size as usize);
                let mut bonded_balances = IndexMap::with_capacity(self.committee_size as usize);
                let mut members = IndexMap::with_capacity(self.committee_size as usize);
                let mut public_balances = IndexMap::with_capacity(self.committee_size as usize);

                for i in 0..self.committee_size {
                    let (key, addr) = match i {
                        0 => (genesis_key, genesis_addr),
                        _ => {
                            let key = PrivateKey::new(&mut rng)?;
                            let addr = Address::try_from(&key)?;

                            (key, addr)
                        }
                    };

                    committee_members.insert(addr, (key, self.bonded_balance));
                    bonded_balances.insert(addr, (addr, get_withdrawal(addr), self.bonded_balance));
                    members.insert(addr, (self.bonded_balance, true, get_commission(addr)));
                    public_balances.insert(addr, self.bonded_balance);
                }

                (
                    Some(committee_members),
                    bonded_balances,
                    members,
                    public_balances,
                )
            }
        };

        // Construct the committee.
        let committee = Committee::new(0u64, members)?;

        // Add additional accounts to the public balances
        type Accounts<N> = IndexMap<Address<N>, (PrivateKey<N>, u64, Option<PTRecord<N>>)>;
        let mut accounts: Accounts<N> = (0..self.additional_accounts)
            .map(|_| {
                // Repeatedly regenerate key/addresses, ensuring they are not in
                // `bonded_balances`.
                let (key, addr) = loop {
                    let key = PrivateKey::new(&mut rng)?;
                    let addr = Address::try_from(&key)?;
                    if !bonded_balances.contains_key(&addr) {
                        break (key, addr);
                    }
                };

                let balance = self.additional_accounts_balance
                    + self.additional_accounts_record_balance.unwrap_or(0);

                public_balances.insert(addr, balance);
                Ok((addr, (key, balance, None)))
            })
            .collect::<Result<IndexMap<_, _>>>()?;

        // Calculate the public balance per validator.
        let remaining_balance = N::STARTING_SUPPLY
            .saturating_sub(committee.total_stake())
            .saturating_sub(public_balances.values().sum());

        if remaining_balance > 0 {
            let balance = public_balances.get_mut(&genesis_addr).unwrap();
            *balance += remaining_balance;

            if let Some(ref mut committee_members) = committee_members {
                let (_, balance) = committee_members.get_mut(&genesis_addr).unwrap();
                *balance += remaining_balance;
            }
        }

        // Check if the sum of committee stakes and public balances equals the total
        // starting supply.
        let public_balances_sum: u64 = public_balances.values().sum();
        if committee.total_stake() + public_balances_sum != N::STARTING_SUPPLY {
            println!(
                "Sum of committee stakes and public balances does not equal total starting supply:
                                {} + {public_balances_sum} != {}",
                committee.total_stake(),
                N::STARTING_SUPPLY
            );
        }

        // Construct the genesis block.
        let compute_span = tracing::span!(tracing::Level::ERROR, "compute span").entered();

        // Initialize a new VM.
        let vm = snarkvm::synthesizer::VM::from(ConsensusStore::<N, ConsensusMemory<_>>::open(
            Some(0),
        )?)?;

        // region: Genesis Records
        let mut txs = Vec::with_capacity(accounts.len());
        if let Some(record_balance) = self.additional_accounts_record_balance {
            accounts = accounts
                .into_iter()
                .map(|(addr, (key, balance, _))| {
                    let record_tx: Transaction<N> =
                        public_transaction::<N, ConsensusMemory<_>, N::Circuit>(
                            "transfer_public_to_private",
                            &vm,
                            addr,
                            record_balance,
                            key,
                            None,
                        )?;
                    // Cannot fail because transfer_public_to_private always emits a
                    // record.
                    let record_enc: CTRecord<N> = record_tx.records().next().unwrap().1.clone();
                    // Decrypt the record.
                    let record = record_enc.decrypt(&ViewKey::try_from(key)?)?;

                    txs.push(record_tx);
                    Ok((addr, (key, balance, Some(record))))
                })
                .collect::<Result<_>>()?;
        }

        // endregion: Genesis Records

        // Initialize the genesis block.
        let block = genesis_quorum(
            &vm,
            &genesis_key,
            committee,
            public_balances,
            bonded_balances,
            txs,
            &mut rng,
        )?;

        compute_span.exit();

        println!();

        // Write the genesis block.
        block.write_le(
            fs::File::options()
                .append(false)
                .truncate(true)
                .create(true)
                .write(true)
                .open(&self.output)?,
        )?;

        // Print the genesis block private key if we generated one.
        if self.genesis_key.is_none() {
            println!(
                "The genesis block private key is: {}",
                genesis_key.to_string().cyan()
            );
        }

        // Print some info about the new genesis block.
        println!(
            "Genesis block written to {}.",
            self.output.display().to_string().yellow()
        );

        let output_json = self.output.with_extension("json");
        serde_json::to_writer_pretty(
            fs::File::options()
                .append(false)
                .truncate(true)
                .create(true)
                .write(true)
                .open(&output_json)?,
            &block,
        )?;

        println!(
            "Genesis block JSON written to {}.",
            output_json.display().to_string().yellow()
        );

        match (self.additional_accounts, self.additional_accounts_output) {
            // Don't display anything if we didn't make any additional accounts.
            (0, _) => (),

            // Write the accounts JSON file.
            (_, Some(accounts_file)) => {
                let file = fs::File::options()
                    .append(false)
                    .create(true)
                    .truncate(true)
                    .write(true)
                    .open(&accounts_file)?;
                serde_json::to_writer_pretty(file, &accounts)?;

                println!(
                    "Additional accounts written to {}.",
                    accounts_file.display().to_string().yellow()
                );
            }

            // Write the accounts to stdout if no file was passed.
            (_, None) => {
                println!(
                    "Additional accounts (each given {} balance):",
                    self.additional_accounts_balance
                );
                for (addr, (key, _, _)) in accounts {
                    println!(
                        "\t{}: {}",
                        addr.to_string().yellow(),
                        key.to_string().cyan()
                    );
                }
            }
        }

        // Display committee information if we generated it.
        match (committee_members, self.committee_output) {
            // file was passed
            (Some(committee_members), Some(committee_file)) => {
                let file = fs::File::options()
                    .append(false)
                    .create(true)
                    .truncate(true)
                    .write(true)
                    .open(&committee_file)?;
                serde_json::to_writer_pretty(file, &committee_members)?;

                println!(
                    "Generated committee written to {}.",
                    committee_file.display().to_string().yellow()
                );
            }

            // file was not passed
            (Some(committee_members), None) => {
                println!("Generated committee:");
                for (addr, (key, _)) in committee_members {
                    println!(
                        "\t{}: {}",
                        addr.to_string().yellow(),
                        key.to_string().cyan()
                    );
                }
            }

            _ => (),
        }

        // Initialize the ledger if a path was given.
        if let Some(ledger) = self.ledger {
            DbLedger::load(block.to_owned(), StorageMode::Custom(ledger.to_owned()))?;
            println!(
                "Initialized a ledger at {}.",
                ledger.display().to_string().yellow()
            );
        }

        println!();
        println!("Genesis block hash: {}", block.hash().to_string().yellow());

        Ok(())
    }
}