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

fix config template for precompilegen #538

Merged
merged 4 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 8 additions & 31 deletions accounts/abi/bind/precompilebind/precompile_config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (

var _ precompileconfig.Config = &Config{}

// Config implements the StatefulPrecompileConfig
// interface while adding in the {{.Contract.Type}} specific precompile address.
// Config implements the precompileconfig.Config interface and
// adds specific configuration for {{.Contract.Type}}.
type Config struct {
{{- if .Contract.AllowList}}
allowlist.AllowListConfig
Expand All @@ -35,38 +35,15 @@ type Config struct {
// Add your own custom fields for Config here
}

{{$structs := .Structs}}
{{range $structs}}
// {{.Name}} is an auto generated low-level Go binding around an user-defined struct.
type {{.Name}} struct {
{{range $field := .Fields}}
{{$field.Name}} {{$field.Type}}{{end}}
}
{{- end}}

{{- range .Contract.Funcs}}
{{ if len .Normalized.Inputs | lt 1}}
type {{capitalise .Normalized.Name}}Input struct{
{{range .Normalized.Inputs}} {{capitalise .Name}} {{bindtype .Type $structs}}; {{end}}
}
{{- end}}
{{ if len .Normalized.Outputs | lt 1}}
type {{capitalise .Normalized.Name}}Output struct{
{{range .Normalized.Outputs}} {{capitalise .Name}} {{bindtype .Type $structs}}; {{end}}
}
{{- end}}
{{- end}}

// NewConfig returns a config for a network upgrade at [blockTimestamp] that enables
// {{.Contract.Type}} with the given [admins] and [enableds] as members of the allowlist.
// {{.Contract.Type}} {{if .Contract.AllowList}} with the given [admins] as members of the allowlist {{end}}.
// {{.Contract.Type}} {{- if .Contract.AllowList}} with the given [admins] as members of the allowlist {{end}}.
func NewConfig(blockTimestamp *big.Int{{if .Contract.AllowList}}, admins []common.Address, enableds []common.Address,{{end}}) *Config {
return &Config{
{{- if .Contract.AllowList}}
AllowListConfig: allowlist.AllowListConfig{
AdminAddresses: admins,
EnabledAddresses: enableds,
},
},
{{- end}}
Upgrade: precompileconfig.Upgrade{BlockTimestamp: blockTimestamp},
}
Expand All @@ -89,12 +66,12 @@ func (*Config) Key() string { return ConfigKey }

// Verify tries to verify Config and returns an error accordingly.
func (c *Config) Verify() error {
{{if .Contract.AllowList}}
{{- if .Contract.AllowList}}
// Verify AllowList first
if err := c.AllowListConfig.Verify(); err != nil {
return err
}
{{end}}
{{- end}}
// CUSTOM CODE STARTS HERE
// Add your own custom verify code for Config here
// and return an error accordingly
Expand All @@ -110,8 +87,8 @@ func (c *Config) Equal(s precompileconfig.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.AllowListConfig.Equal(&other.AllowListConfig) {{end}}
// 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.AllowListConfig.Equal(&other.AllowListConfig) {{end}}
return equals
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (
{{- if .Contract.AllowList}}
// This contract also uses AllowList precompile.
// You should also increase gas costs of functions that read from AllowList storage.
{{- end}}}
{{- end}}
{{- range .Contract.Funcs}}
{{.Normalized.Name}}GasCost uint64 = 0 {{if not .Original.IsConstant | and $contract.AllowList}} + allowlist.ReadAllowListGasCost {{end}} // SET A GAS COST HERE
{{- end}}
Expand Down Expand Up @@ -136,7 +136,7 @@ func Set{{.Contract.Type}}AllowListStatus(stateDB contract.StateDB, address comm

{{range .Contract.Funcs}}
{{if len .Normalized.Inputs | lt 1}}
// Unpack{{capitalise .Normalized.Name}}Input attempts to unpack [input] into the arguments for the {{capitalise .Normalized.Name}}Input{}
// Unpack{{capitalise .Normalized.Name}}Input attempts to unpack [input] as {{capitalise .Normalized.Name}}Input
// assumes that [input] does not include selector (omits first 4 func signature bytes)
func Unpack{{capitalise .Normalized.Name}}Input(input []byte) ({{capitalise .Normalized.Name}}Input, error) {
inputStruct := {{capitalise .Normalized.Name}}Input{}
Expand Down Expand Up @@ -298,7 +298,7 @@ func {{decapitalise $contract.Type}}Fallback (accessibleState contract.Accessibl
{{- end}}

// create{{.Contract.Type}}Precompile returns a StatefulPrecompiledContract with getters and setters for the precompile.
{{if .Contract.AllowList}} // Access to the getters/setters is controlled by an allow list for ContractAddress.{{end}}
{{- if .Contract.AllowList}} // Access to the getters/setters is controlled by an allow list for ContractAddress.{{end}}
func create{{.Contract.Type}}Precompile() contract.StatefulPrecompiledContract {
var functions []*contract.StatefulPrecompileFunction
{{- if .Contract.AllowList}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ func (*configurator) Configure(chainConfig contract.ChainConfig, cfg precompilec
return fmt.Errorf("incorrect config %T: %v", config, config)
}
// CUSTOM CODE STARTS HERE
{{if .Contract.AllowList}}
{{- if .Contract.AllowList}}
// AllowList is activated for this precompile. Configuring allowlist addresses here.
return config.AllowListConfig.Configure(state, ContractAddress)
{{else}}
{{- else}}
return nil
{{end}}
{{- end}}
}
`