Skip to content

Commit

Permalink
Validate that FromBlock/ToBlock epoch is indeed a hex value (#10780)
Browse files Browse the repository at this point in the history
* Validate that FromBlock/ToBlock epoch is indeed a hex value

* Adding tests
  • Loading branch information
fridrik01 committed May 9, 2023
1 parent 5c26a3d commit ceb3f1e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,12 @@ func (h EthSubscriptionID) String() string {
}

type EthFilterSpec struct {
// Interpreted as an epoch or one of "latest" for last mined block, "earliest" for first,
// Interpreted as an epoch (in hex) or one of "latest" for last mined block, "earliest" for first,
// "pending" for not yet committed messages.
// Optional, default: "latest".
FromBlock *string `json:"fromBlock,omitempty"`

// Interpreted as an epoch or one of "latest" for last mined block, "earliest" for first,
// Interpreted as an epoch (in hex) or one of "latest" for last mined block, "earliest" for first,
// "pending" for not yet committed messages.
// Optional, default: "latest".
ToBlock *string `json:"toBlock,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions itests/eth_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ func TestEthNewFilterDefaultSpec(t *testing.T) {
elogs, err := parseEthLogsFromFilterResult(res)
require.NoError(err)
AssertEthLogs(t, elogs, expected, received)

// giving a number to fromBlock/toBlock needs to be in hex format, so make sure passing in decimal fails
blockNrInDecimal := "2812200"
_, err = client.EthNewFilter(ctx, &ethtypes.EthFilterSpec{FromBlock: &blockNrInDecimal, ToBlock: &blockNrInDecimal})
require.Error(err, "expected error when fromBlock/toBlock is not in hex format")
}

func TestEthGetLogsBasic(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"sort"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -1180,6 +1181,9 @@ func (e *EthEvent) installEthFilterSpec(ctx context.Context, filterSpec *ethtype
} else if *filterSpec.FromBlock == "pending" {
return nil, api.ErrNotSupported
} else {
if !strings.HasPrefix(*filterSpec.FromBlock, "0x") {
return nil, xerrors.Errorf("FromBlock is not a hex")
}
epoch, err := ethtypes.EthUint64FromHex(*filterSpec.FromBlock)
if err != nil {
return nil, xerrors.Errorf("invalid epoch")
Expand All @@ -1195,6 +1199,9 @@ func (e *EthEvent) installEthFilterSpec(ctx context.Context, filterSpec *ethtype
} else if *filterSpec.ToBlock == "pending" {
return nil, api.ErrNotSupported
} else {
if !strings.HasPrefix(*filterSpec.ToBlock, "0x") {
return nil, xerrors.Errorf("ToBlock is not a hex")
}
epoch, err := ethtypes.EthUint64FromHex(*filterSpec.ToBlock)
if err != nil {
return nil, xerrors.Errorf("invalid epoch")
Expand Down

0 comments on commit ceb3f1e

Please sign in to comment.