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.

101 lines
2.8 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,
  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. debugAPI := NewDebugAPI(addr, sdb)
  46. ctx, cancel := context.WithCancel(context.Background())
  47. go func() {
  48. err := debugAPI.Run(ctx)
  49. require.Nil(t, err)
  50. }()
  51. var accounts []common.Account
  52. for i := 0; i < 16; i++ {
  53. account := newAccount(t, i)
  54. accounts = append(accounts, *account)
  55. _, err = sdb.CreateAccount(account.Idx, account)
  56. require.Nil(t, err)
  57. }
  58. url := fmt.Sprintf("http://%v/debug/", addr)
  59. var batchNum common.BatchNum
  60. req, err := sling.New().Get(url).Path("sdb/batchnum").ReceiveSuccess(&batchNum)
  61. require.Equal(t, http.StatusOK, req.StatusCode)
  62. require.Nil(t, err)
  63. assert.Equal(t, common.BatchNum(1), batchNum)
  64. var mtroot *big.Int
  65. req, err = sling.New().Get(url).Path("sdb/mtroot").ReceiveSuccess(&mtroot)
  66. require.Equal(t, http.StatusOK, req.StatusCode)
  67. require.Nil(t, err)
  68. // Testing against a hardcoded value obtained by running the test and
  69. // printing the value previously.
  70. assert.Equal(t, "8902613552504893273500019895709436294962812188236308621387152512232191202510",
  71. mtroot.String())
  72. var accountAPI common.Account
  73. req, err = sling.New().Get(url).
  74. Path(fmt.Sprintf("sdb/accounts/%v", accounts[0].Idx)).
  75. ReceiveSuccess(&accountAPI)
  76. require.Equal(t, http.StatusOK, req.StatusCode)
  77. require.Nil(t, err)
  78. assert.Equal(t, accounts[0], accountAPI)
  79. var accountsAPI []common.Account
  80. req, err = sling.New().Get(url).Path("sdb/accounts").ReceiveSuccess(&accountsAPI)
  81. require.Equal(t, http.StatusOK, req.StatusCode)
  82. require.Nil(t, err)
  83. assert.Equal(t, accounts, accountsAPI)
  84. cancel()
  85. }