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
use std::{convert::TryInto, marker::PhantomData};
use kvdb::KeyValueDB;
use kvdb_memorydb::InMemory as MemoryDatabase;
#[cfg(feature = "web")]
use kvdb_web::Database as WebDatabase;
use libzeropool::{
constants,
fawkes_crypto::{ff_uint::Num, ff_uint::PrimeField, BorshDeserialize, BorshSerialize},
native::{
account::Account, account::Account as NativeAccount, note::Note, note::Note as NativeNote,
params::PoolParams,
},
};
use crate::{merkle::MerkleTree, sparse_array::SparseArray};
pub type TxStorage<D, Fr> = SparseArray<D, Transaction<Fr>>;
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
pub enum Transaction<Fr: PrimeField> {
Account(NativeAccount<Fr>),
Note(NativeNote<Fr>),
}
pub struct State<D: KeyValueDB, P: PoolParams> {
pub tree: MerkleTree<D, P>,
pub(crate) txs: TxStorage<D, P::Fr>,
pub(crate) latest_account: Option<NativeAccount<P::Fr>>,
pub latest_account_index: Option<u64>,
pub latest_note_index: u64,
_params: PhantomData<P>,
}
#[cfg(feature = "web")]
impl<P> State<WebDatabase, P>
where
P: PoolParams,
P::Fr: 'static,
{
pub async fn init_web(db_id: String, params: P) -> Self {
let merkle_db_name = format!("zeropool.{}.smt", &db_id);
let tx_db_name = format!("zeropool.{}.txs", &db_id);
let tree = MerkleTree::new_web(&merkle_db_name, params.clone()).await;
let txs = TxStorage::new_web(&tx_db_name).await;
Self::new(tree, txs)
}
}
impl<P> State<MemoryDatabase, P>
where
P: PoolParams,
P::Fr: 'static,
{
pub fn init_test(params: P) -> Self {
let tree = MerkleTree::new_test(params.clone());
let txs = TxStorage::new_test();
Self::new(tree, txs)
}
}
impl<D, P> State<D, P>
where
D: KeyValueDB,
P: PoolParams,
P::Fr: 'static,
{
pub fn new(tree: MerkleTree<D, P>, txs: TxStorage<D, P::Fr>) -> Self {
let mut latest_account_index = None;
let mut latest_note_index = 0;
let mut latest_account = None;
for (index, tx) in txs.iter() {
match tx {
Transaction::Account(acc) => {
if index >= latest_account_index.unwrap_or(0) {
latest_account_index = Some(index);
latest_account = Some(acc);
}
}
Transaction::Note(_) => {
if index >= latest_note_index {
latest_note_index = index;
}
}
}
}
State {
tree,
txs,
latest_account_index,
latest_note_index,
latest_account,
_params: Default::default(),
}
}
pub fn add_hashes(&mut self, at_index: u64, hashes: &[Num<P::Fr>]) {
assert_eq!(
at_index % (constants::OUT as u64 + 1),
0,
"index must be divisible by {}",
constants::OUT + 1
);
self.tree.add_hashes(at_index, hashes.iter().copied());
}
pub fn add_full_tx(
&mut self,
at_index: u64,
hashes: &[Num<P::Fr>],
account: Option<Account<P::Fr>>,
notes: &[(u64, Note<P::Fr>)],
) {
self.add_hashes(at_index, hashes);
if let Some(acc) = account {
self.add_account(at_index, acc);
}
for (index, note) in notes {
self.add_note(*index, *note);
}
}
pub fn add_account(&mut self, at_index: u64, account: Account<P::Fr>) {
self.txs.set(at_index, &Transaction::Account(account));
if at_index >= self.latest_account_index.unwrap_or(0) {
self.latest_account_index = Some(at_index);
self.latest_account = Some(account);
}
}
pub fn add_note(&mut self, at_index: u64, note: Note<P::Fr>) {
if self.txs.get(at_index).is_some() {
return;
}
self.txs.set(at_index, &Transaction::Note(note));
if at_index > self.latest_note_index {
self.latest_note_index = at_index;
}
}
pub fn get_all_txs(&self) -> Vec<(u64, Transaction<P::Fr>)> {
self.txs.iter().collect()
}
pub fn earliest_usable_index(&self) -> u64 {
let latest_account_index = self
.latest_account
.map(|acc| acc.i.to_num())
.unwrap_or(Num::ZERO)
.try_into()
.unwrap();
self.txs
.iter_slice(latest_account_index..=self.latest_note_index)
.map(|(index, _)| index)
.next()
.unwrap_or(latest_account_index)
}
pub fn total_balance(&self) -> Num<P::Fr> {
self.account_balance() + self.note_balance()
}
pub fn account_balance(&self) -> Num<P::Fr> {
self.latest_account
.map(|acc| acc.b.to_num())
.unwrap_or(Num::ZERO)
}
pub fn note_balance(&self) -> Num<P::Fr> {
let starting_index = self
.latest_account
.map(|acc| acc.i.to_num().try_into().unwrap())
.unwrap_or(0);
let mut note_balance = Num::ZERO;
for (_, tx) in self.txs.iter_slice(starting_index..=self.latest_note_index) {
if let Transaction::Note(note) = tx {
note_balance += note.b.to_num();
}
}
note_balance
}
}