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.

155 lines
5.3 KiB

  1. package statedb
  2. import (
  3. "bytes"
  4. "fmt"
  5. ethCommon "github.com/ethereum/go-ethereum/common"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/hermez-node/log"
  8. "github.com/hermeznetwork/tracerr"
  9. "github.com/iden3/go-iden3-crypto/babyjub"
  10. "github.com/iden3/go-merkletree/db"
  11. )
  12. func concatEthAddrTokenID(addr ethCommon.Address, tokenID common.TokenID) []byte {
  13. var b []byte
  14. b = append(b, addr.Bytes()...)
  15. b = append(b[:], tokenID.Bytes()[:]...)
  16. return b
  17. }
  18. func concatEthAddrBJJTokenID(addr ethCommon.Address, pk babyjub.PublicKeyComp,
  19. tokenID common.TokenID) []byte {
  20. pkComp := pk
  21. var b []byte
  22. b = append(b, addr.Bytes()...)
  23. b = append(b[:], pkComp[:]...)
  24. b = append(b[:], tokenID.Bytes()[:]...)
  25. return b
  26. }
  27. // setIdxByEthAddrBJJ stores the given Idx in the StateDB as follows:
  28. // - key: Eth Address, value: idx
  29. // - key: EthAddr & BabyJubJub PublicKey Compressed, value: idx
  30. // If Idx already exist for the given EthAddr & BJJ, the remaining Idx will be
  31. // always the smallest one.
  32. func (s *StateDB) setIdxByEthAddrBJJ(idx common.Idx, addr ethCommon.Address,
  33. pk babyjub.PublicKeyComp, tokenID common.TokenID) error {
  34. oldIdx, err := s.GetIdxByEthAddrBJJ(addr, pk, tokenID)
  35. if err == nil {
  36. // EthAddr & BJJ already have an Idx
  37. // check which Idx is smaller
  38. // if new idx is smaller, store the new one
  39. // if new idx is bigger, don't store and return, as the used one will be the old
  40. if idx >= oldIdx {
  41. log.Debug("StateDB.setIdxByEthAddrBJJ: Idx not stored because there " +
  42. "already exist a smaller Idx for the given EthAddr & BJJ")
  43. return nil
  44. }
  45. }
  46. // store idx for EthAddr & BJJ assuming that EthAddr & BJJ still don't
  47. // have an Idx stored in the DB, and if so, the already stored Idx is
  48. // bigger than the given one, so should be updated to the new one
  49. // (smaller)
  50. tx, err := s.db.DB().NewTx()
  51. if err != nil {
  52. return tracerr.Wrap(err)
  53. }
  54. idxBytes, err := idx.Bytes()
  55. if err != nil {
  56. return tracerr.Wrap(err)
  57. }
  58. // store Addr&BJJ-idx
  59. k := concatEthAddrBJJTokenID(addr, pk, tokenID)
  60. err = tx.Put(append(PrefixKeyAddrBJJ, k...), idxBytes[:])
  61. if err != nil {
  62. return tracerr.Wrap(err)
  63. }
  64. // store Addr-idx
  65. k = concatEthAddrTokenID(addr, tokenID)
  66. err = tx.Put(append(PrefixKeyAddr, k...), idxBytes[:])
  67. if err != nil {
  68. return tracerr.Wrap(err)
  69. }
  70. err = tx.Commit()
  71. if err != nil {
  72. return tracerr.Wrap(err)
  73. }
  74. return nil
  75. }
  76. // GetIdxByEthAddr returns the smallest Idx in the StateDB for the given
  77. // Ethereum Address. Will return common.Idx(0) and error in case that Idx is
  78. // not found in the StateDB.
  79. func (s *StateDB) GetIdxByEthAddr(addr ethCommon.Address, tokenID common.TokenID) (common.Idx,
  80. error) {
  81. k := concatEthAddrTokenID(addr, tokenID)
  82. b, err := s.db.DB().Get(append(PrefixKeyAddr, k...))
  83. if err != nil {
  84. return common.Idx(0), tracerr.Wrap(fmt.Errorf("GetIdxByEthAddr: %s: ToEthAddr: %s, TokenID: %d",
  85. ErrIdxNotFound, addr.Hex(), tokenID))
  86. }
  87. idx, err := common.IdxFromBytes(b)
  88. if err != nil {
  89. return common.Idx(0), tracerr.Wrap(fmt.Errorf("GetIdxByEthAddr: %s: ToEthAddr: %s, TokenID: %d",
  90. err, addr.Hex(), tokenID))
  91. }
  92. return idx, nil
  93. }
  94. // GetIdxByEthAddrBJJ returns the smallest Idx in the StateDB for the given
  95. // Ethereum Address AND the given BabyJubJub PublicKey. If `addr` is the zero
  96. // address, it's ignored in the query. If `pk` is nil, it's ignored in the
  97. // query. Will return common.Idx(0) and error in case that Idx is not found in
  98. // the StateDB.
  99. func (s *StateDB) GetIdxByEthAddrBJJ(addr ethCommon.Address, pk babyjub.PublicKeyComp,
  100. tokenID common.TokenID) (common.Idx, error) {
  101. if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) && pk == common.EmptyBJJComp {
  102. // ToEthAddr
  103. // case ToEthAddr!=0 && ToBJJ=0
  104. return s.GetIdxByEthAddr(addr, tokenID)
  105. } else if !bytes.Equal(addr.Bytes(), common.EmptyAddr.Bytes()) &&
  106. pk != common.EmptyBJJComp {
  107. // case ToEthAddr!=0 && ToBJJ!=0
  108. k := concatEthAddrBJJTokenID(addr, pk, tokenID)
  109. b, err := s.db.DB().Get(append(PrefixKeyAddrBJJ, k...))
  110. if tracerr.Unwrap(err) == db.ErrNotFound {
  111. // return the error (ErrNotFound), so can be traced at upper layers
  112. return common.Idx(0), tracerr.Wrap(ErrIdxNotFound)
  113. } else if err != nil {
  114. return common.Idx(0),
  115. tracerr.Wrap(fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d",
  116. ErrIdxNotFound, addr.Hex(), pk, tokenID))
  117. }
  118. idx, err := common.IdxFromBytes(b)
  119. if err != nil {
  120. return common.Idx(0),
  121. tracerr.Wrap(fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d",
  122. err, addr.Hex(), pk, tokenID))
  123. }
  124. return idx, nil
  125. }
  126. // rest of cases (included case ToEthAddr==0) are not possible
  127. return common.Idx(0),
  128. tracerr.Wrap(
  129. fmt.Errorf("GetIdxByEthAddrBJJ: Not found, %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d",
  130. ErrGetIdxNoCase, addr.Hex(), pk, tokenID))
  131. }
  132. // GetTokenIDsFromIdxs returns a map containing the common.TokenID with its
  133. // respective common.Idx for a given slice of common.Idx
  134. func (s *StateDB) GetTokenIDsFromIdxs(idxs []common.Idx) (map[common.TokenID]common.Idx, error) {
  135. m := make(map[common.TokenID]common.Idx)
  136. for i := 0; i < len(idxs); i++ {
  137. a, err := s.GetAccount(idxs[i])
  138. if err != nil {
  139. return nil,
  140. tracerr.Wrap(fmt.Errorf("GetTokenIDsFromIdxs error on GetAccount with Idx==%d: %s",
  141. idxs[i], err.Error()))
  142. }
  143. m[a.TokenID] = idxs[i]
  144. }
  145. return m, nil
  146. }