Skip to content

Commit 6bf5555

Browse files
authored
accounts/abi/bind: Accept function ptr parameter (#19755)
* accounts/abi/bind: Accept function ptr parameter They are translated as [24]byte * Add Java template version * accounts/abi/bind: fix merge issue * Fix CI
1 parent 0b26a82 commit 6bf5555

6 files changed

Lines changed: 100 additions & 14 deletions

File tree

‎accounts/abi/bind/bind.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const (
4444
// to be used as is in client code, but rather as an intermediate struct which
4545
// enforces compile time type safety and naming convention opposed to having to
4646
// manually maintain hard coded strings that break on runtime.
47-
func Bind(types []string, abis []string, bytecodes []string, pkg string, lang Lang) (string, error) {
47+
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang) (string, error) {
4848
// Process each individual contract requested binding
4949
contracts := make(map[string]*tmplContract)
5050

@@ -125,6 +125,9 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string, lang La
125125
Transacts: transacts,
126126
Events: events,
127127
}
128+
if len(fsigs) > i {
129+
contracts[types[i]].FuncSigs = fsigs[i]
130+
}
128131
}
129132
// Generate the contract template data content and render it
130133
data := &tmplData{
@@ -180,8 +183,7 @@ func bindBasicTypeGo(kind abi.Type) string {
180183
case abi.BytesTy:
181184
return "[]byte"
182185
case abi.FunctionTy:
183-
// todo(rjl493456442)
184-
return ""
186+
return "[24]byte"
185187
default:
186188
// string, bool types
187189
return kind.String()
@@ -240,8 +242,7 @@ func bindBasicTypeJava(kind abi.Type) string {
240242
case abi.StringTy:
241243
return "String"
242244
case abi.FunctionTy:
243-
// todo(rjl493456442)
244-
return ""
245+
return "byte[24]"
245246
default:
246247
return kind.String()
247248
}

‎accounts/abi/bind/bind_test.go‎

Lines changed: 62 additions & 3 deletions
Large diffs are not rendered by default.

‎accounts/abi/bind/template.go‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type tmplContract struct {
2929
Type string // Type name of the main contract binding
3030
InputABI string // JSON ABI used as the input to generate the binding from
3131
InputBin string // Optional EVM bytecode used to denetare deploy code from
32+
FuncSigs map[string]string // Optional map: string signature -> 4-byte signature
3233
Constructor abi.Method // Contract constructor for deploy parametrization
3334
Calls map[string]*tmplMethod // Contract calls that only read state data
3435
Transacts map[string]*tmplMethod // Contract calls that write state data
@@ -92,6 +93,15 @@ var (
9293
// {{.Type}}ABI is the input ABI used to generate the binding from.
9394
const {{.Type}}ABI = "{{.InputABI}}"
9495
96+
{{if $contract.FuncSigs}}
97+
// {{.Type}}FuncSigs maps the 4-byte function signature to its string representation.
98+
var {{.Type}}FuncSigs = map[string]string{
99+
{{range $strsig, $binsig := .FuncSigs}}
100+
"{{$binsig}}": "{{$strsig}}",
101+
{{end}}
102+
}
103+
{{end}}
104+
95105
{{if .InputBin}}
96106
// {{.Type}}Bin is the compiled bytecode used for deploying new contracts.
97107
const {{.Type}}Bin = ` + "`" + `{{.InputBin}}` + "`" + `
@@ -464,12 +474,23 @@ const tmplSourceJava = `
464474
package {{.Package}};
465475
466476
import org.ethereum.geth.*;
477+
import java.util.*;
467478
468479
{{range $contract := .Contracts}}
469480
public class {{.Type}} {
470481
// ABI is the input ABI used to generate the binding from.
471482
public final static String ABI = "{{.InputABI}}";
472-
483+
{{if $contract.FuncSigs}}
484+
// {{.Type}}FuncSigs maps the 4-byte function signature to its string representation.
485+
public final static Map<String, String> {{.Type}}FuncSigs;
486+
static {
487+
Hashtable<String, String> temp = new Hashtable<String, String>();
488+
{{range $strsig, $binsig := .FuncSigs}}
489+
temp.put("{{$binsig}}", "{{$strsig}}");
490+
{{end}}
491+
{{.Type}}FuncSigs = Collections.unmodifiableMap(temp);
492+
}
493+
{{end}}
473494
{{if .InputBin}}
474495
// BYTECODE is the compiled bytecode used for deploying new contracts.
475496
public final static String BYTECODE = "0x{{.InputBin}}";

‎cmd/abigen/main.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func main() {
7878
abis []string
7979
bins []string
8080
types []string
81+
sigs []map[string]string
8182
)
8283
if *solFlag != "" || *vyFlag != "" || *abiFlag == "-" {
8384
// Generate the list of types to exclude from binding
@@ -121,6 +122,7 @@ func main() {
121122
}
122123
abis = append(abis, string(abi))
123124
bins = append(bins, contract.Code)
125+
sigs = append(sigs, contract.Hashes)
124126

125127
nameParts := strings.Split(name, ":")
126128
types = append(types, nameParts[len(nameParts)-1])
@@ -151,7 +153,7 @@ func main() {
151153
types = append(types, kind)
152154
}
153155
// Generate the contract binding
154-
code, err := bind.Bind(types, abis, bins, *pkgFlag, lang)
156+
code, err := bind.Bind(types, abis, bins, sigs, *pkgFlag, lang)
155157
if err != nil {
156158
fmt.Printf("Failed to generate ABI binding: %v\n", err)
157159
os.Exit(-1)

‎common/compiler/helpers.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ var versionRegexp = regexp.MustCompile(`([0-9]+)\.([0-9]+)\.([0-9]+)`)
2727

2828
// Contract contains information about a compiled contract, alongside its code and runtime code.
2929
type Contract struct {
30-
Code string `json:"code"`
31-
RuntimeCode string `json:"runtime-code"`
32-
Info ContractInfo `json:"info"`
30+
Code string `json:"code"`
31+
RuntimeCode string `json:"runtime-code"`
32+
Info ContractInfo `json:"info"`
33+
Hashes map[string]string `json:"hashes"`
3334
}
3435

3536
// ContractInfo contains information about a compiled contract, including access

‎common/compiler/solidity.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type solcOutput struct {
3939
BinRuntime string `json:"bin-runtime"`
4040
SrcMapRuntime string `json:"srcmap-runtime"`
4141
Bin, SrcMap, Abi, Devdoc, Userdoc, Metadata string
42+
Hashes map[string]string
4243
}
4344
Version string
4445
}
@@ -49,7 +50,7 @@ func (s *Solidity) makeArgs() []string {
4950
"--optimize", // code optimizer switched on
5051
}
5152
if s.Major > 0 || s.Minor > 4 || s.Patch > 6 {
52-
p[1] += ",metadata"
53+
p[1] += ",metadata,hashes"
5354
}
5455
return p
5556
}
@@ -161,6 +162,7 @@ func ParseCombinedJSON(combinedJSON []byte, source string, languageVersion strin
161162
contracts[name] = &Contract{
162163
Code: "0x" + info.Bin,
163164
RuntimeCode: "0x" + info.BinRuntime,
165+
Hashes: info.Hashes,
164166
Info: ContractInfo{
165167
Source: source,
166168
Language: "Solidity",

0 commit comments

Comments
 (0)