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.

109 lines
2.8 KiB

  1. package statedb
  2. import (
  3. "io/ioutil"
  4. "math/big"
  5. "testing"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestGetIdx(t *testing.T) {
  13. dir, err := ioutil.TempDir("", "tmpdb")
  14. require.Nil(t, err)
  15. sdb, err := NewStateDB(dir, TypeTxSelector, 0)
  16. assert.Nil(t, err)
  17. var sk babyjub.PrivateKey
  18. copy(sk[:], []byte("1234")) // only for testing
  19. pk := sk.Public()
  20. var sk2 babyjub.PrivateKey
  21. copy(sk2[:], []byte("12345")) // only for testing
  22. pk2 := sk2.Public()
  23. addr := ethCommon.HexToAddress("0x74E803744B7EEFc272E852f89a05D41515d431f2")
  24. addr2 := ethCommon.HexToAddress("0x54A0706531cEa2ee8F09bAd22f604e377bb56948")
  25. idx := common.Idx(1234)
  26. idx2 := common.Idx(12345)
  27. idx3 := common.Idx(1233)
  28. // store the keys for idx by Addr & BJJ
  29. err = sdb.setIdxByEthAddrBJJ(idx, addr, pk)
  30. require.Nil(t, err)
  31. idxR, err := sdb.GetIdxByEthAddrBJJ(addr, pk)
  32. assert.Nil(t, err)
  33. assert.Equal(t, idx, idxR)
  34. // expect error when getting only by EthAddr, as value does not exist
  35. // in the db for only EthAddr
  36. _, err = sdb.GetIdxByEthAddr(addr)
  37. assert.Nil(t, err)
  38. _, err = sdb.GetIdxByEthAddr(addr2)
  39. assert.NotNil(t, err)
  40. // expect to fail
  41. idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk)
  42. assert.NotNil(t, err)
  43. assert.Equal(t, common.Idx(0), idxR)
  44. idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk2)
  45. assert.NotNil(t, err)
  46. assert.Equal(t, common.Idx(0), idxR)
  47. // try to store bigger idx, will not affect as already exist a smaller
  48. // Idx for that Addr & BJJ
  49. err = sdb.setIdxByEthAddrBJJ(idx2, addr, pk)
  50. assert.Nil(t, err)
  51. // store smaller idx
  52. err = sdb.setIdxByEthAddrBJJ(idx3, addr, pk)
  53. assert.Nil(t, err)
  54. idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk)
  55. assert.Nil(t, err)
  56. assert.Equal(t, idx3, idxR)
  57. // by EthAddr should work
  58. idxR, err = sdb.GetIdxByEthAddr(addr)
  59. assert.Nil(t, err)
  60. assert.Equal(t, idx3, idxR)
  61. // expect error when trying to get Idx by addr2 & pk2
  62. idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk2)
  63. assert.NotNil(t, err)
  64. assert.Equal(t, ErrToIdxNotFound, err)
  65. assert.Equal(t, common.Idx(0), idxR)
  66. }
  67. func TestBJJCompressedTo256BigInt(t *testing.T) {
  68. var pkComp babyjub.PublicKeyComp
  69. r := BJJCompressedTo256BigInts(pkComp)
  70. zero := big.NewInt(0)
  71. for i := 0; i < 256; i++ {
  72. assert.Equal(t, zero, r[i])
  73. }
  74. pkComp[0] = 3
  75. r = BJJCompressedTo256BigInts(pkComp)
  76. one := big.NewInt(1)
  77. for i := 0; i < 256; i++ {
  78. if i != 0 && i != 1 {
  79. assert.Equal(t, zero, r[i])
  80. } else {
  81. assert.Equal(t, one, r[i])
  82. }
  83. }
  84. pkComp[31] = 4
  85. r = BJJCompressedTo256BigInts(pkComp)
  86. for i := 0; i < 256; i++ {
  87. if i != 0 && i != 1 && i != 250 {
  88. assert.Equal(t, zero, r[i])
  89. } else {
  90. assert.Equal(t, one, r[i])
  91. }
  92. }
  93. }