snarkvm_ledger_store/helpers/memory/
block.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
// 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.

use crate::{
    BlockStorage,
    ConfirmedTxType,
    TransactionStore,
    TransitionStore,
    helpers::memory::{MemoryMap, TransactionMemory, TransitionMemory},
};
use console::{prelude::*, types::Field};
use ledger_authority::Authority;
use ledger_block::{Header, Ratifications, Rejected, Solutions};
use ledger_puzzle::SolutionID;
use synthesizer_program::FinalizeOperation;

use aleo_std_storage::StorageMode;

/// An in-memory block storage.
#[derive(Clone)]
#[allow(clippy::type_complexity)]
pub struct BlockMemory<N: Network> {
    /// The mapping of `block height` to `state root`.
    state_root_map: MemoryMap<u32, N::StateRoot>,
    /// The mapping of `state root` to `block height`.
    reverse_state_root_map: MemoryMap<N::StateRoot, u32>,
    /// The mapping of `block height` to `block hash`.
    id_map: MemoryMap<u32, N::BlockHash>,
    /// The mapping of `block hash` to `block height`.
    reverse_id_map: MemoryMap<N::BlockHash, u32>,
    /// The header map.
    header_map: MemoryMap<N::BlockHash, Header<N>>,
    /// The authority map.
    authority_map: MemoryMap<N::BlockHash, Authority<N>>,
    /// The certificate map.
    certificate_map: MemoryMap<Field<N>, (u32, u64)>,
    /// The ratifications map.
    ratifications_map: MemoryMap<N::BlockHash, Ratifications<N>>,
    /// The solutions map.
    solutions_map: MemoryMap<N::BlockHash, Solutions<N>>,
    /// The solution IDs map.
    solution_ids_map: MemoryMap<SolutionID<N>, u32>,
    /// The aborted solution IDs map.
    aborted_solution_ids_map: MemoryMap<N::BlockHash, Vec<SolutionID<N>>>,
    /// The aborted solution heights map.
    aborted_solution_heights_map: MemoryMap<SolutionID<N>, u32>,
    /// The transactions map.
    transactions_map: MemoryMap<N::BlockHash, Vec<N::TransactionID>>,
    /// The aborted transaction IDs map.
    aborted_transaction_ids_map: MemoryMap<N::BlockHash, Vec<N::TransactionID>>,
    /// The rejected transaction ID or aborted transaction ID map.
    rejected_or_aborted_transaction_id_map: MemoryMap<N::TransactionID, N::BlockHash>,
    /// The confirmed transactions map.
    confirmed_transactions_map:
        MemoryMap<N::TransactionID, (N::BlockHash, ConfirmedTxType<N>, Vec<FinalizeOperation<N>>)>,
    /// The rejected deployment or execution map.
    rejected_deployment_or_execution_map: MemoryMap<Field<N>, Rejected<N>>,
    /// The transaction store.
    transaction_store: TransactionStore<N, TransactionMemory<N>>,
}

#[rustfmt::skip]
impl<N: Network> BlockStorage<N> for BlockMemory<N> {
    type StateRootMap = MemoryMap<u32, N::StateRoot>;
    type ReverseStateRootMap = MemoryMap<N::StateRoot, u32>;
    type IDMap = MemoryMap<u32, N::BlockHash>;
    type ReverseIDMap = MemoryMap<N::BlockHash, u32>;
    type HeaderMap = MemoryMap<N::BlockHash, Header<N>>;
    type AuthorityMap = MemoryMap<N::BlockHash, Authority<N>>;
    type CertificateMap = MemoryMap<Field<N>, (u32, u64)>;
    type RatificationsMap = MemoryMap<N::BlockHash, Ratifications<N>>;
    type SolutionsMap = MemoryMap<N::BlockHash, Solutions<N>>;
    type SolutionIDsMap = MemoryMap<SolutionID<N>, u32>;
    type AbortedSolutionIDsMap = MemoryMap<N::BlockHash, Vec<SolutionID<N>>>;
    type AbortedSolutionHeightsMap = MemoryMap<SolutionID<N>, u32>;
    type TransactionsMap = MemoryMap<N::BlockHash, Vec<N::TransactionID>>;
    type AbortedTransactionIDsMap = MemoryMap<N::BlockHash, Vec<N::TransactionID>>;
    type RejectedOrAbortedTransactionIDMap = MemoryMap<N::TransactionID, N::BlockHash>;
    type ConfirmedTransactionsMap = MemoryMap<N::TransactionID, (N::BlockHash, ConfirmedTxType<N>, Vec<FinalizeOperation<N>>)>;
    type RejectedDeploymentOrExecutionMap = MemoryMap<Field<N>, Rejected<N>>;
    type TransactionStorage = TransactionMemory<N>;
    type TransitionStorage = TransitionMemory<N>;

    /// Initializes the block storage.
    fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
        // Initialize the transition store.
        let transition_store = TransitionStore::<N, TransitionMemory<N>>::open(storage)?;
        // Initialize the transaction store.
        let transaction_store = TransactionStore::<N, TransactionMemory<N>>::open(transition_store)?;
        // Return the block storage.
        Ok(Self {
            state_root_map: MemoryMap::default(),
            reverse_state_root_map: MemoryMap::default(),
            id_map: MemoryMap::default(),
            reverse_id_map: MemoryMap::default(),
            header_map: MemoryMap::default(),
            authority_map: MemoryMap::default(),
            certificate_map: MemoryMap::default(),
            ratifications_map: MemoryMap::default(),
            solutions_map: MemoryMap::default(),
            solution_ids_map: MemoryMap::default(),
            aborted_solution_ids_map: MemoryMap::default(),
            aborted_solution_heights_map: MemoryMap::default(),
            transactions_map: MemoryMap::default(),
            aborted_transaction_ids_map: MemoryMap::default(),
            rejected_or_aborted_transaction_id_map: MemoryMap::default(),
            confirmed_transactions_map: MemoryMap::default(),
            rejected_deployment_or_execution_map: MemoryMap::default(),
            transaction_store,
        })
    }

    /// Returns the state root map.
    fn state_root_map(&self) -> &Self::StateRootMap {
        &self.state_root_map
    }

    /// Returns the reverse state root map.
    fn reverse_state_root_map(&self) -> &Self::ReverseStateRootMap {
        &self.reverse_state_root_map
    }

    /// Returns the ID map.
    fn id_map(&self) -> &Self::IDMap {
        &self.id_map
    }

    /// Returns the reverse ID map.
    fn reverse_id_map(&self) -> &Self::ReverseIDMap {
        &self.reverse_id_map
    }

    /// Returns the header map.
    fn header_map(&self) -> &Self::HeaderMap {
        &self.header_map
    }

    /// Returns the certificate map.
    fn certificate_map(&self) -> &Self::CertificateMap {
        &self.certificate_map
    }

    /// Returns the authority map.
    fn authority_map(&self) -> &Self::AuthorityMap {
        &self.authority_map
    }

    /// Returns the ratifications map.
    fn ratifications_map(&self) -> &Self::RatificationsMap {
        &self.ratifications_map
    }

    /// Returns the solutions map.
    fn solutions_map(&self) -> &Self::SolutionsMap {
        &self.solutions_map
    }

    /// Returns the solution IDs map.
    fn solution_ids_map(&self) -> &Self::SolutionIDsMap {
        &self.solution_ids_map
    }

    /// Returns the aborted solution IDs map.
    fn aborted_solution_ids_map(&self) -> &Self::AbortedSolutionIDsMap {
        &self.aborted_solution_ids_map
    }

    /// Returns the aborted solution heights map.
    fn aborted_solution_heights_map(&self) -> &Self::AbortedSolutionHeightsMap {
        &self.aborted_solution_heights_map
    }

    /// Returns the transactions map.
    fn transactions_map(&self) -> &Self::TransactionsMap {
        &self.transactions_map
    }

    /// Returns the aborted transaction IDs map.
    fn aborted_transaction_ids_map(&self) -> &Self::AbortedTransactionIDsMap {
        &self.aborted_transaction_ids_map
    }

    /// Returns the rejected transaction ID or aborted transaction ID map.
    fn rejected_or_aborted_transaction_id_map(&self) -> &Self::RejectedOrAbortedTransactionIDMap {
        &self.rejected_or_aborted_transaction_id_map
    }

    /// Returns the confirmed transactions map.
    fn confirmed_transactions_map(&self) -> &Self::ConfirmedTransactionsMap {
        &self.confirmed_transactions_map
    }

    /// Returns the rejected deployment or execution map.
    fn rejected_deployment_or_execution_map(&self) -> &Self::RejectedDeploymentOrExecutionMap {
        &self.rejected_deployment_or_execution_map
    }

    /// Returns the transaction store.
    fn transaction_store(&self) -> &TransactionStore<N, Self::TransactionStorage> {
        &self.transaction_store
    }
}