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.

147 lines
4.3 KiB

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