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.

339 lines
9.4 KiB

  1. package statedb
  2. import (
  3. "math/big"
  4. "github.com/hermeznetwork/hermez-node/common"
  5. "github.com/iden3/go-iden3-crypto/poseidon"
  6. "github.com/iden3/go-merkletree"
  7. "github.com/iden3/go-merkletree/db"
  8. "github.com/iden3/go-merkletree/db/memory"
  9. )
  10. // keyidx is used as key in the db to store the current Idx
  11. var keyidx = []byte("idx")
  12. // FUTURE This will be used from common once pending PR is merged
  13. type ExitInfo struct {
  14. Idx *common.Idx
  15. Proof *merkletree.CircomVerifierProof
  16. Nullifier *big.Int
  17. Balance *big.Int
  18. }
  19. // ProcessTxs process the given L1Txs & L2Txs applying the needed updates to
  20. // the StateDB depending on the transaction Type. Returns the common.ZKInputs
  21. // to generate the SnarkProof later used by the BatchBuilder, and if
  22. // cmpExitTree is set to true, returns common.ExitTreeLeaf that is later used
  23. // by the Synchronizer to update the HistoryDB.
  24. func (s *StateDB) ProcessTxs(cmpExitTree bool, l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.L2Tx) (*common.ZKInputs, []*ExitInfo, error) {
  25. var err error
  26. var exitTree *merkletree.MerkleTree
  27. exits := make(map[common.Idx]common.Account)
  28. if cmpExitTree {
  29. // TBD if ExitTree is only in memory or stored in disk, for the moment
  30. // only needed in memory
  31. exitTree, err = merkletree.NewMerkleTree(memory.NewMemoryStorage(), s.mt.MaxLevels())
  32. if err != nil {
  33. return nil, nil, err
  34. }
  35. }
  36. for _, tx := range l1usertxs {
  37. exitIdx, exitAccount, err := s.processL1Tx(exitTree, tx)
  38. if err != nil {
  39. return nil, nil, err
  40. }
  41. if exitIdx != nil && cmpExitTree {
  42. exits[*exitIdx] = *exitAccount
  43. }
  44. }
  45. for _, tx := range l1coordinatortxs {
  46. exitIdx, exitAccount, err := s.processL1Tx(exitTree, tx)
  47. if err != nil {
  48. return nil, nil, err
  49. }
  50. if exitIdx != nil && cmpExitTree {
  51. exits[*exitIdx] = *exitAccount
  52. }
  53. }
  54. for _, tx := range l2txs {
  55. exitIdx, exitAccount, err := s.processL2Tx(exitTree, tx)
  56. if err != nil {
  57. return nil, nil, err
  58. }
  59. if exitIdx != nil && cmpExitTree {
  60. exits[*exitIdx] = *exitAccount
  61. }
  62. }
  63. if !cmpExitTree {
  64. return nil, nil, nil
  65. }
  66. // once all txs processed (exitTree root frozen), for each leaf
  67. // generate ExitInfo data
  68. var exitInfos []*ExitInfo
  69. for exitIdx, exitAccount := range exits {
  70. // 0. generate MerkleProof
  71. p, err := exitTree.GenerateCircomVerifierProof(exitIdx.BigInt(), nil)
  72. if err != nil {
  73. return nil, nil, err
  74. }
  75. // 1. compute nullifier
  76. exitAccStateValue, err := exitAccount.HashValue()
  77. if err != nil {
  78. return nil, nil, err
  79. }
  80. nullifier, err := poseidon.Hash([]*big.Int{
  81. exitAccStateValue,
  82. big.NewInt(int64(s.currentBatch)),
  83. exitTree.Root().BigInt(),
  84. })
  85. if err != nil {
  86. return nil, nil, err
  87. }
  88. // 2. generate ExitInfo
  89. ei := &ExitInfo{
  90. Idx: &exitIdx,
  91. Proof: p,
  92. Nullifier: nullifier,
  93. Balance: exitAccount.Balance,
  94. }
  95. exitInfos = append(exitInfos, ei)
  96. }
  97. // return exitInfos, so Synchronizer will be able to store it into
  98. // HistoryDB for the concrete BatchNum
  99. return nil, exitInfos, nil
  100. }
  101. // processL2Tx process the given L2Tx applying the needed updates to
  102. // the StateDB depending on the transaction Type.
  103. func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.L2Tx) (*common.Idx, *common.Account, error) {
  104. switch tx.Type {
  105. case common.TxTypeTransfer:
  106. // go to the MT account of sender and receiver, and update
  107. // balance & nonce
  108. err := s.applyTransfer(tx.Tx())
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. case common.TxTypeExit:
  113. // execute exit flow
  114. exitAccount, err := s.applyExit(exitTree, tx.Tx())
  115. if err != nil {
  116. return nil, nil, err
  117. }
  118. return &tx.FromIdx, exitAccount, nil
  119. default:
  120. }
  121. return nil, nil, nil
  122. }
  123. // processL1Tx process the given L1Tx applying the needed updates to the
  124. // StateDB depending on the transaction Type.
  125. func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx) (*common.Idx, *common.Account, error) {
  126. switch tx.Type {
  127. case common.TxTypeForceTransfer, common.TxTypeTransfer:
  128. // go to the MT account of sender and receiver, and update balance
  129. // & nonce
  130. err := s.applyTransfer(tx.Tx())
  131. if err != nil {
  132. return nil, nil, err
  133. }
  134. case common.TxTypeCreateAccountDeposit:
  135. // add new account to the MT, update balance of the MT account
  136. err := s.applyCreateAccount(tx)
  137. if err != nil {
  138. return nil, nil, err
  139. }
  140. case common.TxTypeDeposit:
  141. // update balance of the MT account
  142. err := s.applyDeposit(tx, false)
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. case common.TxTypeDepositTransfer:
  147. // update balance in MT account, update balance & nonce of sender
  148. // & receiver
  149. err := s.applyDeposit(tx, true)
  150. if err != nil {
  151. return nil, nil, err
  152. }
  153. case common.TxTypeCreateAccountDepositTransfer:
  154. // add new account to the merkletree, update balance in MT account,
  155. // update balance & nonce of sender & receiver
  156. err := s.applyCreateAccount(tx)
  157. if err != nil {
  158. return nil, nil, err
  159. }
  160. err = s.applyTransfer(tx.Tx())
  161. if err != nil {
  162. return nil, nil, err
  163. }
  164. case common.TxTypeExit:
  165. // execute exit flow
  166. exitAccount, err := s.applyExit(exitTree, tx.Tx())
  167. if err != nil {
  168. return nil, nil, err
  169. }
  170. return &tx.FromIdx, exitAccount, nil
  171. default:
  172. }
  173. return nil, nil, nil
  174. }
  175. // applyCreateAccount creates a new account in the account of the depositer, it
  176. // stores the deposit value
  177. func (s *StateDB) applyCreateAccount(tx *common.L1Tx) error {
  178. account := &common.Account{
  179. TokenID: tx.TokenID,
  180. Nonce: 0,
  181. Balance: tx.LoadAmount,
  182. PublicKey: tx.FromBJJ,
  183. EthAddr: tx.FromEthAddr,
  184. }
  185. _, err := s.CreateAccount(common.Idx(s.idx+1), account)
  186. if err != nil {
  187. return err
  188. }
  189. s.idx = s.idx + 1
  190. return s.setIdx(s.idx)
  191. }
  192. // applyDeposit updates the balance in the account of the depositer, if
  193. // andTransfer parameter is set to true, the method will also apply the
  194. // Transfer of the L1Tx/DepositTransfer
  195. func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
  196. // deposit the tx.LoadAmount into the sender account
  197. accSender, err := s.GetAccount(tx.FromIdx)
  198. if err != nil {
  199. return err
  200. }
  201. accSender.Balance = new(big.Int).Add(accSender.Balance, tx.LoadAmount)
  202. // in case that the tx is a L1Tx>DepositTransfer
  203. if transfer {
  204. accReceiver, err := s.GetAccount(tx.ToIdx)
  205. if err != nil {
  206. return err
  207. }
  208. // substract amount to the sender
  209. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  210. // add amount to the receiver
  211. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  212. // update receiver account in localStateDB
  213. _, err = s.UpdateAccount(tx.ToIdx, accReceiver)
  214. if err != nil {
  215. return err
  216. }
  217. }
  218. // update sender account in localStateDB
  219. _, err = s.UpdateAccount(tx.FromIdx, accSender)
  220. if err != nil {
  221. return err
  222. }
  223. return nil
  224. }
  225. // applyTransfer updates the balance & nonce in the account of the sender, and
  226. // the balance in the account of the receiver
  227. func (s *StateDB) applyTransfer(tx *common.Tx) error {
  228. // get sender and receiver accounts from localStateDB
  229. accSender, err := s.GetAccount(tx.FromIdx)
  230. if err != nil {
  231. return err
  232. }
  233. accReceiver, err := s.GetAccount(tx.ToIdx)
  234. if err != nil {
  235. return err
  236. }
  237. // increment nonce
  238. accSender.Nonce++
  239. // substract amount to the sender
  240. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  241. // add amount to the receiver
  242. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  243. // update receiver account in localStateDB
  244. _, err = s.UpdateAccount(tx.ToIdx, accReceiver)
  245. if err != nil {
  246. return err
  247. }
  248. // update sender account in localStateDB
  249. _, err = s.UpdateAccount(tx.FromIdx, accSender)
  250. if err != nil {
  251. return err
  252. }
  253. return nil
  254. }
  255. func (s *StateDB) applyExit(exitTree *merkletree.MerkleTree, tx *common.Tx) (*common.Account, error) {
  256. // 0. substract tx.Amount from current Account in StateMT
  257. // add the tx.Amount into the Account (tx.FromIdx) in the ExitMT
  258. acc, err := s.GetAccount(tx.FromIdx)
  259. if err != nil {
  260. return nil, err
  261. }
  262. acc.Balance = new(big.Int).Sub(acc.Balance, tx.Amount)
  263. _, err = s.UpdateAccount(tx.FromIdx, acc)
  264. if err != nil {
  265. return nil, err
  266. }
  267. exitAccount, err := getAccountInTreeDB(exitTree.DB(), tx.FromIdx)
  268. if err == db.ErrNotFound {
  269. // 1a. if idx does not exist in exitTree:
  270. // add new leaf 'ExitTreeLeaf', where ExitTreeLeaf.Balance = exitAmount (exitAmount=tx.Amount)
  271. exitAccount := &common.Account{
  272. TokenID: acc.TokenID,
  273. Nonce: common.Nonce(1),
  274. Balance: tx.Amount,
  275. PublicKey: acc.PublicKey,
  276. EthAddr: acc.EthAddr,
  277. }
  278. _, err = createAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  279. return exitAccount, err
  280. } else if err != nil {
  281. return exitAccount, err
  282. }
  283. // 1b. if idx already exist in exitTree:
  284. // update account, where account.Balance += exitAmount
  285. exitAccount.Balance = new(big.Int).Add(exitAccount.Balance, tx.Amount)
  286. _, err = updateAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  287. return exitAccount, err
  288. }
  289. // getIdx returns the stored Idx from the localStateDB, which is the last Idx
  290. // used for an Account in the localStateDB.
  291. func (s *StateDB) getIdx() (common.Idx, error) {
  292. idxBytes, err := s.DB().Get(keyidx)
  293. if err == db.ErrNotFound {
  294. return 0, nil
  295. }
  296. if err != nil {
  297. return 0, err
  298. }
  299. return common.IdxFromBytes(idxBytes[:4])
  300. }
  301. // setIdx stores Idx in the localStateDB
  302. func (s *StateDB) setIdx(idx common.Idx) error {
  303. tx, err := s.DB().NewTx()
  304. if err != nil {
  305. return err
  306. }
  307. tx.Put(keyidx, idx.Bytes())
  308. if err := tx.Commit(); err != nil {
  309. return err
  310. }
  311. return nil
  312. }