arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/debugapi/debugapi.go
test/debugapi/debugapi.goRaw
  1package debugapi
  2
  3import (
  4	"context"
  5	"net/http"
  6	"time"
  7
  8	"github.com/gin-contrib/cors"
  9	"github.com/gin-gonic/gin"
 10	"github.com/hermeznetwork/hermez-node/common"
 11	"github.com/hermeznetwork/hermez-node/db/statedb"
 12	"github.com/hermeznetwork/hermez-node/log"
 13)
 14
 15func handleNoRoute(c *gin.Context) {
 16	c.JSON(http.StatusNotFound, gin.H{
 17		"error": "404 page not found",
 18	})
 19}
 20
 21type errorMsg struct {
 22	Message string
 23}
 24
 25func badReq(err error, c *gin.Context) {
 26	log.Errorw("Bad request", "err", err)
 27	c.JSON(http.StatusBadRequest, errorMsg{
 28		Message: err.Error(),
 29	})
 30}
 31
 32// DebugAPI is an http API with debugging endpoints
 33type DebugAPI struct {
 34	addr    string
 35	stateDB *statedb.StateDB // synchronizer statedb
 36}
 37
 38// NewDebugAPI creates a new DebugAPI
 39func NewDebugAPI(addr string, stateDB *statedb.StateDB) *DebugAPI {
 40	return &DebugAPI{
 41		stateDB: stateDB,
 42		addr:    addr,
 43	}
 44}
 45
 46func (a *DebugAPI) handleAccount(c *gin.Context) {
 47	uri := struct {
 48		Idx uint32
 49	}{}
 50	if err := c.ShouldBindUri(&uri); err != nil {
 51		badReq(err, c)
 52		return
 53	}
 54	account, err := a.stateDB.GetAccount(common.Idx(uri.Idx))
 55	if err != nil {
 56		badReq(err, c)
 57		return
 58	}
 59	c.JSON(http.StatusOK, account)
 60}
 61
 62func (a *DebugAPI) handleAccounts(c *gin.Context) {
 63	accounts, err := a.stateDB.GetAccounts()
 64	if err != nil {
 65		badReq(err, c)
 66		return
 67	}
 68	c.JSON(http.StatusOK, accounts)
 69}
 70
 71func (a *DebugAPI) handleCurrentBatch(c *gin.Context) {
 72	batchNum, err := a.stateDB.GetCurrentBatch()
 73	if err != nil {
 74		badReq(err, c)
 75		return
 76	}
 77	c.JSON(http.StatusOK, batchNum)
 78}
 79
 80func (a *DebugAPI) handleMTRoot(c *gin.Context) {
 81	root := a.stateDB.MTGetRoot()
 82	c.JSON(http.StatusOK, root)
 83}
 84
 85// Run starts the http server of the DebugAPI.  To stop it, pass a context with
 86// cancelation (see `debugapi_test.go` for an example).
 87func (a *DebugAPI) Run(ctx context.Context) error {
 88	api := gin.Default()
 89	api.NoRoute(handleNoRoute)
 90	api.Use(cors.Default())
 91	debugAPI := api.Group("/debug")
 92
 93	debugAPI.GET("sdb/batchnum", a.handleCurrentBatch)
 94	debugAPI.GET("sdb/mtroot", a.handleMTRoot)
 95	// Accounts returned by these endpoints will always have BatchNum = 0,
 96	// because the stateDB doesn't store the BatchNum in which an account
 97	// is created.
 98	debugAPI.GET("sdb/accounts", a.handleAccounts)
 99	debugAPI.GET("sdb/accounts/:Idx", a.handleAccount)
100
101	debugAPIServer := &http.Server{
102		Addr:    a.addr,
103		Handler: api,
104		// Use some hardcoded numberes that are suitable for testing
105		ReadTimeout:    30 * time.Second, //nolint:gomnd
106		WriteTimeout:   30 * time.Second, //nolint:gomnd
107		MaxHeaderBytes: 1 << 20,          //nolint:gomnd
108	}
109	go func() {
110		log.Infof("DebugAPI is ready at %v", a.addr)
111		if err := debugAPIServer.ListenAndServe(); err != nil &&
112			err != http.ErrServerClosed {
113			log.Fatalf("Listen: %s\n", err)
114		}
115	}()
116
117	<-ctx.Done()
118	log.Info("Stopping DebugAPI...")
119	if err := debugAPIServer.Shutdown(context.Background()); err != nil {
120		return err
121	}
122	log.Info("DebugAPI done")
123	return nil
124}