Skip to content

Commit 6abd538

Browse files
authored
throw error on over limit get blocks (#684)
* throw error on over limit get blocks * move limit validation to handler layer
1 parent 443dfd0 commit 6abd538

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

api/handler/blockHandler.go

+8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package handler
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/zoobc/zoobc-core/api/service"
8+
"github.com/zoobc/zoobc-core/common/constant"
79
"github.com/zoobc/zoobc-core/common/chaintype"
810
"github.com/zoobc/zoobc-core/common/model"
11+
"google.golang.org/grpc/codes"
12+
"google.golang.org/grpc/status"
913
)
1014

1115
// BlockHandler to handle request related to Blocks from client
@@ -35,6 +39,10 @@ func (bs *BlockHandler) GetBlock(ctx context.Context, req *model.GetBlockRequest
3539

3640
// GetBlocks handles request to get data of multiple blocks
3741
func (bs *BlockHandler) GetBlocks(ctx context.Context, req *model.GetBlocksRequest) (*model.GetBlocksResponse, error) {
42+
if req.Limit > constant.MaxAPIGetBlocks {
43+
return nil, status.Error(codes.OutOfRange, fmt.Sprintf("limit exceeded, max. %d", constant.MaxAPIGetBlocks))
44+
}
45+
3846
chainType := chaintype.GetChainType(req.ChainType)
3947
blocksResponse, err := bs.Service.GetBlocks(chainType, req.Limit, req.Height)
4048
if err != nil {

common/constant/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ package constant
22

33
var (
44
MaxAPIRequestPerSecond uint32 = 10
5+
MaxAPIGetBlocks uint32 = 500
56
)

0 commit comments

Comments
 (0)