Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions l2geth/internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,24 +1008,25 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash
// 1. get the gas that would be used by the transaction
gasUsed, err := legacyDoEstimateGas(ctx, b, args, blockNrOrHash, gasCap)
if err != nil {
return 0, err
return 0, fmt.Errorf("execution gas used error: %w", err)
}

// 2a. fetch the data price, depends on how the sequencer has chosen to update their values based on the
// l1 gas prices
dataPrice, err := b.SuggestDataPrice(ctx)
if err != nil {
return 0, err
return 0, fmt.Errorf("data gas pricing error: %w", err)
}

// 2b. fetch the execution gas price, by the typical mempool dynamics
executionPrice, err := b.SuggestPrice(ctx)
if err != nil {
return 0, err
return 0, fmt.Errorf("unknown gas price: %w", err)
}

// 3. calculate the fee and normalize by the default gas price
fee := core.CalculateRollupFee(*args.Data, uint64(gasUsed), dataPrice, executionPrice).Uint64() / defaultGasPrice
log.Debug("Estimate Gas", "gas-used", uint64(gasUsed), "data-price", dataPrice.Uint64(), "execution-price", executionPrice.Uint64(), "fee", fee, "default-gas-price", defaultGasPrice)
return (hexutil.Uint64)(fee), nil
}

Expand Down Expand Up @@ -1065,28 +1066,30 @@ func legacyDoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrO
args.From = &common.Address{}
}
// Create a helper to check if a gas allowance results in an executable transaction
executable := func(gas uint64) bool {
executable := func(gas uint64) (uint64, bool) {
args.Gas = (*hexutil.Uint64)(&gas)

_, _, failed, err := DoCall(ctx, b, args, blockNrOrHash, nil, vm.Config{}, 0, gasCap)
_, gas, failed, err := DoCall(ctx, b, args, blockNrOrHash, nil, vm.Config{}, 0, gasCap)
if err != nil || failed {
return false
return gas, false
}
return true
return gas, true
}
// Execute the binary search and hone in on an executable gas limit
for lo+1 < hi {
mid := (hi + lo) / 2
if !executable(mid) {
_, executed := executable(mid)
if !executed {
lo = mid
} else {
hi = mid
}
}
// Reject the transaction as invalid if it still fails at the highest allowance
if hi == cap {
if !executable(hi) {
return 0, fmt.Errorf("gas required exceeds allowance (%d) or always failing transaction", cap)
gas, executed := executable(hi)
if !executed {
return 0, fmt.Errorf("gas required (%d) exceeds allowance (%d) or always failing transaction", gas, cap)
}
}
return hexutil.Uint64(hi), nil
Expand Down
29 changes: 20 additions & 9 deletions l2geth/rollup/sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ func (s *SyncService) Stop() error {
func (s *SyncService) VerifierLoop() {
log.Info("Starting Verifier Loop", "poll-interval", s.pollInterval, "timestamp-refresh-threshold", s.timestampRefreshThreshold)
for {
if err := s.updateL1GasPrice(); err != nil {
log.Error("Cannot update L1 gas price", "msg", err)
}
if err := s.verify(); err != nil {
log.Error("Could not verify", "error", err)
}
Expand Down Expand Up @@ -344,8 +347,13 @@ func (s *SyncService) verify() error {
func (s *SyncService) SequencerLoop() {
log.Info("Starting Sequencer Loop", "poll-interval", s.pollInterval, "timestamp-refresh-threshold", s.timestampRefreshThreshold)
for {
err := s.updateL1GasPrice()
if err != nil {
log.Error("Cannot update L1 gas price", "msg", err)
continue
}
s.txLock.Lock()
err := s.sequence()
err = s.sequence()
if err != nil {
log.Error("Could not sequence", "error", err)
}
Expand All @@ -360,14 +368,6 @@ func (s *SyncService) SequencerLoop() {
}

func (s *SyncService) sequence() error {
// Update to the latest L1 gas price
l1GasPrice, err := s.client.GetL1GasPrice()
if err != nil {
return err
}
s.L1gpo.SetL1GasPrice(l1GasPrice)
log.Info("Adjusted L1 Gas Price", "gasprice", l1GasPrice)

// Only the sequencer needs to poll for enqueue transactions
// and then can choose when to apply them. We choose to apply
// transactions such that it makes for efficient batch submitting.
Expand Down Expand Up @@ -445,6 +445,17 @@ func (s *SyncService) sequence() error {
return nil
}

func (s *SyncService) updateL1GasPrice() error {
// Update to the latest L1 gas price
l1GasPrice, err := s.client.GetL1GasPrice()
if err != nil {
return err
}
s.L1gpo.SetL1GasPrice(l1GasPrice)
log.Info("Adjusted L1 Gas Price", "gasprice", l1GasPrice)
return nil
}

/// Update the execution context's timestamp and blocknumber
/// over time. This is only necessary for the sequencer.
func (s *SyncService) updateContext() error {
Expand Down