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.

103 lines
3.0 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. chainID := uint16(0)
  41. sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeSynchronizer, 32, chainID)
  42. require.Nil(t, err)
  43. err = sdb.MakeCheckpoint() // Make a checkpoint to increment the batchNum
  44. require.Nil(t, err)
  45. addr := "localhost:12345"
  46. // We won't test the sync/stats endpoint, so we can se the Syncrhonizer to nil
  47. debugAPI := NewDebugAPI(addr, sdb, nil)
  48. ctx, cancel := context.WithCancel(context.Background())
  49. go func() {
  50. err := debugAPI.Run(ctx)
  51. require.Nil(t, err)
  52. }()
  53. var accounts []common.Account
  54. for i := 0; i < 16; i++ {
  55. account := newAccount(t, i)
  56. accounts = append(accounts, *account)
  57. _, err = sdb.CreateAccount(account.Idx, account)
  58. require.Nil(t, err)
  59. }
  60. url := fmt.Sprintf("http://%v/debug/", addr)
  61. var batchNum common.BatchNum
  62. req, err := sling.New().Get(url).Path("sdb/batchnum").ReceiveSuccess(&batchNum)
  63. require.Equal(t, http.StatusOK, req.StatusCode)
  64. require.Nil(t, err)
  65. assert.Equal(t, common.BatchNum(1), batchNum)
  66. var mtroot *big.Int
  67. req, err = sling.New().Get(url).Path("sdb/mtroot").ReceiveSuccess(&mtroot)
  68. require.Equal(t, http.StatusOK, req.StatusCode)
  69. require.Nil(t, err)
  70. // Testing against a hardcoded value obtained by running the test and
  71. // printing the value previously.
  72. assert.Equal(t, "21765339739823365993496282904432398015268846626944509989242908567129545640185",
  73. mtroot.String())
  74. var accountAPI common.Account
  75. req, err = sling.New().Get(url).
  76. Path(fmt.Sprintf("sdb/accounts/%v", accounts[0].Idx)).
  77. ReceiveSuccess(&accountAPI)
  78. require.Equal(t, http.StatusOK, req.StatusCode)
  79. require.Nil(t, err)
  80. assert.Equal(t, accounts[0], accountAPI)
  81. var accountsAPI []common.Account
  82. req, err = sling.New().Get(url).Path("sdb/accounts").ReceiveSuccess(&accountsAPI)
  83. require.Equal(t, http.StatusOK, req.StatusCode)
  84. require.Nil(t, err)
  85. assert.Equal(t, accounts, accountsAPI)
  86. cancel()
  87. }