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.

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