From fdd9d63a3ed032396ae32a7b2a45b36760de9524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Garamv=C3=B6lgyi?= Date: Thu, 22 Oct 2026 13:13:20 +0200 Subject: [PATCH] fix(bridge-history-api): bound request work and hide internal errors --- bridge-history-api/cmd/api/app/app.go | 2 ++ .../internal/controller/api/controller.go | 4 ++++ .../api/l2unclaimed_withdrawals_by_address.go | 4 ++-- .../controller/api/l2withdrawals_by_address.go | 4 ++-- .../internal/controller/api/txs_by_address.go | 4 ++-- .../internal/controller/api/txs_by_hashes.go | 2 +- bridge-history-api/internal/logic/history_logic.go | 14 ++++++++++++-- .../internal/orm/bridge_batch_deposit.go | 4 ++++ bridge-history-api/internal/orm/cross_message.go | 8 ++++++++ common/version/version.go | 2 +- 10 files changed, 38 insertions(+), 10 deletions(-) diff --git a/bridge-history-api/cmd/api/app/app.go b/bridge-history-api/cmd/api/app/app.go index 66c31d3d37..2c4620295f 100644 --- a/bridge-history-api/cmd/api/app/app.go +++ b/bridge-history-api/cmd/api/app/app.go @@ -75,6 +75,8 @@ func action(ctx *cli.Context) error { api.InitController(db, redisClient) router := gin.Default() + // Let *gin.Context carry request cancellation, so DB and Redis calls stop when the client goes away. + router.ContextWithFallback = true registry := prometheus.DefaultRegisterer route.Route(router, cfg, registry) diff --git a/bridge-history-api/internal/controller/api/controller.go b/bridge-history-api/internal/controller/api/controller.go index 7e4356fb38..70bbb88a1a 100644 --- a/bridge-history-api/internal/controller/api/controller.go +++ b/bridge-history-api/internal/controller/api/controller.go @@ -1,6 +1,7 @@ package api import ( + "errors" "sync" "github.com/go-redis/redis/v8" @@ -21,6 +22,9 @@ var ( L2WithdrawalsByAddressCtl *L2WithdrawalsByAddressController initControllerOnce sync.Once + + // errInternal hides internal error details from clients. The logic layer logs the full error. + errInternal = errors.New("internal error") ) // InitController inits Controller with database diff --git a/bridge-history-api/internal/controller/api/l2unclaimed_withdrawals_by_address.go b/bridge-history-api/internal/controller/api/l2unclaimed_withdrawals_by_address.go index 965e27b306..08afac3a0f 100644 --- a/bridge-history-api/internal/controller/api/l2unclaimed_withdrawals_by_address.go +++ b/bridge-history-api/internal/controller/api/l2unclaimed_withdrawals_by_address.go @@ -24,14 +24,14 @@ func NewL2UnclaimedWithdrawalsByAddressController(db *gorm.DB, redisClient *redi // GetL2UnclaimedWithdrawalsByAddress defines the http get method behavior func (c *L2UnclaimedWithdrawalsByAddressController) GetL2UnclaimedWithdrawalsByAddress(ctx *gin.Context) { var req types.QueryByAddressRequest - if err := ctx.ShouldBind(&req); err != nil { + if err := ctx.ShouldBindQuery(&req); err != nil { types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) return } pagedTxs, total, err := c.historyLogic.GetL2UnclaimedWithdrawalsByAddress(ctx, req.Address, req.Page, req.PageSize) if err != nil { - types.RenderFailure(ctx, types.ErrGetL2ClaimableWithdrawalsError, err) + types.RenderFailure(ctx, types.ErrGetL2ClaimableWithdrawalsError, errInternal) return } diff --git a/bridge-history-api/internal/controller/api/l2withdrawals_by_address.go b/bridge-history-api/internal/controller/api/l2withdrawals_by_address.go index 2764ff4ea8..2d381a248b 100644 --- a/bridge-history-api/internal/controller/api/l2withdrawals_by_address.go +++ b/bridge-history-api/internal/controller/api/l2withdrawals_by_address.go @@ -24,14 +24,14 @@ func NewL2WithdrawalsByAddressController(db *gorm.DB, redisClient *redis.Client) // GetL2WithdrawalsByAddress defines the http get method behavior func (c *L2WithdrawalsByAddressController) GetL2WithdrawalsByAddress(ctx *gin.Context) { var req types.QueryByAddressRequest - if err := ctx.ShouldBind(&req); err != nil { + if err := ctx.ShouldBindQuery(&req); err != nil { types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) return } pagedTxs, total, err := c.historyLogic.GetL2WithdrawalsByAddress(ctx, req.Address, req.Page, req.PageSize) if err != nil { - types.RenderFailure(ctx, types.ErrGetL2WithdrawalsError, err) + types.RenderFailure(ctx, types.ErrGetL2WithdrawalsError, errInternal) return } diff --git a/bridge-history-api/internal/controller/api/txs_by_address.go b/bridge-history-api/internal/controller/api/txs_by_address.go index 0918dbfa0c..5cc358bee9 100644 --- a/bridge-history-api/internal/controller/api/txs_by_address.go +++ b/bridge-history-api/internal/controller/api/txs_by_address.go @@ -24,14 +24,14 @@ func NewTxsByAddressController(db *gorm.DB, redisClient *redis.Client) *TxsByAdd // GetTxsByAddress defines the http get method behavior func (c *TxsByAddressController) GetTxsByAddress(ctx *gin.Context) { var req types.QueryByAddressRequest - if err := ctx.ShouldBind(&req); err != nil { + if err := ctx.ShouldBindQuery(&req); err != nil { types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) return } pagedTxs, total, err := c.historyLogic.GetTxsByAddress(ctx, req.Address, req.Page, req.PageSize) if err != nil { - types.RenderFailure(ctx, types.ErrGetTxsError, err) + types.RenderFailure(ctx, types.ErrGetTxsError, errInternal) return } diff --git a/bridge-history-api/internal/controller/api/txs_by_hashes.go b/bridge-history-api/internal/controller/api/txs_by_hashes.go index 480edf3e6f..2ada336533 100644 --- a/bridge-history-api/internal/controller/api/txs_by_hashes.go +++ b/bridge-history-api/internal/controller/api/txs_by_hashes.go @@ -44,7 +44,7 @@ func (c *TxsByHashesController) PostQueryTxsByHashes(ctx *gin.Context) { results, err := c.historyLogic.GetTxsByHashes(ctx, req.Txs) if err != nil { - types.RenderFailure(ctx, types.ErrGetTxsByHashError, err) + types.RenderFailure(ctx, types.ErrGetTxsByHashError, errInternal) return } diff --git a/bridge-history-api/internal/logic/history_logic.go b/bridge-history-api/internal/logic/history_logic.go index cb613fd945..09ab2da95e 100644 --- a/bridge-history-api/internal/logic/history_logic.go +++ b/bridge-history-api/internal/logic/history_logic.go @@ -32,6 +32,7 @@ const ( cacheKeyPrefixTxsByAddr = cacheKeyPrefixBridgeHistory + "txsByAddr:" cacheKeyPrefixQueryTxsByHashes = cacheKeyPrefixBridgeHistory + "queryTxsByHashes:" cacheKeyExpiredTime = 1 * time.Minute + sharedQueryTimeout = 10 * time.Second ) // HistoryLogic services. @@ -76,6 +77,9 @@ func (h *HistoryLogic) GetL2UnclaimedWithdrawalsByAddress(ctx context.Context, a log.Info("cache miss", "cache key", cacheKey) result, err, _ := h.singleFlight.Do(cacheKey, func() (interface{}, error) { + // Callers share this query, so one disconnecting caller must not cancel it for the others. + ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), sharedQueryTimeout) + defer cancel() var txHistoryInfos []*types.TxHistoryInfo crossMessages, getErr := h.crossMessageOrm.GetL2UnclaimedWithdrawalsByAddress(ctx, address) if getErr != nil { @@ -119,6 +123,9 @@ func (h *HistoryLogic) GetL2WithdrawalsByAddress(ctx context.Context, address st log.Info("cache miss", "cache key", cacheKey) result, err, _ := h.singleFlight.Do(cacheKey, func() (interface{}, error) { + // Callers share this query, so one disconnecting caller must not cancel it for the others. + ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), sharedQueryTimeout) + defer cancel() var txHistoryInfos []*types.TxHistoryInfo crossMessages, getErr := h.crossMessageOrm.GetL2WithdrawalsByAddress(ctx, address) if getErr != nil { @@ -162,6 +169,9 @@ func (h *HistoryLogic) GetTxsByAddress(ctx context.Context, address string, page log.Info("cache miss", "cache key", cacheKey) result, err, _ := h.singleFlight.Do(cacheKey, func() (interface{}, error) { + // Callers share this query, so one disconnecting caller must not cancel it for the others. + ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), sharedQueryTimeout) + defer cancel() var txHistoryInfos []*types.TxHistoryInfo crossMessages, getErr := h.crossMessageOrm.GetTxsByAddress(ctx, address) if getErr != nil { @@ -262,7 +272,7 @@ func (h *HistoryLogic) GetTxsByHashes(ctx context.Context, txHashes []string) ([ crossMessages, err := h.crossMessageOrm.GetMessagesByTxHashes(ctx, txHashes) if err != nil { - log.Error("failed to get cross messages by tx hashes", "hashes", txHashes) + log.Error("failed to get cross messages by tx hashes", "hashes", txHashes, "error", err) return nil, err } for _, message := range crossMessages { @@ -271,7 +281,7 @@ func (h *HistoryLogic) GetTxsByHashes(ctx context.Context, txHashes []string) ([ batchDepositMessages, err := h.bridgeBatchDepositOrm.GetMessagesByTxHashes(ctx, txHashes) if err != nil { - log.Error("failed to get batch deposit messages by tx hashes", "hashes", txHashes) + log.Error("failed to get batch deposit messages by tx hashes", "hashes", txHashes, "error", err) return nil, err } for _, message := range batchDepositMessages { diff --git a/bridge-history-api/internal/orm/bridge_batch_deposit.go b/bridge-history-api/internal/orm/bridge_batch_deposit.go index cd0876c18e..c2a8760a27 100644 --- a/bridge-history-api/internal/orm/bridge_batch_deposit.go +++ b/bridge-history-api/internal/orm/bridge_batch_deposit.go @@ -66,9 +66,13 @@ func (c *BridgeBatchDepositEvent) GetMessagesByTxHashes(ctx context.Context, txH db := c.db.WithContext(ctx) db = db.Model(&BridgeBatchDepositEvent{}) db = db.Where("l1_tx_hash in (?) or l2_tx_hash in (?)", txHashes, txHashes) + db = db.Limit(maxMessagesByTxHashes + 1) if err := db.Find(&messages).Error; err != nil { return nil, fmt.Errorf("failed to GetMessagesByTxHashes by tx hashes, tx hashes: %v, error: %w", txHashes, err) } + if len(messages) > maxMessagesByTxHashes { + return nil, fmt.Errorf("too many batch deposit messages for tx hashes, limit: %d", maxMessagesByTxHashes) + } return messages, nil } diff --git a/bridge-history-api/internal/orm/cross_message.go b/bridge-history-api/internal/orm/cross_message.go index 05e6eecfcf..471b0d8643 100644 --- a/bridge-history-api/internal/orm/cross_message.go +++ b/bridge-history-api/internal/orm/cross_message.go @@ -136,15 +136,23 @@ func (c *CrossMessage) GetL2WithdrawalsByBlockRange(ctx context.Context, startBl return messages, nil } +// maxMessagesByTxHashes bounds the rows one tx hashes query loads. Exceeding it is an error, +// so a truncated result is never returned or cached. +const maxMessagesByTxHashes = 500 + // GetMessagesByTxHashes retrieves all cross messages from the database that match the provided transaction hashes. func (c *CrossMessage) GetMessagesByTxHashes(ctx context.Context, txHashes []string) ([]*CrossMessage, error) { var messages []*CrossMessage db := c.db.WithContext(ctx) db = db.Model(&CrossMessage{}) db = db.Where("l1_tx_hash in (?) or l2_tx_hash in (?)", txHashes, txHashes) + db = db.Limit(maxMessagesByTxHashes + 1) if err := db.Find(&messages).Error; err != nil { return nil, fmt.Errorf("failed to get L2 messages by tx hashes, tx hashes: %v, error: %w", txHashes, err) } + if len(messages) > maxMessagesByTxHashes { + return nil, fmt.Errorf("too many messages for tx hashes, limit: %d", maxMessagesByTxHashes) + } return messages, nil } diff --git a/common/version/version.go b/common/version/version.go index 4b30af77d8..fe73989b6c 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "v4.8.4" +var tag = "v4.8.5" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok {