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.

90 lines
2.8 KiB

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