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.

102 lines
2.9 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. PublicKey: 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(dir, statedb.TypeSynchronizer, 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. url := fmt.Sprintf("http://%v/debug/", addr)
  60. var batchNum common.BatchNum
  61. req, err := sling.New().Get(url).Path("sdb/batchnum").ReceiveSuccess(&batchNum)
  62. require.Equal(t, http.StatusOK, req.StatusCode)
  63. require.Nil(t, err)
  64. assert.Equal(t, common.BatchNum(1), batchNum)
  65. var mtroot *big.Int
  66. req, err = sling.New().Get(url).Path("sdb/mtroot").ReceiveSuccess(&mtroot)
  67. require.Equal(t, http.StatusOK, req.StatusCode)
  68. require.Nil(t, err)
  69. // Testing against a hardcoded value obtained by running the test and
  70. // printing the value previously.
  71. assert.Equal(t, "21765339739823365993496282904432398015268846626944509989242908567129545640185",
  72. mtroot.String())
  73. var accountAPI common.Account
  74. req, err = sling.New().Get(url).
  75. Path(fmt.Sprintf("sdb/accounts/%v", accounts[0].Idx)).
  76. ReceiveSuccess(&accountAPI)
  77. require.Equal(t, http.StatusOK, req.StatusCode)
  78. require.Nil(t, err)
  79. assert.Equal(t, accounts[0], accountAPI)
  80. var accountsAPI []common.Account
  81. req, err = sling.New().Get(url).Path("sdb/accounts").ReceiveSuccess(&accountsAPI)
  82. require.Equal(t, http.StatusOK, req.StatusCode)
  83. require.Nil(t, err)
  84. assert.Equal(t, accounts, accountsAPI)
  85. cancel()
  86. }