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.

198 lines
5.0 KiB

  1. package statedb
  2. import (
  3. "encoding/binary"
  4. "math/big"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/iden3/go-merkletree/db"
  7. )
  8. // KEYIDX is used as key in the db to store the current Idx
  9. var KEYIDX = []byte("idx")
  10. // ProcessPoolL2Tx process the given PoolL2Tx applying the needed updates to
  11. // the StateDB depending on the transaction Type.
  12. func (s *StateDB) ProcessPoolL2Tx(tx *common.PoolL2Tx) error {
  13. switch tx.Type {
  14. case common.TxTypeTransfer:
  15. // go to the MT account of sender and receiver, and update
  16. // balance & nonce
  17. err := s.applyTransfer(tx.Tx())
  18. if err != nil {
  19. return err
  20. }
  21. case common.TxTypeExit:
  22. // execute exit flow
  23. default:
  24. }
  25. return nil
  26. }
  27. // ProcessL1Tx process the given L1Tx applying the needed updates to the
  28. // StateDB depending on the transaction Type.
  29. func (s *StateDB) ProcessL1Tx(tx *common.L1Tx) error {
  30. switch tx.Type {
  31. case common.TxTypeForceTransfer, common.TxTypeTransfer:
  32. // go to the MT account of sender and receiver, and update balance
  33. // & nonce
  34. err := s.applyTransfer(tx.Tx())
  35. if err != nil {
  36. return err
  37. }
  38. case common.TxTypeCreateAccountDeposit:
  39. // add new account to the MT, update balance of the MT account
  40. err := s.applyCreateAccount(tx)
  41. if err != nil {
  42. return err
  43. }
  44. case common.TxTypeDeposit:
  45. // update balance of the MT account
  46. err := s.applyDeposit(tx, false)
  47. if err != nil {
  48. return err
  49. }
  50. case common.TxTypeDepositTransfer:
  51. // update balance in MT account, update balance & nonce of sender
  52. // & receiver
  53. err := s.applyDeposit(tx, true)
  54. if err != nil {
  55. return err
  56. }
  57. case common.TxTypeCreateAccountDepositTransfer:
  58. // add new account to the merkletree, update balance in MT account,
  59. // update balance & nonce of sender & receiver
  60. err := s.applyCreateAccount(tx)
  61. if err != nil {
  62. return err
  63. }
  64. err = s.applyTransfer(tx.Tx())
  65. if err != nil {
  66. return err
  67. }
  68. case common.TxTypeExit:
  69. // execute exit flow
  70. default:
  71. }
  72. return nil
  73. }
  74. // applyCreateAccount creates a new account in the account of the depositer, it
  75. // stores the deposit value
  76. func (s *StateDB) applyCreateAccount(tx *common.L1Tx) error {
  77. account := &common.Account{
  78. TokenID: tx.TokenID,
  79. Nonce: 0,
  80. Balance: tx.LoadAmount,
  81. PublicKey: tx.FromBJJ,
  82. EthAddr: tx.FromEthAddr,
  83. }
  84. _, err := s.CreateAccount(common.Idx(s.idx+1), account)
  85. if err != nil {
  86. return err
  87. }
  88. s.idx = s.idx + 1
  89. return s.setIdx(s.idx)
  90. }
  91. // applyDeposit updates the balance in the account of the depositer, if
  92. // andTransfer parameter is set to true, the method will also apply the
  93. // Transfer of the L1Tx/DepositTransfer
  94. func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
  95. // deposit the tx.LoadAmount into the sender account
  96. accSender, err := s.GetAccount(tx.FromIdx)
  97. if err != nil {
  98. return err
  99. }
  100. accSender.Balance = new(big.Int).Add(accSender.Balance, tx.LoadAmount)
  101. // in case that the tx is a L1Tx>DepositTransfer
  102. if transfer {
  103. accReceiver, err := s.GetAccount(tx.ToIdx)
  104. if err != nil {
  105. return err
  106. }
  107. // substract amount to the sender
  108. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  109. // add amount to the receiver
  110. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  111. // update receiver account in localStateDB
  112. _, err = s.UpdateAccount(tx.ToIdx, accReceiver)
  113. if err != nil {
  114. return err
  115. }
  116. }
  117. // update sender account in localStateDB
  118. _, err = s.UpdateAccount(tx.FromIdx, accSender)
  119. if err != nil {
  120. return err
  121. }
  122. return nil
  123. }
  124. // applyTransfer updates the balance & nonce in the account of the sender, and
  125. // the balance in the account of the receiver
  126. func (s *StateDB) applyTransfer(tx *common.Tx) error {
  127. // get sender and receiver accounts from localStateDB
  128. accSender, err := s.GetAccount(tx.FromIdx)
  129. if err != nil {
  130. return err
  131. }
  132. accReceiver, err := s.GetAccount(tx.ToIdx)
  133. if err != nil {
  134. return err
  135. }
  136. // increment nonce
  137. accSender.Nonce++
  138. // substract amount to the sender
  139. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  140. // add amount to the receiver
  141. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  142. // update receiver account in localStateDB
  143. _, err = s.UpdateAccount(tx.ToIdx, accReceiver)
  144. if err != nil {
  145. return err
  146. }
  147. // update sender account in localStateDB
  148. _, err = s.UpdateAccount(tx.FromIdx, accSender)
  149. if err != nil {
  150. return err
  151. }
  152. return nil
  153. }
  154. // getIdx returns the stored Idx from the localStateDB, which is the last Idx
  155. // used for an Account in the localStateDB.
  156. func (s *StateDB) getIdx() (uint64, error) {
  157. idxBytes, err := s.DB().Get(KEYIDX)
  158. if err == db.ErrNotFound {
  159. return 0, nil
  160. }
  161. if err != nil {
  162. return 0, err
  163. }
  164. idx := binary.LittleEndian.Uint64(idxBytes[:8])
  165. return idx, nil
  166. }
  167. // setIdx stores Idx in the localStateDB
  168. func (s *StateDB) setIdx(idx uint64) error {
  169. tx, err := s.DB().NewTx()
  170. if err != nil {
  171. return err
  172. }
  173. var idxBytes [8]byte
  174. binary.LittleEndian.PutUint64(idxBytes[:], idx)
  175. tx.Put(KEYIDX, idxBytes[:])
  176. if err := tx.Commit(); err != nil {
  177. return err
  178. }
  179. return nil
  180. }