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.

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