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.

120 lines
3.3 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. 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, tokenID0)
  34. require.Nil(t, err)
  35. idxR, err := sdb.GetIdxByEthAddrBJJ(addr, pk, tokenID0)
  36. assert.Nil(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.Nil(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, tokenID1)
  46. assert.NotNil(t, err)
  47. // expect to fail
  48. idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk, tokenID0)
  49. assert.NotNil(t, err)
  50. assert.Equal(t, common.Idx(0), idxR)
  51. idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk2, 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, tokenID0)
  57. assert.Nil(t, err)
  58. // store smaller idx
  59. err = sdb.setIdxByEthAddrBJJ(idx3, addr, pk, tokenID0)
  60. assert.Nil(t, err)
  61. idxR, err = sdb.GetIdxByEthAddrBJJ(addr, pk, tokenID0)
  62. assert.Nil(t, err)
  63. assert.Equal(t, idx3, idxR)
  64. // by EthAddr should work
  65. idxR, err = sdb.GetIdxByEthAddr(addr, tokenID0)
  66. assert.Nil(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, tokenID0)
  70. assert.NotNil(t, err)
  71. assert.Equal(t, ErrToIdxNotFound, 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. }
  77. func TestBJJCompressedTo256BigInt(t *testing.T) {
  78. var pkComp babyjub.PublicKeyComp
  79. r := BJJCompressedTo256BigInts(pkComp)
  80. zero := big.NewInt(0)
  81. for i := 0; i < 256; i++ {
  82. assert.Equal(t, zero, r[i])
  83. }
  84. pkComp[0] = 3
  85. r = BJJCompressedTo256BigInts(pkComp)
  86. one := big.NewInt(1)
  87. for i := 0; i < 256; i++ {
  88. if i != 0 && i != 1 {
  89. assert.Equal(t, zero, r[i])
  90. } else {
  91. assert.Equal(t, one, r[i])
  92. }
  93. }
  94. pkComp[31] = 4
  95. r = BJJCompressedTo256BigInts(pkComp)
  96. for i := 0; i < 256; i++ {
  97. if i != 0 && i != 1 && i != 250 {
  98. assert.Equal(t, zero, r[i])
  99. } else {
  100. assert.Equal(t, one, r[i])
  101. }
  102. }
  103. }