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.

123 lines
2.7 KiB

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