Skip to content

Commit

Permalink
refactor: split IPrecompile and TestSuite contracts + move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N committed Dec 20, 2024
1 parent 2200b8e commit 08e4985
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 204 deletions.
File renamed without changes.
31 changes: 31 additions & 0 deletions libevm/precompilegen/testprecompile/IPrecompile.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: LGPL-3.0
pragma solidity 0.8.28;

/// @dev Interface of precompiled contract for implementation via `precompilegen`.
interface IPrecompile {
function Echo(string memory) external view returns (string memory);

function Echo(uint256) external view returns (uint256);

function HashPacked(uint256, bytes2, address) external view returns (bytes32);

struct Wrapper {
int256 val;
}

function Extract(Wrapper memory) external view returns (int256);

function Self() external view returns (address);

function RevertWith(bytes memory) external;

function View() external view returns (bool canReadState, bool canWriteState);

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;
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0
pragma solidity 0.8.28;

/// @dev Interface of precompiled contract for implementation via `precompilegen`.
interface IPrecompile {
function Echo(string memory) external view returns (string memory);
import {IPrecompile} from "./IPrecompile.sol";

function Echo(uint256) external view returns (uint256);

function HashPacked(uint256, bytes2, address) external view returns (bytes32);

struct Wrapper {
int256 val;
}

function Extract(Wrapper memory) external view returns (int256);

function Self() external view returns (address);

function RevertWith(bytes memory) external;

function View() external view returns (bool canReadState, bool canWriteState);

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 {
/// @dev Testing contract to exercise the Go implementaiton of `IPrecompile`.
contract TestSuite {
IPrecompile immutable precompile;

constructor(IPrecompile _precompile) payable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see
// <http://www.gnu.org/licenses/>.
package main
package testprecompile

import (
"context"
Expand All @@ -34,16 +34,15 @@ import (
"github.com/ava-labs/libevm/libevm"
"github.com/ava-labs/libevm/libevm/ethtest"
"github.com/ava-labs/libevm/libevm/hookstest"
"github.com/ava-labs/libevm/libevm/precompilegen/testprecompile"
"github.com/ava-labs/libevm/node"
"github.com/ava-labs/libevm/params"
)

// Note that the .abi and .bin files are .gitignored as only the generated Go
// files are necessary.
//go:generate solc -o ./ --overwrite --abi --bin Test.sol
//go:generate go run . -in IPrecompile.abi -out ./testprecompile/generated.go -package testprecompile
//go:generate go run ../../cmd/abigen --abi PrecompileTest.abi --bin PrecompileTest.bin --pkg main --out ./abigen.gen_test.go --type PrecompileTest
//go:generate solc -o ./ --overwrite --abi --bin IPrecompile.sol TestSuite.sol
//go:generate go run ../ -in IPrecompile.abi -out ./generated.go -package testprecompile
//go:generate go run ../../../cmd/abigen --abi TestSuite.abi --bin TestSuite.bin --pkg testprecompile --out ./suite.abigen_test.go --type TestSuite

func successfulTxReceipt(ctx context.Context, tb testing.TB, client bind.DeployBackend, tx *types.Transaction) *types.Receipt {
tb.Helper()
Expand All @@ -60,7 +59,7 @@ func TestGeneratedPrecompile(t *testing.T) {

hooks := &hookstest.Stub{
PrecompileOverrides: map[common.Address]libevm.PrecompiledContract{
precompile: testprecompile.New(contract{}),
precompile: New(contract{}),
},
}
extras := hookstest.Register(t, params.Extras[*hookstest.Stub, *hookstest.Stub]{
Expand Down Expand Up @@ -92,13 +91,13 @@ func TestGeneratedPrecompile(t *testing.T) {
txOpts.Value = big.NewInt(1e9)

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

txOpts.Value = nil
suite := &PrecompileTestSession{
suite := &TestSuiteSession{
Contract: test,
TransactOpts: *txOpts,
}
Expand Down Expand Up @@ -196,11 +195,11 @@ func TestGeneratedPrecompile(t *testing.T) {

type contract struct{}

var _ testprecompile.Contract = contract{}
var _ Contract = contract{}

func (contract) Fallback(env vm.PrecompileEnvironment, callData []byte) ([]byte, error) {
// Note the test-suite assumption of the fallback's behaviour:
var _ = (*PrecompileTest).EchoingFallback
var _ = (*TestSuite).EchoingFallback
return callData, nil
}

Expand Down
Loading

0 comments on commit 08e4985

Please sign in to comment.