From 73a2df2b0a7d096714af66ff4ef544f93480894f Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 4 Dec 2025 11:02:42 +0100 Subject: [PATCH] eth/filters: change error code for invalid parameter errors (#33320) This improves the error code for cases where invalid query parameters are submitted to `eth_getLogs`. I also improved the error message that is emitted when querying into the future. --- eth/filters/api.go | 22 +++++++++++++++++----- eth/filters/filter.go | 5 ++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/eth/filters/api.go b/eth/filters/api.go index 58baf2c3aa..4ed7e5be0a 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -35,17 +35,29 @@ import ( ) var ( - errInvalidTopic = errors.New("invalid topic(s)") - errFilterNotFound = errors.New("filter not found") - errInvalidBlockRange = errors.New("invalid block range params") + errInvalidTopic = invalidParamsErr("invalid topic(s)") + errInvalidBlockRange = invalidParamsErr("invalid block range params") + errBlockRangeIntoFuture = invalidParamsErr("block range extends beyond current head block") + errBlockHashWithRange = invalidParamsErr("can't specify fromBlock/toBlock with blockHash") + errPendingLogsUnsupported = invalidParamsErr("pending logs are not supported") errUnknownBlock = errors.New("unknown block") - errBlockHashWithRange = errors.New("can't specify fromBlock/toBlock with blockHash") - errPendingLogsUnsupported = errors.New("pending logs are not supported") + errFilterNotFound = errors.New("filter not found") errExceedMaxTopics = errors.New("exceed max topics") errExceedLogQueryLimit = errors.New("exceed max addresses or topics per search position") errExceedMaxTxHashes = errors.New("exceed max number of transaction hashes allowed per transactionReceipts subscription") ) +type invalidParamsError struct { + err error +} + +func (e invalidParamsError) Error() string { return e.err.Error() } +func (e invalidParamsError) ErrorCode() int { return -32602 } + +func invalidParamsErr(format string, args ...any) error { + return invalidParamsError{fmt.Errorf(format, args...)} +} + const ( // The maximum number of topic criteria allowed, vm.LOG4 - vm.LOG0 maxTopics = 4 diff --git a/eth/filters/filter.go b/eth/filters/filter.go index a818f0b607..10afc84fe9 100644 --- a/eth/filters/filter.go +++ b/eth/filters/filter.go @@ -221,9 +221,12 @@ func (s *searchSession) updateChainView() error { if lastBlock == math.MaxUint64 { lastBlock = head } - if firstBlock > lastBlock || lastBlock > head { + if firstBlock > lastBlock { return errInvalidBlockRange } + if lastBlock > head { + return errBlockRangeIntoFuture + } s.searchRange = common.NewRange(firstBlock, lastBlock+1-firstBlock) // Trim existing match set in case a reorg may have invalidated some results