massa_ledger_exports/
controller.rs

1use massa_models::{address::Address, amount::Amount, bytecode::Bytecode};
2use std::{collections::BTreeSet, ops::Bound};
3
4use crate::{LedgerChanges, LedgerError};
5use massa_db_exports::DBBatch;
6
7#[cfg_attr(feature = "test-exports", mockall_wrap::wrap, mockall::automock)]
8pub trait LedgerController: Send + Sync {
9    /// Loads ledger from file
10    fn load_initial_ledger(&mut self) -> Result<(), LedgerError>;
11
12    /// Gets the balance of a ledger entry
13    ///
14    /// # Returns
15    /// The balance, or None if the ledger entry was not found
16    fn get_balance(&self, addr: &Address) -> Option<Amount>;
17
18    /// Gets a copy of the bytecode of a ledger entry
19    ///
20    /// # Returns
21    /// A copy of the found bytecode, or None if the ledger entry was not found
22    fn get_bytecode(&self, addr: &Address) -> Option<Bytecode>;
23
24    /// Checks if a ledger entry exists
25    ///
26    /// # Returns
27    /// true if it exists, false otherwise.
28    fn entry_exists(&self, addr: &Address) -> bool;
29
30    /// Gets a copy of the value of a datastore entry for a given address.
31    ///
32    /// # Arguments
33    /// * `addr`: target address
34    /// * `key`: datastore key
35    ///
36    /// # Returns
37    /// A copy of the datastore value, or `None` if the ledger entry or datastore entry was not found
38    fn get_data_entry(&self, addr: &Address, key: &[u8]) -> Option<Vec<u8>>;
39
40    /// Get every key of the datastore for a given address.
41    ///
42    /// # Returns
43    /// A `BTreeSet` of the datastore keys
44    fn get_datastore_keys(
45        &self,
46        addr: &Address,
47        prefix: &[u8],
48        start_key: Bound<Vec<u8>>,
49        end_key: Bound<Vec<u8>>,
50        count: Option<u32>,
51    ) -> Option<BTreeSet<Vec<u8>>>;
52
53    /// Reset the ledger
54    ///
55    /// USED FOR BOOTSTRAP ONLY
56    fn reset(&mut self);
57
58    fn apply_changes_to_batch(&mut self, changes: LedgerChanges, ledger_batch: &mut DBBatch);
59
60    /// Deserializes the key and value, useful after bootstrap
61    fn is_key_value_valid(&self, serialized_key: &[u8], serialized_value: &[u8]) -> bool;
62
63    /// Get every address and their corresponding balance.
64    ///
65    /// IMPORTANT: This should only be used for debug and test purposes.
66    ///
67    /// # Returns
68    /// A `BTreeMap` with the address as key and the balance as value
69    #[cfg(feature = "test-exports")]
70    fn get_every_address(&self) -> std::collections::BTreeMap<Address, Amount>;
71
72    /// Get the entire datastore for a given address.
73    ///
74    /// IMPORTANT: This should only be used for debug purposes.
75    ///
76    /// # Returns
77    /// A `BTreeMap` with the entry hash as key and the data bytes as value
78    #[cfg(feature = "test-exports")]
79    fn get_entire_datastore(&self, addr: &Address) -> std::collections::BTreeMap<Vec<u8>, Vec<u8>>;
80}