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.

195 lines
4.9 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. // substract amount to the sender
  137. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  138. // add amount to the receiver
  139. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  140. // update receiver account in localStateDB
  141. err = s.UpdateAccount(tx.ToIdx, accReceiver)
  142. if err != nil {
  143. return err
  144. }
  145. // update sender account in localStateDB
  146. err = s.UpdateAccount(tx.FromIdx, accSender)
  147. if err != nil {
  148. return err
  149. }
  150. return nil
  151. }
  152. // getIdx returns the stored Idx from the localStateDB, which is the last Idx
  153. // used for an Account in the localStateDB.
  154. func (s *StateDB) getIdx() (uint64, error) {
  155. idxBytes, err := s.DB().Get(KEYIDX)
  156. if err == db.ErrNotFound {
  157. return 0, nil
  158. }
  159. if err != nil {
  160. return 0, err
  161. }
  162. idx := binary.LittleEndian.Uint64(idxBytes[:8])
  163. return idx, nil
  164. }
  165. // setIdx stores Idx in the localStateDB
  166. func (s *StateDB) setIdx(idx uint64) error {
  167. tx, err := s.DB().NewTx()
  168. if err != nil {
  169. return err
  170. }
  171. var idxBytes [8]byte
  172. binary.LittleEndian.PutUint64(idxBytes[:], idx)
  173. tx.Put(KEYIDX, idxBytes[:])
  174. if err := tx.Commit(); err != nil {
  175. return err
  176. }
  177. return nil
  178. }