-
Notifications
You must be signed in to change notification settings - Fork 173
feat: add e2e test stateful contract #2703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fbac
merged 33 commits into
feat/stateful-precompiled-contracts
from
feat/add-stateful-contract
Aug 21, 2024
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
d94f788
feat: add test precompiled contract
fbac b9878a6
feat: add test stateful contract
fbac 40ff210
feat: enable contracts granularly
fbac 83457fc
feat: allow passing the app ctx to contracts
fbac d13079a
revert ctx changes
fbac e58c783
format ABI
fbac eaac523
add first e2e test
fbac fadf37a
add RegularCaller contract to e2e
fbac 8ad612b
add signer to deployment
fbac a717f01
fix e2e, use regular abi
fbac 5c88cf1
minor rewording
fbac f32ff32
use idiomatic errors
fbac ddee0c5
remove unused files
fbac c1d41e2
rename and generate bindings automatically
fbac 797dbc4
create a new e2e section for precompiles
fbac bb69a61
add bindings generator and makefile target
fbac 53098a1
add GetGasStabilityPoolBalance as prototype contract function
fbac dbe971c
include getGasStabilityPoolBalance function
fbac 906c438
apply code review fixes
fbac cefb8d2
include tests for precompiles package
fbac 1750644
delete regularcaller
fbac 0f0a517
ignore bindings from codecov
fbac ee52f66
introduce more unit testing
fbac 3fa0cab
add unit testing for prototype.go
fbac 3a31ddb
minor linting fixes
fbac fe78f69
remove ctx, keep fungible keeper private
fbac b62a99e
increase unit tests coverage
fbac d2bcc79
add keys for new user_precompile
fbac adbc0a6
add init unit test
fbac de401aa
switch to double quotes
fbac f49ac43
avoid go:embed
fbac ac48e29
fixes derived from code review
fbac 9cbc274
refactor some parts of bech32 functions
fbac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package local | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/fatih/color" | ||
|
|
||
| "github.com/zeta-chain/zetacore/e2e/config" | ||
| "github.com/zeta-chain/zetacore/e2e/e2etests" | ||
| "github.com/zeta-chain/zetacore/e2e/runner" | ||
| ) | ||
|
|
||
| // statefulPrecompilesTestRoutine runs steateful precompiles related e2e tests | ||
| func statefulPrecompilesTestRoutine( | ||
| conf config.Config, | ||
| deployerRunner *runner.E2ERunner, | ||
| verbose bool, | ||
| testNames ...string, | ||
| ) func() error { | ||
| return func() (err error) { | ||
| account := conf.AdditionalAccounts.UserPrecompile | ||
|
|
||
| precompileRunner, err := initTestRunner( | ||
| "precompiles", | ||
| conf, | ||
| deployerRunner, | ||
| account, | ||
| runner.NewLogger(verbose, color.FgRed, "precompiles"), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| precompileRunner.Logger.Print("🏃 starting stateful precompiled contracts tests") | ||
| startTime := time.Now() | ||
|
|
||
| testsToRun, err := precompileRunner.GetE2ETestsToRunByName( | ||
| e2etests.AllE2ETests, | ||
| testNames..., | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("precompiled contracts tests failed: %v", err) | ||
| } | ||
|
|
||
| if err := precompileRunner.RunE2ETests(testsToRun); err != nil { | ||
| return fmt.Errorf("precompiled contracts tests failed: %v", err) | ||
| } | ||
|
|
||
| precompileRunner.Logger.Print("🍾 precompiled contracts tests completed in %s", time.Since(startTime).String()) | ||
|
|
||
| return err | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package e2etests | ||
|
|
||
| import ( | ||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/zeta-chain/zetacore/e2e/runner" | ||
| "github.com/zeta-chain/zetacore/precompiles/prototype" | ||
| ) | ||
|
|
||
| func TestPrecompilesRegular(r *runner.E2ERunner, args []string) { | ||
| require.Len(r, args, 0, "No arguments expected") | ||
|
|
||
| iPrototype, err := prototype.NewIPrototype(prototype.ContractAddress, r.ZEVMClient) | ||
| require.NoError(r, err, "Failed to create prototype contract caller") | ||
|
|
||
| res, err := iPrototype.Bech32ify(nil, "zeta", common.HexToAddress("0xB9Dbc229Bf588A613C00BEE8e662727AB8121cfE")) | ||
| require.NoError(r, err, "Error calling Bech32ify") | ||
| require.Equal(r, "zeta1h8duy2dltz9xz0qqhm5wvcnj02upy887fyn43u", res, "Failed to validate Bech32ify result") | ||
|
|
||
| addr, err := iPrototype.Bech32ToHexAddr(nil, "zeta1h8duy2dltz9xz0qqhm5wvcnj02upy887fyn43u") | ||
| require.NoError(r, err, "Error calling Bech32ToHexAddr") | ||
| require.Equal( | ||
| r, | ||
| "0xB9Dbc229Bf588A613C00BEE8e662727AB8121cfE", | ||
| addr.String(), | ||
| "Failed to validate Bech32ToHexAddr result", | ||
| ) | ||
|
|
||
| chainID, err := r.EVMClient.ChainID(r.Ctx) | ||
| require.NoError(r, err, "Error retrieving ChainID") | ||
|
|
||
| balance, err := iPrototype.GetGasStabilityPoolBalance(nil, chainID.Int64()) | ||
| require.NoError(r, err, "Error calling GetGasStabilityPoolBalance") | ||
| require.NotNil(r, balance, "GetGasStabilityPoolBalance returned balance is nil") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package precompiles | ||
fbac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "github.com/cosmos/cosmos-sdk/codec" | ||
| storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
| sdktypes "github.com/cosmos/cosmos-sdk/types" | ||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/core/vm" | ||
| ethparams "github.com/ethereum/go-ethereum/params" | ||
| evmkeeper "github.com/zeta-chain/ethermint/x/evm/keeper" | ||
|
|
||
| "github.com/zeta-chain/zetacore/precompiles/prototype" | ||
| "github.com/zeta-chain/zetacore/x/fungible/keeper" | ||
| ) | ||
|
|
||
| // EnabledStatefulContracts contains the list of all enabled stateful precompiles. | ||
| // This is useful for listing and reading from other packages, such as BlockedAddrs() function. | ||
| // Setting to false a contract here will disable it, not being included in the blockchain. | ||
| var EnabledStatefulContracts = map[common.Address]bool{ | ||
fbac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| prototype.ContractAddress: true, | ||
| } | ||
|
|
||
| // StatefulContracts returns all the registered precompiled contracts. | ||
| func StatefulContracts( | ||
| fungibleKeeper *keeper.Keeper, | ||
| cdc codec.Codec, | ||
| gasConfig storetypes.GasConfig, | ||
| ) (precompiledContracts []evmkeeper.CustomContractFn) { | ||
| // Initialize at 0 the custom compiled contracts and the addresses. | ||
| precompiledContracts = make([]evmkeeper.CustomContractFn, 0) | ||
|
|
||
| // Define the regular contract function. | ||
| if EnabledStatefulContracts[prototype.ContractAddress] { | ||
| prototype := func(_ sdktypes.Context, _ ethparams.Rules) vm.PrecompiledContract { | ||
| return prototype.NewIPrototypeContract(fungibleKeeper, cdc, gasConfig) | ||
| } | ||
|
|
||
| // Append the regular contract to the precompiledContracts slice. | ||
| precompiledContracts = append(precompiledContracts, prototype) | ||
| } | ||
|
|
||
| return precompiledContracts | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package precompiles | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
| ethparams "github.com/ethereum/go-ethereum/params" | ||
| "github.com/stretchr/testify/require" | ||
| ethermint "github.com/zeta-chain/ethermint/types" | ||
| "github.com/zeta-chain/zetacore/testutil/keeper" | ||
| ) | ||
|
|
||
| func Test_StatefulContracts(t *testing.T) { | ||
| k, ctx, _, _ := keeper.FungibleKeeper(t) | ||
| gasConfig := storetypes.TransientGasConfig() | ||
|
|
||
| var encoding ethermint.EncodingConfig | ||
| appCodec := encoding.Codec | ||
|
|
||
| var expectedContracts int | ||
| for _, enabled := range EnabledStatefulContracts { | ||
| if enabled { | ||
| expectedContracts++ | ||
| } | ||
| } | ||
|
|
||
| // StatefulContracts() should return all the enabled contracts. | ||
| contracts := StatefulContracts(k, appCodec, gasConfig) | ||
| require.NotNil(t, contracts, "StatefulContracts() should not return a nil slice") | ||
| require.Len(t, contracts, expectedContracts, "StatefulContracts() should return all the enabled contracts") | ||
|
|
||
| // Extract the contract function from the first contract. | ||
| customContractFn := contracts[0] | ||
| contract := customContractFn(ctx, ethparams.Rules{}) | ||
|
|
||
| // Check the contract function returns a valid address. | ||
| contractAddr := contract.Address() | ||
| require.NotNil(t, contractAddr, "The called contract should have a valid address") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.