Skip to content

Commit

Permalink
Merge pull request #10464 from filecoin-project/steb/fee-history-gw-l…
Browse files Browse the repository at this point in the history
…ookback-max

fix: gateway: correctly apply the fee history lookback max
  • Loading branch information
magik6k authored Mar 15, 2023
2 parents a7c9a83 + 3556a4b commit 486904b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
3 changes: 3 additions & 0 deletions gateway/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,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

0 comments on commit 486904b

Please sign in to comment.