Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion bridge-history-api/internal/controller/api/txs_by_hashes.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package api

import (
"io"
"net/http"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-redis/redis/v8"
"gorm.io/gorm"

"scroll-tech/bridge-history-api/internal/logic"
"scroll-tech/bridge-history-api/internal/types"
)

// maxRequestBodyBytes fits 100 tx hashes with ample headroom.
const maxRequestBodyBytes = 64 * 1024

// TxsByHashesController the controller of PostQueryTxsByHashes
type TxsByHashesController struct {
historyLogic *logic.HistoryLogic
Expand All @@ -23,8 +30,14 @@ func NewTxsByHashesController(db *gorm.DB, redisClient *redis.Client) *TxsByHash

// PostQueryTxsByHashes query the txs by hashes
func (c *TxsByHashesController) PostQueryTxsByHashes(ctx *gin.Context) {
body, err := readBodyWithSizeLimit(ctx, maxRequestBodyBytes)
if err != nil {
types.RenderFailure(ctx, types.ErrParameterInvalidNo, err)
return
}

var req types.QueryByHashRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
if err := binding.JSON.BindBody(body, &req); err != nil {
types.RenderFailure(ctx, types.ErrParameterInvalidNo, err)
return
}
Expand All @@ -38,3 +51,12 @@ func (c *TxsByHashesController) PostQueryTxsByHashes(ctx *gin.Context) {
resultData := &types.ResultData{Results: results, Total: uint64(len(results))}
types.RenderSuccess(ctx, resultData)
}

func readBodyWithSizeLimit(ctx *gin.Context, limit int64) ([]byte, error) {
// Reject early if the declared size is too large.
if ctx.Request.ContentLength > limit {
return nil, &http.MaxBytesError{Limit: limit}
}
// Read to EOF to catch trailing data past the limit.
return io.ReadAll(http.MaxBytesReader(ctx.Writer, ctx.Request.Body, limit))
}
4 changes: 2 additions & 2 deletions bridge-history-api/internal/types/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ const (

// QueryByAddressRequest the request parameter of address api
type QueryByAddressRequest struct {
Address string `form:"address" binding:"required"`
Address string `form:"address" binding:"required,eth_addr"`
Page uint64 `form:"page" binding:"required,min=1"`
PageSize uint64 `form:"page_size" binding:"required,min=1,max=100"`
}

// QueryByHashRequest the request parameter of hash api
type QueryByHashRequest struct {
Txs []string `json:"txs" binding:"required,min=1,max=100"`
Txs []string `json:"txs" binding:"required,min=1,max=100,dive,len=66,startswith=0x,hexadecimal"`
}

// ResultData contains return txs and total
Expand Down
2 changes: 1 addition & 1 deletion common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime/debug"
)

var tag = "v4.8.0"
var tag = "v4.8.1"

var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
Expand Down
Loading