Skip to content
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

nit improvements #529

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions accounts/abi/bind/precompilebind/precompile_config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,48 @@ const tmplSourcePrecompileConfigGo = `
// See the tutorial in https://docs.avax.network/subnets/hello-world-precompile-tutorial for more information about precompile development.

/* General guidelines for precompile development:
1- Read the comment and set a suitable contract address in generated module.go. E.g:
1- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
2- Read the comment and set a suitable contract address in generated module.go. E.g:
ContractAddress = common.HexToAddress("ASUITABLEHEXADDRESS")
2- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
3- It is recommended to only modify code in the highlighted areas marked with "CUSTOM CODE STARTS HERE". Typically, custom codes are required in only those areas.
Modifying code outside of these areas should be done with caution and with a deep understanding of how these changes may impact the EVM.
4- Set gas costs in generated contract.go
5- Add your config unit tests under generated package config_test.go
6- Add your contract unit tests undertgenerated package contract_test.go
7- Additionally you can add a full-fledged VM test for your precompile under plugin/vm/vm_test.go. See existing precompile tests for examples.
8- Add your solidity interface and test contract to contract-examples/contracts
9- Write solidity tests for your precompile in contract-examples/test
10- Create your genesis with your precompile enabled in tests/e2e/genesis/
11- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
12- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh'
5- Force import your precompile package in precompile/registry/registry.go
6- Add your config unit tests under generated package config_test.go
7- Add your contract unit tests undertgenerated package contract_test.go
aaronbuchwald marked this conversation as resolved.
Show resolved Hide resolved
8- Additionally you can add a full-fledged VM test for your precompile under plugin/vm/vm_test.go. See existing precompile tests for examples.
9- Add your solidity interface and test contract to contract-examples/contracts
10- Write solidity tests for your precompile in contract-examples/test
11- Create your genesis with your precompile enabled in tests/e2e/genesis/
12- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
13- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh
*/

package {{.Package}}

import (
"math/big"

"github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
{{- if .Contract.AllowList}}
"github.com/ava-labs/subnet-evm/precompile/allowlist"
{{- end}}

"github.com/ethereum/go-ethereum/common"
{{- end}}

)

var _ config.Config = &Config{}
var _ precompileconfig.Config = &Config{}

// Config implements the StatefulPrecompileConfig
// interface while adding in the {{.Contract.Type}} specific precompile address.
type Config struct {
{{- if .Contract.AllowList}}
allowlist.Config
allowlist.AllowListConfig
{{- end}}
config.Upgrade
precompileconfig.Upgrade
// CUSTOM CODE STARTS HERE
// Add your own custom fields for Config here
}

{{$structs := .Structs}}
Expand Down Expand Up @@ -83,35 +87,35 @@ type {{capitalise .Normalized.Name}}Output struct{
func NewConfig(blockTimestamp *big.Int{{if .Contract.AllowList}}, admins []common.Address, enableds []common.Address,{{end}}) *Config {
return &Config{
{{- if .Contract.AllowList}}
Config: allowlist.Config{
AllowListConfig: allowlist.AllowListConfig{
AdminAddresses: admins,
EnabledAddresses: enableds,
},
{{- end}}
Upgrade: config.Upgrade{BlockTimestamp: blockTimestamp},
Upgrade: precompileconfig.Upgrade{BlockTimestamp: blockTimestamp},
}
}

// NewDisableConfig returns config for a network upgrade at [blockTimestamp]
// that disables {{.Contract.Type}}.
func NewDisableConfig(blockTimestamp *big.Int) *Config {
return &Config{
Upgrade: config.Upgrade{
Upgrade: precompileconfig.Upgrade{
BlockTimestamp: blockTimestamp,
Disable: true,
},
}
}

// Key returns the key for the {{.Contract.Type}} config.
// Key returns the key for the {{.Contract.Type}} precompileconfig.
// This should be the same key as used in the precompile module.
func (*Config) Key() string { return ConfigKey }

// Verify tries to verify Config and returns an error accordingly.
func (c *Config) Verify() error {
{{if .Contract.AllowList}}
// Verify AllowList first
if err := c.Config.Verify(); err != nil {
if err := c.AllowListConfig.Verify(); err != nil {
return err
}
{{end}}
Expand All @@ -122,7 +126,7 @@ func (c *Config) Verify() error {
}

// Equal returns true if [s] is a [*Config] and it has been configured identical to [c].
func (c *Config) Equal(s config.Config) bool {
func (c *Config) Equal(s precompileconfig.Config) bool {
// typecast before comparison
other, ok := (s).(*Config)
if !ok {
Expand All @@ -131,7 +135,7 @@ func (c *Config) Equal(s config.Config) bool {
// CUSTOM CODE STARTS HERE
// modify this boolean accordingly with your custom Config, to check if [other] and the current [c] are equal
// if Config contains only Upgrade {{if .Contract.AllowList}} and AllowListConfig {{end}} you can skip modifying it.
equals := c.Upgrade.Equal(&other.Upgrade) {{if .Contract.AllowList}} && c.Config.Equal(&other.Config) {{end}}
equals := c.Upgrade.Equal(&other.Upgrade) {{if .Contract.AllowList}} && c.AllowListConfig.Equal(&other.AllowListConfig) {{end}}
return equals
}
`
21 changes: 11 additions & 10 deletions accounts/abi/bind/precompilebind/precompile_contract_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@ const tmplSourcePrecompileContractGo = `
// See the tutorial in https://docs.avax.network/subnets/hello-world-precompile-tutorial for more information about precompile development.

/* General guidelines for precompile development:
1- Read the comment and set a suitable contract address in generated module.go. E.g:
1- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
2- Read the comment and set a suitable contract address in generated module.go. E.g:
ContractAddress = common.HexToAddress("ASUITABLEHEXADDRESS")
2- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
3- It is recommended to only modify code in the highlighted areas marked with "CUSTOM CODE STARTS HERE". Typically, custom codes are required in only those areas.
Modifying code outside of these areas should be done with caution and with a deep understanding of how these changes may impact the EVM.
4- Set gas costs in generated contract.go
5- Add your config unit tests under generated package config_test.go
6- Add your contract unit tests undertgenerated package contract_test.go
7- Additionally you can add a full-fledged VM test for your precompile under plugin/vm/vm_test.go. See existing precompile tests for examples.
8- Add your solidity interface and test contract to contract-examples/contracts
9- Write solidity tests for your precompile in contract-examples/test
10- Create your genesis with your precompile enabled in tests/e2e/genesis/
11- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
12- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh'
5- Force import your precompile package in precompile/registry/registry.go
6- Add your config unit tests under generated package config_test.go
7- Add your contract unit tests undertgenerated package contract_test.go
8- Additionally you can add a full-fledged VM test for your precompile under plugin/vm/vm_test.go. See existing precompile tests for examples.
9- Add your solidity interface and test contract to contract-examples/contracts
10- Write solidity tests for your precompile in contract-examples/test
11- Create your genesis with your precompile enabled in tests/e2e/genesis/
12- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
13- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh
*/

package {{.Package}}
Expand Down
37 changes: 19 additions & 18 deletions accounts/abi/bind/precompilebind/precompile_module_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,29 @@ const tmplSourcePrecompileModuleGo = `
// See the tutorial in https://docs.avax.network/subnets/hello-world-precompile-tutorial for more information about precompile development.

/* General guidelines for precompile development:
1- Read the comment and set a suitable contract address in generated module.go. E.g:
1- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
2- Read the comment and set a suitable contract address in generated module.go. E.g:
ContractAddress = common.HexToAddress("ASUITABLEHEXADDRESS")
2- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
3- It is recommended to only modify code in the highlighted areas marked with "CUSTOM CODE STARTS HERE". Typically, custom codes are required in only those areas.
Modifying code outside of these areas should be done with caution and with a deep understanding of how these changes may impact the EVM.
4- Set gas costs in generated contract.go
5- Add your config unit tests under generated package config_test.go
6- Add your contract unit tests undertgenerated package contract_test.go
7- Additionally you can add a full-fledged VM test for your precompile under plugin/vm/vm_test.go. See existing precompile tests for examples.
8- Add your solidity interface and test contract to contract-examples/contracts
9- Write solidity tests for your precompile in contract-examples/test
10- Create your genesis with your precompile enabled in tests/e2e/genesis/
11- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
12- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh'
5- Force import your precompile package in precompile/registry/registry.go
6- Add your config unit tests under generated package config_test.go
7- Add your contract unit tests undertgenerated package contract_test.go
8- Additionally you can add a full-fledged VM test for your precompile under plugin/vm/vm_test.go. See existing precompile tests for examples.
9- Add your solidity interface and test contract to contract-examples/contracts
10- Write solidity tests for your precompile in contract-examples/test
11- Create your genesis with your precompile enabled in tests/e2e/genesis/
12- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
13- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh
*/

package {{.Package}}

import (
"fmt"

"github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ava-labs/subnet-evm/precompile/modules"

Expand All @@ -45,7 +46,7 @@ import (

var _ contract.Configurator = &configurator{}

// ConfigKey is the key used in json config files to specify this precompile config.
// ConfigKey is the key used in json config files to specify this precompile precompileconfig.
// must be unique across all precompiles.
const ConfigKey = "{{decapitalise .Contract.Type}}Config"

Expand All @@ -72,25 +73,25 @@ func init() {
}
}

// NewConfig returns a new precompile config.
// This is required for Marshal/Unmarshal the precompile config.
func (*configurator) NewConfig() config.Config {
// NewConfig returns a new precompile precompileconfig.
// This is required for Marshal/Unmarshal the precompile precompileconfig.
func (*configurator) NewConfig() precompileconfig.Config {
return &Config{}
}

// Configure configures [state] with the given [cfg] config.
// Configure configures [state] with the given [cfg] precompileconfig.
// This function is called by the EVM once per precompile contract activation.
// You can use this function to set up your precompile contract's initial state,
// by using the [cfg] config and [state] stateDB.
func (*configurator) Configure(chainConfig contract.ChainConfig, cfg config.Config, state contract.StateDB, _ contract.BlockContext) error {
func (*configurator) Configure(chainConfig contract.ChainConfig, cfg precompileconfig.Config, state contract.StateDB, _ contract.BlockContext) error {
config, ok := cfg.(*Config)
if !ok {
return fmt.Errorf("incorrect config %T: %v", config, config)
}
// CUSTOM CODE STARTS HERE
{{if .Contract.AllowList}}
// AllowList is activated for this precompile. Configuring allowlist addresses here.
return config.Configure(state, ContractAddress)
return config.AllowListConfig.Configure(state, ContractAddress)
{{else}}
return nil
{{end}}
Expand Down
10 changes: 5 additions & 5 deletions cmd/precompilegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ var (
// Flags needed by abigen
abiFlag = &cli.StringFlag{
Name: "abi",
Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
Usage: "Path to the contract ABI json to generate, - for STDIN",
}
typeFlag = &cli.StringFlag{
Name: "type",
Usage: "Struct name for the precompile (default = {ABI name})",
Usage: "Struct name for the precompile (default = {abi file name})",
}
pkgFlag = &cli.StringFlag{
Name: "pkg",
Usage: "Package name to generate the precompile into (default = {type})",
Usage: "Go package name to generate the precompile into (default = {type})",
}
outFlag = &cli.StringFlag{
Name: "out",
Usage: "Output folder for the generated precompile files, - for STDOUT (default = ./{pkg})",
Usage: "Output folder for the generated precompile files, - for STDOUT (default = ./precompile/contracts/{pkg})",
}
)

Expand Down Expand Up @@ -134,7 +134,7 @@ func precompilegen(c *cli.Context) error {
}

if outFlagStr == "" {
outFlagStr = filepath.Join("./", pkg)
outFlagStr = filepath.Join("./precompile/contracts", pkg)
}

abifilename := ""
Expand Down
1 change: 1 addition & 0 deletions precompile/allowlist/test_allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func SetDefaultRoles(contractAddress common.Address) func(t *testing.T, state co
}

func RunPrecompileWithAllowListTests(t *testing.T, module modules.Module, newStateDB func(t *testing.T) contract.StateDB, contractTests map[string]testutils.PrecompileTest) {
t.Helper()
tests := AllowListTests(module)
// Add the contract specific tests to the map of tests to run.
for name, test := range contractTests {
Expand Down