casper-storage 2.1.1

Storage for a node on the Casper network.
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
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//! Types for balance queries.
use casper_types::{
    account::AccountHash,
    global_state::TrieMerkleProof,
    system::{
        handle_payment::{ACCUMULATION_PURSE_KEY, PAYMENT_PURSE_KEY, REFUND_PURSE_KEY},
        mint::BalanceHoldAddrTag,
        HANDLE_PAYMENT,
    },
    AccessRights, BlockTime, Digest, EntityAddr, HoldBalanceHandling, InitiatorAddr, Key,
    ProtocolVersion, PublicKey, StoredValue, TimeDiff, URef, URefAddr, U512,
};
use itertools::Itertools;
use num_rational::Ratio;
use num_traits::CheckedMul;
use std::{
    collections::{btree_map::Entry, BTreeMap},
    fmt::{Display, Formatter},
};
use tracing::error;

use crate::{
    global_state::state::StateReader,
    tracking_copy::{TrackingCopyEntityExt, TrackingCopyError, TrackingCopyExt},
    TrackingCopy,
};

/// How to handle available balance inquiry?
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub enum BalanceHandling {
    /// Ignore balance holds.
    #[default]
    Total,
    /// Adjust for balance holds (if any).
    Available,
}

/// Merkle proof handling options.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub enum ProofHandling {
    /// Do not attempt to provide proofs.
    #[default]
    NoProofs,
    /// Provide proofs.
    Proofs,
}

/// Represents a way to make a balance inquiry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BalanceIdentifier {
    /// Use system refund purse (held by handle payment system contract).
    Refund,
    /// Use system payment purse (held by handle payment system contract).
    Payment,
    /// Use system accumulate purse (held by handle payment system contract).
    Accumulate,
    /// Use purse associated to specified uref.
    Purse(URef),
    /// Use main purse of entity derived from public key.
    Public(PublicKey),
    /// Use main purse of entity from account hash.
    Account(AccountHash),
    /// Use main purse of entity.
    Entity(EntityAddr),
    /// Use purse at Key::Purse(URefAddr).
    Internal(URefAddr),
    /// Penalized account identifier.
    PenalizedAccount(AccountHash),
    /// Penalized payment identifier.
    PenalizedPayment,
}

impl BalanceIdentifier {
    /// Returns underlying uref addr from balance identifier, if any.
    pub fn as_purse_addr(&self) -> Option<URefAddr> {
        match self {
            BalanceIdentifier::Internal(addr) => Some(*addr),
            BalanceIdentifier::Purse(uref) => Some(uref.addr()),
            BalanceIdentifier::Public(_)
            | BalanceIdentifier::Account(_)
            | BalanceIdentifier::PenalizedAccount(_)
            | BalanceIdentifier::PenalizedPayment
            | BalanceIdentifier::Entity(_)
            | BalanceIdentifier::Refund
            | BalanceIdentifier::Payment
            | BalanceIdentifier::Accumulate => None,
        }
    }

    /// Return purse_uref, if able.
    pub fn purse_uref<S>(
        &self,
        tc: &mut TrackingCopy<S>,
        protocol_version: ProtocolVersion,
    ) -> Result<URef, TrackingCopyError>
    where
        S: StateReader<Key, StoredValue, Error = crate::global_state::error::Error>,
    {
        let purse_uref = match self {
            BalanceIdentifier::Internal(addr) => URef::new(*addr, AccessRights::READ),
            BalanceIdentifier::Purse(purse_uref) => *purse_uref,
            BalanceIdentifier::Public(public_key) => {
                let account_hash = public_key.to_account_hash();
                match tc.runtime_footprint_by_account_hash(protocol_version, account_hash) {
                    Ok((_, entity)) => entity
                        .main_purse()
                        .ok_or(TrackingCopyError::Authorization)?,
                    Err(tce) => return Err(tce),
                }
            }
            BalanceIdentifier::Account(account_hash)
            | BalanceIdentifier::PenalizedAccount(account_hash) => {
                match tc.runtime_footprint_by_account_hash(protocol_version, *account_hash) {
                    Ok((_, entity)) => entity
                        .main_purse()
                        .ok_or(TrackingCopyError::Authorization)?,
                    Err(tce) => return Err(tce),
                }
            }
            BalanceIdentifier::Entity(entity_addr) => {
                match tc.runtime_footprint_by_entity_addr(*entity_addr) {
                    Ok(entity) => entity
                        .main_purse()
                        .ok_or(TrackingCopyError::Authorization)?,
                    Err(tce) => return Err(tce),
                }
            }
            BalanceIdentifier::Refund => {
                self.get_system_purse(tc, HANDLE_PAYMENT, REFUND_PURSE_KEY)?
            }
            BalanceIdentifier::Payment | BalanceIdentifier::PenalizedPayment => {
                self.get_system_purse(tc, HANDLE_PAYMENT, PAYMENT_PURSE_KEY)?
            }
            BalanceIdentifier::Accumulate => {
                self.get_system_purse(tc, HANDLE_PAYMENT, ACCUMULATION_PURSE_KEY)?
            }
        };
        Ok(purse_uref)
    }

    fn get_system_purse<S>(
        &self,
        tc: &mut TrackingCopy<S>,
        system_contract_name: &str,
        named_key_name: &str,
    ) -> Result<URef, TrackingCopyError>
    where
        S: StateReader<Key, StoredValue, Error = crate::global_state::error::Error>,
    {
        let system_contract_registry = tc.get_system_entity_registry()?;

        let entity_hash = system_contract_registry
            .get(system_contract_name)
            .ok_or_else(|| {
                error!("Missing system handle payment contract hash");
                TrackingCopyError::MissingSystemContractHash(system_contract_name.to_string())
            })?;

        let named_keys = tc
            .runtime_footprint_by_entity_addr(EntityAddr::System(*entity_hash))?
            .take_named_keys();

        let named_key =
            named_keys
                .get(named_key_name)
                .ok_or(TrackingCopyError::NamedKeyNotFound(
                    named_key_name.to_string(),
                ))?;
        let uref = named_key
            .as_uref()
            .ok_or(TrackingCopyError::UnexpectedKeyVariant(*named_key))?;
        Ok(*uref)
    }

    /// Is this balance identifier for penalty?
    pub fn is_penalty(&self) -> bool {
        matches!(
            self,
            BalanceIdentifier::PenalizedAccount(_) | BalanceIdentifier::PenalizedPayment
        )
    }
}

impl Default for BalanceIdentifier {
    fn default() -> Self {
        BalanceIdentifier::Purse(URef::default())
    }
}

impl From<InitiatorAddr> for BalanceIdentifier {
    fn from(value: InitiatorAddr) -> Self {
        match value {
            InitiatorAddr::PublicKey(public_key) => BalanceIdentifier::Public(public_key),
            InitiatorAddr::AccountHash(account_hash) => BalanceIdentifier::Account(account_hash),
        }
    }
}

/// Processing hold balance handling.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct ProcessingHoldBalanceHandling {}

impl ProcessingHoldBalanceHandling {
    /// Returns new instance.
    pub fn new() -> Self {
        ProcessingHoldBalanceHandling::default()
    }

    /// Returns handling.
    pub fn handling(&self) -> HoldBalanceHandling {
        HoldBalanceHandling::Accrued
    }

    /// Returns true if handling is amortized.
    pub fn is_amortized(&self) -> bool {
        false
    }

    /// Returns hold interval.
    pub fn interval(&self) -> TimeDiff {
        TimeDiff::default()
    }
}

impl From<(HoldBalanceHandling, u64)> for ProcessingHoldBalanceHandling {
    fn from(_value: (HoldBalanceHandling, u64)) -> Self {
        ProcessingHoldBalanceHandling::default()
    }
}

/// Gas hold balance handling.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct GasHoldBalanceHandling {
    handling: HoldBalanceHandling,
    interval: TimeDiff,
}

impl GasHoldBalanceHandling {
    /// Returns new instance.
    pub fn new(handling: HoldBalanceHandling, interval: TimeDiff) -> Self {
        GasHoldBalanceHandling { handling, interval }
    }

    /// Returns handling.
    pub fn handling(&self) -> HoldBalanceHandling {
        self.handling
    }

    /// Returns interval.
    pub fn interval(&self) -> TimeDiff {
        self.interval
    }

    /// Returns true if handling is amortized.
    pub fn is_amortized(&self) -> bool {
        matches!(self.handling, HoldBalanceHandling::Amortized)
    }
}

impl From<(HoldBalanceHandling, TimeDiff)> for GasHoldBalanceHandling {
    fn from(value: (HoldBalanceHandling, TimeDiff)) -> Self {
        GasHoldBalanceHandling {
            handling: value.0,
            interval: value.1,
        }
    }
}

impl From<(HoldBalanceHandling, u64)> for GasHoldBalanceHandling {
    fn from(value: (HoldBalanceHandling, u64)) -> Self {
        GasHoldBalanceHandling {
            handling: value.0,
            interval: TimeDiff::from_millis(value.1),
        }
    }
}

/// Represents a balance request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BalanceRequest {
    state_hash: Digest,
    protocol_version: ProtocolVersion,
    identifier: BalanceIdentifier,
    balance_handling: BalanceHandling,
    proof_handling: ProofHandling,
}

impl BalanceRequest {
    /// Creates a new [`BalanceRequest`].
    pub fn new(
        state_hash: Digest,
        protocol_version: ProtocolVersion,
        identifier: BalanceIdentifier,
        balance_handling: BalanceHandling,
        proof_handling: ProofHandling,
    ) -> Self {
        BalanceRequest {
            state_hash,
            protocol_version,
            identifier,
            balance_handling,
            proof_handling,
        }
    }

    /// Creates a new [`BalanceRequest`].
    pub fn from_purse(
        state_hash: Digest,
        protocol_version: ProtocolVersion,
        purse_uref: URef,
        balance_handling: BalanceHandling,
        proof_handling: ProofHandling,
    ) -> Self {
        BalanceRequest {
            state_hash,
            protocol_version,
            identifier: BalanceIdentifier::Purse(purse_uref),
            balance_handling,
            proof_handling,
        }
    }

    /// Creates a new [`BalanceRequest`].
    pub fn from_public_key(
        state_hash: Digest,
        protocol_version: ProtocolVersion,
        public_key: PublicKey,
        balance_handling: BalanceHandling,
        proof_handling: ProofHandling,
    ) -> Self {
        BalanceRequest {
            state_hash,
            protocol_version,
            identifier: BalanceIdentifier::Public(public_key),
            balance_handling,
            proof_handling,
        }
    }

    /// Creates a new [`BalanceRequest`].
    pub fn from_account_hash(
        state_hash: Digest,
        protocol_version: ProtocolVersion,
        account_hash: AccountHash,
        balance_handling: BalanceHandling,
        proof_handling: ProofHandling,
    ) -> Self {
        BalanceRequest {
            state_hash,
            protocol_version,
            identifier: BalanceIdentifier::Account(account_hash),
            balance_handling,
            proof_handling,
        }
    }

    /// Creates a new [`BalanceRequest`].
    pub fn from_entity_addr(
        state_hash: Digest,
        protocol_version: ProtocolVersion,
        entity_addr: EntityAddr,
        balance_handling: BalanceHandling,
        proof_handling: ProofHandling,
    ) -> Self {
        BalanceRequest {
            state_hash,
            protocol_version,
            identifier: BalanceIdentifier::Entity(entity_addr),
            balance_handling,
            proof_handling,
        }
    }

    /// Creates a new [`BalanceRequest`].
    pub fn from_internal(
        state_hash: Digest,
        protocol_version: ProtocolVersion,
        balance_addr: URefAddr,
        balance_handling: BalanceHandling,
        proof_handling: ProofHandling,
    ) -> Self {
        BalanceRequest {
            state_hash,
            protocol_version,
            identifier: BalanceIdentifier::Internal(balance_addr),
            balance_handling,
            proof_handling,
        }
    }

    /// Returns a state hash.
    pub fn state_hash(&self) -> Digest {
        self.state_hash
    }

    /// Protocol version.
    pub fn protocol_version(&self) -> ProtocolVersion {
        self.protocol_version
    }

    /// Returns the identifier [`BalanceIdentifier`].
    pub fn identifier(&self) -> &BalanceIdentifier {
        &self.identifier
    }

    /// Returns the block time.
    pub fn balance_handling(&self) -> BalanceHandling {
        self.balance_handling
    }

    /// Returns proof handling.
    pub fn proof_handling(&self) -> ProofHandling {
        self.proof_handling
    }
}

/// Available balance checker.
pub trait AvailableBalanceChecker {
    /// Calculate and return available balance.
    fn available_balance(
        &self,
        block_time: BlockTime,
        total_balance: U512,
        gas_hold_balance_handling: GasHoldBalanceHandling,
        processing_hold_balance_handling: ProcessingHoldBalanceHandling,
    ) -> Result<U512, BalanceFailure> {
        if self.is_empty() {
            return Ok(total_balance);
        }

        let gas_held = match gas_hold_balance_handling.handling() {
            HoldBalanceHandling::Accrued => self.accrued(BalanceHoldAddrTag::Gas),
            HoldBalanceHandling::Amortized => {
                let interval = gas_hold_balance_handling.interval();
                self.amortization(BalanceHoldAddrTag::Gas, block_time, interval)?
            }
        };

        let processing_held = match processing_hold_balance_handling.handling() {
            HoldBalanceHandling::Accrued => self.accrued(BalanceHoldAddrTag::Processing),
            HoldBalanceHandling::Amortized => {
                let interval = processing_hold_balance_handling.interval();
                self.amortization(BalanceHoldAddrTag::Processing, block_time, interval)?
            }
        };

        let held = gas_held.saturating_add(processing_held);

        if held > total_balance {
            return Ok(U512::zero());
        }

        debug_assert!(
            total_balance >= held,
            "it should not be possible to hold more than the total available"
        );
        match total_balance.checked_sub(held) {
            Some(available_balance) => Ok(available_balance),
            None => {
                error!(%held, %total_balance, "held amount exceeds total balance, which should never occur.");
                Err(BalanceFailure::HeldExceedsTotal)
            }
        }
    }

    /// Calculates amortization.
    fn amortization(
        &self,
        hold_kind: BalanceHoldAddrTag,
        block_time: BlockTime,
        interval: TimeDiff,
    ) -> Result<U512, BalanceFailure> {
        let mut held = U512::zero();
        let block_time = block_time.value();
        let interval = interval.millis();

        for (hold_created_time, holds) in self.holds(hold_kind) {
            let hold_created_time = hold_created_time.value();
            if hold_created_time > block_time {
                continue;
            }
            let expiry = hold_created_time.saturating_add(interval);
            if block_time > expiry {
                continue;
            }
            // total held amount
            let held_ratio = Ratio::new_raw(
                holds.values().copied().collect_vec().into_iter().sum(),
                U512::one(),
            );
            // remaining time
            let remaining_time = U512::from(expiry.saturating_sub(block_time));
            // remaining time over total time
            let ratio = Ratio::new_raw(remaining_time, U512::from(interval));
            /*
                EXAMPLE: 1000 held for 24 hours
                if 1 hours has elapsed, held amount = 1000 * (23/24) == 958
                if 2 hours has elapsed, held amount = 1000 * (22/24) == 916
                ...
                if 23 hours has elapsed, held amount    = 1000 * (1/24) == 41
                if 23.50 hours has elapsed, held amount = 1000 * (1/48) == 20
                if 23.75 hours has elapsed, held amount = 1000 * (1/96) == 10
                                                (54000 ms / 5184000 ms)
            */
            match held_ratio.checked_mul(&ratio) {
                Some(amortized) => held += amortized.to_integer(),
                None => return Err(BalanceFailure::AmortizationFailure),
            }
        }
        Ok(held)
    }

    /// Return accrued amount.
    fn accrued(&self, hold_kind: BalanceHoldAddrTag) -> U512;

    /// Return holds.
    fn holds(&self, hold_kind: BalanceHoldAddrTag) -> BTreeMap<BlockTime, BalanceHolds>;

    /// Return true if empty.
    fn is_empty(&self) -> bool;
}

/// Balance holds with Merkle proofs.
pub type BalanceHolds = BTreeMap<BalanceHoldAddrTag, U512>;

impl AvailableBalanceChecker for BTreeMap<BlockTime, BalanceHolds> {
    fn accrued(&self, hold_kind: BalanceHoldAddrTag) -> U512 {
        self.values()
            .filter_map(|holds| holds.get(&hold_kind).copied())
            .collect_vec()
            .into_iter()
            .sum()
    }

    fn holds(&self, hold_kind: BalanceHoldAddrTag) -> BTreeMap<BlockTime, BalanceHolds> {
        let mut ret = BTreeMap::new();
        for (k, v) in self {
            if let Some(hold) = v.get(&hold_kind) {
                let mut inner = BTreeMap::new();
                inner.insert(hold_kind, *hold);
                ret.insert(*k, inner);
            }
        }
        ret
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }
}

/// Balance holds with Merkle proofs.
pub type BalanceHoldsWithProof =
    BTreeMap<BalanceHoldAddrTag, (U512, TrieMerkleProof<Key, StoredValue>)>;

impl AvailableBalanceChecker for BTreeMap<BlockTime, BalanceHoldsWithProof> {
    fn accrued(&self, hold_kind: BalanceHoldAddrTag) -> U512 {
        self.values()
            .filter_map(|holds| holds.get(&hold_kind))
            .map(|(amount, _)| *amount)
            .collect_vec()
            .into_iter()
            .sum()
    }

    fn holds(&self, hold_kind: BalanceHoldAddrTag) -> BTreeMap<BlockTime, BalanceHolds> {
        let mut ret: BTreeMap<BlockTime, BalanceHolds> = BTreeMap::new();
        for (block_time, holds_with_proof) in self {
            let mut holds: BTreeMap<BalanceHoldAddrTag, U512> = BTreeMap::new();
            for (addr, (held, _)) in holds_with_proof {
                if addr == &hold_kind {
                    match holds.entry(*addr) {
                        Entry::Vacant(v) => v.insert(*held),
                        Entry::Occupied(mut o) => &mut o.insert(*held),
                    };
                }
            }
            if !holds.is_empty() {
                match ret.entry(*block_time) {
                    Entry::Vacant(v) => v.insert(holds),
                    Entry::Occupied(mut o) => &mut o.insert(holds),
                };
            }
        }
        ret
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }
}

/// Proofs result.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProofsResult {
    /// Not requested.
    NotRequested {
        /// Any time-relevant active holds on the balance, without proofs.
        balance_holds: BTreeMap<BlockTime, BalanceHolds>,
    },
    /// Proofs.
    Proofs {
        /// A proof that the given value is present in the Merkle trie.
        total_balance_proof: Box<TrieMerkleProof<Key, StoredValue>>,
        /// Any time-relevant active holds on the balance, with proofs..
        balance_holds: BTreeMap<BlockTime, BalanceHoldsWithProof>,
    },
}

impl ProofsResult {
    /// Returns total balance proof, if any.
    pub fn total_balance_proof(&self) -> Option<&TrieMerkleProof<Key, StoredValue>> {
        match self {
            ProofsResult::NotRequested { .. } => None,
            ProofsResult::Proofs {
                total_balance_proof,
                ..
            } => Some(total_balance_proof),
        }
    }

    /// Returns balance holds, if any.
    pub fn balance_holds_with_proof(&self) -> Option<&BTreeMap<BlockTime, BalanceHoldsWithProof>> {
        match self {
            ProofsResult::NotRequested { .. } => None,
            ProofsResult::Proofs { balance_holds, .. } => Some(balance_holds),
        }
    }

    /// Returns balance holds, if any.
    pub fn balance_holds(&self) -> Option<&BTreeMap<BlockTime, BalanceHolds>> {
        match self {
            ProofsResult::NotRequested { balance_holds } => Some(balance_holds),
            ProofsResult::Proofs { .. } => None,
        }
    }

    /// Returns the total held amount.
    pub fn total_held_amount(&self) -> U512 {
        match self {
            ProofsResult::NotRequested { balance_holds } => balance_holds
                .values()
                .flat_map(|holds| holds.values().copied())
                .collect_vec()
                .into_iter()
                .sum(),
            ProofsResult::Proofs { balance_holds, .. } => balance_holds
                .values()
                .flat_map(|holds| holds.values().map(|(v, _)| *v))
                .collect_vec()
                .into_iter()
                .sum(),
        }
    }

    /// Returns the available balance, calculated using imputed values.
    #[allow(clippy::result_unit_err)]
    pub fn available_balance(
        &self,
        block_time: BlockTime,
        total_balance: U512,
        gas_hold_balance_handling: GasHoldBalanceHandling,
        processing_hold_balance_handling: ProcessingHoldBalanceHandling,
    ) -> Result<U512, BalanceFailure> {
        match self {
            ProofsResult::NotRequested { balance_holds } => balance_holds.available_balance(
                block_time,
                total_balance,
                gas_hold_balance_handling,
                processing_hold_balance_handling,
            ),
            ProofsResult::Proofs { balance_holds, .. } => balance_holds.available_balance(
                block_time,
                total_balance,
                gas_hold_balance_handling,
                processing_hold_balance_handling,
            ),
        }
    }
}

/// Balance failure.
#[derive(Debug, Clone)]
pub enum BalanceFailure {
    /// Failed to calculate amortization (checked multiplication).
    AmortizationFailure,
    /// Held amount exceeds total balance, which should never occur.
    HeldExceedsTotal,
}

impl Display for BalanceFailure {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            BalanceFailure::AmortizationFailure => {
                write!(
                    f,
                    "AmortizationFailure: failed to calculate amortization (checked multiplication)."
                )
            }
            BalanceFailure::HeldExceedsTotal => {
                write!(
                    f,
                    "HeldExceedsTotal: held amount exceeds total balance, which should never occur."
                )
            }
        }
    }
}

/// Result enum that represents all possible outcomes of a balance request.
#[derive(Debug, Clone)]
pub enum BalanceResult {
    /// Returned if a passed state root hash is not found.
    RootNotFound,
    /// A query returned a balance.
    Success {
        /// The purse address.
        purse_addr: URefAddr,
        /// The purses total balance, not considering holds.
        total_balance: U512,
        /// The available balance (total balance - sum of all active holds).
        available_balance: U512,
        /// Proofs result.
        proofs_result: ProofsResult,
    },
    /// Failure.
    Failure(TrackingCopyError),
}

impl BalanceResult {
    /// Returns the purse address for a [`BalanceResult::Success`] variant.
    pub fn purse_addr(&self) -> Option<URefAddr> {
        match self {
            BalanceResult::Success { purse_addr, .. } => Some(*purse_addr),
            _ => None,
        }
    }

    /// Returns the total balance for a [`BalanceResult::Success`] variant.
    pub fn total_balance(&self) -> Option<&U512> {
        match self {
            BalanceResult::Success { total_balance, .. } => Some(total_balance),
            _ => None,
        }
    }

    /// Returns the available balance for a [`BalanceResult::Success`] variant.
    pub fn available_balance(&self) -> Option<&U512> {
        match self {
            BalanceResult::Success {
                available_balance, ..
            } => Some(available_balance),
            _ => None,
        }
    }

    /// Returns the Merkle proofs, if any.
    pub fn proofs_result(self) -> Option<ProofsResult> {
        match self {
            BalanceResult::Success { proofs_result, .. } => Some(proofs_result),
            _ => None,
        }
    }

    /// Is the available balance sufficient to cover the cost?
    pub fn is_sufficient(&self, cost: U512) -> bool {
        match self {
            BalanceResult::RootNotFound | BalanceResult::Failure(_) => false,
            BalanceResult::Success {
                available_balance, ..
            } => available_balance >= &cost,
        }
    }

    /// Was the balance request successful?
    pub fn is_success(&self) -> bool {
        match self {
            BalanceResult::RootNotFound | BalanceResult::Failure(_) => false,
            BalanceResult::Success { .. } => true,
        }
    }

    /// Tracking copy error, if any.
    pub fn error(&self) -> Option<&TrackingCopyError> {
        match self {
            BalanceResult::RootNotFound | BalanceResult::Success { .. } => None,
            BalanceResult::Failure(err) => Some(err),
        }
    }
}

impl From<TrackingCopyError> for BalanceResult {
    fn from(tce: TrackingCopyError) -> Self {
        BalanceResult::Failure(tce)
    }
}