Skip to content

Commit d487b26

Browse files
committed
refactor: WithUNSAFECallerAddressProxying() instead of ForceDelegate
This matches the pattern used by `ava-labs/coreth` `NativeAssetCall`.
1 parent 584fa5f commit d487b26

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

core/vm/contracts.libevm.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,23 +220,24 @@ func (args *evmCallArgs) Call(addr common.Address, input []byte, gas uint64, val
220220
// methods to pass to [EVMInterpreter.Run], which are then propagated by the
221221
// *CALL* opcodes as the caller.
222222
precompile := NewContract(args.caller, AccountRef(args.self()), args.value, args.gas)
223+
if args.delegation == delegated {
224+
precompile = precompile.AsDelegate()
225+
}
226+
var caller ContractRef = precompile
223227

224-
asDelegate := args.delegation == delegated // precompile was DELEGATECALLed
225228
for _, o := range opts {
226229
switch o := o.(type) {
227-
case callOptForceDelegate:
228-
// See documentation of [WithUNSAFEForceDelegate].
229-
asDelegate = true
230+
case callOptUNSAFECallerAddressProxy:
231+
// Note that, in addition to being unsafe, this breaks an EVM
232+
// assumption that the caller ContractRef is always a *Contract.
233+
caller = AccountRef(args.caller.Address())
230234
case nil:
231235
default:
232236
return nil, gas, fmt.Errorf("unsupported option %T", o)
233237
}
234238
}
235-
if asDelegate {
236-
precompile = precompile.AsDelegate()
237-
}
238239

239-
return args.evm.Call(precompile, addr, input, gas, value)
240+
return args.evm.Call(caller, addr, input, gas, value)
240241
}
241242

242243
var (

core/vm/options.libevm.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ type CallOption interface {
2121
libevmCallOption() // noop to only allow internally defined options
2222
}
2323

24-
// WithUNSAFEForceDelegate results in precompiles making contract calls acting
25-
// as if they themselves were DELEGATECALLed. This is not safe for regular use
26-
// as the precompile will act as its own caller even when not expected to.
24+
// WithUNSAFECallerAddressProxying results in precompiles making contract calls
25+
// specifying their own caller's address as the caller. This is NOT SAFE for
26+
// regular use as callers of the precompile may not understand that they are
27+
// escalating the precompile's privileges.
2728
//
2829
// Deprecated: this option MUST NOT be used other than to allow migration to
2930
// libevm when backwards compatibility is required.
30-
func WithUNSAFEForceDelegate() CallOption {
31-
return callOptForceDelegate{}
31+
func WithUNSAFECallerAddressProxying() CallOption {
32+
return callOptUNSAFECallerAddressProxy{}
3233
}
3334

34-
type callOptForceDelegate struct{}
35+
// Deprecated: see [WithUNSAFECallerAddressProxying].
36+
type callOptUNSAFECallerAddressProxy struct{}
3537

36-
func (callOptForceDelegate) libevmCallOption() {}
38+
func (callOptUNSAFECallerAddressProxy) libevmCallOption() {}

0 commit comments

Comments
 (0)