@@ -19,8 +19,8 @@ package eip4844
19
19
import (
20
20
"errors"
21
21
"fmt"
22
+ "math"
22
23
"math/big"
23
- "slices"
24
24
25
25
"github.com/ethereum/go-ethereum/core/types"
26
26
"github.com/ethereum/go-ethereum/params"
@@ -38,15 +38,22 @@ func VerifyEIP4844Header(config *params.Config2, parent, header *types.Header) e
38
38
if header .Number .Uint64 () != parent .Number .Uint64 ()+ 1 {
39
39
panic ("bad header pair" )
40
40
}
41
+
41
42
// Verify the header is not malformed
42
43
if header .ExcessBlobGas == nil {
43
44
return errors .New ("header is missing excessBlobGas" )
44
45
}
45
46
if header .BlobGasUsed == nil {
46
47
return errors .New ("header is missing blobGasUsed" )
47
48
}
49
+
50
+ blobcfg := scheduleAtTime (config , header .Time )
51
+ if blobcfg == nil {
52
+ return fmt .Errorf ("blob schedule is undefined at time %d" , header .Time )
53
+ }
54
+
48
55
// Verify that the blob gas used remains within reasonable limits.
49
- maxBlobGas := MaxBlobGasPerBlock ( config , header . Time )
56
+ maxBlobGas := uint64 ( blobcfg . Max ) * params . BlobTxBlobGasPerBlob
50
57
if * header .BlobGasUsed > maxBlobGas {
51
58
return fmt .Errorf ("blob gas used %d exceeds maximum allowance %d" , * header .BlobGasUsed , maxBlobGas )
52
59
}
@@ -101,18 +108,21 @@ func CalcExcessBlobGas(config *params.Config2, parent *types.Header, headTimesta
101
108
102
109
// CalcBlobFee calculates the blobfee from the header's excess blob gas field.
103
110
func CalcBlobFee (config * params.Config2 , header * types.Header ) * big.Int {
104
- schedule := params . BlobSchedule . Get (config )
105
- if schedule == nil {
111
+ blobcfg := scheduleAtTime (config , header . Time )
112
+ if blobcfg == nil {
106
113
return new (big.Int )
107
114
}
108
- f := config .LatestFork (header .Time )
109
- frac := schedule [f ].UpdateFraction
115
+ frac := blobcfg .UpdateFraction
110
116
return fakeExponential (minBlobGasPrice , new (big.Int ).SetUint64 (* header .ExcessBlobGas ), new (big.Int ).SetUint64 (frac ))
111
117
}
112
118
113
119
// MaxBlobsPerBlock returns the max blobs per block for a block at the given timestamp.
114
120
func MaxBlobsPerBlock (cfg * params.Config2 , time uint64 ) int {
115
- return scheduleAtTime (cfg , time ).Max
121
+ blobcfg := scheduleAtTime (cfg , time )
122
+ if blobcfg == nil {
123
+ return 0
124
+ }
125
+ return blobcfg .Max
116
126
}
117
127
118
128
// MaxBlobsPerBlock returns the maximum blob gas that can be spent in a block at the given timestamp.
@@ -123,30 +133,38 @@ func MaxBlobGasPerBlock(cfg *params.Config2, time uint64) uint64 {
123
133
// LatestMaxBlobsPerBlock returns the latest max blobs per block defined by the
124
134
// configuration, regardless of the currently active fork.
125
135
func LatestMaxBlobsPerBlock (cfg * params.Config2 ) int {
126
- schedule := params .BlobSchedule .Get (cfg )
127
- if schedule == nil {
128
- return 0
129
- }
130
- for _ , f := range slices .Backward (forks .CanonOrder ) {
131
- if f .HasBlobs () && cfg .Scheduled (f ) {
132
- return schedule [f ].Max
133
- }
134
- }
135
- return 0
136
+ blobcfg := scheduleAtTime (cfg , math .MaxUint64 )
137
+ return blobcfg .Max
136
138
}
137
139
138
140
// targetBlobsPerBlock returns the target number of blobs in a block at the given timestamp.
139
141
func targetBlobsPerBlock (cfg * params.Config2 , time uint64 ) int {
140
142
return scheduleAtTime (cfg , time ).Target
141
143
}
142
144
143
- func scheduleAtTime (cfg * params.Config2 , time uint64 ) params.BlobConfig {
145
+ // scheduleAtTime resolves the blob schedule at the given timestamp.
146
+ func scheduleAtTime (cfg * params.Config2 , time uint64 ) * params.BlobConfig {
144
147
schedule := params .BlobSchedule .Get (cfg )
145
148
if schedule == nil {
146
- return params.BlobConfig {}
149
+ return nil
150
+ }
151
+
152
+ // Find the latest fork defined by the schedule.
153
+ forkList := make ([]forks.Fork , 0 , len (schedule ))
154
+ for f := range schedule {
155
+ act , ok := cfg .Activation (f )
156
+ if ok && act <= time {
157
+ forkList = append (forkList , f )
158
+ }
159
+ }
160
+ forkList = forks .DependencyOrder (forkList )
161
+
162
+ // Return the blob config of the last available fork.
163
+ if len (forkList ) == 0 {
164
+ return nil
147
165
}
148
- f := cfg . LatestFork ( time )
149
- return schedule [ f ]
166
+ blobcfg := schedule [ forkList [ len ( forkList ) - 1 ]]
167
+ return & blobcfg
150
168
}
151
169
152
170
// fakeExponential approximates factor * e ** (numerator / denominator) using
0 commit comments