Skip to content

Commit 018bd54

Browse files
address PR comments
1 parent 9830725 commit 018bd54

File tree

2 files changed

+27
-51
lines changed

2 files changed

+27
-51
lines changed

eth/gasestimator/gasestimator.go

+10-43
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ const EstimateGasErrorRatio = 0.015
4242
// these together, it would be excessively hard to test. Splitting the parts out
4343
// allows testing without needing a proper live chain.
4444
type Options struct {
45-
Config *params.ChainConfig // Chain configuration for hard fork selection
46-
Chain core.ChainContext // Chain context to access past block hashes
47-
Header *types.Header // Header defining the block context to execute in
48-
State *state.StateDB // Pre-state on top of which to estimate the gas
49-
Backend core.NodeInterfaceBackendAPI
45+
Config *params.ChainConfig // Chain configuration for hard fork selection
46+
Chain core.ChainContext // Chain context to access past block hashes
47+
Header *types.Header // Header defining the block context to execute in
48+
State *state.StateDB // Pre-state on top of which to estimate the gas
49+
Backend core.NodeInterfaceBackendAPI
50+
RunScheduledTxes func(context.Context, core.NodeInterfaceBackendAPI, *state.StateDB, *types.Header, vm.BlockContext, core.MessageRunMode, *core.ExecutionResult) (*core.ExecutionResult, error)
5051

5152
ErrorRatio float64 // Allowed overestimation ratio for faster estimation termination
5253
}
@@ -235,8 +236,7 @@ func run(ctx context.Context, call *core.Message, opts *Options) (*core.Executio
235236
evm.Cancel()
236237
}()
237238
// Execute the call, returning a wrapped error or the result
238-
gp := new(core.GasPool).AddGas(math.MaxUint64)
239-
result, err := core.ApplyMessage(evm, call, gp)
239+
result, err := core.ApplyMessage(evm, call, new(core.GasPool).AddGas(math.MaxUint64))
240240
if vmerr := dirtyState.Error(); vmerr != nil {
241241
return nil, vmerr
242242
}
@@ -245,42 +245,9 @@ func run(ctx context.Context, call *core.Message, opts *Options) (*core.Executio
245245
}
246246

247247
// Arbitrum: a tx can schedule another (see retryables)
248-
scheduled := result.ScheduledTxes
249-
for len(scheduled) > 0 {
250-
// This will panic if the scheduled tx is signed, but we only schedule unsigned ones
251-
msg, err := core.TransactionToMessage(scheduled[0], types.NewArbitrumSigner(nil), opts.Header.BaseFee)
252-
if err != nil {
253-
return nil, err
254-
}
255-
// The scheduling transaction will "use" all of the gas available to it,
256-
// but it's really just passing it on to the scheduled tx, so we subtract it out here.
257-
if result.UsedGas >= msg.GasLimit {
258-
result.UsedGas -= msg.GasLimit
259-
} else {
260-
log.Warn("Scheduling tx used less gas than scheduled tx has available", "usedGas", result.UsedGas, "scheduledGas", msg.GasLimit)
261-
result.UsedGas = 0
262-
}
263-
msg.TxRunMode = core.MessageGasEstimationMode
264-
// make a new EVM for the scheduled Tx (an EVM must never be reused)
265-
evm := opts.Backend.GetEVM(ctx, msg, dirtyState, opts.Header, &vm.Config{NoBaseFee: true}, &evmContext)
266-
go func() {
267-
<-ctx.Done()
268-
evm.Cancel()
269-
}()
270-
271-
scheduledTxResult, err := core.ApplyMessage(evm, msg, gp)
272-
if err != nil {
273-
return nil, err // Bail out
274-
}
275-
if err := dirtyState.Error(); err != nil {
276-
return nil, err
277-
}
278-
if scheduledTxResult.Failed() {
279-
return scheduledTxResult, nil
280-
}
281-
// Add back in any gas used by the scheduled transaction.
282-
result.UsedGas += scheduledTxResult.UsedGas
283-
scheduled = append(scheduled[1:], scheduledTxResult.ScheduledTxes...)
248+
result, err = opts.RunScheduledTxes(ctx, opts.Backend, dirtyState, opts.Header, evmContext, core.MessageGasEstimationMode, result)
249+
if err != nil {
250+
return nil, err
284251
}
285252

286253
return result, nil

internal/ethapi/api.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,15 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S
11291129
}
11301130

11311131
// Arbitrum: a tx can schedule another (see retryables)
1132+
result, err = runScheduledTxes(ctx, b, state, header, blockCtx, core.MessageGasEstimationMode, result)
1133+
if err != nil {
1134+
return nil, err
1135+
}
1136+
1137+
return result, nil
1138+
}
1139+
1140+
func runScheduledTxes(ctx context.Context, b core.NodeInterfaceBackendAPI, state *state.StateDB, header *types.Header, blockCtx vm.BlockContext, runMode core.MessageRunMode, result *core.ExecutionResult) (*core.ExecutionResult, error) {
11321141
scheduled := result.ScheduledTxes
11331142
for runMode == core.MessageGasEstimationMode && len(scheduled) > 0 {
11341143
// This will panic if the scheduled tx is signed, but we only schedule unsigned ones
@@ -1152,7 +1161,7 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S
11521161
evm.Cancel()
11531162
}()
11541163

1155-
scheduledTxResult, err := core.ApplyMessage(evm, msg, gp)
1164+
scheduledTxResult, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(math.MaxUint64))
11561165
if err != nil {
11571166
return nil, err // Bail out
11581167
}
@@ -1166,7 +1175,6 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S
11661175
result.UsedGas += scheduledTxResult.UsedGas
11671176
scheduled = append(scheduled[1:], scheduledTxResult.ScheduledTxes...)
11681177
}
1169-
11701178
return result, nil
11711179
}
11721180

@@ -1280,12 +1288,13 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
12801288

12811289
// Construct the gas estimator option from the user input
12821290
opts := &gasestimator.Options{
1283-
Config: b.ChainConfig(),
1284-
Chain: NewChainContext(ctx, b),
1285-
Header: header,
1286-
State: state,
1287-
Backend: b,
1288-
ErrorRatio: gasestimator.EstimateGasErrorRatio,
1291+
Config: b.ChainConfig(),
1292+
Chain: NewChainContext(ctx, b),
1293+
Header: header,
1294+
State: state,
1295+
Backend: b,
1296+
ErrorRatio: gasestimator.EstimateGasErrorRatio,
1297+
RunScheduledTxes: runScheduledTxes,
12891298
}
12901299
// Run the gas estimation andwrap any revertals into a custom return
12911300
// Arbitrum: this also appropriately recursively calls another args.ToMessage with increased gasCap by posterCostInL2Gas amount

0 commit comments

Comments
 (0)