Skip to content

Commit

Permalink
Make secondsPerSlot available in chain config (#13467)
Browse files Browse the repository at this point in the history
Follow-up to PR #13426
  • Loading branch information
yperbasis authored Jan 17, 2025
1 parent acb5eee commit 7b17a34
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 43 deletions.
10 changes: 3 additions & 7 deletions consensus/misc/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,9 @@ func (f eip1559Calculator) CurrentFees(chainConfig *chain.Config, db kv.Getter)
}

if currentHeader.ExcessBlobGas != nil {
var nextHeaderTime = currentHeader.Time + 1 // Speculative - Next header must be at least 1 second ahead
parentHeader := rawdb.ReadHeaderByNumber(db, currentHeader.Number.Uint64()-1)
if parentHeader != nil {
nextHeaderTime = currentHeader.Time + (currentHeader.Time - parentHeader.Time) // This difference should be close enough to seconds per slot
}
excessBlobGas := CalcExcessBlobGas(chainConfig, currentHeader, nextHeaderTime)
b, err := GetBlobGasPrice(chainConfig, excessBlobGas, nextHeaderTime)
nextBlockTime := currentHeader.Time + chainConfig.SecondsPerSlot()
excessBlobGas := CalcExcessBlobGas(chainConfig, currentHeader, nextBlockTime)
b, err := GetBlobGasPrice(chainConfig, excessBlobGas, nextBlockTime)
if err != nil {
return 0, 0, 0, 0, err
}
Expand Down
10 changes: 10 additions & 0 deletions erigon-lib/chain/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@ func (c *Config) GetMaxBlobsPerBlock(time uint64) uint64 {
return c.GetMaxBlobGasPerBlock(time) / fixedgas.BlobGasPerBlob
}

func (c *Config) SecondsPerSlot() uint64 {
if c.Bor != nil {
return 2 // Polygon
}
if c.Aura != nil {
return 5 // Gnosis
}
return 12 // Ethereum
}

// CheckCompatible checks whether scheduled fork transitions have been imported
// with a mismatching chain configuration.
func (c *Config) CheckCompatible(newcfg *Config, height uint64) *ConfigCompatError {
Expand Down
33 changes: 10 additions & 23 deletions eth/gasprice/feehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type blockFees struct {
blobBaseFee, nextBlobBaseFee *big.Int
gasUsedRatio float64
blobGasUsedRatio float64
secondsPerSlot uint64
err error
}

Expand Down Expand Up @@ -103,7 +102,8 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
bf.err = err
return
}
nextBlobBaseFee256, err := misc.GetBlobGasPrice(chainconfig, misc.CalcExcessBlobGas(chainconfig, bf.header, bf.header.Time+bf.secondsPerSlot), bf.header.Time+bf.secondsPerSlot)
nextBlockTime := bf.header.Time + chainconfig.SecondsPerSlot()
nextBlobBaseFee256, err := misc.GetBlobGasPrice(chainconfig, misc.CalcExcessBlobGas(chainconfig, bf.header, nextBlockTime), nextBlockTime)
if err != nil {
bf.err = err
return
Expand Down Expand Up @@ -169,51 +169,38 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
// also returned if requested and available.
// Note: an error is only returned if retrieving the head header has failed. If there are no
// retrievable blocks in the specified range then zero block count is returned with no error.
func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.BlockNumber, blocks, maxHistory int) (*types.Block, []*types.Receipt, uint64, int, uint64, error) {
func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.BlockNumber, blocks, maxHistory int) (*types.Block, []*types.Receipt, uint64, int, error) {
var (
headBlock rpc.BlockNumber
pendingBlock *types.Block
pendingReceipts types.Receipts
secondsPerSlot uint64 // Time diff from parent block as an approx
lastBlockTime uint64
)
// query either pending block or head header and set headBlock
if lastBlock == rpc.PendingBlockNumber {
if pendingBlock, pendingReceipts = oracle.backend.PendingBlockAndReceipts(); pendingBlock != nil {
lastBlock = rpc.BlockNumber(pendingBlock.NumberU64())
headBlock = lastBlock - 1
lastBlockTime = pendingBlock.Time()
} else {
// pending block not supported by backend, process until latest block
lastBlock = rpc.LatestBlockNumber
blocks--
if blocks == 0 {
return nil, nil, 0, 0, 0, nil
return nil, nil, 0, 0, nil
}
}
}
if pendingBlock == nil {
// if pending block is not fetched then we retrieve the head header to get the head block number
if latestHeader, err := oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber); err == nil {
headBlock = rpc.BlockNumber(latestHeader.Number.Uint64())
lastBlockTime = latestHeader.Time
} else {
return nil, nil, 0, 0, 0, err
return nil, nil, 0, 0, err
}
}
if lastBlock == rpc.LatestBlockNumber {
lastBlock = headBlock
} else if pendingBlock == nil && lastBlock > headBlock {
return nil, nil, 0, 0, 0, fmt.Errorf("%w: requested %d, head %d", ErrRequestBeyondHead, lastBlock, headBlock)
}
if lastBlock > 0 {
parentHeader, err := oracle.backend.HeaderByNumber(ctx, lastBlock-1)
if err != nil {
return nil, nil, 0, 0, 0, err
}
if parentHeader != nil {
secondsPerSlot = parentHeader.Time - lastBlockTime
}
return nil, nil, 0, 0, fmt.Errorf("%w: requested %d, head %d", ErrRequestBeyondHead, lastBlock, headBlock)
}
if maxHistory != 0 {
// limit retrieval to the given number of latest blocks
Expand All @@ -222,15 +209,15 @@ func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.Block
if int64(blocks) > tooOldCount {
blocks -= int(tooOldCount)
} else {
return nil, nil, 0, 0, 0, nil
return nil, nil, 0, 0, nil
}
}
}
// ensure not trying to retrieve before genesis
if rpc.BlockNumber(blocks) > lastBlock+1 {
blocks = int(lastBlock + 1)
}
return pendingBlock, pendingReceipts, uint64(lastBlock), blocks, secondsPerSlot, nil
return pendingBlock, pendingReceipts, uint64(lastBlock), blocks, nil
}

// FeeHistory returns data relevant for fee estimation based on the specified range of blocks.
Expand Down Expand Up @@ -276,7 +263,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast
pendingReceipts []*types.Receipt
err error
)
pendingBlock, pendingReceipts, lastBlock, blocks, secondsPerSlot, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks, maxHistory)
pendingBlock, pendingReceipts, lastBlock, blocks, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks, maxHistory)
if err != nil || blocks == 0 {
return libcommon.Big0, nil, nil, nil, nil, nil, err
}
Expand All @@ -303,7 +290,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast
continue
}

fees := &blockFees{blockNumber: blockNumber, secondsPerSlot: secondsPerSlot}
fees := &blockFees{blockNumber: blockNumber}
if pendingBlock != nil && blockNumber >= pendingBlock.NumberU64() {
fees.block, fees.receipts = pendingBlock, pendingReceipts
} else {
Expand Down
4 changes: 1 addition & 3 deletions turbo/jsonrpc/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ import (
"github.com/erigontech/erigon-lib/kv"
"github.com/erigontech/erigon-lib/kv/rawdbv3"
"github.com/erigontech/erigon-lib/log/v3"

"github.com/erigontech/erigon-lib/rlp"
"github.com/erigontech/erigon/cl/clparams"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/rawdb"
"github.com/erigontech/erigon/core/state"
Expand Down Expand Up @@ -122,7 +120,7 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat

blockNumber := stateBlockNumber + 1

timestamp := parent.Time + clparams.MainnetBeaconConfig.SecondsPerSlot
timestamp := parent.Time + chainConfig.SecondsPerSlot()

coinbase := parent.Coinbase
header := &types.Header{
Expand Down
6 changes: 1 addition & 5 deletions turbo/jsonrpc/eth_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,7 @@ func (api *APIImpl) BlobBaseFee(ctx context.Context) (*hexutil.Big, error) {
if config == nil {
return (*hexutil.Big)(common.Big0), nil
}
nextBlockTime := header.Time + 1 // At least 1 second ahead
parent := rawdb.ReadHeaderByNumber(tx, header.Number.Uint64()-1)
if parent != nil {
nextBlockTime = header.Time + (header.Time - parent.Time) // Close enough to seconds per slot
}
nextBlockTime := header.Time + config.SecondsPerSlot()
ret256, err := misc.GetBlobGasPrice(config, misc.CalcExcessBlobGas(config, header, nextBlockTime), nextBlockTime)
if err != nil {
return nil, err
Expand Down
6 changes: 1 addition & 5 deletions turbo/stages/stageloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,7 @@ func (h *Hook) sendNotifications(tx kv.Tx, finishStageBeforeSync uint64) error {
pendingBaseFee := misc.CalcBaseFee(h.chainConfig, currentHeader)
pendingBlobFee := h.chainConfig.GetMinBlobGasPrice()
if currentHeader.ExcessBlobGas != nil {
nextBlockTime := currentHeader.Time + 1
parentHeader := rawdb.ReadHeaderByNumber(tx, currentHeader.Number.Uint64())
if parentHeader != nil {
nextBlockTime = currentHeader.Time + (currentHeader.Time - parentHeader.Time) // Approximately next block time
}
nextBlockTime := currentHeader.Time + h.chainConfig.SecondsPerSlot()
excessBlobGas := misc.CalcExcessBlobGas(h.chainConfig, currentHeader, nextBlockTime)
f, err := misc.GetBlobGasPrice(h.chainConfig, excessBlobGas, nextBlockTime)
if err != nil {
Expand Down

0 comments on commit 7b17a34

Please sign in to comment.