Skip to content

Commit 22399a7

Browse files
Merge pull request #292 from OffchainLabs/postfix-signatures
Postfix duplicate solidity function bindings with 4 byte signature
2 parents 58a1245 + 6f0a9e7 commit 22399a7

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

accounts/abi/bind/bind.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ func isKeyWord(arg string) bool {
7777
return true
7878
}
7979

80+
func duplicates(methods map[string]abi.Method) map[string]bool {
81+
var (
82+
identifiers = make(map[string]bool)
83+
dups = make(map[string]bool)
84+
)
85+
for _, method := range methods {
86+
identifiers, dups := identifiers, dups
87+
if identifiers[method.RawName] {
88+
dups[method.RawName] = true
89+
}
90+
identifiers[method.RawName] = true
91+
}
92+
return dups
93+
}
94+
8095
// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
8196
// to be used as is in client code, but rather as an intermediate struct which
8297
// enforces compile time type safety and naming convention opposed to having to
@@ -121,6 +136,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
121136
callIdentifiers = make(map[string]bool)
122137
transactIdentifiers = make(map[string]bool)
123138
eventIdentifiers = make(map[string]bool)
139+
dups = duplicates(evmABI.Methods)
124140
)
125141

126142
for _, input := range evmABI.Constructor.Inputs {
@@ -132,12 +148,16 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
132148
for _, original := range evmABI.Methods {
133149
// Normalize the method for capital cases and non-anonymous inputs/outputs
134150
normalized := original
135-
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
136151
// Ensure there is no duplicated identifier
137152
var identifiers = callIdentifiers
138153
if !original.IsConstant() {
139154
identifiers = transactIdentifiers
140155
}
156+
name := original.RawName
157+
if dups[original.RawName] {
158+
name = fmt.Sprintf("%s%x", original.RawName, original.ID)
159+
}
160+
normalizedName := methodNormalizer[lang](alias(aliases, name))
141161
// Name shouldn't start with a digit. It will make the generated code invalid.
142162
if len(normalizedName) > 0 && unicode.IsDigit(rune(normalizedName[0])) {
143163
normalizedName = fmt.Sprintf("M%s", normalizedName)

accounts/abi/bind/bind_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ var bindTests = []struct {
14861486
}
14871487
}
14881488
}()
1489-
contract.Foo(auth, big.NewInt(1), big.NewInt(2))
1489+
contract.Foo04bc52f8(auth, big.NewInt(1), big.NewInt(2))
14901490
sim.Commit()
14911491
select {
14921492
case n := <-resCh:
@@ -1497,7 +1497,7 @@ var bindTests = []struct {
14971497
t.Fatalf("Wait bar0 event timeout")
14981498
}
14991499
1500-
contract.Foo0(auth, big.NewInt(1))
1500+
contract.Foo2fbebd38(auth, big.NewInt(1))
15011501
sim.Commit()
15021502
select {
15031503
case n := <-resCh:

0 commit comments

Comments
 (0)