Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gateway: correctly apply the fee history lookback max #10464

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gateway/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ func (gw *Node) checkTipset(ts *types.TipSet) error {
}

func (gw *Node) checkTipsetHeight(ts *types.TipSet, h abi.ChainEpoch) error {
if h > ts.Height() {
return fmt.Errorf("tipset height in future")
}
tsBlock := ts.Blocks()[0]
heightDelta := time.Duration(uint64(tsBlock.Height-h)*build.BlockDelaySecs) * time.Second
timeAtHeight := time.Unix(int64(tsBlock.Timestamp), 0).Add(-heightDelta)
Expand Down
52 changes: 28 additions & 24 deletions gateway/proxy_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,36 @@ func (gw *Node) checkBlkHash(ctx context.Context, blkHash ethtypes.EthHash) erro
return gw.checkTipsetKey(ctx, tsk)
}

func (gw *Node) checkBlkParam(ctx context.Context, blkParam string) error {
func (gw *Node) checkBlkParam(ctx context.Context, blkParam string, lookback ethtypes.EthUint64) error {
if blkParam == "earliest" {
// also not supported in node impl
return fmt.Errorf("block param \"earliest\" is not supported")
}

head, err := gw.target.ChainHead(ctx)
if err != nil {
return err
}

var num ethtypes.EthUint64
switch blkParam {
case "pending", "latest":
// those will be recent enough, so we don't need to check
return nil
default:
var num ethtypes.EthUint64
err := num.UnmarshalJSON([]byte(`"` + blkParam + `"`))
if err != nil {
return fmt.Errorf("cannot parse block number: %v", err)
// Head is always ok.
if lookback == 0 {
return nil
}
head, err := gw.target.ChainHead(ctx)
if err != nil {
return err
// Can't look beyond 0 anyways.
if lookback > ethtypes.EthUint64(head.Height()) {
break
}
if err := gw.checkTipsetHeight(head, abi.ChainEpoch(num)); err != nil {
return err
num = ethtypes.EthUint64(head.Height()) - lookback
default:
if err := num.UnmarshalJSON([]byte(`"` + blkParam + `"`)); err != nil {
return fmt.Errorf("cannot parse block number: %v", err)
}
}

return nil
}
return gw.checkTipsetHeight(head, abi.ChainEpoch(num))
}

func (gw *Node) EthGetBlockTransactionCountByHash(ctx context.Context, blkHash ethtypes.EthHash) (ethtypes.EthUint64, error) {
Expand Down Expand Up @@ -133,7 +137,7 @@ func (gw *Node) EthGetBlockByNumber(ctx context.Context, blkNum string, fullTxIn
return ethtypes.EthBlock{}, err
}

if err := gw.checkBlkParam(ctx, blkNum); err != nil {
if err := gw.checkBlkParam(ctx, blkNum, 0); err != nil {
return ethtypes.EthBlock{}, err
}

Expand Down Expand Up @@ -169,7 +173,7 @@ func (gw *Node) EthGetTransactionCount(ctx context.Context, sender ethtypes.EthA
return 0, err
}

if err := gw.checkBlkParam(ctx, blkOpt); err != nil {
if err := gw.checkBlkParam(ctx, blkOpt, 0); err != nil {
return 0, err
}

Expand All @@ -189,7 +193,7 @@ func (gw *Node) EthGetCode(ctx context.Context, address ethtypes.EthAddress, blk
return nil, err
}

if err := gw.checkBlkParam(ctx, blkOpt); err != nil {
if err := gw.checkBlkParam(ctx, blkOpt, 0); err != nil {
return nil, err
}

Expand All @@ -201,7 +205,7 @@ func (gw *Node) EthGetStorageAt(ctx context.Context, address ethtypes.EthAddress
return nil, err
}

if err := gw.checkBlkParam(ctx, blkParam); err != nil {
if err := gw.checkBlkParam(ctx, blkParam, 0); err != nil {
return nil, err
}

Expand All @@ -213,7 +217,7 @@ func (gw *Node) EthGetBalance(ctx context.Context, address ethtypes.EthAddress,
return ethtypes.EthBigInt(big.Zero()), err
}

if err := gw.checkBlkParam(ctx, blkParam); err != nil {
if err := gw.checkBlkParam(ctx, blkParam, 0); err != nil {
return ethtypes.EthBigInt(big.Zero()), err
}

Expand Down Expand Up @@ -272,7 +276,7 @@ func (gw *Node) EthFeeHistory(ctx context.Context, p jsonrpc.RawParams) (ethtype
return ethtypes.EthFeeHistory{}, err
}

if err := gw.checkBlkParam(ctx, params.NewestBlkNum); err != nil {
if err := gw.checkBlkParam(ctx, params.NewestBlkNum, params.BlkCount); err != nil {
return ethtypes.EthFeeHistory{}, err
}

Expand Down Expand Up @@ -305,7 +309,7 @@ func (gw *Node) EthCall(ctx context.Context, tx ethtypes.EthCall, blkParam strin
return nil, err
}

if err := gw.checkBlkParam(ctx, blkParam); err != nil {
if err := gw.checkBlkParam(ctx, blkParam, 0); err != nil {
return nil, err
}

Expand All @@ -327,12 +331,12 @@ func (gw *Node) EthGetLogs(ctx context.Context, filter *ethtypes.EthFilterSpec)
}

if filter.FromBlock != nil {
if err := gw.checkBlkParam(ctx, *filter.FromBlock); err != nil {
if err := gw.checkBlkParam(ctx, *filter.FromBlock, 0); err != nil {
return nil, err
}
}
if filter.ToBlock != nil {
if err := gw.checkBlkParam(ctx, *filter.ToBlock); err != nil {
if err := gw.checkBlkParam(ctx, *filter.ToBlock, 0); err != nil {
return nil, err
}
}
Expand Down