Skip to content

Commit f651814

Browse files
committed
test: vm.PrecompileEnvironment.Value()
1 parent 405740f commit f651814

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

core/vm/contracts.libevm.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ func (args *evmCallArgs) run(p PrecompiledContract, input []byte, suppliedGas ui
104104
// PrecompiledStatefulContract is the stateful equivalent of a
105105
// [PrecompiledContract].
106106
//
107-
// Instead of receiving and returning gas limits, stateful precompiles use the
108-
// respective methods on [PrecompileEnvironment]. If a call to UseGas() returns
109-
// false, a stateful precompile SHOULD return [ErrOutOfGas].
107+
// Instead of receiving and returning gas arguments, stateful precompiles use
108+
// the respective methods on [PrecompileEnvironment]. If a call to UseGas()
109+
// returns false, a stateful precompile SHOULD return [ErrOutOfGas].
110110
type PrecompiledStatefulContract func(env PrecompileEnvironment, input []byte) (ret []byte, err error)
111111

112112
// NewStatefulPrecompile constructs a new PrecompiledContract that can be used

core/vm/contracts.libevm_test.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ type statefulPrecompileOutput struct {
107107
ChainID *big.Int
108108
Addresses *libevm.AddressContext
109109
StateValue common.Hash
110+
ValueReceived *uint256.Int
110111
ReadOnly bool
111112
BlockNumber, Difficulty *big.Int
112113
BlockTime uint64
@@ -160,6 +161,7 @@ func TestNewStatefulPrecompile(t *testing.T) {
160161
ChainID: env.ChainConfig().ChainID,
161162
Addresses: env.Addresses(),
162163
StateValue: env.ReadOnlyState().GetState(precompile, slot),
164+
ValueReceived: env.Value(),
163165
ReadOnly: env.ReadOnly(),
164166
BlockNumber: env.BlockNumber(),
165167
BlockTime: env.BlockTime(),
@@ -186,7 +188,8 @@ func TestNewStatefulPrecompile(t *testing.T) {
186188
Difficulty: rng.BigUint64(),
187189
}
188190
input := rng.Bytes(8)
189-
value := rng.Hash()
191+
stateValue := rng.Hash()
192+
transferValue := rng.Uint256()
190193
chainID := rng.BigUint64()
191194

192195
caller := common.HexToAddress("CA11E12") // caller of the precompile
@@ -202,13 +205,15 @@ func TestNewStatefulPrecompile(t *testing.T) {
202205
&params.ChainConfig{ChainID: chainID},
203206
),
204207
)
205-
state.SetState(precompile, slot, value)
208+
state.SetState(precompile, slot, stateValue)
209+
state.SetBalance(caller, new(uint256.Int).Not(uint256.NewInt(0)))
206210
evm.Origin = eoa
207211

208212
tests := []struct {
209-
name string
210-
call func() ([]byte, uint64, error)
211-
wantAddresses *libevm.AddressContext
213+
name string
214+
call func() ([]byte, uint64, error)
215+
wantAddresses *libevm.AddressContext
216+
wantTransferValue *uint256.Int
212217
// Note that this only covers evm.readOnly being true because of the
213218
// precompile's call. See TestInheritReadOnly for alternate case.
214219
wantReadOnly bool
@@ -217,28 +222,30 @@ func TestNewStatefulPrecompile(t *testing.T) {
217222
{
218223
name: "EVM.Call()",
219224
call: func() ([]byte, uint64, error) {
220-
return evm.Call(callerContract, precompile, input, gasLimit, uint256.NewInt(0))
225+
return evm.Call(callerContract, precompile, input, gasLimit, transferValue)
221226
},
222227
wantAddresses: &libevm.AddressContext{
223228
Origin: eoa,
224229
Caller: caller,
225230
Self: precompile,
226231
},
227-
wantReadOnly: false,
228-
wantCallType: vm.Call,
232+
wantReadOnly: false,
233+
wantTransferValue: transferValue,
234+
wantCallType: vm.Call,
229235
},
230236
{
231237
name: "EVM.CallCode()",
232238
call: func() ([]byte, uint64, error) {
233-
return evm.CallCode(callerContract, precompile, input, gasLimit, uint256.NewInt(0))
239+
return evm.CallCode(callerContract, precompile, input, gasLimit, transferValue)
234240
},
235241
wantAddresses: &libevm.AddressContext{
236242
Origin: eoa,
237243
Caller: caller,
238244
Self: caller,
239245
},
240-
wantReadOnly: false,
241-
wantCallType: vm.CallCode,
246+
wantReadOnly: false,
247+
wantTransferValue: transferValue,
248+
wantCallType: vm.CallCode,
242249
},
243250
{
244251
name: "EVM.DelegateCall()",
@@ -250,8 +257,9 @@ func TestNewStatefulPrecompile(t *testing.T) {
250257
Caller: eoa, // inherited from caller
251258
Self: caller,
252259
},
253-
wantReadOnly: false,
254-
wantCallType: vm.DelegateCall,
260+
wantReadOnly: false,
261+
wantTransferValue: uint256.NewInt(0),
262+
wantCallType: vm.DelegateCall,
255263
},
256264
{
257265
name: "EVM.StaticCall()",
@@ -263,8 +271,9 @@ func TestNewStatefulPrecompile(t *testing.T) {
263271
Caller: caller,
264272
Self: precompile,
265273
},
266-
wantReadOnly: true,
267-
wantCallType: vm.StaticCall,
274+
wantReadOnly: true,
275+
wantTransferValue: uint256.NewInt(0),
276+
wantCallType: vm.StaticCall,
268277
},
269278
}
270279

@@ -273,7 +282,8 @@ func TestNewStatefulPrecompile(t *testing.T) {
273282
wantOutput := statefulPrecompileOutput{
274283
ChainID: chainID,
275284
Addresses: tt.wantAddresses,
276-
StateValue: value,
285+
StateValue: stateValue,
286+
ValueReceived: tt.wantTransferValue,
277287
ReadOnly: tt.wantReadOnly,
278288
BlockNumber: header.Number,
279289
BlockTime: header.Time,

0 commit comments

Comments
 (0)