consensus/misc/eip4844: more changes for blob gas calculation (#31128)
This PR changes the signature of `CalcExcessBlobGas` to take in just the header timestamp instead of the whole object. It also adds a sanity check for the parent->child block order to `VerifyEIP4844Header`.
This commit is contained in:
parent
c4ad459bd2
commit
ed1d46b3d3
|
@ -198,7 +198,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
|
||||||
Time: pre.Env.Timestamp,
|
Time: pre.Env.Timestamp,
|
||||||
ExcessBlobGas: &excessBlobGas,
|
ExcessBlobGas: &excessBlobGas,
|
||||||
}
|
}
|
||||||
excessBlobGas = eip4844.CalcExcessBlobGas(chainConfig, parent, header)
|
excessBlobGas = eip4844.CalcExcessBlobGas(chainConfig, parent, header.Time)
|
||||||
vmContext.BlobBaseFee = eip4844.CalcBlobFee(chainConfig, header)
|
vmContext.BlobBaseFee = eip4844.CalcBlobFee(chainConfig, header)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,9 @@ var (
|
||||||
// if the current block contains no transactions, the excessBlobGas is updated
|
// if the current block contains no transactions, the excessBlobGas is updated
|
||||||
// accordingly.
|
// accordingly.
|
||||||
func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Header) error {
|
func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Header) error {
|
||||||
|
if header.Number.Uint64() != parent.Number.Uint64()+1 {
|
||||||
|
panic("bad header pair")
|
||||||
|
}
|
||||||
// Verify the header is not malformed
|
// Verify the header is not malformed
|
||||||
if header.ExcessBlobGas == nil {
|
if header.ExcessBlobGas == nil {
|
||||||
return errors.New("header is missing excessBlobGas")
|
return errors.New("header is missing excessBlobGas")
|
||||||
|
@ -50,7 +53,7 @@ func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Heade
|
||||||
return fmt.Errorf("blob gas used %d not a multiple of blob gas per blob %d", header.BlobGasUsed, params.BlobTxBlobGasPerBlob)
|
return fmt.Errorf("blob gas used %d not a multiple of blob gas per blob %d", header.BlobGasUsed, params.BlobTxBlobGasPerBlob)
|
||||||
}
|
}
|
||||||
// Verify the excessBlobGas is correct based on the parent header
|
// Verify the excessBlobGas is correct based on the parent header
|
||||||
expectedExcessBlobGas := CalcExcessBlobGas(config, parent, header)
|
expectedExcessBlobGas := CalcExcessBlobGas(config, parent, header.Time)
|
||||||
if *header.ExcessBlobGas != expectedExcessBlobGas {
|
if *header.ExcessBlobGas != expectedExcessBlobGas {
|
||||||
return fmt.Errorf("invalid excessBlobGas: have %d, want %d", *header.ExcessBlobGas, expectedExcessBlobGas)
|
return fmt.Errorf("invalid excessBlobGas: have %d, want %d", *header.ExcessBlobGas, expectedExcessBlobGas)
|
||||||
}
|
}
|
||||||
|
@ -59,9 +62,8 @@ func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Heade
|
||||||
|
|
||||||
// CalcExcessBlobGas calculates the excess blob gas after applying the set of
|
// CalcExcessBlobGas calculates the excess blob gas after applying the set of
|
||||||
// blobs on top of the excess blob gas.
|
// blobs on top of the excess blob gas.
|
||||||
func CalcExcessBlobGas(config *params.ChainConfig, parent, header *types.Header) uint64 {
|
func CalcExcessBlobGas(config *params.ChainConfig, parent *types.Header, headTimestamp uint64) uint64 {
|
||||||
var (
|
var (
|
||||||
targetGas = uint64(targetBlobsPerBlock(config, header.Time)) * params.BlobTxBlobGasPerBlob
|
|
||||||
parentExcessBlobGas uint64
|
parentExcessBlobGas uint64
|
||||||
parentBlobGasUsed uint64
|
parentBlobGasUsed uint64
|
||||||
)
|
)
|
||||||
|
@ -70,6 +72,7 @@ func CalcExcessBlobGas(config *params.ChainConfig, parent, header *types.Header)
|
||||||
parentBlobGasUsed = *parent.BlobGasUsed
|
parentBlobGasUsed = *parent.BlobGasUsed
|
||||||
}
|
}
|
||||||
excessBlobGas := parentExcessBlobGas + parentBlobGasUsed
|
excessBlobGas := parentExcessBlobGas + parentBlobGasUsed
|
||||||
|
targetGas := uint64(targetBlobsPerBlock(config, headTimestamp)) * params.BlobTxBlobGasPerBlob
|
||||||
if excessBlobGas < targetGas {
|
if excessBlobGas < targetGas {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,15 +57,11 @@ func TestCalcExcessBlobGas(t *testing.T) {
|
||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
blobGasUsed := uint64(tt.blobs) * params.BlobTxBlobGasPerBlob
|
blobGasUsed := uint64(tt.blobs) * params.BlobTxBlobGasPerBlob
|
||||||
head := &types.Header{
|
header := &types.Header{
|
||||||
Time: *config.CancunTime,
|
|
||||||
}
|
|
||||||
parent := &types.Header{
|
|
||||||
Time: *config.CancunTime,
|
|
||||||
ExcessBlobGas: &tt.excess,
|
ExcessBlobGas: &tt.excess,
|
||||||
BlobGasUsed: &blobGasUsed,
|
BlobGasUsed: &blobGasUsed,
|
||||||
}
|
}
|
||||||
result := CalcExcessBlobGas(config, parent, head)
|
result := CalcExcessBlobGas(config, header, *config.CancunTime)
|
||||||
if result != tt.want {
|
if result != tt.want {
|
||||||
t.Errorf("test %d: excess blob gas mismatch: have %v, want %v", i, result, tt.want)
|
t.Errorf("test %d: excess blob gas mismatch: have %v, want %v", i, result, tt.want)
|
||||||
}
|
}
|
||||||
|
|
|
@ -582,25 +582,26 @@ func GenerateVerkleChainWithGenesis(genesis *Genesis, engine consensus.Engine, n
|
||||||
|
|
||||||
func (cm *chainMaker) makeHeader(parent *types.Block, state *state.StateDB, engine consensus.Engine) *types.Header {
|
func (cm *chainMaker) makeHeader(parent *types.Block, state *state.StateDB, engine consensus.Engine) *types.Header {
|
||||||
time := parent.Time() + 10 // block time is fixed at 10 seconds
|
time := parent.Time() + 10 // block time is fixed at 10 seconds
|
||||||
|
parentHeader := parent.Header()
|
||||||
header := &types.Header{
|
header := &types.Header{
|
||||||
Root: state.IntermediateRoot(cm.config.IsEIP158(parent.Number())),
|
Root: state.IntermediateRoot(cm.config.IsEIP158(parent.Number())),
|
||||||
ParentHash: parent.Hash(),
|
ParentHash: parent.Hash(),
|
||||||
Coinbase: parent.Coinbase(),
|
Coinbase: parent.Coinbase(),
|
||||||
Difficulty: engine.CalcDifficulty(cm, time, parent.Header()),
|
Difficulty: engine.CalcDifficulty(cm, time, parentHeader),
|
||||||
GasLimit: parent.GasLimit(),
|
GasLimit: parent.GasLimit(),
|
||||||
Number: new(big.Int).Add(parent.Number(), common.Big1),
|
Number: new(big.Int).Add(parent.Number(), common.Big1),
|
||||||
Time: time,
|
Time: time,
|
||||||
}
|
}
|
||||||
|
|
||||||
if cm.config.IsLondon(header.Number) {
|
if cm.config.IsLondon(header.Number) {
|
||||||
header.BaseFee = eip1559.CalcBaseFee(cm.config, parent.Header())
|
header.BaseFee = eip1559.CalcBaseFee(cm.config, parentHeader)
|
||||||
if !cm.config.IsLondon(parent.Number()) {
|
if !cm.config.IsLondon(parent.Number()) {
|
||||||
parentGasLimit := parent.GasLimit() * cm.config.ElasticityMultiplier()
|
parentGasLimit := parent.GasLimit() * cm.config.ElasticityMultiplier()
|
||||||
header.GasLimit = CalcGasLimit(parentGasLimit, parentGasLimit)
|
header.GasLimit = CalcGasLimit(parentGasLimit, parentGasLimit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if cm.config.IsCancun(header.Number, header.Time) {
|
if cm.config.IsCancun(header.Number, header.Time) {
|
||||||
excessBlobGas := eip4844.CalcExcessBlobGas(cm.config, parent.Header(), header)
|
excessBlobGas := eip4844.CalcExcessBlobGas(cm.config, parentHeader, time)
|
||||||
header.ExcessBlobGas = &excessBlobGas
|
header.ExcessBlobGas = &excessBlobGas
|
||||||
header.BlobGasUsed = new(uint64)
|
header.BlobGasUsed = new(uint64)
|
||||||
header.ParentBeaconRoot = new(common.Hash)
|
header.ParentBeaconRoot = new(common.Hash)
|
||||||
|
|
|
@ -407,7 +407,7 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr
|
||||||
}
|
}
|
||||||
header.Root = common.BytesToHash(hasher.Sum(nil))
|
header.Root = common.BytesToHash(hasher.Sum(nil))
|
||||||
if config.IsCancun(header.Number, header.Time) {
|
if config.IsCancun(header.Number, header.Time) {
|
||||||
excess := eip4844.CalcExcessBlobGas(config, parent.Header(), header)
|
excess := eip4844.CalcExcessBlobGas(config, parent.Header(), header.Time)
|
||||||
used := uint64(nBlobs * params.BlobTxBlobGasPerBlob)
|
used := uint64(nBlobs * params.BlobTxBlobGasPerBlob)
|
||||||
header.ExcessBlobGas = &excess
|
header.ExcessBlobGas = &excess
|
||||||
header.BlobGasUsed = &used
|
header.BlobGasUsed = &used
|
||||||
|
|
|
@ -97,7 +97,7 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
|
||||||
// Fill in blob base fee and next blob base fee.
|
// Fill in blob base fee and next blob base fee.
|
||||||
if excessBlobGas := bf.header.ExcessBlobGas; excessBlobGas != nil {
|
if excessBlobGas := bf.header.ExcessBlobGas; excessBlobGas != nil {
|
||||||
bf.results.blobBaseFee = eip4844.CalcBlobFee(config, bf.header)
|
bf.results.blobBaseFee = eip4844.CalcBlobFee(config, bf.header)
|
||||||
excess := eip4844.CalcExcessBlobGas(config, bf.header, bf.header)
|
excess := eip4844.CalcExcessBlobGas(config, bf.header, bf.header.Time)
|
||||||
next := &types.Header{Number: bf.header.Number, Time: bf.header.Time, ExcessBlobGas: &excess}
|
next := &types.Header{Number: bf.header.Number, Time: bf.header.Time, ExcessBlobGas: &excess}
|
||||||
bf.results.nextBlobBaseFee = eip4844.CalcBlobFee(config, next)
|
bf.results.nextBlobBaseFee = eip4844.CalcBlobFee(config, next)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -55,7 +55,7 @@ func (c *callContext) toBlockContext(genesis *core.Genesis) vm.BlockContext {
|
||||||
|
|
||||||
if genesis.ExcessBlobGas != nil && genesis.BlobGasUsed != nil {
|
if genesis.ExcessBlobGas != nil && genesis.BlobGasUsed != nil {
|
||||||
header := &types.Header{Number: genesis.Config.LondonBlock, Time: *genesis.Config.CancunTime}
|
header := &types.Header{Number: genesis.Config.LondonBlock, Time: *genesis.Config.CancunTime}
|
||||||
excess := eip4844.CalcExcessBlobGas(genesis.Config, header, genesis.ToBlock().Header())
|
excess := eip4844.CalcExcessBlobGas(genesis.Config, header, genesis.Timestamp)
|
||||||
header.ExcessBlobGas = &excess
|
header.ExcessBlobGas = &excess
|
||||||
context.BlobBaseFee = eip4844.CalcBlobFee(genesis.Config, header)
|
context.BlobBaseFee = eip4844.CalcBlobFee(genesis.Config, header)
|
||||||
}
|
}
|
||||||
|
|
|
@ -159,7 +159,7 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header,
|
||||||
if sim.chainConfig.IsCancun(header.Number, header.Time) {
|
if sim.chainConfig.IsCancun(header.Number, header.Time) {
|
||||||
var excess uint64
|
var excess uint64
|
||||||
if sim.chainConfig.IsCancun(parent.Number, parent.Time) {
|
if sim.chainConfig.IsCancun(parent.Number, parent.Time) {
|
||||||
excess = eip4844.CalcExcessBlobGas(sim.chainConfig, parent, header)
|
excess = eip4844.CalcExcessBlobGas(sim.chainConfig, parent, header.Time)
|
||||||
}
|
}
|
||||||
header.ExcessBlobGas = &excess
|
header.ExcessBlobGas = &excess
|
||||||
}
|
}
|
||||||
|
|
|
@ -210,7 +210,7 @@ func (miner *Miner) prepareWork(genParams *generateParams, witness bool) (*envir
|
||||||
if miner.chainConfig.IsCancun(header.Number, header.Time) {
|
if miner.chainConfig.IsCancun(header.Number, header.Time) {
|
||||||
var excessBlobGas uint64
|
var excessBlobGas uint64
|
||||||
if miner.chainConfig.IsCancun(parent.Number, parent.Time) {
|
if miner.chainConfig.IsCancun(parent.Number, parent.Time) {
|
||||||
excessBlobGas = eip4844.CalcExcessBlobGas(miner.chainConfig, parent, header)
|
excessBlobGas = eip4844.CalcExcessBlobGas(miner.chainConfig, parent, timestamp)
|
||||||
}
|
}
|
||||||
header.BlobGasUsed = new(uint64)
|
header.BlobGasUsed = new(uint64)
|
||||||
header.ExcessBlobGas = &excessBlobGas
|
header.ExcessBlobGas = &excessBlobGas
|
||||||
|
|
Loading…
Reference in New Issue