Skip to content

Commit 08e4985

Browse files
committed
refactor: split IPrecompile and TestSuite contracts + move tests
1 parent 2200b8e commit 08e4985

File tree

5 files changed

+207
-204
lines changed

5 files changed

+207
-204
lines changed
File renamed without changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: LGPL-3.0
2+
pragma solidity 0.8.28;
3+
4+
/// @dev Interface of precompiled contract for implementation via `precompilegen`.
5+
interface IPrecompile {
6+
function Echo(string memory) external view returns (string memory);
7+
8+
function Echo(uint256) external view returns (uint256);
9+
10+
function HashPacked(uint256, bytes2, address) external view returns (bytes32);
11+
12+
struct Wrapper {
13+
int256 val;
14+
}
15+
16+
function Extract(Wrapper memory) external view returns (int256);
17+
18+
function Self() external view returns (address);
19+
20+
function RevertWith(bytes memory) external;
21+
22+
function View() external view returns (bool canReadState, bool canWriteState);
23+
24+
function Pure() external pure returns (bool canReadState, bool canWriteState);
25+
26+
function NeitherViewNorPure() external returns (bool canReadState, bool canWriteState);
27+
28+
function Payable() external payable returns (uint256 value);
29+
30+
function NonPayable() external;
31+
}

libevm/precompilegen/Test.sol renamed to libevm/precompilegen/testprecompile/TestSuite.sol

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
11
// SPDX-License-Identifier: LGPL-3.0
22
pragma solidity 0.8.28;
33

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

8-
function Echo(uint256) external view returns (uint256);
9-
10-
function HashPacked(uint256, bytes2, address) external view returns (bytes32);
11-
12-
struct Wrapper {
13-
int256 val;
14-
}
15-
16-
function Extract(Wrapper memory) external view returns (int256);
17-
18-
function Self() external view returns (address);
19-
20-
function RevertWith(bytes memory) external;
21-
22-
function View() external view returns (bool canReadState, bool canWriteState);
23-
24-
function Pure() external pure returns (bool canReadState, bool canWriteState);
25-
26-
function NeitherViewNorPure() external returns (bool canReadState, bool canWriteState);
27-
28-
function Payable() external payable returns (uint256 value);
29-
30-
function NonPayable() external;
31-
}
32-
33-
/// @dev Testing contract to exercise the implementaiton of `IPrecompile`.
34-
contract PrecompileTest {
6+
/// @dev Testing contract to exercise the Go implementaiton of `IPrecompile`.
7+
contract TestSuite {
358
IPrecompile immutable precompile;
369

3710
constructor(IPrecompile _precompile) payable {

libevm/precompilegen/main_test.go renamed to libevm/precompilegen/testprecompile/generated_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// You should have received a copy of the GNU Lesser General Public License
1414
// along with the go-ethereum library. If not, see
1515
// <http://www.gnu.org/licenses/>.
16-
package main
16+
package testprecompile
1717

1818
import (
1919
"context"
@@ -34,16 +34,15 @@ import (
3434
"github.com/ava-labs/libevm/libevm"
3535
"github.com/ava-labs/libevm/libevm/ethtest"
3636
"github.com/ava-labs/libevm/libevm/hookstest"
37-
"github.com/ava-labs/libevm/libevm/precompilegen/testprecompile"
3837
"github.com/ava-labs/libevm/node"
3938
"github.com/ava-labs/libevm/params"
4039
)
4140

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

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

6160
hooks := &hookstest.Stub{
6261
PrecompileOverrides: map[common.Address]libevm.PrecompiledContract{
63-
precompile: testprecompile.New(contract{}),
62+
precompile: New(contract{}),
6463
},
6564
}
6665
extras := hookstest.Register(t, params.Extras[*hookstest.Stub, *hookstest.Stub]{
@@ -92,13 +91,13 @@ func TestGeneratedPrecompile(t *testing.T) {
9291
txOpts.Value = big.NewInt(1e9)
9392

9493
client := sim.Client()
95-
_, tx, test, err := DeployPrecompileTest(txOpts, client, precompile)
96-
require.NoError(t, err, "DeployPrecompileTest(...)")
94+
_, tx, test, err := DeployTestSuite(txOpts, client, precompile)
95+
require.NoError(t, err, "DeployTestSuite(...)")
9796
sim.Commit()
9897
successfulTxReceipt(ctx, t, client, tx)
9998

10099
txOpts.Value = nil
101-
suite := &PrecompileTestSession{
100+
suite := &TestSuiteSession{
102101
Contract: test,
103102
TransactOpts: *txOpts,
104103
}
@@ -196,11 +195,11 @@ func TestGeneratedPrecompile(t *testing.T) {
196195

197196
type contract struct{}
198197

199-
var _ testprecompile.Contract = contract{}
198+
var _ Contract = contract{}
200199

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

0 commit comments

Comments
 (0)