From 936d43311082f49d0c5ba933036766bf19ae9728 Mon Sep 17 00:00:00 2001 From: simlecode <69969590+simlecode@users.noreply.github.com> Date: Mon, 24 Jun 2024 14:05:00 +0800 Subject: [PATCH] feat: support safe param for eth_getBlockByNumber for 30 blocks --- app/submodule/eth/eth_utils.go | 16 ++++++++++++++++ venus-shared/actors/types/eth.go | 6 ++++++ venus-shared/types/eth.go | 1 + 3 files changed, 23 insertions(+) diff --git a/app/submodule/eth/eth_utils.go b/app/submodule/eth/eth_utils.go index 9ebf700860..1718b4d978 100644 --- a/app/submodule/eth/eth_utils.go +++ b/app/submodule/eth/eth_utils.go @@ -56,6 +56,22 @@ func getTipsetByBlockNumber(ctx context.Context, store *chain.Store, blkParam st return nil, fmt.Errorf("cannot get parent tipset") } return parent, nil + case "safe": + latestHeight := head.Height() - 1 + safeHeight := latestHeight - types.SafeEpochDelay + ts, err := store.GetTipSetByHeight(ctx, head, safeHeight, true) + if err != nil { + return nil, fmt.Errorf("cannot get tipset at height: %v", safeHeight) + } + return ts, nil + case "finalized": + latestHeight := head.Height() - 1 + safeHeight := latestHeight - constants.Finality + ts, err := store.GetTipSetByHeight(ctx, head, safeHeight, true) + if err != nil { + return nil, fmt.Errorf("cannot get tipset at height: %v", safeHeight) + } + return ts, nil default: var num types.EthUint64 err := num.UnmarshalJSON([]byte(`"` + blkParam + `"`)) diff --git a/venus-shared/actors/types/eth.go b/venus-shared/actors/types/eth.go index d9c5d0d3d9..13aeba9d9e 100644 --- a/venus-shared/actors/types/eth.go +++ b/venus-shared/actors/types/eth.go @@ -25,6 +25,12 @@ import ( var ErrInvalidAddress = errors.New("invalid Filecoin Eth address") +// Research into Filecoin chain behaviour suggests that probabilistic finality +// generally approaches the intended stability guarantee at, or near, 30 epochs. +// Although a strictly "finalized" safe recommendation remains 900 epochs. +// See https://github.com/filecoin-project/FIPs/blob/master/FRCs/frc-0089.md +const SafeEpochDelay = abi.ChainEpoch(30) + // mainnet var Eip155ChainID = 314 diff --git a/venus-shared/types/eth.go b/venus-shared/types/eth.go index af43c368a0..bfeb684566 100644 --- a/venus-shared/types/eth.go +++ b/venus-shared/types/eth.go @@ -9,6 +9,7 @@ const ( EthAddressLength = types.EthAddressLength EthBloomSize = types.EthBloomSize EthHashLength = types.EthHashLength + SafeEpochDelay = types.SafeEpochDelay ) var (