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.

122 lines
3.4 KiB

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