casper_storage/data_access_layer/
prune.rs

1//! Support for pruning leaf nodes from the merkle trie.
2use crate::{
3    global_state::trie_store::operations::TriePruneResult, tracking_copy::TrackingCopyError,
4};
5use casper_types::{execution::Effects, Digest, Key};
6
7/// Represents the configuration of a prune operation.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct PruneRequest {
10    state_hash: Digest,
11    keys_to_prune: Vec<Key>,
12}
13
14impl PruneRequest {
15    /// Create new prune config.
16    pub fn new(state_hash: Digest, keys_to_prune: Vec<Key>) -> Self {
17        PruneRequest {
18            state_hash,
19            keys_to_prune,
20        }
21    }
22
23    /// Returns the current state root state hash
24    pub fn state_hash(&self) -> Digest {
25        self.state_hash
26    }
27
28    /// Returns the list of keys to delete.
29    pub fn keys_to_prune(&self) -> &[Key] {
30        &self.keys_to_prune
31    }
32}
33
34/// The result of performing a prune.
35#[derive(Debug, Clone)]
36pub enum PruneResult {
37    /// Root not found.
38    RootNotFound,
39    /// Key does not exists.
40    MissingKey,
41    /// Failed to prune.
42    Failure(TrackingCopyError),
43    /// New state root hash generated after elements were pruned.
44    Success {
45        /// State root hash.
46        post_state_hash: Digest,
47        /// Effects of executing a step request.
48        effects: Effects,
49    },
50}
51
52impl From<TriePruneResult> for PruneResult {
53    fn from(value: TriePruneResult) -> Self {
54        match value {
55            TriePruneResult::Pruned(post_state_hash) => PruneResult::Success {
56                post_state_hash,
57                effects: Effects::default(),
58            },
59            TriePruneResult::MissingKey => PruneResult::MissingKey,
60            TriePruneResult::RootNotFound => PruneResult::RootNotFound,
61            TriePruneResult::Failure(gse) => PruneResult::Failure(TrackingCopyError::Storage(gse)),
62        }
63    }
64}