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.

111 lines
2.8 KiB

  1. package statedb
  2. import (
  3. "io/ioutil"
  4. "math/big"
  5. "os"
  6. "testing"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/hermeznetwork/hermez-node/common"
  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.Nil(t, err)
  16. defer assert.Nil(t, os.RemoveAll(dir))
  17. sdb, err := NewStateDB(dir, TypeTxSelector, 0)
  18. assert.Nil(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. // store the keys for idx by Addr & BJJ
  31. err = sdb.setIdxByEthAddrBJJ(idx, addr, pk)
  32. require.Nil(t, err)
  33. idxR, err := sdb.GetIdxByEthAddrBJJ(addr, pk)
  34. assert.Nil(t, err)
  35. assert.Equal(t, idx, idxR)
  36. // expect error when getting only by EthAddr, as value does not exist
  37. // in the db for only EthAddr
  38. _, err = sdb.GetIdxByEthAddr(addr)
  39. assert.Nil(t, err)
  40. _, err = sdb.GetIdxByEthAddr(addr2)
  41. assert.NotNil(t, err)
  42. // expect to fail
  43. idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk)
  44. assert.NotNil(t, err)
  45. assert.Equal(t, common.Idx(0), idxR)
  46. idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk2)
  47. assert.NotNil(t, err)
  48. assert.Equal(t, common.Idx(0), idxR)
  49. // try to store bigger idx, will not affect as already exist a smaller
  50. // Idx for that Addr & BJJ
  51. err = sdb.setIdxByEthAddrBJJ(idx2, addr, pk)
  52. assert.Nil(t, err)
  53. // store smaller idx
  54. err = sdb.setIdxByEthAddrBJJ(idx3, addr, pk)
  55. assert.Nil(t, err)
  56. idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk)
  57. assert.Nil(t, err)
  58. assert.Equal(t, idx3, idxR)
  59. // by EthAddr should work
  60. idxR, err = sdb.GetIdxByEthAddr(addr)
  61. assert.Nil(t, err)
  62. assert.Equal(t, idx3, idxR)
  63. // expect error when trying to get Idx by addr2 & pk2
  64. idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk2)
  65. assert.NotNil(t, err)
  66. assert.Equal(t, ErrToIdxNotFound, err)
  67. assert.Equal(t, common.Idx(0), idxR)
  68. }
  69. func TestBJJCompressedTo256BigInt(t *testing.T) {
  70. var pkComp babyjub.PublicKeyComp
  71. r := BJJCompressedTo256BigInts(pkComp)
  72. zero := big.NewInt(0)
  73. for i := 0; i < 256; i++ {
  74. assert.Equal(t, zero, r[i])
  75. }
  76. pkComp[0] = 3
  77. r = BJJCompressedTo256BigInts(pkComp)
  78. one := big.NewInt(1)
  79. for i := 0; i < 256; i++ {
  80. if i != 0 && i != 1 {
  81. assert.Equal(t, zero, r[i])
  82. } else {
  83. assert.Equal(t, one, r[i])
  84. }
  85. }
  86. pkComp[31] = 4
  87. r = BJJCompressedTo256BigInts(pkComp)
  88. for i := 0; i < 256; i++ {
  89. if i != 0 && i != 1 && i != 250 {
  90. assert.Equal(t, zero, r[i])
  91. } else {
  92. assert.Equal(t, one, r[i])
  93. }
  94. }
  95. }