Skip to content

Commit

Permalink
Make secondsPerSlot available in EL config
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis committed Jan 16, 2025
1 parent a715eea commit 5fc3a8c
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 41 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
2 changes: 2 additions & 0 deletions erigon-lib/chain/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ type Config struct {
// See also EIP-6110: Supply validator deposits on chain
DepositContract common.Address `json:"depositContractAddress,omitempty"`

SecondsPerSlot uint64 `json:"secondsPerSlot"`

// Various consensus engines
Ethash *EthashConfig `json:"ethash,omitempty"`
Clique *CliqueConfig `json:"clique,omitempty"`
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
1 change: 1 addition & 0 deletions params/chainspecs/amoy.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"0": "0x000000000000000000000000000000000000dead",
"73100": "0xeCDD77cE6f146cCf5dab707941d318Bd50eeD2C9"
},
"secondsPerSlot": 2,
"bor": {
"period": {
"0": 2
Expand Down
1 change: 1 addition & 0 deletions params/chainspecs/bor-devnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"22640000": "0x70bcA57F4579f58670aB2d18Ef16e02C17553C38",
"41874000": "0x617b94CCCC2511808A3C9478ebb96f455CF167aA"
},
"secondsPerSlot": 2,
"bor": {
"period": {
"0": 2
Expand Down
1 change: 1 addition & 0 deletions params/chainspecs/bor-mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"23850000": "0x70bca57f4579f58670ab2d18ef16e02c17553c38",
"50523000": "0x7A8ed27F4C30512326878652d20fC85727401854"
},
"secondsPerSlot": 2,
"bor": {
"period": {
"0": 2
Expand Down
1 change: 1 addition & 0 deletions params/chainspecs/chiado.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"0": "0x1559000000000000000000000000000000000000"
},
"depositContractAddress": "0xb97036A26259B7147018913bD58a774cf91acf25",
"secondsPerSlot": 5,
"aura": {
"stepDuration": 5,
"blockReward": 0,
Expand Down
1 change: 1 addition & 0 deletions params/chainspecs/gnosis.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"19040000": "0x6BBe78ee9e474842Dbd4AB4987b3CeFE88426A92"
},
"depositContractAddress": "0x0B98057eA310F4d31F2a452B414647007d1645d9",
"secondsPerSlot": 5,
"aura": {
"stepDuration": 5,
"blockReward": 0,
Expand Down
3 changes: 2 additions & 1 deletion params/chainspecs/holesky.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"terminalTotalDifficultyPassed": true,
"shanghaiTime": 1696000704,
"cancunTime": 1707305664,
"depositContractAddress": "0x4242424242424242424242424242424242424242"
"depositContractAddress": "0x4242424242424242424242424242424242424242",
"secondsPerSlot": 12
}
1 change: 1 addition & 0 deletions params/chainspecs/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"shanghaiTime": 1681338455,
"cancunTime": 1710338135,
"depositContractAddress": "0x00000000219ab540356cBB839Cbe05303d7705Fa",
"secondsPerSlot": 12,
"ethash": {}
}
1 change: 1 addition & 0 deletions params/chainspecs/sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"shanghaiTime": 1677557088,
"cancunTime": 1706655072,
"depositContractAddress": "0x7f02C3E3c98b133055B8B348B2Ac625669Ed295D",
"secondsPerSlot": 12,
"ethash": {}
}
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 5fc3a8c

Please sign in to comment.