snarkvm_ledger_store/transition/
mod.rs

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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// Copyright 2024 Aleo Network Foundation
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod input;
pub use input::*;

mod output;
pub use output::*;

use crate::{
    atomic_batch_scope,
    cow_to_cloned,
    cow_to_copied,
    helpers::{Map, MapRead},
};
use console::{
    network::prelude::*,
    program::{Ciphertext, Identifier, Plaintext, ProgramID, Record},
    types::{Field, Group},
};
use ledger_block::{Input, Output, Transition};

use aleo_std_storage::StorageMode;
use anyhow::Result;
use std::borrow::Cow;

/// A trait for transition storage.
pub trait TransitionStorage<N: Network>: Clone + Send + Sync {
    /// The transition program IDs and function names.
    type LocatorMap: for<'a> Map<'a, N::TransitionID, (ProgramID<N>, Identifier<N>)>;
    /// The transition inputs.
    type InputStorage: InputStorage<N>;
    /// The transition outputs.
    type OutputStorage: OutputStorage<N>;
    /// The transition public keys.
    type TPKMap: for<'a> Map<'a, N::TransitionID, Group<N>>;
    /// The mapping of `transition public key` to `transition ID`.
    type ReverseTPKMap: for<'a> Map<'a, Group<N>, N::TransitionID>;
    /// The transition commitments.
    type TCMMap: for<'a> Map<'a, N::TransitionID, Field<N>>;
    /// The mapping of `transition commitment` to `transition ID`.
    type ReverseTCMMap: for<'a> Map<'a, Field<N>, N::TransitionID>;
    /// The signer commitments.
    type SCMMap: for<'a> Map<'a, N::TransitionID, Field<N>>;

    /// Initializes the transition storage.
    fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self>;

    /// Returns the transition program IDs and function names.
    fn locator_map(&self) -> &Self::LocatorMap;
    /// Returns the transition input store.
    fn input_store(&self) -> &InputStore<N, Self::InputStorage>;
    /// Returns the transition output store.
    fn output_store(&self) -> &OutputStore<N, Self::OutputStorage>;
    /// Returns the transition public keys map.
    fn tpk_map(&self) -> &Self::TPKMap;
    /// Returns the reverse `tpk` map.
    fn reverse_tpk_map(&self) -> &Self::ReverseTPKMap;
    /// Returns the transition commitments map.
    fn tcm_map(&self) -> &Self::TCMMap;
    /// Returns the reverse `tcm` map.
    fn reverse_tcm_map(&self) -> &Self::ReverseTCMMap;
    /// Returns the signer commitments map.
    fn scm_map(&self) -> &Self::SCMMap;

    /// Returns the storage mode.
    fn storage_mode(&self) -> &StorageMode {
        debug_assert!(self.input_store().storage_mode() == self.output_store().storage_mode());
        self.input_store().storage_mode()
    }

    /// Starts an atomic batch write operation.
    fn start_atomic(&self) {
        self.locator_map().start_atomic();
        self.input_store().start_atomic();
        self.output_store().start_atomic();
        self.tpk_map().start_atomic();
        self.reverse_tpk_map().start_atomic();
        self.tcm_map().start_atomic();
        self.reverse_tcm_map().start_atomic();
        self.scm_map().start_atomic();
    }

    /// Checks if an atomic batch is in progress.
    fn is_atomic_in_progress(&self) -> bool {
        self.locator_map().is_atomic_in_progress()
            || self.input_store().is_atomic_in_progress()
            || self.output_store().is_atomic_in_progress()
            || self.tpk_map().is_atomic_in_progress()
            || self.reverse_tpk_map().is_atomic_in_progress()
            || self.tcm_map().is_atomic_in_progress()
            || self.reverse_tcm_map().is_atomic_in_progress()
            || self.scm_map().is_atomic_in_progress()
    }

    /// Checkpoints the atomic batch.
    fn atomic_checkpoint(&self) {
        self.locator_map().atomic_checkpoint();
        self.input_store().atomic_checkpoint();
        self.output_store().atomic_checkpoint();
        self.tpk_map().atomic_checkpoint();
        self.reverse_tpk_map().atomic_checkpoint();
        self.tcm_map().atomic_checkpoint();
        self.reverse_tcm_map().atomic_checkpoint();
        self.scm_map().atomic_checkpoint();
    }

    /// Clears the latest atomic batch checkpoint.
    fn clear_latest_checkpoint(&self) {
        self.locator_map().clear_latest_checkpoint();
        self.input_store().clear_latest_checkpoint();
        self.output_store().clear_latest_checkpoint();
        self.tpk_map().clear_latest_checkpoint();
        self.reverse_tpk_map().clear_latest_checkpoint();
        self.tcm_map().clear_latest_checkpoint();
        self.reverse_tcm_map().clear_latest_checkpoint();
        self.scm_map().clear_latest_checkpoint();
    }

    /// Rewinds the atomic batch to the previous checkpoint.
    fn atomic_rewind(&self) {
        self.locator_map().atomic_rewind();
        self.input_store().atomic_rewind();
        self.output_store().atomic_rewind();
        self.tpk_map().atomic_rewind();
        self.reverse_tpk_map().atomic_rewind();
        self.tcm_map().atomic_rewind();
        self.reverse_tcm_map().atomic_rewind();
        self.scm_map().atomic_rewind();
    }

    /// Aborts an atomic batch write operation.
    fn abort_atomic(&self) {
        self.locator_map().abort_atomic();
        self.input_store().abort_atomic();
        self.output_store().abort_atomic();
        self.tpk_map().abort_atomic();
        self.reverse_tpk_map().abort_atomic();
        self.tcm_map().abort_atomic();
        self.reverse_tcm_map().abort_atomic();
        self.scm_map().abort_atomic();
    }

    /// Finishes an atomic batch write operation.
    fn finish_atomic(&self) -> Result<()> {
        self.locator_map().finish_atomic()?;
        self.input_store().finish_atomic()?;
        self.output_store().finish_atomic()?;
        self.tpk_map().finish_atomic()?;
        self.reverse_tpk_map().finish_atomic()?;
        self.tcm_map().finish_atomic()?;
        self.reverse_tcm_map().finish_atomic()?;
        self.scm_map().finish_atomic()
    }

    /// Stores the given `transition` into storage.
    fn insert(&self, transition: &Transition<N>) -> Result<()> {
        atomic_batch_scope!(self, {
            // Retrieve the transition ID.
            let transition_id = *transition.id();
            // Store the program ID and function name.
            self.locator_map().insert(transition_id, (*transition.program_id(), *transition.function_name()))?;
            // Store the inputs.
            self.input_store().insert(transition_id, transition.inputs())?;
            // Store the outputs.
            self.output_store().insert(transition_id, transition.outputs())?;
            // Store `tpk`.
            self.tpk_map().insert(transition_id, *transition.tpk())?;
            // Store the reverse `tpk` entry.
            self.reverse_tpk_map().insert(*transition.tpk(), transition_id)?;
            // Store `tcm`.
            self.tcm_map().insert(transition_id, *transition.tcm())?;
            // Store the reverse `tcm` entry.
            self.reverse_tcm_map().insert(*transition.tcm(), transition_id)?;
            // Store `scm`.
            self.scm_map().insert(transition_id, *transition.scm())?;

            Ok(())
        })
    }

    /// Removes the input for the given `transition ID`.
    fn remove(&self, transition_id: &N::TransitionID) -> Result<()> {
        // Retrieve the `tpk`.
        let tpk = match self.tpk_map().get_confirmed(transition_id)? {
            Some(tpk) => cow_to_copied!(tpk),
            None => return Ok(()),
        };
        // Retrieve the `tcm`.
        let tcm = match self.tcm_map().get_confirmed(transition_id)? {
            Some(tcm) => cow_to_copied!(tcm),
            None => return Ok(()),
        };

        atomic_batch_scope!(self, {
            // Remove the program ID and function name.
            self.locator_map().remove(transition_id)?;
            // Remove the inputs.
            self.input_store().remove(transition_id)?;
            // Remove the outputs.
            self.output_store().remove(transition_id)?;
            // Remove `tpk`.
            self.tpk_map().remove(transition_id)?;
            // Remove the reverse `tpk` entry.
            self.reverse_tpk_map().remove(&tpk)?;
            // Remove `tcm`.
            self.tcm_map().remove(transition_id)?;
            // Remove the reverse `tcm` entry.
            self.reverse_tcm_map().remove(&tcm)?;
            // Remove `scm`.
            self.scm_map().remove(transition_id)?;

            Ok(())
        })
    }

    /// Returns the transition for the given `transition ID`.
    fn get(&self, transition_id: &N::TransitionID) -> Result<Option<Transition<N>>> {
        // Retrieve the program ID and function name.
        let (program_id, function_name) = match self.locator_map().get_confirmed(transition_id)? {
            Some(locator) => cow_to_cloned!(locator),
            None => return Ok(None),
        };
        // Retrieve the inputs.
        let inputs = self.input_store().get_inputs(transition_id)?;
        // Retrieve the outputs.
        let outputs = self.output_store().get_outputs(transition_id)?;
        // Retrieve `tpk`.
        let tpk = self.tpk_map().get_confirmed(transition_id)?;
        // Retrieve `tcm`.
        let tcm = self.tcm_map().get_confirmed(transition_id)?;
        // Retrieve `scm`.
        let scm = self.scm_map().get_confirmed(transition_id)?;

        match (tpk, tcm, scm) {
            (Some(tpk), Some(tcm), Some(scm)) => {
                // Construct the transition.
                let transition = Transition::new(
                    program_id,
                    function_name,
                    inputs,
                    outputs,
                    cow_to_cloned!(tpk),
                    cow_to_cloned!(tcm),
                    cow_to_cloned!(scm),
                )?;
                // Ensure the transition ID matches.
                match transition.id() == transition_id {
                    true => Ok(Some(transition)),
                    false => bail!("Mismatch in the transition ID '{transition_id}'"),
                }
            }
            _ => bail!("Transition '{transition_id}' is missing some data (possible corruption)"),
        }
    }
}

/// The transition store.
#[derive(Clone)]
pub struct TransitionStore<N: Network, T: TransitionStorage<N>> {
    /// The map of transition program IDs and function names.
    locator: T::LocatorMap,
    /// The map of transition inputs.
    inputs: InputStore<N, T::InputStorage>,
    /// The map of transition outputs.
    outputs: OutputStore<N, T::OutputStorage>,
    /// The map of transition public keys.
    tpk: T::TPKMap,
    /// The reverse `tpk` map.
    reverse_tpk: T::ReverseTPKMap,
    /// The map of transition commitments.
    tcm: T::TCMMap,
    /// The reverse `tcm` map.
    reverse_tcm: T::ReverseTCMMap,
    /// The map of signer commitments.
    scm: T::SCMMap,
    /// The transition storage.
    storage: T,
}

impl<N: Network, T: TransitionStorage<N>> TransitionStore<N, T> {
    /// Initializes the transition store.
    pub fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
        // Initialize the transition storage.
        let storage = T::open(storage)?;
        // Return the transition store.
        Ok(Self {
            locator: storage.locator_map().clone(),
            inputs: (*storage.input_store()).clone(),
            outputs: (*storage.output_store()).clone(),
            tpk: storage.tpk_map().clone(),
            reverse_tpk: storage.reverse_tpk_map().clone(),
            tcm: storage.tcm_map().clone(),
            reverse_tcm: storage.reverse_tcm_map().clone(),
            scm: storage.scm_map().clone(),
            storage,
        })
    }

    /// Initializes a transition store from storage.
    pub fn from(storage: T) -> Self {
        Self {
            locator: storage.locator_map().clone(),
            inputs: (*storage.input_store()).clone(),
            outputs: (*storage.output_store()).clone(),
            tpk: storage.tpk_map().clone(),
            reverse_tpk: storage.reverse_tpk_map().clone(),
            tcm: storage.tcm_map().clone(),
            reverse_tcm: storage.reverse_tcm_map().clone(),
            scm: storage.scm_map().clone(),
            storage,
        }
    }

    /// Stores the given `transition` into storage.
    pub fn insert(&self, transition: &Transition<N>) -> Result<()> {
        self.storage.insert(transition)
    }

    /// Removes the input for the given `transition ID`.
    pub fn remove(&self, transition_id: &N::TransitionID) -> Result<()> {
        self.storage.remove(transition_id)
    }

    /// Starts an atomic batch write operation.
    pub fn start_atomic(&self) {
        self.storage.start_atomic();
    }

    /// Checks if an atomic batch is in progress.
    pub fn is_atomic_in_progress(&self) -> bool {
        self.storage.is_atomic_in_progress()
    }

    /// Checkpoints the atomic batch.
    pub fn atomic_checkpoint(&self) {
        self.storage.atomic_checkpoint();
    }

    /// Clears the latest atomic batch checkpoint.
    pub fn clear_latest_checkpoint(&self) {
        self.storage.clear_latest_checkpoint();
    }

    /// Rewinds the atomic batch to the previous checkpoint.
    pub fn atomic_rewind(&self) {
        self.storage.atomic_rewind();
    }

    /// Aborts an atomic batch write operation.
    pub fn abort_atomic(&self) {
        self.storage.abort_atomic();
    }

    /// Finishes an atomic batch write operation.
    pub fn finish_atomic(&self) -> Result<()> {
        self.storage.finish_atomic()
    }

    /// Returns the storage mode.
    pub fn storage_mode(&self) -> &StorageMode {
        self.storage.storage_mode()
    }
}

impl<N: Network, T: TransitionStorage<N>> TransitionStore<N, T> {
    /// Returns the transition ID that contains the given `input ID` or `output ID`.
    pub fn find_transition_id(&self, id: &Field<N>) -> Result<N::TransitionID> {
        // Start by checking the output IDs (which is the more likely case).
        if let Some(transition_id) = self.outputs.find_transition_id(id)? {
            return Ok(transition_id);
        }
        // Then check the input IDs.
        if let Some(transition_id) = self.inputs.find_transition_id(id)? {
            return Ok(transition_id);
        }
        // Throw an error.
        bail!("Failed to find the transition ID for the given input or output ID '{id}'")
    }
}

impl<N: Network, T: TransitionStorage<N>> TransitionStore<N, T> {
    /// Returns the transition for the given `transition ID`.
    pub fn get_transition(&self, transition_id: &N::TransitionID) -> Result<Option<Transition<N>>> {
        self.storage.get(transition_id)
    }

    /// Returns the program ID for the given `transition ID`.
    pub fn get_program_id(&self, transition_id: &N::TransitionID) -> Result<Option<ProgramID<N>>> {
        Ok(self.locator.get_confirmed(transition_id)?.map(|locator| match locator {
            Cow::Borrowed((program_id, _)) => *program_id,
            Cow::Owned((program_id, _)) => program_id,
        }))
    }

    /// Returns the function name for the given `transition ID`.
    pub fn get_function_name(&self, transition_id: &N::TransitionID) -> Result<Option<Identifier<N>>> {
        Ok(self.locator.get_confirmed(transition_id)?.map(|locator| match locator {
            Cow::Borrowed((_, function_name)) => *function_name,
            Cow::Owned((_, function_name)) => function_name,
        }))
    }

    /// Returns the input IDs for the given `transition ID`.
    pub fn get_input_ids(&self, transition_id: &N::TransitionID) -> Result<Vec<Field<N>>> {
        self.inputs.get_input_ids(transition_id)
    }

    /// Returns the inputs for the given `transition ID`.
    pub fn get_inputs(&self, transition_id: &N::TransitionID) -> Result<Vec<Input<N>>> {
        self.inputs.get_inputs(transition_id)
    }

    /// Returns the output IDs for the given `transition ID`.
    pub fn get_output_ids(&self, transition_id: &N::TransitionID) -> Result<Vec<Field<N>>> {
        self.outputs.get_output_ids(transition_id)
    }

    /// Returns the outputs for the given `transition ID`.
    pub fn get_outputs(&self, transition_id: &N::TransitionID) -> Result<Vec<Output<N>>> {
        self.outputs.get_outputs(transition_id)
    }

    /// Returns the record for the given `commitment`.
    ///
    /// If the record exists, `Ok(Some(record))` is returned.
    /// If the record was purged, `Ok(None)` is returned.
    /// If the record does not exist, `Err(error)` is returned.
    pub fn get_record(&self, commitment: &Field<N>) -> Result<Option<Record<N, Ciphertext<N>>>> {
        self.outputs.get_record(commitment)
    }
}

impl<N: Network, T: TransitionStorage<N>> TransitionStore<N, T> {
    /// Returns `true` if the given transition ID exists.
    pub fn contains_transition_id(&self, transition_id: &N::TransitionID) -> Result<bool> {
        self.locator.contains_key_confirmed(transition_id)
    }

    /* Input */

    /// Returns `true` if the given input ID exists.
    pub fn contains_input_id(&self, input_id: &Field<N>) -> Result<bool> {
        self.inputs.contains_input_id(input_id)
    }

    /// Returns `true` if the given serial number exists.
    pub fn contains_serial_number(&self, serial_number: &Field<N>) -> Result<bool> {
        self.inputs.contains_serial_number(serial_number)
    }

    /// Returns `true` if the given tag exists.
    pub fn contains_tag(&self, tag: &Field<N>) -> Result<bool> {
        self.inputs.contains_tag(tag)
    }

    /* Output */

    /// Returns `true` if the given output ID exists.
    pub fn contains_output_id(&self, output_id: &Field<N>) -> Result<bool> {
        self.outputs.contains_output_id(output_id)
    }

    /// Returns `true` if the given commitment exists.
    pub fn contains_commitment(&self, commitment: &Field<N>) -> Result<bool> {
        self.outputs.contains_commitment(commitment)
    }

    /// Returns `true` if the given checksum exists.
    pub fn contains_checksum(&self, checksum: &Field<N>) -> bool {
        self.outputs.contains_checksum(checksum)
    }

    /// Returns `true` if the given nonce exists.
    pub fn contains_nonce(&self, nonce: &Group<N>) -> Result<bool> {
        self.outputs.contains_nonce(nonce)
    }

    /* Metadata */

    /// Returns `true` if the given transition public key exists.
    pub fn contains_tpk(&self, tpk: &Group<N>) -> Result<bool> {
        self.reverse_tpk.contains_key_confirmed(tpk)
    }

    /// Returns `true` if the given transition commitment exists.
    pub fn contains_tcm(&self, tcm: &Field<N>) -> Result<bool> {
        self.reverse_tcm.contains_key_confirmed(tcm)
    }
}

impl<N: Network, T: TransitionStorage<N>> TransitionStore<N, T> {
    /// Returns an iterator over the transition IDs, for all transitions.
    pub fn transition_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, N::TransitionID>> {
        self.tcm.keys_confirmed()
    }

    /* Input */

    /// Returns an iterator over the input IDs, for all transition inputs.
    pub fn input_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.inputs.input_ids()
    }

    /// Returns an iterator over the constant input IDs, for all transition inputs that are constant.
    pub fn constant_input_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.inputs.constant_input_ids()
    }

    /// Returns an iterator over the public input IDs, for all transition inputs that are public.
    pub fn public_input_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.inputs.public_input_ids()
    }

    /// Returns an iterator over the private input IDs, for all transition inputs that are private.
    pub fn private_input_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.inputs.private_input_ids()
    }

    /// Returns an iterator over the serial numbers, for all transition inputs that are records.
    pub fn serial_numbers(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.inputs.serial_numbers()
    }

    /// Returns an iterator over the external record input IDs, for all transition inputs that are external records.
    pub fn external_input_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.inputs.external_input_ids()
    }

    /* Output */

    /// Returns an iterator over the output IDs, for all transition outputs.
    pub fn output_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.outputs.output_ids()
    }

    /// Returns an iterator over the constant output IDs, for all transition outputs that are constant.
    pub fn constant_output_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.outputs.constant_output_ids()
    }

    /// Returns an iterator over the public output IDs, for all transition outputs that are public.
    pub fn public_output_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.outputs.public_output_ids()
    }

    /// Returns an iterator over the private output IDs, for all transition outputs that are private.
    pub fn private_output_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.outputs.private_output_ids()
    }

    /// Returns an iterator over the commitments, for all transition outputs that are records.
    pub fn commitments(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.outputs.commitments()
    }

    /// Returns an iterator over the external record output IDs, for all transition outputs that are external records.
    pub fn external_output_ids(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.outputs.external_output_ids()
    }
}

impl<N: Network, T: TransitionStorage<N>> TransitionStore<N, T> {
    /* Input */

    /// Returns an iterator over the constant inputs, for all transitions.
    pub fn constant_inputs(&self) -> impl '_ + Iterator<Item = Cow<'_, Plaintext<N>>> {
        self.inputs.constant_inputs()
    }

    /// Returns an iterator over the constant inputs, for all transitions.
    pub fn public_inputs(&self) -> impl '_ + Iterator<Item = Cow<'_, Plaintext<N>>> {
        self.inputs.public_inputs()
    }

    /// Returns an iterator over the private inputs, for all transitions.
    pub fn private_inputs(&self) -> impl '_ + Iterator<Item = Cow<'_, Ciphertext<N>>> {
        self.inputs.private_inputs()
    }

    /// Returns an iterator over the tags, for all transition inputs that are records.
    pub fn tags(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.inputs.tags()
    }

    /* Output */

    /// Returns an iterator over the constant outputs, for all transitions.
    pub fn constant_outputs(&self) -> impl '_ + Iterator<Item = Cow<'_, Plaintext<N>>> {
        self.outputs.constant_outputs()
    }

    /// Returns an iterator over the constant outputs, for all transitions.
    pub fn public_outputs(&self) -> impl '_ + Iterator<Item = Cow<'_, Plaintext<N>>> {
        self.outputs.public_outputs()
    }

    /// Returns an iterator over the private outputs, for all transitions.
    pub fn private_outputs(&self) -> impl '_ + Iterator<Item = Cow<'_, Ciphertext<N>>> {
        self.outputs.private_outputs()
    }

    /// Returns an iterator over the checksums, for all transition outputs that are records.
    pub fn checksums(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.outputs.checksums()
    }

    /// Returns an iterator over the nonces, for all transition outputs that are records.
    pub fn nonces(&self) -> impl '_ + Iterator<Item = Cow<'_, Group<N>>> {
        self.outputs.nonces()
    }

    /// Returns an iterator over the `(commitment, record)` pairs, for all transition outputs that are records.
    pub fn records(&self) -> impl '_ + Iterator<Item = (Cow<'_, Field<N>>, Cow<'_, Record<N, Ciphertext<N>>>)> {
        self.outputs.records()
    }

    /* Metadata */

    /// Returns an iterator over the transition public keys, for all transitions.
    pub fn tpks(&self) -> impl '_ + Iterator<Item = Cow<'_, Group<N>>> {
        self.tpk.values_confirmed()
    }

    /// Returns an iterator over the transition commitments, for all transitions.
    pub fn tcms(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.tcm.values_confirmed()
    }

    /// Returns an iterator over the signer commitments, for all transitions.
    pub fn scms(&self) -> impl '_ + Iterator<Item = Cow<'_, Field<N>>> {
        self.scm.values_confirmed()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::helpers::memory::TransitionMemory;

    #[test]
    fn test_insert_get_remove() {
        let rng = &mut TestRng::default();

        // Sample the transactions.
        let transaction_0 = ledger_test_helpers::sample_execution_transaction_with_fee(true, rng);
        let transaction_1 = ledger_test_helpers::sample_execution_transaction_with_fee(false, rng);
        let transactions = vec![transaction_0, transaction_1];

        for transaction in transactions {
            let transitions = transaction.transitions().cloned().collect::<Vec<_>>();

            // Ensure there is at least 2 transition.
            println!("\n\nNumber of transitions: {}\n", transitions.len());
            assert!(transitions.len() > 1, "\n\nNumber of transitions: {}\n", transitions.len());

            // Initialize a new transition store.
            let transition_store = TransitionMemory::open(None).unwrap();

            // Test each transition in isolation.
            for transition in transitions.iter() {
                // Retrieve the transition ID.
                let transition_id = *transition.id();

                // Ensure the transition does not exist.
                let candidate = transition_store.get(&transition_id).unwrap();
                assert_eq!(None, candidate);

                // Insert the transition.
                transition_store.insert(transition).unwrap();

                // Retrieve the transition.
                let candidate = transition_store.get(&transition_id).unwrap();
                assert_eq!(Some(transition.clone()), candidate);

                // Remove the transition.
                transition_store.remove(&transition_id).unwrap();

                // Retrieve the transition.
                let candidate = transition_store.get(&transition_id).unwrap();
                assert_eq!(None, candidate);
            }

            // Insert every transition.
            for transition in transitions.iter() {
                // Retrieve the transition ID.
                let transition_id = *transition.id();

                // Ensure the transition does not exist.
                let candidate = transition_store.get(&transition_id).unwrap();
                assert_eq!(None, candidate);

                // Insert the transition.
                transition_store.insert(transition).unwrap();

                // Ensure the transition exists.
                let candidate = transition_store.get(&transition_id).unwrap();
                assert_eq!(Some(transition.clone()), candidate);
            }

            // Get every transition (in reverse).
            for transition in transitions.iter().rev() {
                // Retrieve the transition ID.
                let transition_id = *transition.id();

                // Retrieve the transition.
                let candidate = transition_store.get(&transition_id).unwrap();
                assert_eq!(Some(transition.clone()), candidate);
            }

            // Remove every transition (in reverse).
            for transition in transitions.iter().rev() {
                // Retrieve the transition ID.
                let transition_id = *transition.id();

                // Remove the transition.
                transition_store.remove(&transition_id).unwrap();

                // Ensure the transition does not exist.
                let candidate = transition_store.get(&transition_id).unwrap();
                assert_eq!(None, candidate);
            }
        }
    }
}