Skip to content

Commit

Permalink
Limit block count for fee history request (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanliqun authored Jul 2, 2024
1 parent 669c04a commit 61aaaf9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
6 changes: 5 additions & 1 deletion rpc/cfx_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,11 @@ func (api *cfxAPI) GetParamsFromVote(ctx context.Context, epoch *types.Epoch) (p
func (api *cfxAPI) GetFeeHistory(
ctx context.Context, blockCount types.HexOrDecimalUint64, lastEpoch types.Epoch, rewardPercentiles []float64,
) (feeHistory *types.FeeHistory, err error) {
if len(rewardPercentiles) > maxNumRewardPercentiles {
if blockCount > maxFeeHistoryBlockCnt {
return nil, errTooManyBlocksForFeeHistory
}

if len(rewardPercentiles) > maxRewardPercentileCnt {
return nil, errTooManyRewardPercentiles
}

Expand Down
26 changes: 20 additions & 6 deletions rpc/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,25 @@ import (
)

const (
rpcMethodEthGetLogs = "eth_getLogs"
maxNumRewardPercentiles = 50
rpcMethodEthGetLogs = "eth_getLogs"

// The maximum number of percentile values to sample from each block's
// effective priority fees per gas in ascending order.
maxRewardPercentileCnt = 50
// The maximum number of blocks in the requested range for fee history.
maxFeeHistoryBlockCnt = 1024
)

var (
ethEmptyLogs = []web3Types.Log{}
ethEmptyLogs = []web3Types.Log{}

errTooManyRewardPercentiles = errors.Errorf(
"the length of `rewardPercentiles` exceeds the maximum allowed (%v)",
maxNumRewardPercentiles,
"the number of reward percentiles exceeds the maximum allowed (%v)",
maxRewardPercentileCnt,
)
errTooManyBlocksForFeeHistory = errors.Errorf(
"the number of blocks in the requested range exceeds the maximum allowed (%v)",
maxFeeHistoryBlockCnt,
)
)

Expand Down Expand Up @@ -458,7 +468,11 @@ func (api *ethAPI) GetTransactionByBlockNumberAndIndex(
func (api *ethAPI) FeeHistory(
ctx context.Context, blockCount types.HexOrDecimalUint64, lastBlock web3Types.BlockNumber, rewardPercentiles []float64,
) (val *web3Types.FeeHistory, err error) {
if len(rewardPercentiles) > maxNumRewardPercentiles {
if blockCount > maxFeeHistoryBlockCnt {
return nil, errTooManyBlocksForFeeHistory
}

if len(rewardPercentiles) > maxRewardPercentileCnt {
return nil, errTooManyRewardPercentiles
}

Expand Down

0 comments on commit 61aaaf9

Please sign in to comment.