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.

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