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
use crate::genesis_ledger::GenesisLedger;
use bin_prot::*;
use mina_rs_base::*;
use proof_systems::mina_hasher::Hashable;
use rocksdb::DB;
use std::marker::PhantomData;
use thiserror::Error;
const ACCOUNT_PREFIX: u8 = 0xfe;
type RocksDBResult = Result<(Box<[u8]>, Box<[u8]>), rocksdb::Error>;
pub struct RocksDbGenesisLedger<
'a,
const DEPTH: usize,
Account: Hashable + BinProtSerializationType<'a>,
> {
db: &'a DB,
_pd: PhantomData<Account>,
}
#[derive(Error, Debug)]
pub enum Error {
#[error("Could not deserialize account: {0}\nkey:{1:?}, value:{2:?}")]
Disconnect(bin_prot::error::Error, Vec<u8>, Vec<u8>),
#[error("RocksDBError: {0}")]
RocksDBError(rocksdb::Error),
}
impl<'a, const DEPTH: usize, Account: Hashable + BinProtSerializationType<'a>>
RocksDbGenesisLedger<'a, DEPTH, Account>
{
pub fn new(db: &'a DB) -> Self {
Self {
db,
_pd: Default::default(),
}
}
}
fn decode_account_from_kv<'a, Account: BinProtSerializationType<'a>>(
r: RocksDBResult,
) -> Result<Account, Error> {
let (k, v) = r.map_err(Error::RocksDBError)?;
let account: <Account as BinProtSerializationType>::T =
from_reader_strict(&v[..]).map_err(|err| Error::Disconnect(err, k.to_vec(), v.to_vec()))?;
Ok(account.into())
}
impl<'a, const DEPTH: usize, Account: Hashable + BinProtSerializationType<'a> + 'a> IntoIterator
for &RocksDbGenesisLedger<'a, DEPTH, Account>
{
type Item = Result<Account, Error>;
type IntoIter = Box<dyn Iterator<Item = Result<Account, Error>> + 'a>;
fn into_iter(self) -> Box<dyn Iterator<Item = Result<Account, Error>> + 'a> {
let db_iter = self
.db
.prefix_iterator(&[ACCOUNT_PREFIX]) .take_while(|r| {
if let Ok((k, _)) = r {
k.first() == Some(&ACCOUNT_PREFIX)
} else {
false
}
}); Box::new(db_iter.map(decode_account_from_kv))
}
}
impl<'a, const DEPTH: usize, Account: Hashable + BinProtSerializationType<'a> + 'a>
GenesisLedger<'a, DEPTH, Account> for RocksDbGenesisLedger<'a, DEPTH, Account>
where
<Account as Hashable>::D: Default,
{
type Error = Error;
}