Skip to content

Commit

Permalink
test: payable vs non-payable
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N committed Dec 20, 2024
1 parent 49f2312 commit 2200b8e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10 deletions.
17 changes: 16 additions & 1 deletion libevm/precompilegen/Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ interface IPrecompile {
function Pure() external pure returns (bool canReadState, bool canWriteState);

function NeitherViewNorPure() external returns (bool canReadState, bool canWriteState);

function Payable() external payable returns (uint256 value);

function NonPayable() external;
}

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

constructor(IPrecompile _precompile) {
constructor(IPrecompile _precompile) payable {
precompile = _precompile;
}

Expand Down Expand Up @@ -107,4 +111,15 @@ contract PrecompileTest {
assertReadOnly(IPrecompile.NeitherViewNorPure.selector, true, true);
emit Called("NeitherViewNorPure()");
}

function Transfer() external {
uint256 value = precompile.Payable{value: 42}();
assert(value == 42);

(bool ok,) = address(precompile).call{value: 42}(abi.encodeWithSelector(IPrecompile.NonPayable.selector));
assert(!ok);
// TODO: DO NOT MERGE without checking the return data

emit Called("Transfer()");
}
}
25 changes: 23 additions & 2 deletions libevm/precompilegen/abigen.gen_test.go

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions libevm/precompilegen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@ func TestGeneratedPrecompile(t *testing.T) {
txOpts, err := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
require.NoError(t, err, "bind.NewKeyedTransactorWithChainID(..., 1337)")
txOpts.GasLimit = 30e6
txOpts.Value = big.NewInt(1e9)

client := sim.Client()
_, tx, test, err := DeployPrecompileTest(txOpts, client, precompile)
require.NoError(t, err, "DeployPrecompileTest(...)")
sim.Commit()
successfulTxReceipt(ctx, t, client, tx)

txOpts.Value = nil
suite := &PrecompileTestSession{
Contract: test,
TransactOpts: *txOpts,
Expand Down Expand Up @@ -166,6 +169,12 @@ func TestGeneratedPrecompile(t *testing.T) {
},
wantCalledEvent: "NeitherViewNorPure()",
},
{
transact: func() (*types.Transaction, error) {
return suite.Transfer()
},
wantCalledEvent: "Transfer()",
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -248,3 +257,11 @@ func (contract) Pure(env vm.PrecompileEnvironment) (bool, bool, error) {
func (contract) NeitherViewNorPure(env vm.PrecompileEnvironment) (bool, bool, error) {
return canReadState(env), canWriteState(env), nil
}

func (contract) Payable(env vm.PrecompileEnvironment) (*big.Int, error) {
return env.Value().ToBig(), nil
}

func (contract) NonPayable(env vm.PrecompileEnvironment) error {
return nil
}
8 changes: 5 additions & 3 deletions libevm/precompilegen/precompile.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,19 @@ type dispatch struct{}

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

{{if gt (len .Inputs) 0}}
{{if gt (len .Inputs) 0 -}}
inputs, err := method.Inputs.Unpack(input[abi.SelectorByteLen:])
if err != nil {
return nil, err
}
{{- range $i, $in := .Inputs}}
i{{$i}} := inputs[{{$i}}].({{type $in}})
{{- end}}
{{end}}
{{- end}}

{
{{args "o" (len .Outputs) false true}} := impl.{{.Name}}({{args "i" (len .Inputs) true false}})
Expand All @@ -123,7 +125,7 @@ func (dispatch) {{.Name}}(impl Contract, env vm.PrecompileEnvironment, input []b
return method.Outputs.Pack({{args "o" (len .Outputs) false false}})
{{- else -}}
return nil, nil
{{end}}
{{- end}}
}
}
{{end}}
41 changes: 37 additions & 4 deletions libevm/precompilegen/testprecompile/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2200b8e

Please sign in to comment.