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.

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