Skip to content

Commit

Permalink
Merge pull request ethereum#63 from OffchainLabs/intercept-rpc-gas-cap
Browse files Browse the repository at this point in the history
Intercept RPC Gas Cap
  • Loading branch information
rachel-bousfield authored Mar 9, 2022
2 parents 6a27883 + dbc6fdf commit cbf6010
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/ethereum/go-ethereum/common"
cmath "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -162,6 +163,7 @@ func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation b

var ReadyEVMForL2 func(evm *vm.EVM, msg Message)
var InterceptRPCMessage func(msg types.Message) (types.Message, error)
var InterceptRPCGasCap func(gascap *uint64, msg types.Message, header *types.Header, statedb *state.StateDB)

// NewStateTransition initialises and returns a new state transition object.
func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition {
Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
}
}
// Execute the trace
msg, err := args.ToMessage(api.backend.RPCGasCap(), block.BaseFee())
msg, err := args.ToMessage(api.backend.RPCGasCap(), block.Header(), statedb)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash
defer cancel()

// Get a new instance of the EVM.
msg, err := args.ToMessage(globalGasCap, header.BaseFee)
msg, err := args.ToMessage(globalGasCap, header, state)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1526,7 +1526,7 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
statedb := db.Copy()
// Set the accesslist to the last al
args.AccessList = &accessList
msg, err := args.ToMessage(b.RPCGasCap(), header.BaseFee)
msg, err := args.ToMessage(b.RPCGasCap(), header, statedb)
if err != nil {
return nil, 0, nil, err
}
Expand Down
14 changes: 13 additions & 1 deletion internal/ethapi/transaction_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -175,7 +177,9 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
// ToMessage converts the transaction arguments to the Message type used by the
// core evm. This method is used in calls and traces that do not require a real
// live transaction.
func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (types.Message, error) {
func (args *TransactionArgs) ToMessage(globalGasCap uint64, header *types.Header, state *state.StateDB) (types.Message, error) {
baseFee := header.BaseFee

// Reject invalid combinations of pre- and post-1559 fee styles
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
return types.Message{}, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
Expand Down Expand Up @@ -240,6 +244,14 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (t
accessList = *args.AccessList
}
msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, gasFeeCap, gasTipCap, data, accessList, true)

// Arbitrum: update the gas cap to ignore L1 costs so that it's compute-only
if core.InterceptRPCGasCap != nil && state != nil {
// ToMessage recurses once to allow ArbOS to intercept the result for all callers
// ArbOS uses this to modify globalGasCap so that the cap will ignore this tx's specific L1 data costs
core.InterceptRPCGasCap(&globalGasCap, msg, header, state)
return args.ToMessage(globalGasCap, header, nil) // we pass a nil to avoid another recursion
}
return msg, nil
}

Expand Down

0 comments on commit cbf6010

Please sign in to comment.