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.

121 lines
2.6 KiB

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