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.

92 lines
3.0 KiB

  1. package statedb
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/hermeznetwork/tracerr"
  10. "github.com/iden3/go-iden3-crypto/babyjub"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestGetIdx(t *testing.T) {
  15. dir, err := ioutil.TempDir("", "tmpdb")
  16. require.NoError(t, err)
  17. defer assert.NoError(t, os.RemoveAll(dir))
  18. sdb, err := NewStateDB(dir, 128, TypeTxSelector, 0)
  19. assert.NoError(t, err)
  20. var sk babyjub.PrivateKey
  21. copy(sk[:], []byte("1234")) // only for testing
  22. pk := sk.Public()
  23. var sk2 babyjub.PrivateKey
  24. copy(sk2[:], []byte("12345")) // only for testing
  25. pk2 := sk2.Public()
  26. addr := ethCommon.HexToAddress("0x74E803744B7EEFc272E852f89a05D41515d431f2")
  27. addr2 := ethCommon.HexToAddress("0x54A0706531cEa2ee8F09bAd22f604e377bb56948")
  28. idx := common.Idx(1234)
  29. idx2 := common.Idx(12345)
  30. idx3 := common.Idx(1233)
  31. tokenID0 := common.TokenID(0)
  32. tokenID1 := common.TokenID(1)
  33. // store the keys for idx by Addr & BJJ
  34. err = sdb.setIdxByEthAddrBJJ(idx, addr, pk.Compress(), tokenID0)
  35. require.NoError(t, err)
  36. idxR, err := sdb.GetIdxByEthAddrBJJ(addr, pk.Compress(), tokenID0)
  37. assert.NoError(t, err)
  38. assert.Equal(t, idx, idxR)
  39. // expect error when getting only by EthAddr, as value does not exist
  40. // in the db for only EthAddr
  41. _, err = sdb.GetIdxByEthAddr(addr, tokenID0)
  42. assert.NoError(t, err)
  43. _, err = sdb.GetIdxByEthAddr(addr2, tokenID0)
  44. assert.NotNil(t, err)
  45. // expect error when getting by EthAddr and BJJ, but for another TokenID
  46. _, err = sdb.GetIdxByEthAddrBJJ(addr, pk.Compress(), tokenID1)
  47. assert.NotNil(t, err)
  48. // expect to fail
  49. idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk.Compress(), tokenID0)
  50. assert.NotNil(t, err)
  51. assert.Equal(t, common.Idx(0), idxR)
  52. idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk2.Compress(), tokenID0)
  53. assert.NotNil(t, err)
  54. assert.Equal(t, common.Idx(0), idxR)
  55. // try to store bigger idx, will not affect as already exist a smaller
  56. // Idx for that Addr & BJJ
  57. err = sdb.setIdxByEthAddrBJJ(idx2, addr, pk.Compress(), tokenID0)
  58. assert.NoError(t, err)
  59. // store smaller idx
  60. err = sdb.setIdxByEthAddrBJJ(idx3, addr, pk.Compress(), tokenID0)
  61. assert.NoError(t, err)
  62. idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk.Compress(), tokenID0)
  63. assert.NoError(t, err)
  64. assert.Equal(t, idx3, idxR)
  65. // by EthAddr should work
  66. idxR, err = sdb.GetIdxByEthAddr(addr, tokenID0)
  67. assert.NoError(t, err)
  68. assert.Equal(t, idx3, idxR)
  69. // expect error when trying to get Idx by addr2 & pk2
  70. idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk2.Compress(), tokenID0)
  71. assert.NotNil(t, err)
  72. expectedErr := fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", ErrToIdxNotFound, addr2.Hex(), pk2, tokenID0)
  73. assert.Equal(t, expectedErr, tracerr.Unwrap(err))
  74. assert.Equal(t, common.Idx(0), idxR)
  75. // expect error when trying to get Idx by addr with not used TokenID
  76. _, err = sdb.GetIdxByEthAddr(addr, tokenID1)
  77. assert.NotNil(t, err)
  78. }