Skip to content

Commit c5c1017

Browse files
authored
nit improvements (#529)
* nit improvements * move comments to README * Update cmd/precompilegen/template-readme.md
1 parent e8b55cf commit c5c1017

File tree

6 files changed

+62
-94
lines changed

6 files changed

+62
-94
lines changed

accounts/abi/bind/precompilebind/precompile_config_template.go

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,31 @@ const tmplSourcePrecompileConfigGo = `
88
// This file is a generated precompile contract config with stubbed abstract functions.
99
// The file is generated by a template. Please inspect every code and comment in this file before use.
1010
11-
// 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.
12-
// Additionally there are other files you need to edit to activate your precompile.
13-
// These areas are highlighted with comments "ADD YOUR PRECOMPILE HERE".
14-
// For testing take a look at other precompile tests in contract_test.go and config_test.go in other precompile folders.
15-
// See the tutorial in https://docs.avax.network/subnets/hello-world-precompile-tutorial for more information about precompile development.
16-
17-
/* General guidelines for precompile development:
18-
1- Read the comment and set a suitable contract address in generated module.go. E.g:
19-
ContractAddress = common.HexToAddress("ASUITABLEHEXADDRESS")
20-
2- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
21-
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.
22-
Modifying code outside of these areas should be done with caution and with a deep understanding of how these changes may impact the EVM.
23-
4- Set gas costs in generated contract.go
24-
5- Add your config unit tests under generated package config_test.go
25-
6- Add your contract unit tests undertgenerated package contract_test.go
26-
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.
27-
8- Add your solidity interface and test contract to contract-examples/contracts
28-
9- Write solidity tests for your precompile in contract-examples/test
29-
10- Create your genesis with your precompile enabled in tests/e2e/genesis/
30-
11- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
31-
12- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh'
32-
*/
33-
3411
package {{.Package}}
3512
3613
import (
3714
"math/big"
3815
39-
"github.com/ava-labs/subnet-evm/precompile/config"
16+
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
4017
{{- if .Contract.AllowList}}
4118
"github.com/ava-labs/subnet-evm/precompile/allowlist"
42-
{{- end}}
4319
4420
"github.com/ethereum/go-ethereum/common"
21+
{{- end}}
22+
4523
)
4624
47-
var _ config.Config = &Config{}
25+
var _ precompileconfig.Config = &Config{}
4826
4927
// Config implements the StatefulPrecompileConfig
5028
// interface while adding in the {{.Contract.Type}} specific precompile address.
5129
type Config struct {
5230
{{- if .Contract.AllowList}}
53-
allowlist.Config
31+
allowlist.AllowListConfig
5432
{{- end}}
55-
config.Upgrade
33+
precompileconfig.Upgrade
34+
// CUSTOM CODE STARTS HERE
35+
// Add your own custom fields for Config here
5636
}
5737
5838
{{$structs := .Structs}}
@@ -83,35 +63,35 @@ type {{capitalise .Normalized.Name}}Output struct{
8363
func NewConfig(blockTimestamp *big.Int{{if .Contract.AllowList}}, admins []common.Address, enableds []common.Address,{{end}}) *Config {
8464
return &Config{
8565
{{- if .Contract.AllowList}}
86-
Config: allowlist.Config{
66+
AllowListConfig: allowlist.AllowListConfig{
8767
AdminAddresses: admins,
8868
EnabledAddresses: enableds,
8969
},
9070
{{- end}}
91-
Upgrade: config.Upgrade{BlockTimestamp: blockTimestamp},
71+
Upgrade: precompileconfig.Upgrade{BlockTimestamp: blockTimestamp},
9272
}
9373
}
9474
9575
// NewDisableConfig returns config for a network upgrade at [blockTimestamp]
9676
// that disables {{.Contract.Type}}.
9777
func NewDisableConfig(blockTimestamp *big.Int) *Config {
9878
return &Config{
99-
Upgrade: config.Upgrade{
79+
Upgrade: precompileconfig.Upgrade{
10080
BlockTimestamp: blockTimestamp,
10181
Disable: true,
10282
},
10383
}
10484
}
10585
106-
// Key returns the key for the {{.Contract.Type}} config.
86+
// Key returns the key for the {{.Contract.Type}} precompileconfig.
10787
// This should be the same key as used in the precompile module.
10888
func (*Config) Key() string { return ConfigKey }
10989
11090
// Verify tries to verify Config and returns an error accordingly.
11191
func (c *Config) Verify() error {
11292
{{if .Contract.AllowList}}
11393
// Verify AllowList first
114-
if err := c.Config.Verify(); err != nil {
94+
if err := c.AllowListConfig.Verify(); err != nil {
11595
return err
11696
}
11797
{{end}}
@@ -122,7 +102,7 @@ func (c *Config) Verify() error {
122102
}
123103
124104
// Equal returns true if [s] is a [*Config] and it has been configured identical to [c].
125-
func (c *Config) Equal(s config.Config) bool {
105+
func (c *Config) Equal(s precompileconfig.Config) bool {
126106
// typecast before comparison
127107
other, ok := (s).(*Config)
128108
if !ok {
@@ -131,7 +111,7 @@ func (c *Config) Equal(s config.Config) bool {
131111
// CUSTOM CODE STARTS HERE
132112
// modify this boolean accordingly with your custom Config, to check if [other] and the current [c] are equal
133113
// if Config contains only Upgrade {{if .Contract.AllowList}} and AllowListConfig {{end}} you can skip modifying it.
134-
equals := c.Upgrade.Equal(&other.Upgrade) {{if .Contract.AllowList}} && c.Config.Equal(&other.Config) {{end}}
114+
equals := c.Upgrade.Equal(&other.Upgrade) {{if .Contract.AllowList}} && c.AllowListConfig.Equal(&other.AllowListConfig) {{end}}
135115
return equals
136116
}
137117
`

accounts/abi/bind/precompilebind/precompile_contract_template.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,6 @@ const tmplSourcePrecompileContractGo = `
2525
// This file is a generated precompile contract config with stubbed abstract functions.
2626
// The file is generated by a template. Please inspect every code and comment in this file before use.
2727
28-
// 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.
29-
// Additionally there are other files you need to edit to activate your precompile.
30-
// These areas are highlighted with comments "ADD YOUR PRECOMPILE HERE".
31-
// For testing take a look at other precompile tests in contract_test.go and config_test.go in other precompile folders.
32-
// See the tutorial in https://docs.avax.network/subnets/hello-world-precompile-tutorial for more information about precompile development.
33-
34-
/* General guidelines for precompile development:
35-
1- Read the comment and set a suitable contract address in generated module.go. E.g:
36-
ContractAddress = common.HexToAddress("ASUITABLEHEXADDRESS")
37-
2- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
38-
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.
39-
Modifying code outside of these areas should be done with caution and with a deep understanding of how these changes may impact the EVM.
40-
4- Set gas costs in generated contract.go
41-
5- Add your config unit tests under generated package config_test.go
42-
6- Add your contract unit tests undertgenerated package contract_test.go
43-
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.
44-
8- Add your solidity interface and test contract to contract-examples/contracts
45-
9- Write solidity tests for your precompile in contract-examples/test
46-
10- Create your genesis with your precompile enabled in tests/e2e/genesis/
47-
11- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
48-
12- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh'
49-
*/
50-
5128
package {{.Package}}
5229
5330
import (

accounts/abi/bind/precompilebind/precompile_module_template.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,12 @@ const tmplSourcePrecompileModuleGo = `
88
// This file is a generated precompile contract config with stubbed abstract functions.
99
// The file is generated by a template. Please inspect every code and comment in this file before use.
1010
11-
// 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.
12-
// Additionally there are other files you need to edit to activate your precompile.
13-
// These areas are highlighted with comments "ADD YOUR PRECOMPILE HERE".
14-
// For testing take a look at other precompile tests in contract_test.go and config_test.go in other precompile folders.
15-
// See the tutorial in https://docs.avax.network/subnets/hello-world-precompile-tutorial for more information about precompile development.
16-
17-
/* General guidelines for precompile development:
18-
1- Read the comment and set a suitable contract address in generated module.go. E.g:
19-
ContractAddress = common.HexToAddress("ASUITABLEHEXADDRESS")
20-
2- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
21-
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.
22-
Modifying code outside of these areas should be done with caution and with a deep understanding of how these changes may impact the EVM.
23-
4- Set gas costs in generated contract.go
24-
5- Add your config unit tests under generated package config_test.go
25-
6- Add your contract unit tests undertgenerated package contract_test.go
26-
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.
27-
8- Add your solidity interface and test contract to contract-examples/contracts
28-
9- Write solidity tests for your precompile in contract-examples/test
29-
10- Create your genesis with your precompile enabled in tests/e2e/genesis/
30-
11- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
31-
12- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh'
32-
*/
33-
3411
package {{.Package}}
3512
3613
import (
3714
"fmt"
3815
39-
"github.com/ava-labs/subnet-evm/precompile/config"
16+
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
4017
"github.com/ava-labs/subnet-evm/precompile/contract"
4118
"github.com/ava-labs/subnet-evm/precompile/modules"
4219
@@ -45,7 +22,7 @@ import (
4522
4623
var _ contract.Configurator = &configurator{}
4724
48-
// ConfigKey is the key used in json config files to specify this precompile config.
25+
// ConfigKey is the key used in json config files to specify this precompile precompileconfig.
4926
// must be unique across all precompiles.
5027
const ConfigKey = "{{decapitalise .Contract.Type}}Config"
5128
@@ -72,25 +49,25 @@ func init() {
7249
}
7350
}
7451
75-
// NewConfig returns a new precompile config.
76-
// This is required for Marshal/Unmarshal the precompile config.
77-
func (*configurator) NewConfig() config.Config {
52+
// NewConfig returns a new precompile precompileconfig.
53+
// This is required for Marshal/Unmarshal the precompile precompileconfig.
54+
func (*configurator) NewConfig() precompileconfig.Config {
7855
return &Config{}
7956
}
8057
81-
// Configure configures [state] with the given [cfg] config.
58+
// Configure configures [state] with the given [cfg] precompileconfig.
8259
// This function is called by the EVM once per precompile contract activation.
8360
// You can use this function to set up your precompile contract's initial state,
8461
// by using the [cfg] config and [state] stateDB.
85-
func (*configurator) Configure(chainConfig contract.ChainConfig, cfg config.Config, state contract.StateDB, _ contract.BlockContext) error {
62+
func (*configurator) Configure(chainConfig contract.ChainConfig, cfg precompileconfig.Config, state contract.StateDB, _ contract.BlockContext) error {
8663
config, ok := cfg.(*Config)
8764
if !ok {
8865
return fmt.Errorf("incorrect config %T: %v", config, config)
8966
}
9067
// CUSTOM CODE STARTS HERE
9168
{{if .Contract.AllowList}}
9269
// AllowList is activated for this precompile. Configuring allowlist addresses here.
93-
return config.Configure(state, ContractAddress)
70+
return config.AllowListConfig.Configure(state, ContractAddress)
9471
{{else}}
9572
return nil
9673
{{end}}

cmd/precompilegen/main.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"path/filepath"
3434
"strings"
3535

36+
_ "embed"
37+
3638
"github.com/ava-labs/subnet-evm/accounts/abi/bind"
3739
"github.com/ava-labs/subnet-evm/accounts/abi/bind/precompilebind"
3840
"github.com/ava-labs/subnet-evm/internal/flags"
@@ -47,25 +49,28 @@ var (
4749
gitDate = ""
4850

4951
app *cli.App
52+
53+
//go:embed template-readme.md
54+
readme string
5055
)
5156

5257
var (
5358
// Flags needed by abigen
5459
abiFlag = &cli.StringFlag{
5560
Name: "abi",
56-
Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
61+
Usage: "Path to the contract ABI json to generate, - for STDIN",
5762
}
5863
typeFlag = &cli.StringFlag{
5964
Name: "type",
60-
Usage: "Struct name for the precompile (default = {ABI name})",
65+
Usage: "Struct name for the precompile (default = {abi file name})",
6166
}
6267
pkgFlag = &cli.StringFlag{
6368
Name: "pkg",
64-
Usage: "Package name to generate the precompile into (default = {type})",
69+
Usage: "Go package name to generate the precompile into (default = {type})",
6570
}
6671
outFlag = &cli.StringFlag{
6772
Name: "out",
68-
Usage: "Output folder for the generated precompile files, - for STDOUT (default = ./{pkg})",
73+
Usage: "Output folder for the generated precompile files, - for STDOUT (default = ./precompile/contracts/{pkg})",
6974
}
7075
)
7176

@@ -134,7 +139,7 @@ func precompilegen(c *cli.Context) error {
134139
}
135140

136141
if outFlagStr == "" {
137-
outFlagStr = filepath.Join("./", pkg)
142+
outFlagStr = filepath.Join("./precompile/contracts", pkg)
138143
}
139144

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

195+
readmeOut := filepath.Join(outFlagStr, "README.md")
196+
197+
if err := os.WriteFile(readmeOut, []byte(readme), 0o600); err != nil {
198+
utils.Fatalf("Failed to write README: %v", err)
199+
}
200+
190201
fmt.Println("Precompile files generated successfully at: ", outFlagStr)
191202
return nil
192203
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
There are some must-be-done changes waiting in the generated file. Each area requiring you to add your code is marked with CUSTOM CODE to make them easy to find and modify.
2+
Additionally there are other files you need to edit to activate your precompile.
3+
These areas are highlighted with comments "ADD YOUR PRECOMPILE HERE".
4+
For testing take a look at other precompile tests in contract_test.go and config_test.go in other precompile folders.
5+
See the tutorial in <https://docs.avax.network/subnets/hello-world-precompile-tutorial> for more information about precompile development.
6+
7+
General guidelines for precompile development:
8+
1- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
9+
2- Read the comment and set a suitable contract address in generated module.go. E.g:
10+
ContractAddress = common.HexToAddress("ASUITABLEHEXADDRESS")
11+
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.
12+
Modifying code outside of these areas should be done with caution and with a deep understanding of how these changes may impact the EVM.
13+
4- Set gas costs in generated contract.go
14+
5- Force import your precompile package in precompile/registry/registry.go
15+
6- Add your config unit tests under generated package config_test.go
16+
7- Add your contract unit tests under generated package contract_test.go
17+
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.
18+
9- Add your solidity interface and test contract to contract-examples/contracts
19+
10- Write solidity tests for your precompile in contract-examples/test
20+
11- Create your genesis with your precompile enabled in tests/e2e/genesis/
21+
12- Create e2e test for your solidity test in tests/e2e/solidity/suites.go
22+
13- Run your e2e precompile Solidity tests with 'E2E=true ./scripts/run.sh

precompile/allowlist/test_allowlist.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ func SetDefaultRoles(contractAddress common.Address) func(t *testing.T, state co
273273
}
274274

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

0 commit comments

Comments
 (0)