Skip to content

Commit 195f473

Browse files
Darioush Jalaliqdm12
andauthored
refactor: align accounts/abi/bind with coreth+upstream (#1427)
Co-authored-by: Quentin McGaw <quentin.mcgaw@avalabs.org>
1 parent 68711e0 commit 195f473

File tree

6 files changed

+104
-84
lines changed

6 files changed

+104
-84
lines changed

accounts/abi/bind/bind.go

Lines changed: 21 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,14 @@ import (
4343
"github.com/ethereum/go-ethereum/log"
4444
)
4545

46-
// BindHook is a callback function that can be used to customize the binding.
47-
type BindHook func(lang Lang, pkg string, types []string, contracts map[string]*TmplContract, structs map[string]*TmplStruct) (data interface{}, templateSource string, err error)
48-
4946
// Lang is a target programming language selector to generate bindings for.
5047
type Lang int
5148

5249
const (
5350
LangGo Lang = iota
5451
)
5552

56-
func IsKeyWord(arg string) bool {
53+
func isKeyWord(arg string) bool {
5754
switch arg {
5855
case "break":
5956
case "case":
@@ -101,10 +98,10 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
10198
func BindHelper(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string, bindHook BindHook) (string, error) {
10299
var (
103100
// contracts is the map of each individual contract requested binding
104-
contracts = make(map[string]*TmplContract)
101+
contracts = make(map[string]*tmplContract)
105102

106103
// structs is the map of all redeclared structs shared by passed contracts.
107-
structs = make(map[string]*TmplStruct)
104+
structs = make(map[string]*tmplStruct)
108105

109106
// isLib is the map used to flag each encountered library as such
110107
isLib = make(map[string]struct{})
@@ -125,11 +122,11 @@ func BindHelper(types []string, abis []string, bytecodes []string, fsigs []map[s
125122

126123
// Extract the call and transact methods; events, struct definitions; and sort them alphabetically
127124
var (
128-
calls = make(map[string]*TmplMethod)
129-
transacts = make(map[string]*TmplMethod)
125+
calls = make(map[string]*tmplMethod)
126+
transacts = make(map[string]*tmplMethod)
130127
events = make(map[string]*tmplEvent)
131-
fallback *TmplMethod
132-
receive *TmplMethod
128+
fallback *tmplMethod
129+
receive *tmplMethod
133130

134131
// identifiers are used to detect duplicated identifiers of functions
135132
// and events. For all calls, transacts and events, abigen will generate
@@ -172,7 +169,7 @@ func BindHelper(types []string, abis []string, bytecodes []string, fsigs []map[s
172169
normalized.Inputs = make([]abi.Argument, len(original.Inputs))
173170
copy(normalized.Inputs, original.Inputs)
174171
for j, input := range normalized.Inputs {
175-
if input.Name == "" || IsKeyWord(input.Name) {
172+
if input.Name == "" || isKeyWord(input.Name) {
176173
normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j)
177174
}
178175
if hasStruct(input.Type) {
@@ -191,9 +188,9 @@ func BindHelper(types []string, abis []string, bytecodes []string, fsigs []map[s
191188
}
192189
// Append the methods to the call or transact lists
193190
if original.IsConstant() {
194-
calls[original.Name] = &TmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)}
191+
calls[original.Name] = &tmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)}
195192
} else {
196-
transacts[original.Name] = &TmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)}
193+
transacts[original.Name] = &tmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)}
197194
}
198195
}
199196
for _, original := range evmABI.Events {
@@ -224,7 +221,7 @@ func BindHelper(types []string, abis []string, bytecodes []string, fsigs []map[s
224221
normalized.Inputs = make([]abi.Argument, len(original.Inputs))
225222
copy(normalized.Inputs, original.Inputs)
226223
for j, input := range normalized.Inputs {
227-
if input.Name == "" || IsKeyWord(input.Name) {
224+
if input.Name == "" || isKeyWord(input.Name) {
228225
normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j)
229226
}
230227
// Event is a bit special, we need to define event struct in binding,
@@ -245,12 +242,12 @@ func BindHelper(types []string, abis []string, bytecodes []string, fsigs []map[s
245242
}
246243
// Add two special fallback functions if they exist
247244
if evmABI.HasFallback() {
248-
fallback = &TmplMethod{Original: evmABI.Fallback}
245+
fallback = &tmplMethod{Original: evmABI.Fallback}
249246
}
250247
if evmABI.HasReceive() {
251-
receive = &TmplMethod{Original: evmABI.Receive}
248+
receive = &tmplMethod{Original: evmABI.Receive}
252249
}
253-
contracts[types[i]] = &TmplContract{
250+
contracts[types[i]] = &tmplContract{
254251
Type: capitalise(types[i]),
255252
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""),
256253
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
@@ -341,14 +338,10 @@ func BindHelper(types []string, abis []string, bytecodes []string, fsigs []map[s
341338

342339
// bindType is a set of type binders that convert Solidity types to some supported
343340
// programming language types.
344-
var bindType = map[Lang]func(kind abi.Type, structs map[string]*TmplStruct) string{
341+
var bindType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
345342
LangGo: bindTypeGo,
346343
}
347344

348-
var bindTypeNew = map[Lang]func(kind abi.Type, structs map[string]*TmplStruct) string{
349-
LangGo: bindTypeNewGo,
350-
}
351-
352345
// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go ones.
353346
func bindBasicTypeGo(kind abi.Type) string {
354347
switch kind.T {
@@ -373,43 +366,10 @@ func bindBasicTypeGo(kind abi.Type) string {
373366
}
374367
}
375368

376-
// bindTypeNewGo converts new types to Go ones.
377-
func bindTypeNewGo(kind abi.Type, structs map[string]*TmplStruct) string {
378-
switch kind.T {
379-
case abi.TupleTy:
380-
return structs[kind.TupleRawName+kind.String()].Name + "{}"
381-
case abi.ArrayTy:
382-
return fmt.Sprintf("[%d]", kind.Size) + bindTypeGo(*kind.Elem, structs) + "{}"
383-
case abi.SliceTy:
384-
return "nil"
385-
case abi.AddressTy:
386-
return "common.Address{}"
387-
case abi.IntTy, abi.UintTy:
388-
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
389-
switch parts[2] {
390-
case "8", "16", "32", "64":
391-
return "0"
392-
}
393-
return "new(big.Int)"
394-
case abi.FixedBytesTy:
395-
return fmt.Sprintf("[%d]byte", kind.Size) + "{}"
396-
case abi.BytesTy:
397-
return "[]byte{}"
398-
case abi.FunctionTy:
399-
return "[24]byte{}"
400-
case abi.BoolTy:
401-
return "false"
402-
case abi.StringTy:
403-
return `""`
404-
default:
405-
return "nil"
406-
}
407-
}
408-
409369
// bindTypeGo converts solidity types to Go ones. Since there is no clear mapping
410370
// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
411371
// mapped will use an upscaled type (e.g. BigDecimal).
412-
func bindTypeGo(kind abi.Type, structs map[string]*TmplStruct) string {
372+
func bindTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
413373
switch kind.T {
414374
case abi.TupleTy:
415375
return structs[kind.TupleRawName+kind.String()].Name
@@ -424,13 +384,13 @@ func bindTypeGo(kind abi.Type, structs map[string]*TmplStruct) string {
424384

425385
// bindTopicType is a set of type binders that convert Solidity types to some
426386
// supported programming language topic types.
427-
var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*TmplStruct) string{
387+
var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
428388
LangGo: bindTopicTypeGo,
429389
}
430390

431391
// bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same
432392
// functionality as for simple types, but dynamic types get converted to hashes.
433-
func bindTopicTypeGo(kind abi.Type, structs map[string]*TmplStruct) string {
393+
func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
434394
bound := bindTypeGo(kind, structs)
435395

436396
// todo(rjl493456442) according solidity documentation, indexed event
@@ -447,14 +407,14 @@ func bindTopicTypeGo(kind abi.Type, structs map[string]*TmplStruct) string {
447407

448408
// bindStructType is a set of type binders that convert Solidity tuple types to some supported
449409
// programming language struct definition.
450-
var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*TmplStruct) string{
410+
var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
451411
LangGo: bindStructTypeGo,
452412
}
453413

454414
// bindStructTypeGo converts a Solidity tuple type to a Go one and records the mapping
455415
// in the given map.
456416
// Notably, this function will resolve and record nested struct recursively.
457-
func bindStructTypeGo(kind abi.Type, structs map[string]*TmplStruct) string {
417+
func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
458418
switch kind.T {
459419
case abi.TupleTy:
460420
// We compose a raw struct name and a canonical parameter expression
@@ -483,7 +443,7 @@ func bindStructTypeGo(kind abi.Type, structs map[string]*TmplStruct) string {
483443
}
484444
name = capitalise(name)
485445

486-
structs[id] = &TmplStruct{
446+
structs[id] = &tmplStruct{
487447
Name: name,
488448
Fields: fields,
489449
}
@@ -568,11 +528,3 @@ func hasStruct(t abi.Type) bool {
568528
return false
569529
}
570530
}
571-
572-
func mkList(args ...interface{}) []interface{} {
573-
return args
574-
}
575-
576-
func add(a, b int) int {
577-
return a + b
578-
}

accounts/abi/bind/bind_extra.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// (c) 2025, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package bind
5+
6+
import (
7+
"fmt"
8+
"regexp"
9+
10+
"github.com/ava-labs/subnet-evm/accounts/abi"
11+
)
12+
13+
type (
14+
// These types are exported for use in bind/precompilebind
15+
TmplContract = tmplContract
16+
TmplMethod = tmplMethod
17+
TmplStruct = tmplStruct
18+
)
19+
20+
// BindHook is a callback function that can be used to customize the binding.
21+
type BindHook func(lang Lang, pkg string, types []string, contracts map[string]*tmplContract, structs map[string]*tmplStruct) (data any, templateSource string, err error)
22+
23+
func IsKeyWord(arg string) bool {
24+
return isKeyWord(arg)
25+
}
26+
27+
var bindTypeNew = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
28+
LangGo: bindTypeNewGo,
29+
}
30+
31+
// bindTypeNewGo converts new types to Go ones.
32+
func bindTypeNewGo(kind abi.Type, structs map[string]*tmplStruct) string {
33+
switch kind.T {
34+
case abi.TupleTy:
35+
return structs[kind.TupleRawName+kind.String()].Name + "{}"
36+
case abi.ArrayTy:
37+
return fmt.Sprintf("[%d]", kind.Size) + bindTypeGo(*kind.Elem, structs) + "{}"
38+
case abi.SliceTy:
39+
return "nil"
40+
case abi.AddressTy:
41+
return "common.Address{}"
42+
case abi.IntTy, abi.UintTy:
43+
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String())
44+
switch parts[2] {
45+
case "8", "16", "32", "64":
46+
return "0"
47+
}
48+
return "new(big.Int)"
49+
case abi.FixedBytesTy:
50+
return fmt.Sprintf("[%d]byte", kind.Size) + "{}"
51+
case abi.BytesTy:
52+
return "[]byte{}"
53+
case abi.FunctionTy:
54+
return "[24]byte{}"
55+
case abi.BoolTy:
56+
return "false"
57+
case abi.StringTy:
58+
return `""`
59+
default:
60+
return "nil"
61+
}
62+
}
63+
64+
func mkList(args ...any) []any {
65+
return args
66+
}
67+
68+
func add(a, b int) int {
69+
return a + b
70+
}

accounts/abi/bind/template.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,30 @@ import "github.com/ava-labs/subnet-evm/accounts/abi"
3131
// tmplData is the data structure required to fill the binding template.
3232
type tmplData struct {
3333
Package string // Name of the package to place the generated file in
34-
Contracts map[string]*TmplContract // List of contracts to generate into this file
34+
Contracts map[string]*tmplContract // List of contracts to generate into this file
3535
Libraries map[string]string // Map the bytecode's link pattern to the library name
36-
Structs map[string]*TmplStruct // Contract struct type definitions
36+
Structs map[string]*tmplStruct // Contract struct type definitions
3737
}
3838

39-
// TmplContract contains the data needed to generate an individual contract binding.
40-
type TmplContract struct {
39+
// tmplContract contains the data needed to generate an individual contract binding.
40+
type tmplContract struct {
4141
Type string // Type name of the main contract binding
4242
InputABI string // JSON ABI used as the input to generate the binding from
4343
InputBin string // Optional EVM bytecode used to generate deploy code from
4444
FuncSigs map[string]string // Optional map: string signature -> 4-byte signature
4545
Constructor abi.Method // Contract constructor for deploy parametrization
46-
Calls map[string]*TmplMethod // Contract calls that only read state data
47-
Transacts map[string]*TmplMethod // Contract calls that write state data
48-
Fallback *TmplMethod // Additional special fallback function
49-
Receive *TmplMethod // Additional special receive function
46+
Calls map[string]*tmplMethod // Contract calls that only read state data
47+
Transacts map[string]*tmplMethod // Contract calls that write state data
48+
Fallback *tmplMethod // Additional special fallback function
49+
Receive *tmplMethod // Additional special receive function
5050
Events map[string]*tmplEvent // Contract events accessors
5151
Libraries map[string]string // Same as tmplData, but filtered to only keep what the contract needs
5252
Library bool // Indicator whether the contract is a library
5353
}
5454

55-
// TmplMethod is a wrapper around an abi.Method that contains a few preprocessed
55+
// tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
5656
// and cached data fields.
57-
type TmplMethod struct {
57+
type tmplMethod struct {
5858
Original abi.Method // Original method as parsed by the abi package
5959
Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
6060
Structured bool // Whether the returns should be accumulated into a struct
@@ -75,9 +75,9 @@ type tmplField struct {
7575
SolKind abi.Type // Raw abi type information
7676
}
7777

78-
// TmplStruct is a wrapper around an abi.tuple and contains an auto-generated
78+
// tmplStruct is a wrapper around an abi.tuple and contains an auto-generated
7979
// struct name.
80-
type TmplStruct struct {
80+
type tmplStruct struct {
8181
Name string // Auto-generated struct name(before solidity v0.5.11) or raw name.
8282
Fields []*tmplField // Struct fields definition depends on the binding language.
8383
}

cmd/abigen/namefilter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// original code from which it is derived.
88
//
99
// Much love to the original authors for their work.
10-
// **********
1110
package main
1211

1312
import (

cmd/abigen/namefilter_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// original code from which it is derived.
88
//
99
// Much love to the original authors for their work.
10-
// **********
1110
package main
1211

1312
import (

triedb/pathdb/database_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func (t *tester) generate(parent common.Hash) (common.Hash, *trienode.MergedNode
319319
return root, ctx.nodes, triestate.New(ctx.accountOrigin, ctx.storageOrigin, nil)
320320
}
321321

322-
// lastRoot returns the latest root hash, or empty if nothing is cached.
322+
// lastHash returns the latest root hash, or empty if nothing is cached.
323323
func (t *tester) lastHash() common.Hash {
324324
if len(t.roots) == 0 {
325325
return common.Hash{}

0 commit comments

Comments
 (0)