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.

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