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.

105 lines
3.1 KiB

  1. package debugapi
  2. import (
  3. "crypto/ecdsa"
  4. "fmt"
  5. "io/ioutil"
  6. "math/big"
  7. "net/http"
  8. "strconv"
  9. "testing"
  10. "github.com/dghubble/sling"
  11. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  12. "github.com/hermeznetwork/hermez-node/common"
  13. "github.com/hermeznetwork/hermez-node/db/statedb"
  14. "github.com/iden3/go-iden3-crypto/babyjub"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. "golang.org/x/net/context"
  18. )
  19. func newAccount(t *testing.T, i int) *common.Account {
  20. var sk babyjub.PrivateKey
  21. copy(sk[:], []byte(strconv.Itoa(i))) // only for testing
  22. pk := sk.Public()
  23. var key ecdsa.PrivateKey
  24. key.D = big.NewInt(int64(i + 1)) // only for testing
  25. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  26. key.Curve = ethCrypto.S256()
  27. address := ethCrypto.PubkeyToAddress(key.PublicKey)
  28. return &common.Account{
  29. Idx: common.Idx(256 + i),
  30. TokenID: common.TokenID(i),
  31. Nonce: common.Nonce(i),
  32. Balance: big.NewInt(1000),
  33. BJJ: pk.Compress(),
  34. EthAddr: address,
  35. }
  36. }
  37. func TestDebugAPI(t *testing.T) {
  38. dir, err := ioutil.TempDir("", "tmpdb")
  39. require.Nil(t, err)
  40. sdb, err := statedb.NewStateDB(statedb.Config{Path: dir, Keep: 128, Type: statedb.TypeSynchronizer, NLevels: 32})
  41. require.Nil(t, err)
  42. err = sdb.MakeCheckpoint() // Make a checkpoint to increment the batchNum
  43. require.Nil(t, err)
  44. addr := "localhost:12345"
  45. // We won't test the sync/stats endpoint, so we can se the Syncrhonizer to nil
  46. debugAPI := NewDebugAPI(addr, sdb, nil)
  47. ctx, cancel := context.WithCancel(context.Background())
  48. go func() {
  49. err := debugAPI.Run(ctx)
  50. require.Nil(t, err)
  51. }()
  52. var accounts []common.Account
  53. for i := 0; i < 16; i++ {
  54. account := newAccount(t, i)
  55. accounts = append(accounts, *account)
  56. _, err = sdb.CreateAccount(account.Idx, account)
  57. require.Nil(t, err)
  58. }
  59. // Make a checkpoint (batchNum 2) to make the accounts available in Last
  60. err = sdb.MakeCheckpoint()
  61. require.Nil(t, err)
  62. url := fmt.Sprintf("http://%v/debug/", addr)
  63. var batchNum common.BatchNum
  64. req, err := sling.New().Get(url).Path("sdb/batchnum").ReceiveSuccess(&batchNum)
  65. require.Equal(t, http.StatusOK, req.StatusCode)
  66. require.Nil(t, err)
  67. assert.Equal(t, common.BatchNum(2), batchNum)
  68. var mtroot *big.Int
  69. req, err = sling.New().Get(url).Path("sdb/mtroot").ReceiveSuccess(&mtroot)
  70. require.Equal(t, http.StatusOK, req.StatusCode)
  71. require.Nil(t, err)
  72. // Testing against a hardcoded value obtained by running the test and
  73. // printing the value previously.
  74. assert.Equal(t, "21765339739823365993496282904432398015268846626944509989242908567129545640185",
  75. mtroot.String())
  76. var accountAPI common.Account
  77. req, err = sling.New().Get(url).
  78. Path(fmt.Sprintf("sdb/accounts/%v", accounts[0].Idx)).
  79. ReceiveSuccess(&accountAPI)
  80. require.Equal(t, http.StatusOK, req.StatusCode)
  81. require.Nil(t, err)
  82. assert.Equal(t, accounts[0], accountAPI)
  83. var accountsAPI []common.Account
  84. req, err = sling.New().Get(url).Path("sdb/accounts").ReceiveSuccess(&accountsAPI)
  85. require.Equal(t, http.StatusOK, req.StatusCode)
  86. require.Nil(t, err)
  87. assert.Equal(t, accounts, accountsAPI)
  88. cancel()
  89. }