Skip to content

Commit 4a3a32f

Browse files
committed
feat: AddressContext.Raw.{Caller,Self} fields for precompiles
1 parent 3ed65b0 commit 4a3a32f

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

core/vm/contracts.libevm.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,11 @@ func (args *evmCallArgs) env() *environment {
223223
}
224224

225225
return &environment{
226-
evm: args.evm,
227-
self: contract,
228-
callType: args.callType,
226+
evm: args.evm,
227+
self: contract,
228+
callType: args.callType,
229+
rawCaller: args.caller.Address(),
230+
rawSelf: args.addr,
229231
}
230232
}
231233

core/vm/contracts.libevm_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ type statefulPrecompileOutput struct {
120120
func (o statefulPrecompileOutput) String() string {
121121
var lines []string
122122
out := reflect.ValueOf(o)
123+
FieldLoop:
123124
for i, n := 0, out.NumField(); i < n; i++ {
124125
name := out.Type().Field(i).Name
125126
fld := out.Field(i).Interface()
@@ -129,7 +130,12 @@ func (o statefulPrecompileOutput) String() string {
129130
case []byte:
130131
verb = "%#x"
131132
case *libevm.AddressContext:
132-
verb = "%+v"
133+
lines = append(
134+
lines,
135+
fmt.Sprintf("EVMSemantic addresses: %+v", o.Addresses.EVMSemantic),
136+
fmt.Sprintf("Raw addresses: %+v", o.Addresses.Raw),
137+
)
138+
continue FieldLoop
133139
case vm.CallType:
134140
verb = "%d (%[2]q)"
135141
}
@@ -211,6 +217,13 @@ func TestNewStatefulPrecompile(t *testing.T) {
211217
state.SetBalance(caller, new(uint256.Int).Not(uint256.NewInt(0)))
212218
evm.Origin = eoa
213219

220+
// By definition, the raw caller and self are the same, regardless of the
221+
// incoming call type.
222+
rawAddresses := &libevm.CallerAndSelf{
223+
Caller: caller,
224+
Self: precompile,
225+
}
226+
214227
tests := []struct {
215228
name string
216229
call func() ([]byte, uint64, error)
@@ -232,6 +245,7 @@ func TestNewStatefulPrecompile(t *testing.T) {
232245
Caller: caller,
233246
Self: precompile,
234247
},
248+
Raw: rawAddresses,
235249
},
236250
wantReadOnly: false,
237251
wantTransferValue: transferValue,
@@ -248,6 +262,7 @@ func TestNewStatefulPrecompile(t *testing.T) {
248262
Caller: caller,
249263
Self: caller,
250264
},
265+
Raw: rawAddresses,
251266
},
252267
wantReadOnly: false,
253268
wantTransferValue: transferValue,
@@ -264,6 +279,7 @@ func TestNewStatefulPrecompile(t *testing.T) {
264279
Caller: eoa, // inherited from caller
265280
Self: caller,
266281
},
282+
Raw: rawAddresses,
267283
},
268284
wantReadOnly: false,
269285
wantTransferValue: uint256.NewInt(0),
@@ -280,6 +296,7 @@ func TestNewStatefulPrecompile(t *testing.T) {
280296
Caller: caller,
281297
Self: precompile,
282298
},
299+
Raw: rawAddresses,
283300
},
284301
wantReadOnly: true,
285302
wantTransferValue: uint256.NewInt(0),
@@ -570,6 +587,7 @@ func TestCanCreateContract(t *testing.T) {
570587
Caller: caller,
571588
Self: create,
572589
},
590+
// `Raw` is documented as always being nil.
573591
},
574592
value,
575593
),
@@ -586,6 +604,7 @@ func TestCanCreateContract(t *testing.T) {
586604
Caller: caller,
587605
Self: create2,
588606
},
607+
// As above re `Raw` always being nil.
589608
},
590609
value,
591610
),
@@ -656,7 +675,10 @@ func TestPrecompileMakeCall(t *testing.T) {
656675
if bytes.Equal(input, unsafeCallerProxyOptSentinel) {
657676
opts = append(opts, vm.WithUNSAFECallerAddressProxying())
658677
}
659-
// We are ultimately testing env.Call(), hence why this is the SUT.
678+
// We are ultimately testing env.Call(), hence why this is the
679+
// SUT. If this is ever extended to include DELEGATECALL or
680+
// CALLCODE then the expected [libevm.AddressContext.Raw] values
681+
// of the tests cases also need to change.
660682
return env.Call(dest, precompileCallData, env.Gas(), uint256.NewInt(0), opts...)
661683
}),
662684
dest: vm.NewStatefulPrecompile(func(env vm.PrecompileEnvironment, input []byte) (ret []byte, err error) {
@@ -802,6 +824,9 @@ func TestPrecompileMakeCall(t *testing.T) {
802824

803825
for _, tt := range tests {
804826
t.Run(tt.incomingCallType.String(), func(t *testing.T) {
827+
// From the perspective of `dest` after a CALL.
828+
tt.want.Addresses.Raw = &tt.want.Addresses.EVMSemantic
829+
805830
t.Logf("calldata = %q", tt.eoaTxCallData)
806831
state, evm := ethtest.NewZeroEVM(t)
807832
evm.Origin = eoa

core/vm/environment.libevm.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type environment struct {
3636
evm *EVM
3737
self *Contract
3838
callType CallType
39+
40+
rawSelf, rawCaller common.Address
3941
}
4042

4143
func (e *environment) Gas() uint64 { return e.self.Gas }
@@ -83,6 +85,10 @@ func (e *environment) Addresses() *libevm.AddressContext {
8385
Caller: e.self.CallerAddress,
8486
Self: e.self.Address(),
8587
},
88+
Raw: &libevm.CallerAndSelf{
89+
Caller: e.rawCaller,
90+
Self: e.rawSelf,
91+
},
8692
}
8793
}
8894

0 commit comments

Comments
 (0)