Skip to content

Commit 7b17a34

Browse files
authored
Make secondsPerSlot available in chain config (#13467)
Follow-up to PR #13426
1 parent acb5eee commit 7b17a34

File tree

6 files changed

+26
-43
lines changed

6 files changed

+26
-43
lines changed

consensus/misc/eip1559.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,9 @@ func (f eip1559Calculator) CurrentFees(chainConfig *chain.Config, db kv.Getter)
8787
}
8888

8989
if currentHeader.ExcessBlobGas != nil {
90-
var nextHeaderTime = currentHeader.Time + 1 // Speculative - Next header must be at least 1 second ahead
91-
parentHeader := rawdb.ReadHeaderByNumber(db, currentHeader.Number.Uint64()-1)
92-
if parentHeader != nil {
93-
nextHeaderTime = currentHeader.Time + (currentHeader.Time - parentHeader.Time) // This difference should be close enough to seconds per slot
94-
}
95-
excessBlobGas := CalcExcessBlobGas(chainConfig, currentHeader, nextHeaderTime)
96-
b, err := GetBlobGasPrice(chainConfig, excessBlobGas, nextHeaderTime)
90+
nextBlockTime := currentHeader.Time + chainConfig.SecondsPerSlot()
91+
excessBlobGas := CalcExcessBlobGas(chainConfig, currentHeader, nextBlockTime)
92+
b, err := GetBlobGasPrice(chainConfig, excessBlobGas, nextBlockTime)
9793
if err != nil {
9894
return 0, 0, 0, 0, err
9995
}

erigon-lib/chain/chain_config.go

+10
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,16 @@ func (c *Config) GetMaxBlobsPerBlock(time uint64) uint64 {
316316
return c.GetMaxBlobGasPerBlock(time) / fixedgas.BlobGasPerBlob
317317
}
318318

319+
func (c *Config) SecondsPerSlot() uint64 {
320+
if c.Bor != nil {
321+
return 2 // Polygon
322+
}
323+
if c.Aura != nil {
324+
return 5 // Gnosis
325+
}
326+
return 12 // Ethereum
327+
}
328+
319329
// CheckCompatible checks whether scheduled fork transitions have been imported
320330
// with a mismatching chain configuration.
321331
func (c *Config) CheckCompatible(newcfg *Config, height uint64) *ConfigCompatError {

eth/gasprice/feehistory.go

+10-23
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ type blockFees struct {
6161
blobBaseFee, nextBlobBaseFee *big.Int
6262
gasUsedRatio float64
6363
blobGasUsedRatio float64
64-
secondsPerSlot uint64
6564
err error
6665
}
6766

@@ -103,7 +102,8 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
103102
bf.err = err
104103
return
105104
}
106-
nextBlobBaseFee256, err := misc.GetBlobGasPrice(chainconfig, misc.CalcExcessBlobGas(chainconfig, bf.header, bf.header.Time+bf.secondsPerSlot), bf.header.Time+bf.secondsPerSlot)
105+
nextBlockTime := bf.header.Time + chainconfig.SecondsPerSlot()
106+
nextBlobBaseFee256, err := misc.GetBlobGasPrice(chainconfig, misc.CalcExcessBlobGas(chainconfig, bf.header, nextBlockTime), nextBlockTime)
107107
if err != nil {
108108
bf.err = err
109109
return
@@ -169,51 +169,38 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
169169
// also returned if requested and available.
170170
// Note: an error is only returned if retrieving the head header has failed. If there are no
171171
// retrievable blocks in the specified range then zero block count is returned with no error.
172-
func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.BlockNumber, blocks, maxHistory int) (*types.Block, []*types.Receipt, uint64, int, uint64, error) {
172+
func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.BlockNumber, blocks, maxHistory int) (*types.Block, []*types.Receipt, uint64, int, error) {
173173
var (
174174
headBlock rpc.BlockNumber
175175
pendingBlock *types.Block
176176
pendingReceipts types.Receipts
177-
secondsPerSlot uint64 // Time diff from parent block as an approx
178-
lastBlockTime uint64
179177
)
180178
// query either pending block or head header and set headBlock
181179
if lastBlock == rpc.PendingBlockNumber {
182180
if pendingBlock, pendingReceipts = oracle.backend.PendingBlockAndReceipts(); pendingBlock != nil {
183181
lastBlock = rpc.BlockNumber(pendingBlock.NumberU64())
184182
headBlock = lastBlock - 1
185-
lastBlockTime = pendingBlock.Time()
186183
} else {
187184
// pending block not supported by backend, process until latest block
188185
lastBlock = rpc.LatestBlockNumber
189186
blocks--
190187
if blocks == 0 {
191-
return nil, nil, 0, 0, 0, nil
188+
return nil, nil, 0, 0, nil
192189
}
193190
}
194191
}
195192
if pendingBlock == nil {
196193
// if pending block is not fetched then we retrieve the head header to get the head block number
197194
if latestHeader, err := oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber); err == nil {
198195
headBlock = rpc.BlockNumber(latestHeader.Number.Uint64())
199-
lastBlockTime = latestHeader.Time
200196
} else {
201-
return nil, nil, 0, 0, 0, err
197+
return nil, nil, 0, 0, err
202198
}
203199
}
204200
if lastBlock == rpc.LatestBlockNumber {
205201
lastBlock = headBlock
206202
} else if pendingBlock == nil && lastBlock > headBlock {
207-
return nil, nil, 0, 0, 0, fmt.Errorf("%w: requested %d, head %d", ErrRequestBeyondHead, lastBlock, headBlock)
208-
}
209-
if lastBlock > 0 {
210-
parentHeader, err := oracle.backend.HeaderByNumber(ctx, lastBlock-1)
211-
if err != nil {
212-
return nil, nil, 0, 0, 0, err
213-
}
214-
if parentHeader != nil {
215-
secondsPerSlot = parentHeader.Time - lastBlockTime
216-
}
203+
return nil, nil, 0, 0, fmt.Errorf("%w: requested %d, head %d", ErrRequestBeyondHead, lastBlock, headBlock)
217204
}
218205
if maxHistory != 0 {
219206
// limit retrieval to the given number of latest blocks
@@ -222,15 +209,15 @@ func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.Block
222209
if int64(blocks) > tooOldCount {
223210
blocks -= int(tooOldCount)
224211
} else {
225-
return nil, nil, 0, 0, 0, nil
212+
return nil, nil, 0, 0, nil
226213
}
227214
}
228215
}
229216
// ensure not trying to retrieve before genesis
230217
if rpc.BlockNumber(blocks) > lastBlock+1 {
231218
blocks = int(lastBlock + 1)
232219
}
233-
return pendingBlock, pendingReceipts, uint64(lastBlock), blocks, secondsPerSlot, nil
220+
return pendingBlock, pendingReceipts, uint64(lastBlock), blocks, nil
234221
}
235222

236223
// FeeHistory returns data relevant for fee estimation based on the specified range of blocks.
@@ -276,7 +263,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast
276263
pendingReceipts []*types.Receipt
277264
err error
278265
)
279-
pendingBlock, pendingReceipts, lastBlock, blocks, secondsPerSlot, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks, maxHistory)
266+
pendingBlock, pendingReceipts, lastBlock, blocks, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks, maxHistory)
280267
if err != nil || blocks == 0 {
281268
return libcommon.Big0, nil, nil, nil, nil, nil, err
282269
}
@@ -303,7 +290,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast
303290
continue
304291
}
305292

306-
fees := &blockFees{blockNumber: blockNumber, secondsPerSlot: secondsPerSlot}
293+
fees := &blockFees{blockNumber: blockNumber}
307294
if pendingBlock != nil && blockNumber >= pendingBlock.NumberU64() {
308295
fees.block, fees.receipts = pendingBlock, pendingReceipts
309296
} else {

turbo/jsonrpc/eth_block.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ import (
3030
"github.com/erigontech/erigon-lib/kv"
3131
"github.com/erigontech/erigon-lib/kv/rawdbv3"
3232
"github.com/erigontech/erigon-lib/log/v3"
33-
3433
"github.com/erigontech/erigon-lib/rlp"
35-
"github.com/erigontech/erigon/cl/clparams"
3634
"github.com/erigontech/erigon/core"
3735
"github.com/erigontech/erigon/core/rawdb"
3836
"github.com/erigontech/erigon/core/state"
@@ -122,7 +120,7 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat
122120

123121
blockNumber := stateBlockNumber + 1
124122

125-
timestamp := parent.Time + clparams.MainnetBeaconConfig.SecondsPerSlot
123+
timestamp := parent.Time + chainConfig.SecondsPerSlot()
126124

127125
coinbase := parent.Coinbase
128126
header := &types.Header{

turbo/jsonrpc/eth_system.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,7 @@ func (api *APIImpl) BlobBaseFee(ctx context.Context) (*hexutil.Big, error) {
215215
if config == nil {
216216
return (*hexutil.Big)(common.Big0), nil
217217
}
218-
nextBlockTime := header.Time + 1 // At least 1 second ahead
219-
parent := rawdb.ReadHeaderByNumber(tx, header.Number.Uint64()-1)
220-
if parent != nil {
221-
nextBlockTime = header.Time + (header.Time - parent.Time) // Close enough to seconds per slot
222-
}
218+
nextBlockTime := header.Time + config.SecondsPerSlot()
223219
ret256, err := misc.GetBlobGasPrice(config, misc.CalcExcessBlobGas(config, header, nextBlockTime), nextBlockTime)
224220
if err != nil {
225221
return nil, err

turbo/stages/stageloop.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,7 @@ func (h *Hook) sendNotifications(tx kv.Tx, finishStageBeforeSync uint64) error {
483483
pendingBaseFee := misc.CalcBaseFee(h.chainConfig, currentHeader)
484484
pendingBlobFee := h.chainConfig.GetMinBlobGasPrice()
485485
if currentHeader.ExcessBlobGas != nil {
486-
nextBlockTime := currentHeader.Time + 1
487-
parentHeader := rawdb.ReadHeaderByNumber(tx, currentHeader.Number.Uint64())
488-
if parentHeader != nil {
489-
nextBlockTime = currentHeader.Time + (currentHeader.Time - parentHeader.Time) // Approximately next block time
490-
}
486+
nextBlockTime := currentHeader.Time + h.chainConfig.SecondsPerSlot()
491487
excessBlobGas := misc.CalcExcessBlobGas(h.chainConfig, currentHeader, nextBlockTime)
492488
f, err := misc.GetBlobGasPrice(h.chainConfig, excessBlobGas, nextBlockTime)
493489
if err != nil {

0 commit comments

Comments
 (0)