Skip to content

Commit 2200b8e

Browse files
committed
test: payable vs non-payable
1 parent 49f2312 commit 2200b8e

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

libevm/precompilegen/Test.sol

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ interface IPrecompile {
2424
function Pure() external pure returns (bool canReadState, bool canWriteState);
2525

2626
function NeitherViewNorPure() external returns (bool canReadState, bool canWriteState);
27+
28+
function Payable() external payable returns (uint256 value);
29+
30+
function NonPayable() external;
2731
}
2832

2933
/// @dev Testing contract to exercise the implementaiton of `IPrecompile`.
3034
contract PrecompileTest {
3135
IPrecompile immutable precompile;
3236

33-
constructor(IPrecompile _precompile) {
37+
constructor(IPrecompile _precompile) payable {
3438
precompile = _precompile;
3539
}
3640

@@ -107,4 +111,15 @@ contract PrecompileTest {
107111
assertReadOnly(IPrecompile.NeitherViewNorPure.selector, true, true);
108112
emit Called("NeitherViewNorPure()");
109113
}
114+
115+
function Transfer() external {
116+
uint256 value = precompile.Payable{value: 42}();
117+
assert(value == 42);
118+
119+
(bool ok,) = address(precompile).call{value: 42}(abi.encodeWithSelector(IPrecompile.NonPayable.selector));
120+
assert(!ok);
121+
// TODO: DO NOT MERGE without checking the return data
122+
123+
emit Called("Transfer()");
124+
}
110125
}

libevm/precompilegen/abigen.gen_test.go

Lines changed: 23 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libevm/precompilegen/main_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ func TestGeneratedPrecompile(t *testing.T) {
8989
txOpts, err := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
9090
require.NoError(t, err, "bind.NewKeyedTransactorWithChainID(..., 1337)")
9191
txOpts.GasLimit = 30e6
92+
txOpts.Value = big.NewInt(1e9)
9293

9394
client := sim.Client()
9495
_, tx, test, err := DeployPrecompileTest(txOpts, client, precompile)
9596
require.NoError(t, err, "DeployPrecompileTest(...)")
9697
sim.Commit()
9798
successfulTxReceipt(ctx, t, client, tx)
99+
100+
txOpts.Value = nil
98101
suite := &PrecompileTestSession{
99102
Contract: test,
100103
TransactOpts: *txOpts,
@@ -166,6 +169,12 @@ func TestGeneratedPrecompile(t *testing.T) {
166169
},
167170
wantCalledEvent: "NeitherViewNorPure()",
168171
},
172+
{
173+
transact: func() (*types.Transaction, error) {
174+
return suite.Transfer()
175+
},
176+
wantCalledEvent: "Transfer()",
177+
},
169178
}
170179

171180
for _, tt := range tests {
@@ -248,3 +257,11 @@ func (contract) Pure(env vm.PrecompileEnvironment) (bool, bool, error) {
248257
func (contract) NeitherViewNorPure(env vm.PrecompileEnvironment) (bool, bool, error) {
249258
return canReadState(env), canWriteState(env), nil
250259
}
260+
261+
func (contract) Payable(env vm.PrecompileEnvironment) (*big.Int, error) {
262+
return env.Value().ToBig(), nil
263+
}
264+
265+
func (contract) NonPayable(env vm.PrecompileEnvironment) error {
266+
return nil
267+
}

libevm/precompilegen/precompile.go.tmpl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,19 @@ type dispatch struct{}
102102

103103
{{range methods .ABI}}
104104
func (dispatch) {{.Name}}(impl Contract, env vm.PrecompileEnvironment, input []byte) ([]byte, error) {
105+
{{if or (gt (len .Inputs) 0) (gt (len .Outputs) 0) -}}
105106
method := methods[{{hex .ID}}]
107+
{{- end}}
106108

107-
{{if gt (len .Inputs) 0}}
109+
{{if gt (len .Inputs) 0 -}}
108110
inputs, err := method.Inputs.Unpack(input[abi.SelectorByteLen:])
109111
if err != nil {
110112
return nil, err
111113
}
112114
{{- range $i, $in := .Inputs}}
113115
i{{$i}} := inputs[{{$i}}].({{type $in}})
114116
{{- end}}
115-
{{end}}
117+
{{- end}}
116118

117119
{
118120
{{args "o" (len .Outputs) false true}} := impl.{{.Name}}({{args "i" (len .Inputs) true false}})
@@ -123,7 +125,7 @@ func (dispatch) {{.Name}}(impl Contract, env vm.PrecompileEnvironment, input []b
123125
return method.Outputs.Pack({{args "o" (len .Outputs) false false}})
124126
{{- else -}}
125127
return nil, nil
126-
{{end}}
128+
{{- end}}
127129
}
128130
}
129131
{{end}}

libevm/precompilegen/testprecompile/generated.go

Lines changed: 37 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)