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.

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