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.

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