arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/batch.go
api/batch.goRaw
  1package api
  2
  3import (
  4	"database/sql"
  5	"errors"
  6	"net/http"
  7
  8	"github.com/gin-gonic/gin"
  9	"github.com/hermeznetwork/hermez-node/common"
 10	"github.com/hermeznetwork/hermez-node/db/historydb"
 11	"github.com/hermeznetwork/tracerr"
 12)
 13
 14func (a *API) getBatches(c *gin.Context) {
 15	// Get query parameters
 16	// minBatchNum
 17	minBatchNum, err := parseQueryUint("minBatchNum", nil, 0, maxUint32, c)
 18	if err != nil {
 19		retBadReq(err, c)
 20		return
 21	}
 22	// maxBatchNum
 23	maxBatchNum, err := parseQueryUint("maxBatchNum", nil, 0, maxUint32, c)
 24	if err != nil {
 25		retBadReq(err, c)
 26		return
 27	}
 28	// slotNum
 29	slotNum, err := parseQueryUint("slotNum", nil, 0, maxUint32, c)
 30	if err != nil {
 31		retBadReq(err, c)
 32		return
 33	}
 34	// forgerAddr
 35	forgerAddr, err := parseQueryEthAddr("forgerAddr", c)
 36	if err != nil {
 37		retBadReq(err, c)
 38		return
 39	}
 40	// pagination
 41	fromItem, order, limit, err := parsePagination(c)
 42	if err != nil {
 43		retBadReq(err, c)
 44		return
 45	}
 46	// Fetch batches from historyDB
 47	batches, pendingItems, err := a.h.GetBatchesAPI(
 48		minBatchNum, maxBatchNum, slotNum, forgerAddr, fromItem, limit, order,
 49	)
 50	if err != nil {
 51		retSQLErr(err, c)
 52		return
 53	}
 54
 55	// Build succesfull response
 56	type batchesResponse struct {
 57		Batches      []historydb.BatchAPI `json:"batches"`
 58		PendingItems uint64               `json:"pendingItems"`
 59	}
 60	c.JSON(http.StatusOK, &batchesResponse{
 61		Batches:      batches,
 62		PendingItems: pendingItems,
 63	})
 64}
 65
 66func (a *API) getBatch(c *gin.Context) {
 67	// Get batchNum
 68	batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
 69	if err != nil {
 70		retBadReq(err, c)
 71		return
 72	}
 73	if batchNum == nil { // batchNum is required
 74		retBadReq(errors.New("Invalid batchNum"), c)
 75		return
 76	}
 77	// Fetch batch from historyDB
 78	batch, err := a.h.GetBatchAPI(common.BatchNum(*batchNum))
 79	if err != nil {
 80		retSQLErr(err, c)
 81		return
 82	}
 83	// JSON response
 84	c.JSON(http.StatusOK, batch)
 85}
 86
 87type fullBatch struct {
 88	Batch *historydb.BatchAPI `json:"batch"`
 89	Txs   []historydb.TxAPI   `json:"transactions"`
 90}
 91
 92func (a *API) getFullBatch(c *gin.Context) {
 93	// Get batchNum
 94	batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
 95	if err != nil {
 96		retBadReq(err, c)
 97		return
 98	}
 99	if batchNum == nil {
100		retBadReq(errors.New("Invalid batchNum"), c)
101		return
102	}
103	// Fetch batch from historyDB
104	batch, err := a.h.GetBatchAPI(common.BatchNum(*batchNum))
105	if err != nil {
106		retSQLErr(err, c)
107		return
108	}
109	// Fetch txs forged in the batch from historyDB
110	maxTxsPerBatch := uint(2048) //nolint:gomnd
111	txs, _, err := a.h.GetTxsAPI(
112		nil, nil, nil, nil, batchNum, nil, nil, &maxTxsPerBatch, historydb.OrderAsc,
113	)
114	if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows {
115		retSQLErr(err, c)
116		return
117	}
118	// JSON response
119	c.JSON(http.StatusOK, fullBatch{
120		Batch: batch,
121		Txs:   txs,
122	})
123}