forked from AleoNet/snarkOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.rs
107 lines (93 loc) · 4.01 KB
/
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
// Copyright (C) 2019-2022 Aleo Systems Inc.
// This file is part of the snarkOS library.
// The snarkOS library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The snarkOS library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.
use crate::store::{
rocksdb::{self, DataMap, Database},
DataID,
TransactionDB,
TransitionDB,
};
use snarkvm::prelude::*;
/// A RocksDB block storage.
#[derive(Clone)]
pub struct BlockDB<N: Network> {
/// The mapping of `block height` to `block hash`.
id_map: DataMap<u32, N::BlockHash>,
/// The mapping of `block hash` to `block height`.
reverse_id_map: DataMap<N::BlockHash, u32>,
/// The header map.
header_map: DataMap<N::BlockHash, Header<N>>,
/// The transactions map.
transactions_map: DataMap<N::BlockHash, Vec<N::TransactionID>>,
/// The reverse transactions map.
reverse_transactions_map: DataMap<N::TransactionID, N::BlockHash>,
/// The transaction store.
transaction_store: TransactionStore<N, TransactionDB<N>>,
/// The signature map.
signature_map: DataMap<N::BlockHash, Signature<N>>,
}
#[rustfmt::skip]
impl<N: Network> BlockStorage<N> for BlockDB<N> {
type IDMap = DataMap<u32, N::BlockHash>;
type ReverseIDMap = DataMap<N::BlockHash, u32>;
type HeaderMap = DataMap<N::BlockHash, Header<N>>;
type TransactionsMap = DataMap<N::BlockHash, Vec<N::TransactionID>>;
type ReverseTransactionsMap = DataMap<N::TransactionID, N::BlockHash>;
type TransactionStorage = TransactionDB<N>;
type TransitionStorage = TransitionDB<N>;
type SignatureMap = DataMap<N::BlockHash, Signature<N>>;
/// Initializes the block storage.
fn open(dev: Option<u16>) -> Result<Self> {
// Initialize the transition store.
let transition_store = TransitionStore::<N, TransitionDB<N>>::open(dev)?;
// Initialize the transaction store.
let transaction_store = TransactionStore::<N, TransactionDB<N>>::open(transition_store)?;
// Return the block storage.
Ok(Self {
id_map: rocksdb::RocksDB::open_map(N::ID, dev, DataID::BlockIDMap)?,
reverse_id_map: rocksdb::RocksDB::open_map(N::ID, dev, DataID::BlockReverseIDMap)?,
header_map: rocksdb::RocksDB::open_map(N::ID, dev, DataID::BlockHeaderMap)?,
transactions_map: rocksdb::RocksDB::open_map(N::ID, dev, DataID::BlockTransactionsMap)?,
reverse_transactions_map: rocksdb::RocksDB::open_map(N::ID, dev, DataID::BlockReverseTransactionsMap)?,
transaction_store,
signature_map: rocksdb::RocksDB::open_map(N::ID, dev, DataID::BlockSignatureMap)?,
})
}
/// 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 transactions map.
fn transactions_map(&self) -> &Self::TransactionsMap {
&self.transactions_map
}
/// Returns the reverse transactions map.
fn reverse_transactions_map(&self) -> &Self::ReverseTransactionsMap {
&self.reverse_transactions_map
}
/// Returns the transaction store.
fn transaction_store(&self) -> &TransactionStore<N, Self::TransactionStorage> {
&self.transaction_store
}
/// Returns the signature map.
fn signature_map(&self) -> &Self::SignatureMap {
&self.signature_map
}
}