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.

388 lines
12 KiB

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