api: ethrpc: implement eth_feeHistory (#9539)
Co-authored-by: Raúl Kripalani <raul@protocol.ai>
This commit is contained in:
committed by
vyzo
co-authored by
Raúl Kripalani
parent
c0cbcda1c2
commit
64afdfc642
@@ -45,6 +45,7 @@ type EthModuleAPI interface {
|
||||
EthGetCode(ctx context.Context, address api.EthAddress, blkOpt string) (api.EthBytes, error)
|
||||
EthGetStorageAt(ctx context.Context, address api.EthAddress, position api.EthBytes, blkParam string) (api.EthBytes, error)
|
||||
EthGetBalance(ctx context.Context, address api.EthAddress, blkParam string) (api.EthBigInt, error)
|
||||
EthFeeHistory(ctx context.Context, blkCount uint64, newestBlk string) (api.EthFeeHistory, error)
|
||||
EthChainId(ctx context.Context) (api.EthUint64, error)
|
||||
NetVersion(ctx context.Context) (string, error)
|
||||
NetListening(ctx context.Context) (bool, error)
|
||||
@@ -375,6 +376,73 @@ func (a *EthModule) EthChainId(ctx context.Context) (api.EthUint64, error) {
|
||||
return api.EthUint64(build.Eip155ChainId), nil
|
||||
}
|
||||
|
||||
func (a *EthModule) EthFeeHistory(ctx context.Context, blkCount uint64, newestBlkNum string) (api.EthFeeHistory, error) {
|
||||
if blkCount > 1024 {
|
||||
return api.EthFeeHistory{}, fmt.Errorf("block count should be smaller than 1024")
|
||||
}
|
||||
|
||||
newestBlkHeight := uint64(a.Chain.GetHeaviestTipSet().Height())
|
||||
|
||||
// TODO https://github.com/filecoin-project/ref-fvm/issues/1016
|
||||
var blkNum api.EthUint64
|
||||
err := blkNum.UnmarshalJSON([]byte(`"` + newestBlkNum + `"`))
|
||||
if err == nil && uint64(blkNum) < newestBlkHeight {
|
||||
newestBlkHeight = uint64(blkNum)
|
||||
}
|
||||
|
||||
// Deal with the case that the chain is shorter than the number of
|
||||
// requested blocks.
|
||||
oldestBlkHeight := uint64(1)
|
||||
if blkCount <= newestBlkHeight {
|
||||
oldestBlkHeight = newestBlkHeight - blkCount + 1
|
||||
}
|
||||
|
||||
ts, err := a.Chain.GetTipsetByHeight(ctx, abi.ChainEpoch(newestBlkHeight), nil, false)
|
||||
if err != nil {
|
||||
return api.EthFeeHistory{}, fmt.Errorf("cannot load find block height: %v", newestBlkHeight)
|
||||
}
|
||||
|
||||
// FIXME: baseFeePerGas should include the next block after the newest of the returned range, because this
|
||||
// can be inferred from the newest block. we use the newest block's baseFeePerGas for now but need to fix it
|
||||
// In other words, due to deferred execution, we might not be returning the most useful value here for the client.
|
||||
baseFeeArray := []api.EthBigInt{api.EthBigInt(ts.Blocks()[0].ParentBaseFee)}
|
||||
gasUsedRatioArray := []float64{}
|
||||
|
||||
for ts.Height() >= abi.ChainEpoch(oldestBlkHeight) {
|
||||
// Unfortunately we need to rebuild the full message view so we can
|
||||
// totalize gas used in the tipset.
|
||||
block, err := a.ethBlockFromFilecoinTipSet(ctx, ts, false)
|
||||
if err != nil {
|
||||
return api.EthFeeHistory{}, fmt.Errorf("cannot create eth block: %v", err)
|
||||
}
|
||||
|
||||
// both arrays should be reversed at the end
|
||||
baseFeeArray = append(baseFeeArray, api.EthBigInt(ts.Blocks()[0].ParentBaseFee))
|
||||
gasUsedRatioArray = append(gasUsedRatioArray, float64(block.GasUsed)/float64(build.BlockGasLimit))
|
||||
|
||||
parentTsKey := ts.Parents()
|
||||
ts, err = a.Chain.LoadTipSet(ctx, parentTsKey)
|
||||
if err != nil {
|
||||
return api.EthFeeHistory{}, fmt.Errorf("cannot load tipset key: %v", parentTsKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse the arrays; we collected them newest to oldest; the client expects oldest to newest.
|
||||
|
||||
for i, j := 0, len(baseFeeArray)-1; i < j; i, j = i+1, j-1 {
|
||||
baseFeeArray[i], baseFeeArray[j] = baseFeeArray[j], baseFeeArray[i]
|
||||
}
|
||||
for i, j := 0, len(gasUsedRatioArray)-1; i < j; i, j = i+1, j-1 {
|
||||
gasUsedRatioArray[i], gasUsedRatioArray[j] = gasUsedRatioArray[j], gasUsedRatioArray[i]
|
||||
}
|
||||
|
||||
return api.EthFeeHistory{
|
||||
OldestBlock: oldestBlkHeight,
|
||||
BaseFeePerGas: baseFeeArray,
|
||||
GasUsedRatio: gasUsedRatioArray,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *EthModule) NetVersion(ctx context.Context) (string, error) {
|
||||
// Note that networkId is not encoded in hex
|
||||
nv, err := a.StateNetworkVersion(ctx, types.EmptyTSK)
|
||||
|
||||
@@ -91,6 +91,7 @@ func FullNodeHandler(a v1api.FullNode, permissioned bool, opts ...jsonrpc.Server
|
||||
rpcServer.AliasMethod("eth_getStorageAt", "Filecoin.EthGetStorageAt")
|
||||
rpcServer.AliasMethod("eth_getBalance", "Filecoin.EthGetBalance")
|
||||
rpcServer.AliasMethod("eth_chainId", "Filecoin.EthChainId")
|
||||
rpcServer.AliasMethod("eth_feeHistory", "Filecoin.EthFeeHistory")
|
||||
rpcServer.AliasMethod("eth_protocolVersion", "Filecoin.EthProtocolVersion")
|
||||
rpcServer.AliasMethod("eth_maxPriorityFeePerGas", "Filecoin.EthMaxPriorityFeePerGas")
|
||||
rpcServer.AliasMethod("eth_gasPrice", "Filecoin.EthGasPrice")
|
||||
|
||||
Reference in New Issue
Block a user