Skip to content

Commit b47cf8f

Browse files
s1naholiman
andauthored
internal/ethapi: fix defaults for blob fields (#29037)
Co-authored-by: Martin HS <[email protected]>
1 parent b9ca38b commit b47cf8f

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

internal/ethapi/transaction_args.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
177177

178178
// setFeeDefaults fills in default fee values for unspecified tx fields.
179179
func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) error {
180+
head := b.CurrentHeader()
181+
// Sanity check the EIP-4844 fee parameters.
182+
if args.BlobFeeCap != nil && args.BlobFeeCap.ToInt().Sign() == 0 {
183+
return errors.New("maxFeePerBlobGas, if specified, must be non-zero")
184+
}
185+
if err := args.setCancunFeeDefaults(ctx, head, b); err != nil {
186+
return err
187+
}
180188
// If both gasPrice and at least one of the EIP-1559 fee parameters are specified, error.
181189
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
182190
return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
@@ -186,7 +194,6 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
186194
// other tx values. See https://github.com/ethereum/go-ethereum/pull/23274
187195
// for more information.
188196
eip1559ParamsSet := args.MaxFeePerGas != nil && args.MaxPriorityFeePerGas != nil
189-
190197
// Sanity check the EIP-1559 fee parameters if present.
191198
if args.GasPrice == nil && eip1559ParamsSet {
192199
if args.MaxFeePerGas.ToInt().Sign() == 0 {
@@ -198,13 +205,7 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
198205
return nil // No need to set anything, user already set MaxFeePerGas and MaxPriorityFeePerGas
199206
}
200207

201-
// Sanity check the EIP-4844 fee parameters.
202-
if args.BlobFeeCap != nil && args.BlobFeeCap.ToInt().Sign() == 0 {
203-
return errors.New("maxFeePerBlobGas must be non-zero")
204-
}
205-
206208
// Sanity check the non-EIP-1559 fee parameters.
207-
head := b.CurrentHeader()
208209
isLondon := b.ChainConfig().IsLondon(head.Number)
209210
if args.GasPrice != nil && !eip1559ParamsSet {
210211
// Zero gas-price is not allowed after London fork
@@ -215,21 +216,14 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
215216
}
216217

217218
// Now attempt to fill in default value depending on whether London is active or not.
218-
if b.ChainConfig().IsCancun(head.Number, head.Time) {
219-
if err := args.setCancunFeeDefaults(ctx, head, b); err != nil {
220-
return err
221-
}
222-
} else if isLondon {
223-
if args.BlobFeeCap != nil {
224-
return errors.New("maxFeePerBlobGas is not valid before Cancun is active")
225-
}
219+
if isLondon {
226220
// London is active, set maxPriorityFeePerGas and maxFeePerGas.
227221
if err := args.setLondonFeeDefaults(ctx, head, b); err != nil {
228222
return err
229223
}
230224
} else {
231-
if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil || args.BlobFeeCap != nil {
232-
return errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active")
225+
if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil {
226+
return errors.New("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active")
233227
}
234228
// London not active, set gas price.
235229
price, err := b.SuggestGasTipCap(ctx)
@@ -245,15 +239,19 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
245239
func (args *TransactionArgs) setCancunFeeDefaults(ctx context.Context, head *types.Header, b Backend) error {
246240
// Set maxFeePerBlobGas if it is missing.
247241
if args.BlobHashes != nil && args.BlobFeeCap == nil {
242+
var excessBlobGas uint64
243+
if head.ExcessBlobGas != nil {
244+
excessBlobGas = *head.ExcessBlobGas
245+
}
248246
// ExcessBlobGas must be set for a Cancun block.
249-
blobBaseFee := eip4844.CalcBlobFee(*head.ExcessBlobGas)
247+
blobBaseFee := eip4844.CalcBlobFee(excessBlobGas)
250248
// Set the max fee to be 2 times larger than the previous block's blob base fee.
251249
// The additional slack allows the tx to not become invalidated if the base
252250
// fee is rising.
253251
val := new(big.Int).Mul(blobBaseFee, big.NewInt(2))
254252
args.BlobFeeCap = (*hexutil.Big)(val)
255253
}
256-
return args.setLondonFeeDefaults(ctx, head, b)
254+
return nil
257255
}
258256

259257
// setLondonFeeDefaults fills in reasonable default fee values for unspecified fields.

internal/ethapi/transaction_args_test.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ func TestSetFeeDefaults(t *testing.T) {
153153
"legacy",
154154
&TransactionArgs{MaxFeePerGas: maxFee},
155155
nil,
156-
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"),
156+
errors.New("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"),
157157
},
158158
{
159159
"dynamic fee tx pre-London, priorityFee set",
160160
"legacy",
161161
&TransactionArgs{MaxPriorityFeePerGas: fortytwo},
162162
nil,
163-
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"),
163+
errors.New("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"),
164164
},
165165
{
166166
"dynamic fee tx, maxFee < priorityFee",
@@ -207,20 +207,6 @@ func TestSetFeeDefaults(t *testing.T) {
207207
errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"),
208208
},
209209
// EIP-4844
210-
{
211-
"set maxFeePerBlobGas pre cancun",
212-
"london",
213-
&TransactionArgs{BlobFeeCap: fortytwo},
214-
nil,
215-
errors.New("maxFeePerBlobGas is not valid before Cancun is active"),
216-
},
217-
{
218-
"set maxFeePerBlobGas pre london",
219-
"legacy",
220-
&TransactionArgs{BlobFeeCap: fortytwo},
221-
nil,
222-
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"),
223-
},
224210
{
225211
"set gas price and maxFee for blob transaction",
226212
"cancun",
@@ -235,6 +221,13 @@ func TestSetFeeDefaults(t *testing.T) {
235221
&TransactionArgs{BlobHashes: []common.Hash{}, BlobFeeCap: (*hexutil.Big)(big.NewInt(4)), MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
236222
nil,
237223
},
224+
{
225+
"fill maxFeePerBlobGas when dynamic fees are set",
226+
"cancun",
227+
&TransactionArgs{BlobHashes: []common.Hash{}, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
228+
&TransactionArgs{BlobHashes: []common.Hash{}, BlobFeeCap: (*hexutil.Big)(big.NewInt(4)), MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
229+
nil,
230+
},
238231
}
239232

240233
ctx := context.Background()
@@ -244,11 +237,16 @@ func TestSetFeeDefaults(t *testing.T) {
244237
}
245238
got := test.in
246239
err := got.setFeeDefaults(ctx, b)
247-
if err != nil && err.Error() == test.err.Error() {
248-
// Test threw expected error.
240+
if err != nil {
241+
if test.err == nil {
242+
t.Fatalf("test %d (%s): unexpected error: %s", i, test.name, err)
243+
} else if err.Error() != test.err.Error() {
244+
t.Fatalf("test %d (%s): unexpected error: (got: %s, want: %s)", i, test.name, err, test.err)
245+
}
246+
// Matching error.
249247
continue
250-
} else if err != nil {
251-
t.Fatalf("test %d (%s): unexpected error: %s", i, test.name, err)
248+
} else if test.err != nil {
249+
t.Fatalf("test %d (%s): expected error: %s", i, test.name, test.err)
252250
}
253251
if !reflect.DeepEqual(got, test.want) {
254252
t.Fatalf("test %d (%s): did not fill defaults as expected: (got: %v, want: %v)", i, test.name, got, test.want)

0 commit comments

Comments
 (0)