hermez-node/db/statedb/statedb.go
db/statedb/statedb.goRaw
1package statedb
2
3import (
4 "errors"
5 "fmt"
6 "math/big"
7
8 "github.com/hermeznetwork/hermez-node/common"
9 "github.com/hermeznetwork/hermez-node/db/kvdb"
10 "github.com/hermeznetwork/hermez-node/log"
11 "github.com/hermeznetwork/tracerr"
12 "github.com/iden3/go-merkletree"
13 "github.com/iden3/go-merkletree/db"
14)
15
16var (
17 // ErrStateDBWithoutMT is used when a method that requires a MerkleTree
18 // is called in a StateDB that does not have a MerkleTree defined
19 ErrStateDBWithoutMT = errors.New("Can not call method to use MerkleTree in a StateDB without MerkleTree")
20
21 // ErrAccountAlreadyExists is used when CreateAccount is called and the
22 // Account already exists
23 ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account already exists")
24
25 // ErrIdxNotFound is used when trying to get the Idx from EthAddr or
26 // EthAddr&ToBJJ
27 ErrIdxNotFound = errors.New("Idx can not be found")
28 // ErrGetIdxNoCase is used when trying to get the Idx from EthAddr &
29 // BJJ with not compatible combination
30 ErrGetIdxNoCase = errors.New("Can not get Idx due unexpected combination of ethereum Address & BabyJubJub PublicKey")
31
32 // PrefixKeyIdx is the key prefix for idx in the db
33 PrefixKeyIdx = []byte("i:")
34 // PrefixKeyAccHash is the key prefix for account hash in the db
35 PrefixKeyAccHash = []byte("h:")
36 // PrefixKeyMT is the key prefix for merkle tree in the db
37 PrefixKeyMT = []byte("m:")
38 // PrefixKeyAddr is the key prefix for address in the db
39 PrefixKeyAddr = []byte("a:")
40 // PrefixKeyAddrBJJ is the key prefix for address-babyjubjub in the db
41 PrefixKeyAddrBJJ = []byte("ab:")
42)
43
44const (
45 // TypeSynchronizer defines a StateDB used by the Synchronizer, that
46 // generates the ExitTree when processing the txs
47 TypeSynchronizer = "synchronizer"
48 // TypeTxSelector defines a StateDB used by the TxSelector, without
49 // computing ExitTree neither the ZKInputs
50 TypeTxSelector = "txselector"
51 // TypeBatchBuilder defines a StateDB used by the BatchBuilder, that
52 // generates the ExitTree and the ZKInput when processing the txs
53 TypeBatchBuilder = "batchbuilder"
54)
55
56// TypeStateDB determines the type of StateDB
57type TypeStateDB string
58
59// StateDB represents the StateDB object
60type StateDB struct {
61 path string
62 Typ TypeStateDB
63 db *kvdb.KVDB
64 MT *merkletree.MerkleTree
65 keep int
66}
67
68// NewStateDB creates a new StateDB, allowing to use an in-memory or in-disk
69// storage. Checkpoints older than the value defined by `keep` will be
70// deleted.
71func NewStateDB(pathDB string, keep int, typ TypeStateDB, nLevels int) (*StateDB, error) {
72 var kv *kvdb.KVDB
73 var err error
74
75 kv, err = kvdb.NewKVDB(pathDB, keep)
76 if err != nil {
77 return nil, tracerr.Wrap(err)
78 }
79
80 var mt *merkletree.MerkleTree = nil
81 if typ == TypeSynchronizer || typ == TypeBatchBuilder {
82 mt, err = merkletree.NewMerkleTree(kv.StorageWithPrefix(PrefixKeyMT), nLevels)
83 if err != nil {
84 return nil, tracerr.Wrap(err)
85 }
86 }
87 if typ == TypeTxSelector && nLevels != 0 {
88 return nil, tracerr.Wrap(fmt.Errorf("invalid StateDB parameters: StateDB type==TypeStateDB can not have nLevels!=0"))
89 }
90
91 return &StateDB{
92 path: pathDB,
93 db: kv,
94 MT: mt,
95 Typ: typ,
96 keep: keep,
97 }, nil
98}
99
100// MakeCheckpoint does a checkpoint at the given batchNum in the defined path.
101// Internally this advances & stores the current BatchNum, and then stores a
102// Checkpoint of the current state of the StateDB.
103func (s *StateDB) MakeCheckpoint() error {
104 log.Debugw("Making StateDB checkpoint", "batch", s.CurrentBatch()+1, "type", s.Typ)
105 return s.db.MakeCheckpoint()
106}
107
108// CurrentBatch returns the current in-memory CurrentBatch of the StateDB.db
109func (s *StateDB) CurrentBatch() common.BatchNum {
110 return s.db.CurrentBatch
111}
112
113// CurrentIdx returns the current in-memory CurrentIdx of the StateDB.db
114func (s *StateDB) CurrentIdx() common.Idx {
115 return s.db.CurrentIdx
116}
117
118// GetCurrentBatch returns the current BatchNum stored in the StateDB.db
119func (s *StateDB) GetCurrentBatch() (common.BatchNum, error) {
120 return s.db.GetCurrentBatch()
121}
122
123// GetCurrentIdx returns the stored Idx from the localStateDB, which is the
124// last Idx used for an Account in the localStateDB.
125func (s *StateDB) GetCurrentIdx() (common.Idx, error) {
126 return s.db.GetCurrentIdx()
127}
128
129// SetCurrentIdx stores Idx in the StateDB
130func (s *StateDB) SetCurrentIdx(idx common.Idx) error {
131 return s.db.SetCurrentIdx(idx)
132}
133
134// Reset resets the StateDB to the checkpoint at the given batchNum. Reset
135// does not delete the checkpoints between old current and the new current,
136// those checkpoints will remain in the storage, and eventually will be
137// deleted when MakeCheckpoint overwrites them.
138func (s *StateDB) Reset(batchNum common.BatchNum) error {
139 err := s.db.Reset(batchNum)
140 if err != nil {
141 return tracerr.Wrap(err)
142 }
143 if s.MT != nil {
144 // open the MT for the current s.db
145 mt, err := merkletree.NewMerkleTree(s.db.StorageWithPrefix(PrefixKeyMT), s.MT.MaxLevels())
146 if err != nil {
147 return tracerr.Wrap(err)
148 }
149 s.MT = mt
150 }
151 log.Debugw("Making StateDB Reset", "batch", batchNum)
152 return nil
153}
154
155// GetAccount returns the account for the given Idx
156func (s *StateDB) GetAccount(idx common.Idx) (*common.Account, error) {
157 return GetAccountInTreeDB(s.db.DB(), idx)
158}
159
160// GetAccounts returns all the accounts in the db. Use for debugging pruposes
161// only.
162func (s *StateDB) GetAccounts() ([]common.Account, error) {
163 idxDB := s.db.StorageWithPrefix(PrefixKeyIdx)
164 idxs := []common.Idx{}
165 // NOTE: Current implementation of Iterate in the pebble interface is
166 // not efficient, as it iterates over all keys. Improve it following
167 // this example: https://github.com/cockroachdb/pebble/pull/923/files
168 if err := idxDB.Iterate(func(k []byte, v []byte) (bool, error) {
169 idx, err := common.IdxFromBytes(k)
170 if err != nil {
171 return false, tracerr.Wrap(err)
172 }
173 idxs = append(idxs, idx)
174 return true, nil
175 }); err != nil {
176 return nil, tracerr.Wrap(err)
177 }
178 accs := []common.Account{}
179 for i := range idxs {
180 acc, err := s.GetAccount(idxs[i])
181 if err != nil {
182 return nil, tracerr.Wrap(err)
183 }
184 accs = append(accs, *acc)
185 }
186 return accs, nil
187}
188
189// GetAccountInTreeDB is abstracted from StateDB to be used from StateDB and
190// from ExitTree. GetAccount returns the account for the given Idx
191func GetAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error) {
192 idxBytes, err := idx.Bytes()
193 if err != nil {
194 return nil, tracerr.Wrap(err)
195 }
196 vBytes, err := sto.Get(append(PrefixKeyIdx, idxBytes[:]...))
197 if err != nil {
198 return nil, tracerr.Wrap(err)
199 }
200 accBytes, err := sto.Get(append(PrefixKeyAccHash, vBytes...))
201 if err != nil {
202 return nil, tracerr.Wrap(err)
203 }
204 var b [32 * common.NLeafElems]byte
205 copy(b[:], accBytes)
206 account, err := common.AccountFromBytes(b)
207 if err != nil {
208 return nil, tracerr.Wrap(err)
209 }
210 account.Idx = idx
211 return account, nil
212}
213
214// CreateAccount creates a new Account in the StateDB for the given Idx. If
215// StateDB.MT==nil, MerkleTree is not affected, otherwise updates the
216// MerkleTree, returning a CircomProcessorProof.
217func (s *StateDB) CreateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
218 cpp, err := CreateAccountInTreeDB(s.db.DB(), s.MT, idx, account)
219 if err != nil {
220 return cpp, tracerr.Wrap(err)
221 }
222 // store idx by EthAddr & BJJ
223 err = s.setIdxByEthAddrBJJ(idx, account.EthAddr, account.BJJ, account.TokenID)
224 return cpp, tracerr.Wrap(err)
225}
226
227// CreateAccountInTreeDB is abstracted from StateDB to be used from StateDB and
228// from ExitTree. Creates a new Account in the StateDB for the given Idx. If
229// StateDB.MT==nil, MerkleTree is not affected, otherwise updates the
230// MerkleTree, returning a CircomProcessorProof.
231func CreateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
232 // store at the DB the key: v, and value: leaf.Bytes()
233 v, err := account.HashValue()
234 if err != nil {
235 return nil, tracerr.Wrap(err)
236 }
237 accountBytes, err := account.Bytes()
238 if err != nil {
239 return nil, tracerr.Wrap(err)
240 }
241
242 // store the Leaf value
243 tx, err := sto.NewTx()
244 if err != nil {
245 return nil, tracerr.Wrap(err)
246 }
247
248 idxBytes, err := idx.Bytes()
249 if err != nil {
250 return nil, tracerr.Wrap(err)
251 }
252 _, err = tx.Get(append(PrefixKeyIdx, idxBytes[:]...))
253 if tracerr.Unwrap(err) != db.ErrNotFound {
254 return nil, tracerr.Wrap(ErrAccountAlreadyExists)
255 }
256
257 err = tx.Put(append(PrefixKeyAccHash, v.Bytes()...), accountBytes[:])
258 if err != nil {
259 return nil, tracerr.Wrap(err)
260 }
261 err = tx.Put(append(PrefixKeyIdx, idxBytes[:]...), v.Bytes())
262 if err != nil {
263 return nil, tracerr.Wrap(err)
264 }
265
266 if err := tx.Commit(); err != nil {
267 return nil, tracerr.Wrap(err)
268 }
269
270 if mt != nil {
271 return mt.AddAndGetCircomProof(idx.BigInt(), v)
272 }
273
274 return nil, nil
275}
276
277// UpdateAccount updates the Account in the StateDB for the given Idx. If
278// StateDB.mt==nil, MerkleTree is not affected, otherwise updates the
279// MerkleTree, returning a CircomProcessorProof.
280func (s *StateDB) UpdateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
281 return UpdateAccountInTreeDB(s.db.DB(), s.MT, idx, account)
282}
283
284// UpdateAccountInTreeDB is abstracted from StateDB to be used from StateDB and
285// from ExitTree. Updates the Account in the StateDB for the given Idx. If
286// StateDB.mt==nil, MerkleTree is not affected, otherwise updates the
287// MerkleTree, returning a CircomProcessorProof.
288func UpdateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
289 // store at the DB the key: v, and value: account.Bytes()
290 v, err := account.HashValue()
291 if err != nil {
292 return nil, tracerr.Wrap(err)
293 }
294 accountBytes, err := account.Bytes()
295 if err != nil {
296 return nil, tracerr.Wrap(err)
297 }
298
299 tx, err := sto.NewTx()
300 if err != nil {
301 return nil, tracerr.Wrap(err)
302 }
303 err = tx.Put(append(PrefixKeyAccHash, v.Bytes()...), accountBytes[:])
304 if err != nil {
305 return nil, tracerr.Wrap(err)
306 }
307 idxBytes, err := idx.Bytes()
308 if err != nil {
309 return nil, tracerr.Wrap(err)
310 }
311 err = tx.Put(append(PrefixKeyIdx, idxBytes[:]...), v.Bytes())
312 if err != nil {
313 return nil, tracerr.Wrap(err)
314 }
315
316 if err := tx.Commit(); err != nil {
317 return nil, tracerr.Wrap(err)
318 }
319
320 if mt != nil {
321 proof, err := mt.Update(idx.BigInt(), v)
322 return proof, tracerr.Wrap(err)
323 }
324 return nil, nil
325}
326
327// MTGetProof returns the CircomVerifierProof for a given Idx
328func (s *StateDB) MTGetProof(idx common.Idx) (*merkletree.CircomVerifierProof, error) {
329 if s.MT == nil {
330 return nil, tracerr.Wrap(ErrStateDBWithoutMT)
331 }
332 p, err := s.MT.GenerateSCVerifierProof(idx.BigInt(), s.MT.Root())
333 if err != nil {
334 return nil, tracerr.Wrap(err)
335 }
336 return p, nil
337}
338
339// MTGetRoot returns the current root of the underlying Merkle Tree
340func (s *StateDB) MTGetRoot() *big.Int {
341 return s.MT.Root().BigInt()
342}
343
344// LocalStateDB represents the local StateDB which allows to make copies from
345// the synchronizer StateDB, and is used by the tx-selector and the
346// batch-builder. LocalStateDB is an in-memory storage.
347type LocalStateDB struct {
348 *StateDB
349 synchronizerStateDB *StateDB
350}
351
352// NewLocalStateDB returns a new LocalStateDB connected to the given
353// synchronizerDB. Checkpoints older than the value defined by `keep` will be
354// deleted.
355func NewLocalStateDB(path string, keep int, synchronizerDB *StateDB, typ TypeStateDB,
356 nLevels int) (*LocalStateDB, error) {
357 s, err := NewStateDB(path, keep, typ, nLevels)
358 if err != nil {
359 return nil, tracerr.Wrap(err)
360 }
361 return &LocalStateDB{
362 s,
363 synchronizerDB,
364 }, nil
365}
366
367// Reset performs a reset in the LocaStateDB. If fromSynchronizer is true, it
368// gets the state from LocalStateDB.synchronizerStateDB for the given batchNum.
369// If fromSynchronizer is false, get the state from LocalStateDB checkpoints.
370func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
371 if fromSynchronizer {
372 err := l.db.ResetFromSynchronizer(batchNum, l.synchronizerStateDB.db)
373 if err != nil {
374 return tracerr.Wrap(err)
375 }
376 // open the MT for the current s.db
377 if l.MT != nil {
378 mt, err := merkletree.NewMerkleTree(l.db.StorageWithPrefix(PrefixKeyMT), l.MT.MaxLevels())
379 if err != nil {
380 return tracerr.Wrap(err)
381 }
382 l.MT = mt
383 }
384 return nil
385 }
386 // use checkpoint from LocalStateDB
387 return l.StateDB.Reset(batchNum)
388}