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 // ErrToIdxNotFound is used when trying to get the ToIdx from ToEthAddr
26 // or ToEthAddr&ToBJJ
27 ErrToIdxNotFound = errors.New("ToIdx 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())
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 return nil
152}
153
154// GetAccount returns the account for the given Idx
155func (s *StateDB) GetAccount(idx common.Idx) (*common.Account, error) {
156 return GetAccountInTreeDB(s.db.DB(), idx)
157}
158
159// GetAccounts returns all the accounts in the db. Use for debugging pruposes
160// only.
161func (s *StateDB) GetAccounts() ([]common.Account, error) {
162 idxDB := s.db.StorageWithPrefix(PrefixKeyIdx)
163 idxs := []common.Idx{}
164 // NOTE: Current implementation of Iterate in the pebble interface is
165 // not efficient, as it iterates over all keys. Improve it following
166 // this example: https://github.com/cockroachdb/pebble/pull/923/files
167 if err := idxDB.Iterate(func(k []byte, v []byte) (bool, error) {
168 idx, err := common.IdxFromBytes(k)
169 if err != nil {
170 return false, tracerr.Wrap(err)
171 }
172 idxs = append(idxs, idx)
173 return true, nil
174 }); err != nil {
175 return nil, tracerr.Wrap(err)
176 }
177 accs := []common.Account{}
178 for i := range idxs {
179 acc, err := s.GetAccount(idxs[i])
180 if err != nil {
181 return nil, tracerr.Wrap(err)
182 }
183 accs = append(accs, *acc)
184 }
185 return accs, nil
186}
187
188// GetAccountInTreeDB is abstracted from StateDB to be used from StateDB and
189// from ExitTree. GetAccount returns the account for the given Idx
190func GetAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error) {
191 idxBytes, err := idx.Bytes()
192 if err != nil {
193 return nil, tracerr.Wrap(err)
194 }
195 vBytes, err := sto.Get(append(PrefixKeyIdx, idxBytes[:]...))
196 if err != nil {
197 return nil, tracerr.Wrap(err)
198 }
199 accBytes, err := sto.Get(append(PrefixKeyAccHash, vBytes...))
200 if err != nil {
201 return nil, tracerr.Wrap(err)
202 }
203 var b [32 * common.NLeafElems]byte
204 copy(b[:], accBytes)
205 account, err := common.AccountFromBytes(b)
206 if err != nil {
207 return nil, tracerr.Wrap(err)
208 }
209 account.Idx = idx
210 return account, nil
211}
212
213// CreateAccount creates a new Account in the StateDB for the given Idx. If
214// StateDB.MT==nil, MerkleTree is not affected, otherwise updates the
215// MerkleTree, returning a CircomProcessorProof.
216func (s *StateDB) CreateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
217 cpp, err := CreateAccountInTreeDB(s.db.DB(), s.MT, idx, account)
218 if err != nil {
219 return cpp, tracerr.Wrap(err)
220 }
221 // store idx by EthAddr & BJJ
222 err = s.setIdxByEthAddrBJJ(idx, account.EthAddr, account.PublicKey, account.TokenID)
223 return cpp, tracerr.Wrap(err)
224}
225
226// CreateAccountInTreeDB is abstracted from StateDB to be used from StateDB and
227// from ExitTree. Creates a new Account in the StateDB for the given Idx. If
228// StateDB.MT==nil, MerkleTree is not affected, otherwise updates the
229// MerkleTree, returning a CircomProcessorProof.
230func CreateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
231 // store at the DB the key: v, and value: leaf.Bytes()
232 v, err := account.HashValue()
233 if err != nil {
234 return nil, tracerr.Wrap(err)
235 }
236 accountBytes, err := account.Bytes()
237 if err != nil {
238 return nil, tracerr.Wrap(err)
239 }
240
241 // store the Leaf value
242 tx, err := sto.NewTx()
243 if err != nil {
244 return nil, tracerr.Wrap(err)
245 }
246
247 idxBytes, err := idx.Bytes()
248 if err != nil {
249 return nil, tracerr.Wrap(err)
250 }
251 _, err = tx.Get(append(PrefixKeyIdx, idxBytes[:]...))
252 if tracerr.Unwrap(err) != db.ErrNotFound {
253 return nil, tracerr.Wrap(ErrAccountAlreadyExists)
254 }
255
256 err = tx.Put(append(PrefixKeyAccHash, v.Bytes()...), accountBytes[:])
257 if err != nil {
258 return nil, tracerr.Wrap(err)
259 }
260 err = tx.Put(append(PrefixKeyIdx, idxBytes[:]...), v.Bytes())
261 if err != nil {
262 return nil, tracerr.Wrap(err)
263 }
264
265 if err := tx.Commit(); err != nil {
266 return nil, tracerr.Wrap(err)
267 }
268
269 if mt != nil {
270 return mt.AddAndGetCircomProof(idx.BigInt(), v)
271 }
272
273 return nil, nil
274}
275
276// UpdateAccount updates the Account in the StateDB for the given Idx. If
277// StateDB.mt==nil, MerkleTree is not affected, otherwise updates the
278// MerkleTree, returning a CircomProcessorProof.
279func (s *StateDB) UpdateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
280 return UpdateAccountInTreeDB(s.db.DB(), s.MT, idx, account)
281}
282
283// UpdateAccountInTreeDB is abstracted from StateDB to be used from StateDB and
284// from ExitTree. Updates the Account in the StateDB for the given Idx. If
285// StateDB.mt==nil, MerkleTree is not affected, otherwise updates the
286// MerkleTree, returning a CircomProcessorProof.
287func UpdateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
288 // store at the DB the key: v, and value: account.Bytes()
289 v, err := account.HashValue()
290 if err != nil {
291 return nil, tracerr.Wrap(err)
292 }
293 accountBytes, err := account.Bytes()
294 if err != nil {
295 return nil, tracerr.Wrap(err)
296 }
297
298 tx, err := sto.NewTx()
299 if err != nil {
300 return nil, tracerr.Wrap(err)
301 }
302 err = tx.Put(append(PrefixKeyAccHash, v.Bytes()...), accountBytes[:])
303 if err != nil {
304 return nil, tracerr.Wrap(err)
305 }
306 idxBytes, err := idx.Bytes()
307 if err != nil {
308 return nil, tracerr.Wrap(err)
309 }
310 err = tx.Put(append(PrefixKeyIdx, idxBytes[:]...), v.Bytes())
311 if err != nil {
312 return nil, tracerr.Wrap(err)
313 }
314
315 if err := tx.Commit(); err != nil {
316 return nil, tracerr.Wrap(err)
317 }
318
319 if mt != nil {
320 proof, err := mt.Update(idx.BigInt(), v)
321 return proof, tracerr.Wrap(err)
322 }
323 return nil, nil
324}
325
326// MTGetProof returns the CircomVerifierProof for a given Idx
327func (s *StateDB) MTGetProof(idx common.Idx) (*merkletree.CircomVerifierProof, error) {
328 if s.MT == nil {
329 return nil, tracerr.Wrap(ErrStateDBWithoutMT)
330 }
331 return s.MT.GenerateCircomVerifierProof(idx.BigInt(), s.MT.Root())
332}
333
334// MTGetRoot returns the current root of the underlying Merkle Tree
335func (s *StateDB) MTGetRoot() *big.Int {
336 return s.MT.Root().BigInt()
337}
338
339// LocalStateDB represents the local StateDB which allows to make copies from
340// the synchronizer StateDB, and is used by the tx-selector and the
341// batch-builder. LocalStateDB is an in-memory storage.
342type LocalStateDB struct {
343 *StateDB
344 synchronizerStateDB *StateDB
345}
346
347// NewLocalStateDB returns a new LocalStateDB connected to the given
348// synchronizerDB. Checkpoints older than the value defined by `keep` will be
349// deleted.
350func NewLocalStateDB(path string, keep int, synchronizerDB *StateDB, typ TypeStateDB,
351 nLevels int) (*LocalStateDB, error) {
352 s, err := NewStateDB(path, keep, typ, nLevels)
353 if err != nil {
354 return nil, tracerr.Wrap(err)
355 }
356 return &LocalStateDB{
357 s,
358 synchronizerDB,
359 }, nil
360}
361
362// Reset performs a reset in the LocaStateDB. If fromSynchronizer is true, it
363// gets the state from LocalStateDB.synchronizerStateDB for the given batchNum.
364// If fromSynchronizer is false, get the state from LocalStateDB checkpoints.
365func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
366 if fromSynchronizer {
367 err := l.db.ResetFromSynchronizer(batchNum, l.synchronizerStateDB.db)
368 if err != nil {
369 return tracerr.Wrap(err)
370 }
371 // open the MT for the current s.db
372 if l.MT != nil {
373 mt, err := merkletree.NewMerkleTree(l.db.StorageWithPrefix(PrefixKeyMT), l.MT.MaxLevels())
374 if err != nil {
375 return tracerr.Wrap(err)
376 }
377 l.MT = mt
378 }
379 return nil
380 }
381 // use checkpoint from LocalStateDB
382 return l.StateDB.Reset(batchNum)
383}