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 2 commits
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
50 changes: 15 additions & 35 deletions accounts/abi/bind/precompilebind/precompile_config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,31 @@ const tmplSourcePrecompileConfigGo = `
// This file is a generated precompile contract config with stubbed abstract functions.
// The file is generated by a template. Please inspect every code and comment in this file before use.

// There are some must-be-done changes waiting in the file. Each area requiring you to add your code is marked with CUSTOM CODE to make them easy to find and modify.
// Additionally there are other files you need to edit to activate your precompile.
// These areas are highlighted with comments "ADD YOUR PRECOMPILE HERE".
// For testing take a look at other precompile tests in contract_test.go and config_test.go in other precompile folders.
// 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:
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'
*/

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 +63,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 +102,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 +111,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
}
`
23 changes: 0 additions & 23 deletions accounts/abi/bind/precompilebind/precompile_contract_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,6 @@ const tmplSourcePrecompileContractGo = `
// This file is a generated precompile contract config with stubbed abstract functions.
// The file is generated by a template. Please inspect every code and comment in this file before use.

// There are some must-be-done changes waiting in the file. Each area requiring you to add your code is marked with CUSTOM CODE to make them easy to find and modify.
// Additionally there are other files you need to edit to activate your precompile.
// These areas are highlighted with comments "ADD YOUR PRECOMPILE HERE".
// For testing take a look at other precompile tests in contract_test.go and config_test.go in other precompile folders.
// 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:
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'
*/

package {{.Package}}

import (
Expand Down
39 changes: 8 additions & 31 deletions accounts/abi/bind/precompilebind/precompile_module_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,12 @@ const tmplSourcePrecompileModuleGo = `
// This file is a generated precompile contract config with stubbed abstract functions.
// The file is generated by a template. Please inspect every code and comment in this file before use.

// There are some must-be-done changes waiting in the file. Each area requiring you to add your code is marked with CUSTOM CODE to make them easy to find and modify.
// Additionally there are other files you need to edit to activate your precompile.
// These areas are highlighted with comments "ADD YOUR PRECOMPILE HERE".
// For testing take a look at other precompile tests in contract_test.go and config_test.go in other precompile folders.
// 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:
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'
*/

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 +22,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 +49,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
21 changes: 16 additions & 5 deletions cmd/precompilegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"path/filepath"
"strings"

_ "embed"

"github.com/ava-labs/subnet-evm/accounts/abi/bind"
"github.com/ava-labs/subnet-evm/accounts/abi/bind/precompilebind"
"github.com/ava-labs/subnet-evm/internal/flags"
Expand All @@ -47,25 +49,28 @@ var (
gitDate = ""

app *cli.App

//go:embed template-readme.md
readme string
)

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 +139,7 @@ func precompilegen(c *cli.Context) error {
}

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

abifilename := ""
Expand Down Expand Up @@ -187,6 +192,12 @@ func precompilegen(c *cli.Context) error {
utils.Fatalf("Failed to write ABI: %v", err)
}

readmeOut := filepath.Join(outFlagStr, "README.md")

if err := os.WriteFile(readmeOut, []byte(readme), 0o600); err != nil {
utils.Fatalf("Failed to write README: %v", err)
}

fmt.Println("Precompile files generated successfully at: ", outFlagStr)
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions cmd/precompilegen/template-readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
There are some must-be-done changes waiting in the file. Each area requiring you to add your code is marked with CUSTOM CODE to make them easy to find and modify.
aaronbuchwald marked this conversation as resolved.
Show resolved Hide resolved
Additionally there are other files you need to edit to activate your precompile.
These areas are highlighted with comments "ADD YOUR PRECOMPILE HERE".
For testing take a look at other precompile tests in contract_test.go and config_test.go in other precompile folders.
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- 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")
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- 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 under generated 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
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