From e9771ac497c117a8c8574e15d6543b124ebfa0a5 Mon Sep 17 00:00:00 2001 From: Tran Tien Duc Date: Wed, 11 Dec 2024 13:57:12 +0700 Subject: [PATCH] internal/ethapi: implement eth_blobBaseFee --- eth/api_backend.go | 8 ++++++++ internal/ethapi/api.go | 5 +++++ internal/ethapi/api_test.go | 3 +++ internal/ethapi/backend.go | 1 + internal/jsre/deps/web3.js | 5 +++++ les/api_backend.go | 8 ++++++++ 6 files changed, 30 insertions(+) diff --git a/eth/api_backend.go b/eth/api_backend.go index 93637e7c17..52c0520b07 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -22,6 +22,7 @@ import ( "math/big" "time" + "github.com/ethereum/go-ethereum/consensus/misc/eip4844" "github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum" @@ -357,6 +358,13 @@ func (b *EthAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBloc return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles) } +func (b *EthAPIBackend) BlobBaseFee(ctx context.Context) *big.Int { + if excess := b.CurrentHeader().ExcessBlobGas; excess != nil { + return eip4844.CalcBlobFee(*excess) + } + return nil +} + func (b *EthAPIBackend) ChainDb() ethdb.Database { return b.eth.ChainDb() } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 4e4fe97044..1369e528ad 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -130,6 +130,11 @@ func (s *PublicEthereumAPI) FeeHistory(ctx context.Context, blockCount rpc.Decim return results, nil } +// BlobBaseFee returns the base fee for blob gas at the current head. +func (s *PublicEthereumAPI) BlobBaseFee(ctx context.Context) *hexutil.Big { + return (*hexutil.Big)(s.b.BlobBaseFee(ctx)) +} + // Syncing returns false in case the node is currently not syncing with the network. It can be up to date or has not // yet received the latest block headers from its pears. In case it is synchronizing: // - startingBlock: block number this node started to synchronise from diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index e4226fb304..eecee740ae 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -451,6 +451,9 @@ func (b testBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { func (b testBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) { return nil, nil, nil, nil, nil } +func (b testBackend) BlobBaseFee(ctx context.Context) *big.Int { + return new(big.Int) +} func (b testBackend) ChainDb() ethdb.Database { return b.db } func (b testBackend) AccountManager() *accounts.Manager { return b.accman } func (b testBackend) ExtRPCEnabled() bool { return false } diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index bb9d564be1..4db8458951 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -45,6 +45,7 @@ type Backend interface { SuggestGasTipCap(ctx context.Context) (*big.Int, error) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) + BlobBaseFee(ctx context.Context) *big.Int ChainDb() ethdb.Database AccountManager() *accounts.Manager ExtRPCEnabled() bool diff --git a/internal/jsre/deps/web3.js b/internal/jsre/deps/web3.js index c130f80dea..ec69de09ce 100644 --- a/internal/jsre/deps/web3.js +++ b/internal/jsre/deps/web3.js @@ -5494,6 +5494,11 @@ var properties = function () { getter: 'eth_gasPrice', outputFormatter: formatters.outputBigNumberFormatter }), + new Property({ + name: 'blobBaseFee', + getter: 'eth_blobBaseFee', + outputFormatter: formatters.outputBigNumberFormatter + }), new Property({ name: 'accounts', getter: 'eth_accounts' diff --git a/les/api_backend.go b/les/api_backend.go index 00091b434a..4054e397a6 100644 --- a/les/api_backend.go +++ b/les/api_backend.go @@ -22,6 +22,7 @@ import ( "math/big" "time" + "github.com/ethereum/go-ethereum/consensus/misc/eip4844" "github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum" @@ -303,6 +304,13 @@ func (b *LesApiBackend) FeeHistory(ctx context.Context, blockCount int, lastBloc return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles) } +func (b *LesApiBackend) BlobBaseFee(ctx context.Context) *big.Int { + if excess := b.CurrentHeader().ExcessBlobGas; excess != nil { + return eip4844.CalcBlobFee(*excess) + } + return nil +} + func (b *LesApiBackend) ChainDb() ethdb.Database { return b.eth.chainDb }