Skip to content

Commit

Permalink
fix config template for precompilegen (#538)
Browse files Browse the repository at this point in the history
* fix config template for precompilegen

* nits

* nit

* nits
  • Loading branch information
darioush authored Feb 23, 2023
1 parent 4455733 commit 1d7b284
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 37 deletions.
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}}
}
`

0 comments on commit 1d7b284

Please sign in to comment.