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.

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