Skip to content

Commit

Permalink
Index methods by signature when there is ambiguity (#1880)
Browse files Browse the repository at this point in the history
* Index methods by signature when there is ambiguity

* Lint fix and renames

* Simplify
  • Loading branch information
Stefan-Ethernal authored Sep 7, 2023
1 parent b467a12 commit c3da0d4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
53 changes: 35 additions & 18 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,19 @@ func main() {
// There could be two objects with the same name in the generated JSON ABI (hardhat bug).
// This case can be fixed by specifying a function signature instead of just name
// e.g. "myFunc(address,bool,uint256)" instead of just "myFunc"
var method *abi.Method
var (
method *abi.Method
resolvedBySignature = false
)

if signatureFunctionFormat.MatchString(methodRaw) {
method = c.artifact.Abi.GetMethodBySignature(methodRaw)
resolvedBySignature = true
} else {
method = c.artifact.Abi.GetMethod(methodRaw)
}

if err := generateFunction(generatedData, c.contractName, method); err != nil {
if err := generateFunction(generatedData, c.contractName, method, resolvedBySignature); err != nil {
log.Fatal(err)
}
}
Expand Down Expand Up @@ -769,7 +774,8 @@ func ({{.Sig}} *{{.TName}}) DecodeAbi(buf []byte) error {
}

// generateFunction generates code for smart contract function and its parameters
func generateFunction(generatedData *generatedData, contractName string, method *abi.Method) error {
func generateFunction(generatedData *generatedData, contractName string,
method *abi.Method, fnSigResolution bool) error {
methodName := fmt.Sprintf(functionNameFormat, strings.Title(method.Name+contractName))
res := []string{}

Expand All @@ -779,32 +785,43 @@ func generateFunction(generatedData *generatedData, contractName string, method
}

// write encode/decode functions
tmplStr := `
{{range .Structs}}
{{.}}
{{ end }}
func ({{.Sig}} *{{.TName}}) Sig() []byte {
return {{.ContractName}}.Abi.Methods["{{.Name}}"].ID()
}

func ({{.Sig}} *{{.TName}}) EncodeAbi() ([]byte, error) {
return {{.ContractName}}.Abi.Methods["{{.Name}}"].Encode({{.Sig}})
}
tmplString := `
{{range .Structs}}
{{.}}
{{ end }}
func ({{.Sig}} *{{.TName}}) Sig() []byte {
return {{.ContractName}}.Abi.{{.MethodGetter}}["{{.Name}}"].ID()
}
func ({{.Sig}} *{{.TName}}) EncodeAbi() ([]byte, error) {
return {{.ContractName}}.Abi.{{.MethodGetter}}["{{.Name}}"].Encode({{.Sig}})
}
func ({{.Sig}} *{{.TName}}) DecodeAbi(buf []byte) error {
return decodeMethod({{.ContractName}}.Abi.{{.MethodGetter}}["{{.Name}}"], buf, {{.Sig}})
}`

func ({{.Sig}} *{{.TName}}) DecodeAbi(buf []byte) error {
return decodeMethod({{.ContractName}}.Abi.Methods["{{.Name}}"], buf, {{.Sig}})
}`
methodGetter := "Methods"
if fnSigResolution {
methodGetter = "MethodsBySignature"
}

inputs := map[string]interface{}{
"Structs": res,
"Sig": strings.ToLower(string(methodName[0])),
"Name": method.Name,
"ContractName": contractName,
"TName": strings.Title(methodName),
"MethodGetter": methodGetter,
}

renderedString, err := renderTmpl(tmplStr, inputs)
if fnSigResolution {
inputs["Name"] = method.Sig()
}

renderedString, err := renderTmpl(tmplString, inputs)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions consensus/polybft/contractsapi/contractsapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c3da0d4

Please sign in to comment.