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