You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

600 lines
18 KiB

  1. package statedb
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. "os"
  7. "strconv"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/hermeznetwork/hermez-node/log"
  10. "github.com/hermeznetwork/tracerr"
  11. "github.com/iden3/go-merkletree"
  12. "github.com/iden3/go-merkletree/db"
  13. "github.com/iden3/go-merkletree/db/pebble"
  14. )
  15. // TODO(Edu): Document here how StateDB is kept consistent
  16. var (
  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. // ErrAccountAlreadyExists is used when CreateAccount is called and the
  21. // Account already exists
  22. ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account already exists")
  23. // ErrToIdxNotFound is used when trying to get the ToIdx from ToEthAddr
  24. // or ToEthAddr&ToBJJ
  25. ErrToIdxNotFound = errors.New("ToIdx can not be found")
  26. // ErrGetIdxNoCase is used when trying to get the Idx from EthAddr &
  27. // BJJ with not compatible combination
  28. ErrGetIdxNoCase = errors.New("Can not get Idx due unexpected combination of ethereum Address & BabyJubJub PublicKey")
  29. // KeyCurrentBatch is used as key in the db to store the current BatchNum
  30. KeyCurrentBatch = []byte("k:currentbatch")
  31. // PrefixKeyIdx is the key prefix for idx in the db
  32. PrefixKeyIdx = []byte("i:")
  33. // PrefixKeyAccHash is the key prefix for account hash in the db
  34. PrefixKeyAccHash = []byte("h:")
  35. // PrefixKeyMT is the key prefix for merkle tree in the db
  36. PrefixKeyMT = []byte("m:")
  37. // PrefixKeyAddr is the key prefix for address in the db
  38. PrefixKeyAddr = []byte("a:")
  39. // PrefixKeyAddrBJJ is the key prefix for address-babyjubjub in the db
  40. PrefixKeyAddrBJJ = []byte("ab:")
  41. )
  42. const (
  43. // PathBatchNum defines the subpath of the Batch Checkpoint in the
  44. // subpath of the StateDB
  45. PathBatchNum = "/BatchNum"
  46. // PathCurrent defines the subpath of the current Batch in the subpath
  47. // of the StateDB
  48. PathCurrent = "/current"
  49. // TypeSynchronizer defines a StateDB used by the Synchronizer, that
  50. // generates the ExitTree when processing the txs
  51. TypeSynchronizer = "synchronizer"
  52. // TypeTxSelector defines a StateDB used by the TxSelector, without
  53. // computing ExitTree neither the ZKInputs
  54. TypeTxSelector = "txselector"
  55. // TypeBatchBuilder defines a StateDB used by the BatchBuilder, that
  56. // generates the ExitTree and the ZKInput when processing the txs
  57. TypeBatchBuilder = "batchbuilder"
  58. )
  59. // TypeStateDB determines the type of StateDB
  60. type TypeStateDB string
  61. // StateDB represents the StateDB object
  62. type StateDB struct {
  63. path string
  64. currentBatch common.BatchNum
  65. db *pebble.PebbleStorage
  66. mt *merkletree.MerkleTree
  67. typ TypeStateDB
  68. chainID uint16
  69. // idx holds the current Idx that the BatchBuilder is using
  70. idx common.Idx
  71. zki *common.ZKInputs
  72. // i is the current transaction index in the ZKInputs generation (zki)
  73. i int
  74. // AccumulatedFees contains the accumulated fees for each token (Coord
  75. // Idx) in the processed batch
  76. AccumulatedFees map[common.Idx]*big.Int
  77. }
  78. // NewStateDB creates a new StateDB, allowing to use an in-memory or in-disk
  79. // storage
  80. func NewStateDB(path string, typ TypeStateDB, nLevels int, chainID uint16) (*StateDB, error) {
  81. var sto *pebble.PebbleStorage
  82. var err error
  83. sto, err = pebble.NewPebbleStorage(path+PathCurrent, false)
  84. if err != nil {
  85. return nil, tracerr.Wrap(err)
  86. }
  87. var mt *merkletree.MerkleTree = nil
  88. if typ == TypeSynchronizer || typ == TypeBatchBuilder {
  89. mt, err = merkletree.NewMerkleTree(sto.WithPrefix(PrefixKeyMT), nLevels)
  90. if err != nil {
  91. return nil, tracerr.Wrap(err)
  92. }
  93. }
  94. if typ == TypeTxSelector && nLevels != 0 {
  95. return nil, tracerr.Wrap(fmt.Errorf("invalid StateDB parameters: StateDB type==TypeStateDB can not have nLevels!=0"))
  96. }
  97. sdb := &StateDB{
  98. path: path,
  99. db: sto,
  100. mt: mt,
  101. typ: typ,
  102. chainID: chainID,
  103. }
  104. // load currentBatch
  105. sdb.currentBatch, err = sdb.GetCurrentBatch()
  106. if err != nil {
  107. return nil, tracerr.Wrap(err)
  108. }
  109. // make reset (get checkpoint) at currentBatch
  110. err = sdb.reset(sdb.currentBatch, false)
  111. if err != nil {
  112. return nil, tracerr.Wrap(err)
  113. }
  114. return sdb, nil
  115. }
  116. // DB returns the *pebble.PebbleStorage from the StateDB
  117. func (s *StateDB) DB() *pebble.PebbleStorage {
  118. return s.db
  119. }
  120. // GetCurrentBatch returns the current BatchNum stored in the StateDB
  121. func (s *StateDB) GetCurrentBatch() (common.BatchNum, error) {
  122. cbBytes, err := s.db.Get(KeyCurrentBatch)
  123. if tracerr.Unwrap(err) == db.ErrNotFound {
  124. return 0, nil
  125. }
  126. if err != nil {
  127. return 0, tracerr.Wrap(err)
  128. }
  129. return common.BatchNumFromBytes(cbBytes)
  130. }
  131. // setCurrentBatch stores the current BatchNum in the StateDB
  132. func (s *StateDB) setCurrentBatch() error {
  133. tx, err := s.db.NewTx()
  134. if err != nil {
  135. return tracerr.Wrap(err)
  136. }
  137. err = tx.Put(KeyCurrentBatch, s.currentBatch.Bytes())
  138. if err != nil {
  139. return tracerr.Wrap(err)
  140. }
  141. if err := tx.Commit(); err != nil {
  142. return tracerr.Wrap(err)
  143. }
  144. return nil
  145. }
  146. // MakeCheckpoint does a checkpoint at the given batchNum in the defined path. Internally this advances & stores the current BatchNum, and then stores a Checkpoint of the current state of the StateDB.
  147. func (s *StateDB) MakeCheckpoint() error {
  148. // advance currentBatch
  149. s.currentBatch++
  150. log.Debugw("Making StateDB checkpoint", "batch", s.currentBatch, "type", s.typ)
  151. checkpointPath := s.path + PathBatchNum + strconv.Itoa(int(s.currentBatch))
  152. err := s.setCurrentBatch()
  153. if err != nil {
  154. return tracerr.Wrap(err)
  155. }
  156. // if checkpoint BatchNum already exist in disk, delete it
  157. if _, err := os.Stat(checkpointPath); !os.IsNotExist(err) {
  158. err := os.RemoveAll(checkpointPath)
  159. if err != nil {
  160. return tracerr.Wrap(err)
  161. }
  162. } else if err != nil && !os.IsNotExist(err) {
  163. return tracerr.Wrap(err)
  164. }
  165. // execute Checkpoint
  166. err = s.db.Pebble().Checkpoint(checkpointPath)
  167. if err != nil {
  168. return tracerr.Wrap(err)
  169. }
  170. return nil
  171. }
  172. // DeleteCheckpoint removes if exist the checkpoint of the given batchNum
  173. func (s *StateDB) DeleteCheckpoint(batchNum common.BatchNum) error {
  174. checkpointPath := s.path + PathBatchNum + strconv.Itoa(int(batchNum))
  175. if _, err := os.Stat(checkpointPath); os.IsNotExist(err) {
  176. return tracerr.Wrap(fmt.Errorf("Checkpoint with batchNum %d does not exist in DB", batchNum))
  177. }
  178. return os.RemoveAll(checkpointPath)
  179. }
  180. func pebbleMakeCheckpoint(source, dest string) error {
  181. // Remove dest folder (if it exists) before doing the checkpoint
  182. if _, err := os.Stat(dest); !os.IsNotExist(err) {
  183. err := os.RemoveAll(dest)
  184. if err != nil {
  185. return tracerr.Wrap(err)
  186. }
  187. } else if err != nil && !os.IsNotExist(err) {
  188. return tracerr.Wrap(err)
  189. }
  190. sto, err := pebble.NewPebbleStorage(source, false)
  191. if err != nil {
  192. return tracerr.Wrap(err)
  193. }
  194. defer func() {
  195. errClose := sto.Pebble().Close()
  196. if errClose != nil {
  197. log.Errorw("Pebble.Close", "err", errClose)
  198. }
  199. }()
  200. // execute Checkpoint
  201. err = sto.Pebble().Checkpoint(dest)
  202. if err != nil {
  203. return tracerr.Wrap(err)
  204. }
  205. return nil
  206. }
  207. // Reset resets the StateDB to the checkpoint at the given batchNum. Reset
  208. // does not delete the checkpoints between old current and the new current,
  209. // those checkpoints will remain in the storage, and eventually will be
  210. // deleted when MakeCheckpoint overwrites them.
  211. func (s *StateDB) Reset(batchNum common.BatchNum) error {
  212. return s.reset(batchNum, true)
  213. }
  214. // reset resets the StateDB to the checkpoint at the given batchNum. Reset
  215. // does not delete the checkpoints between old current and the new current,
  216. // those checkpoints will remain in the storage, and eventually will be
  217. // deleted when MakeCheckpoint overwrites them. `closeCurrent` will close the
  218. // currently opened db before doing the reset.
  219. func (s *StateDB) reset(batchNum common.BatchNum, closeCurrent bool) error {
  220. currentPath := s.path + PathCurrent
  221. if closeCurrent {
  222. if err := s.db.Pebble().Close(); err != nil {
  223. return tracerr.Wrap(err)
  224. }
  225. }
  226. // remove 'current'
  227. err := os.RemoveAll(currentPath)
  228. if err != nil {
  229. return tracerr.Wrap(err)
  230. }
  231. if batchNum == 0 {
  232. // if batchNum == 0, open the new fresh 'current'
  233. sto, err := pebble.NewPebbleStorage(currentPath, false)
  234. if err != nil {
  235. return tracerr.Wrap(err)
  236. }
  237. s.db = sto
  238. s.idx = 255
  239. s.currentBatch = batchNum
  240. if s.mt != nil {
  241. // open the MT for the current s.db
  242. mt, err := merkletree.NewMerkleTree(s.db.WithPrefix(PrefixKeyMT), s.mt.MaxLevels())
  243. if err != nil {
  244. return tracerr.Wrap(err)
  245. }
  246. s.mt = mt
  247. }
  248. return nil
  249. }
  250. checkpointPath := s.path + PathBatchNum + strconv.Itoa(int(batchNum))
  251. // copy 'BatchNumX' to 'current'
  252. err = pebbleMakeCheckpoint(checkpointPath, currentPath)
  253. if err != nil {
  254. return tracerr.Wrap(err)
  255. }
  256. // open the new 'current'
  257. sto, err := pebble.NewPebbleStorage(currentPath, false)
  258. if err != nil {
  259. return tracerr.Wrap(err)
  260. }
  261. s.db = sto
  262. // get currentBatch num
  263. s.currentBatch, err = s.GetCurrentBatch()
  264. if err != nil {
  265. return tracerr.Wrap(err)
  266. }
  267. // idx is obtained from the statedb reset
  268. s.idx, err = s.GetIdx()
  269. if err != nil {
  270. return tracerr.Wrap(err)
  271. }
  272. if s.mt != nil {
  273. // open the MT for the current s.db
  274. mt, err := merkletree.NewMerkleTree(s.db.WithPrefix(PrefixKeyMT), s.mt.MaxLevels())
  275. if err != nil {
  276. return tracerr.Wrap(err)
  277. }
  278. s.mt = mt
  279. }
  280. return nil
  281. }
  282. // GetAccount returns the account for the given Idx
  283. func (s *StateDB) GetAccount(idx common.Idx) (*common.Account, error) {
  284. return getAccountInTreeDB(s.db, idx)
  285. }
  286. // GetAccounts returns all the accounts in the db. Use for debugging pruposes
  287. // only.
  288. func (s *StateDB) GetAccounts() ([]common.Account, error) {
  289. idxDB := s.db.WithPrefix(PrefixKeyIdx)
  290. idxs := []common.Idx{}
  291. // NOTE: Current implementation of Iterate in the pebble interface is
  292. // not efficient, as it iterates over all keys. Improve it following
  293. // this example: https://github.com/cockroachdb/pebble/pull/923/files
  294. if err := idxDB.Iterate(func(k []byte, v []byte) (bool, error) {
  295. idx, err := common.IdxFromBytes(k)
  296. if err != nil {
  297. return false, tracerr.Wrap(err)
  298. }
  299. idxs = append(idxs, idx)
  300. return true, nil
  301. }); err != nil {
  302. return nil, tracerr.Wrap(err)
  303. }
  304. accs := []common.Account{}
  305. for i := range idxs {
  306. acc, err := s.GetAccount(idxs[i])
  307. if err != nil {
  308. return nil, tracerr.Wrap(err)
  309. }
  310. accs = append(accs, *acc)
  311. }
  312. return accs, nil
  313. }
  314. // getAccountInTreeDB is abstracted from StateDB to be used from StateDB and
  315. // from ExitTree. GetAccount returns the account for the given Idx
  316. func getAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error) {
  317. idxBytes, err := idx.Bytes()
  318. if err != nil {
  319. return nil, tracerr.Wrap(err)
  320. }
  321. vBytes, err := sto.Get(append(PrefixKeyIdx, idxBytes[:]...))
  322. if err != nil {
  323. return nil, tracerr.Wrap(err)
  324. }
  325. accBytes, err := sto.Get(append(PrefixKeyAccHash, vBytes...))
  326. if err != nil {
  327. return nil, tracerr.Wrap(err)
  328. }
  329. var b [32 * common.NLeafElems]byte
  330. copy(b[:], accBytes)
  331. account, err := common.AccountFromBytes(b)
  332. if err != nil {
  333. return nil, tracerr.Wrap(err)
  334. }
  335. account.Idx = idx
  336. return account, nil
  337. }
  338. // CreateAccount creates a new Account in the StateDB for the given Idx. If
  339. // StateDB.mt==nil, MerkleTree is not affected, otherwise updates the
  340. // MerkleTree, returning a CircomProcessorProof.
  341. func (s *StateDB) CreateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
  342. cpp, err := createAccountInTreeDB(s.db, s.mt, idx, account)
  343. if err != nil {
  344. return cpp, tracerr.Wrap(err)
  345. }
  346. // store idx by EthAddr & BJJ
  347. err = s.setIdxByEthAddrBJJ(idx, account.EthAddr, account.PublicKey, account.TokenID)
  348. return cpp, tracerr.Wrap(err)
  349. }
  350. // createAccountInTreeDB is abstracted from StateDB to be used from StateDB and
  351. // from ExitTree. Creates a new Account in the StateDB for the given Idx. If
  352. // StateDB.mt==nil, MerkleTree is not affected, otherwise updates the
  353. // MerkleTree, returning a CircomProcessorProof.
  354. func createAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
  355. // store at the DB the key: v, and value: leaf.Bytes()
  356. v, err := account.HashValue()
  357. if err != nil {
  358. return nil, tracerr.Wrap(err)
  359. }
  360. accountBytes, err := account.Bytes()
  361. if err != nil {
  362. return nil, tracerr.Wrap(err)
  363. }
  364. // store the Leaf value
  365. tx, err := sto.NewTx()
  366. if err != nil {
  367. return nil, tracerr.Wrap(err)
  368. }
  369. idxBytes, err := idx.Bytes()
  370. if err != nil {
  371. return nil, tracerr.Wrap(err)
  372. }
  373. _, err = tx.Get(append(PrefixKeyIdx, idxBytes[:]...))
  374. if tracerr.Unwrap(err) != db.ErrNotFound {
  375. return nil, tracerr.Wrap(ErrAccountAlreadyExists)
  376. }
  377. err = tx.Put(append(PrefixKeyAccHash, v.Bytes()...), accountBytes[:])
  378. if err != nil {
  379. return nil, tracerr.Wrap(err)
  380. }
  381. err = tx.Put(append(PrefixKeyIdx, idxBytes[:]...), v.Bytes())
  382. if err != nil {
  383. return nil, tracerr.Wrap(err)
  384. }
  385. if err := tx.Commit(); err != nil {
  386. return nil, tracerr.Wrap(err)
  387. }
  388. if mt != nil {
  389. return mt.AddAndGetCircomProof(idx.BigInt(), v)
  390. }
  391. return nil, nil
  392. }
  393. // UpdateAccount updates the Account in the StateDB for the given Idx. If
  394. // StateDB.mt==nil, MerkleTree is not affected, otherwise updates the
  395. // MerkleTree, returning a CircomProcessorProof.
  396. func (s *StateDB) UpdateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
  397. return updateAccountInTreeDB(s.db, s.mt, idx, account)
  398. }
  399. // updateAccountInTreeDB is abstracted from StateDB to be used from StateDB and
  400. // from ExitTree. Updates the Account in the StateDB for the given Idx. If
  401. // StateDB.mt==nil, MerkleTree is not affected, otherwise updates the
  402. // MerkleTree, returning a CircomProcessorProof.
  403. func updateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
  404. // store at the DB the key: v, and value: account.Bytes()
  405. v, err := account.HashValue()
  406. if err != nil {
  407. return nil, tracerr.Wrap(err)
  408. }
  409. accountBytes, err := account.Bytes()
  410. if err != nil {
  411. return nil, tracerr.Wrap(err)
  412. }
  413. tx, err := sto.NewTx()
  414. if err != nil {
  415. return nil, tracerr.Wrap(err)
  416. }
  417. err = tx.Put(append(PrefixKeyAccHash, v.Bytes()...), accountBytes[:])
  418. if err != nil {
  419. return nil, tracerr.Wrap(err)
  420. }
  421. idxBytes, err := idx.Bytes()
  422. if err != nil {
  423. return nil, tracerr.Wrap(err)
  424. }
  425. err = tx.Put(append(PrefixKeyIdx, idxBytes[:]...), v.Bytes())
  426. if err != nil {
  427. return nil, tracerr.Wrap(err)
  428. }
  429. if err := tx.Commit(); err != nil {
  430. return nil, tracerr.Wrap(err)
  431. }
  432. if mt != nil {
  433. proof, err := mt.Update(idx.BigInt(), v)
  434. return proof, tracerr.Wrap(err)
  435. }
  436. return nil, nil
  437. }
  438. // MTGetProof returns the CircomVerifierProof for a given Idx
  439. func (s *StateDB) MTGetProof(idx common.Idx) (*merkletree.CircomVerifierProof, error) {
  440. if s.mt == nil {
  441. return nil, tracerr.Wrap(ErrStateDBWithoutMT)
  442. }
  443. return s.mt.GenerateCircomVerifierProof(idx.BigInt(), s.mt.Root())
  444. }
  445. // MTGetRoot returns the current root of the underlying Merkle Tree
  446. func (s *StateDB) MTGetRoot() *big.Int {
  447. return s.mt.Root().BigInt()
  448. }
  449. // MerkleTree returns the underlying StateDB merkle tree. It can be nil.
  450. func (s *StateDB) MerkleTree() *merkletree.MerkleTree {
  451. return s.mt
  452. }
  453. // LocalStateDB represents the local StateDB which allows to make copies from
  454. // the synchronizer StateDB, and is used by the tx-selector and the
  455. // batch-builder. LocalStateDB is an in-memory storage.
  456. type LocalStateDB struct {
  457. *StateDB
  458. synchronizerStateDB *StateDB
  459. }
  460. // NewLocalStateDB returns a new LocalStateDB connected to the given
  461. // synchronizerDB
  462. func NewLocalStateDB(path string, synchronizerDB *StateDB, typ TypeStateDB, nLevels int) (*LocalStateDB, error) {
  463. s, err := NewStateDB(path, typ, nLevels, synchronizerDB.chainID)
  464. if err != nil {
  465. return nil, tracerr.Wrap(err)
  466. }
  467. return &LocalStateDB{
  468. s,
  469. synchronizerDB,
  470. }, nil
  471. }
  472. // Reset performs a reset in the LocaStateDB. If fromSynchronizer is true, it
  473. // gets the state from LocalStateDB.synchronizerStateDB for the given batchNum. If fromSynchronizer is false, get the state from LocalStateDB checkpoints.
  474. func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
  475. if batchNum == 0 {
  476. l.idx = 0
  477. return nil
  478. }
  479. synchronizerCheckpointPath := l.synchronizerStateDB.path + PathBatchNum + strconv.Itoa(int(batchNum))
  480. checkpointPath := l.path + PathBatchNum + strconv.Itoa(int(batchNum))
  481. currentPath := l.path + PathCurrent
  482. if fromSynchronizer {
  483. // use checkpoint from SynchronizerStateDB
  484. if _, err := os.Stat(synchronizerCheckpointPath); os.IsNotExist(err) {
  485. // if synchronizerStateDB does not have checkpoint at batchNum, return err
  486. return tracerr.Wrap(fmt.Errorf("Checkpoint \"%v\" not exist in Synchronizer",
  487. synchronizerCheckpointPath))
  488. }
  489. if err := l.db.Pebble().Close(); err != nil {
  490. return tracerr.Wrap(err)
  491. }
  492. // remove 'current'
  493. err := os.RemoveAll(currentPath)
  494. if err != nil {
  495. return tracerr.Wrap(err)
  496. }
  497. // copy synchronizer'BatchNumX' to 'current'
  498. err = pebbleMakeCheckpoint(synchronizerCheckpointPath, currentPath)
  499. if err != nil {
  500. return tracerr.Wrap(err)
  501. }
  502. // copy synchronizer'BatchNumX' to 'BatchNumX'
  503. err = pebbleMakeCheckpoint(synchronizerCheckpointPath, checkpointPath)
  504. if err != nil {
  505. return tracerr.Wrap(err)
  506. }
  507. // open the new 'current'
  508. sto, err := pebble.NewPebbleStorage(currentPath, false)
  509. if err != nil {
  510. return tracerr.Wrap(err)
  511. }
  512. l.db = sto
  513. // get currentBatch num
  514. l.currentBatch, err = l.GetCurrentBatch()
  515. if err != nil {
  516. return tracerr.Wrap(err)
  517. }
  518. // open the MT for the current s.db
  519. if l.mt != nil {
  520. mt, err := merkletree.NewMerkleTree(l.db.WithPrefix(PrefixKeyMT), l.mt.MaxLevels())
  521. if err != nil {
  522. return tracerr.Wrap(err)
  523. }
  524. l.mt = mt
  525. }
  526. return nil
  527. }
  528. // use checkpoint from LocalStateDB
  529. return l.StateDB.reset(batchNum, true)
  530. }