Skip to content

Commit f8a95d9

Browse files
rjl493456442gballet
authored andcommitted
accounts/abi/bind, cmd/abigen: implement alias for abigen (#20244)
* accounts/abi/bind, cmd/abigen: implement alias for abigen * accounts/abi/bind: minor fixes * accounts/abi/bind: address comments * cmd/abigen: address comments * accounts/abi/bind: print error log when identifier collision * accounts/abi/bind: address comments * accounts/abi/bind: address comment
1 parent bf5c6b2 commit f8a95d9

3 files changed

Lines changed: 130 additions & 12 deletions

File tree

‎accounts/abi/bind/bind.go‎

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

@@ -74,12 +74,29 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
7474
transacts = make(map[string]*tmplMethod)
7575
events = make(map[string]*tmplEvent)
7676
structs = make(map[string]*tmplStruct)
77+
78+
// identifiers are used to detect duplicated identifier of function
79+
// and event. For all calls, transacts and events, abigen will generate
80+
// corresponding bindings. However we have to ensure there is no
81+
// identifier coliision in the bindings of these categories.
82+
callIdentifiers = make(map[string]bool)
83+
transactIdentifiers = make(map[string]bool)
84+
eventIdentifiers = make(map[string]bool)
7785
)
7886
for _, original := range evmABI.Methods {
7987
// Normalize the method for capital cases and non-anonymous inputs/outputs
8088
normalized := original
81-
normalized.Name = methodNormalizer[lang](original.Name)
82-
89+
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
90+
// Ensure there is no duplicated identifier
91+
var identifiers = callIdentifiers
92+
if !original.Const {
93+
identifiers = transactIdentifiers
94+
}
95+
if identifiers[normalizedName] {
96+
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName)
97+
}
98+
identifiers[normalizedName] = true
99+
normalized.Name = normalizedName
83100
normalized.Inputs = make([]abi.Argument, len(original.Inputs))
84101
copy(normalized.Inputs, original.Inputs)
85102
for j, input := range normalized.Inputs {
@@ -114,7 +131,14 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
114131
}
115132
// Normalize the event for capital cases and non-anonymous outputs
116133
normalized := original
117-
normalized.Name = methodNormalizer[lang](original.Name)
134+
135+
// Ensure there is no duplicated identifier
136+
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
137+
if eventIdentifiers[normalizedName] {
138+
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName)
139+
}
140+
eventIdentifiers[normalizedName] = true
141+
normalized.Name = normalizedName
118142

119143
normalized.Inputs = make([]abi.Argument, len(original.Inputs))
120144
copy(normalized.Inputs, original.Inputs)
@@ -483,6 +507,15 @@ func namedTypeJava(javaKind string, solKind abi.Type) string {
483507
}
484508
}
485509

510+
// alias returns an alias of the given string based on the aliasing rules
511+
// or returns itself if no rule is matched.
512+
func alias(aliases map[string]string, n string) string {
513+
if alias, exist := aliases[n]; exist {
514+
return alias
515+
}
516+
return n
517+
}
518+
486519
// methodNormalizer is a name transformer that modifies Solidity method names to
487520
// conform to target language naming concentions.
488521
var methodNormalizer = map[Lang]func(string) string{

‎accounts/abi/bind/bind_test.go‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var bindTests = []struct {
3838
tester string
3939
fsigs []map[string]string
4040
libs map[string]string
41+
aliases map[string]string
4142
types []string
4243
}{
4344
// Test that the binding is available in combined and separate forms too
@@ -61,6 +62,7 @@ var bindTests = []struct {
6162
nil,
6263
nil,
6364
nil,
65+
nil,
6466
},
6567
// Test that all the official sample contracts bind correctly
6668
{
@@ -77,6 +79,7 @@ var bindTests = []struct {
7779
nil,
7880
nil,
7981
nil,
82+
nil,
8083
},
8184
{
8285
`Crowdsale`,
@@ -92,6 +95,7 @@ var bindTests = []struct {
9295
nil,
9396
nil,
9497
nil,
98+
nil,
9599
},
96100
{
97101
`DAO`,
@@ -107,6 +111,7 @@ var bindTests = []struct {
107111
nil,
108112
nil,
109113
nil,
114+
nil,
110115
},
111116
// Test that named and anonymous inputs are handled correctly
112117
{
@@ -143,6 +148,7 @@ var bindTests = []struct {
143148
nil,
144149
nil,
145150
nil,
151+
nil,
146152
},
147153
// Test that named and anonymous outputs are handled correctly
148154
{
@@ -182,6 +188,7 @@ var bindTests = []struct {
182188
nil,
183189
nil,
184190
nil,
191+
nil,
185192
},
186193
// Tests that named, anonymous and indexed events are handled correctly
187194
{
@@ -250,6 +257,7 @@ var bindTests = []struct {
250257
nil,
251258
nil,
252259
nil,
260+
nil,
253261
},
254262
// Test that contract interactions (deploy, transact and call) generate working code
255263
{
@@ -311,6 +319,7 @@ var bindTests = []struct {
311319
nil,
312320
nil,
313321
nil,
322+
nil,
314323
},
315324
// Tests that plain values can be properly returned and deserialized
316325
{
@@ -356,6 +365,7 @@ var bindTests = []struct {
356365
nil,
357366
nil,
358367
nil,
368+
nil,
359369
},
360370
// Tests that tuples can be properly returned and deserialized
361371
{
@@ -401,6 +411,7 @@ var bindTests = []struct {
401411
nil,
402412
nil,
403413
nil,
414+
nil,
404415
},
405416
// Tests that arrays/slices can be properly returned and deserialized.
406417
// Only addresses are tested, remainder just compiled to keep the test small.
@@ -458,6 +469,7 @@ var bindTests = []struct {
458469
nil,
459470
nil,
460471
nil,
472+
nil,
461473
},
462474
// Tests that anonymous default methods can be correctly invoked
463475
{
@@ -508,6 +520,7 @@ var bindTests = []struct {
508520
nil,
509521
nil,
510522
nil,
523+
nil,
511524
},
512525
// Tests that non-existent contracts are reported as such (though only simulator test)
513526
{
@@ -547,6 +560,7 @@ var bindTests = []struct {
547560
nil,
548561
nil,
549562
nil,
563+
nil,
550564
},
551565
// Tests that gas estimation works for contracts with weird gas mechanics too.
552566
{
@@ -602,6 +616,7 @@ var bindTests = []struct {
602616
nil,
603617
nil,
604618
nil,
619+
nil,
605620
},
606621
// Test that constant functions can be called from an (optional) specified address
607622
{
@@ -655,6 +670,7 @@ var bindTests = []struct {
655670
nil,
656671
nil,
657672
nil,
673+
nil,
658674
},
659675
// Tests that methods and returns with underscores inside work correctly.
660676
{
@@ -734,6 +750,7 @@ var bindTests = []struct {
734750
nil,
735751
nil,
736752
nil,
753+
nil,
737754
},
738755
// Tests that logs can be successfully filtered and decoded.
739756
{
@@ -955,6 +972,7 @@ var bindTests = []struct {
955972
nil,
956973
nil,
957974
nil,
975+
nil,
958976
},
959977
{
960978
`DeeplyNestedArray`,
@@ -1035,6 +1053,7 @@ var bindTests = []struct {
10351053
nil,
10361054
nil,
10371055
nil,
1056+
nil,
10381057
},
10391058
{
10401059
`CallbackParam`,
@@ -1076,6 +1095,7 @@ var bindTests = []struct {
10761095
},
10771096
nil,
10781097
nil,
1098+
nil,
10791099
}, {
10801100
`Tuple`,
10811101
`
@@ -1219,6 +1239,7 @@ var bindTests = []struct {
12191239
nil,
12201240
nil,
12211241
nil,
1242+
nil,
12221243
},
12231244
{
12241245
`UseLibrary`,
@@ -1287,6 +1308,7 @@ var bindTests = []struct {
12871308
map[string]string{
12881309
"b98c933f0a6ececcd167bd4f9d3299b1a0": "Math",
12891310
},
1311+
nil,
12901312
[]string{"UseLibrary", "Math"},
12911313
}, {
12921314
"Overload",
@@ -1381,6 +1403,50 @@ var bindTests = []struct {
13811403
nil,
13821404
nil,
13831405
nil,
1406+
nil,
1407+
},
1408+
{
1409+
"IdentifierCollision",
1410+
`
1411+
pragma solidity >=0.4.19 <0.6.0;
1412+
1413+
contract IdentifierCollision {
1414+
uint public _myVar;
1415+
1416+
function MyVar() public view returns (uint) {
1417+
return _myVar;
1418+
}
1419+
}
1420+
`,
1421+
[]string{"60806040523480156100115760006000fd5b50610017565b60c3806100256000396000f3fe608060405234801560105760006000fd5b506004361060365760003560e01c806301ad4d8714603c5780634ef1f0ad146058576036565b60006000fd5b60426074565b6040518082815260200191505060405180910390f35b605e607d565b6040518082815260200191505060405180910390f35b60006000505481565b60006000600050549050608b565b9056fea265627a7a7231582067c8d84688b01c4754ba40a2a871cede94ea1f28b5981593ab2a45b46ac43af664736f6c634300050c0032"},
1422+
[]string{`[{"constant":true,"inputs":[],"name":"MyVar","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_myVar","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]`},
1423+
`
1424+
"math/big"
1425+
1426+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
1427+
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
1428+
"github.com/ethereum/go-ethereum/crypto"
1429+
"github.com/ethereum/go-ethereum/core"
1430+
`,
1431+
`
1432+
// Initialize test accounts
1433+
key, _ := crypto.GenerateKey()
1434+
addr := crypto.PubkeyToAddress(key.PublicKey)
1435+
1436+
// Deploy registrar contract
1437+
sim := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000)
1438+
defer sim.Close()
1439+
1440+
transactOpts := bind.NewKeyedTransactor(key)
1441+
_, _, _, err := DeployIdentifierCollision(transactOpts, sim)
1442+
if err != nil {
1443+
t.Fatalf("failed to deploy contract: %v", err)
1444+
}
1445+
`,
1446+
nil,
1447+
nil,
1448+
map[string]string{"_myVar": "pubVar"}, // alias MyVar to PubVar
1449+
nil,
13841450
},
13851451
}
13861452

@@ -1412,7 +1478,7 @@ func TestGolangBindings(t *testing.T) {
14121478
types = []string{tt.name}
14131479
}
14141480
// Generate the binding and create a Go source file in the workspace
1415-
bind, err := Bind(types, tt.abi, tt.bytecode, tt.fsigs, "bindtest", LangGo, tt.libs)
1481+
bind, err := Bind(types, tt.abi, tt.bytecode, tt.fsigs, "bindtest", LangGo, tt.libs, tt.aliases)
14161482
if err != nil {
14171483
t.Fatalf("test %d: failed to generate binding: %v", i, err)
14181484
}
@@ -1835,7 +1901,7 @@ public class Test {
18351901
},
18361902
}
18371903
for i, c := range cases {
1838-
binding, err := Bind([]string{c.name}, []string{c.abi}, []string{c.bytecode}, nil, "bindtest", LangJava, nil)
1904+
binding, err := Bind([]string{c.name}, []string{c.abi}, []string{c.bytecode}, nil, "bindtest", LangJava, nil, nil)
18391905
if err != nil {
18401906
t.Fatalf("test %d: failed to generate binding: %v", i, err)
18411907
}

‎cmd/abigen/main.go‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io/ioutil"
2323
"os"
24+
"regexp"
2425
"strings"
2526

2627
"github.com/ethereum/go-ethereum/accounts/abi/bind"
@@ -103,6 +104,10 @@ var (
103104
Usage: "Destination language for the bindings (go, java, objc)",
104105
Value: "go",
105106
}
107+
aliasFlag = cli.StringFlag{
108+
Name: "alias",
109+
Usage: "Comma separated aliases for function and event renaming, e.g. foo=bar",
110+
}
106111
)
107112

108113
func init() {
@@ -120,6 +125,7 @@ func init() {
120125
pkgFlag,
121126
outFlag,
122127
langFlag,
128+
aliasFlag,
123129
}
124130
app.Action = utils.MigrateFlags(abigen)
125131
cli.CommandHelpTemplate = commandHelperTemplate
@@ -144,11 +150,12 @@ func abigen(c *cli.Context) error {
144150
}
145151
// If the entire solidity code was specified, build and bind based on that
146152
var (
147-
abis []string
148-
bins []string
149-
types []string
150-
sigs []map[string]string
151-
libs = make(map[string]string)
153+
abis []string
154+
bins []string
155+
types []string
156+
sigs []map[string]string
157+
libs = make(map[string]string)
158+
aliases = make(map[string]string)
152159
)
153160
if c.GlobalString(abiFlag.Name) != "" {
154161
// Load up the ABI, optional bytecode and type name from the parameters
@@ -232,8 +239,20 @@ func abigen(c *cli.Context) error {
232239
libs[libPattern] = nameParts[len(nameParts)-1]
233240
}
234241
}
242+
// Extract all aliases from the flags
243+
if c.GlobalIsSet(aliasFlag.Name) {
244+
// We support multi-versions for aliasing
245+
// e.g.
246+
// foo=bar,foo2=bar2
247+
// foo:bar,foo2:bar2
248+
re := regexp.MustCompile(`(?:(\w+)[:=](\w+))`)
249+
submatches := re.FindAllStringSubmatch(c.GlobalString(aliasFlag.Name), -1)
250+
for _, match := range submatches {
251+
aliases[match[1]] = match[2]
252+
}
253+
}
235254
// Generate the contract binding
236-
code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs)
255+
code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs, aliases)
237256
if err != nil {
238257
utils.Fatalf("Failed to generate ABI binding: %v", err)
239258
}

0 commit comments

Comments
 (0)