Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postfix duplicate solidity function bindings with 4 byte signature #292

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion accounts/abi/bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ func isKeyWord(arg string) bool {
return true
}

func duplicates(methods map[string]abi.Method) map[string]bool {
var (
identifiers = make(map[string]bool)
dups = make(map[string]bool)
)
for _, method := range methods {
identifiers, dups := identifiers, dups
if identifiers[method.RawName] {
dups[method.RawName] = true
}
identifiers[method.RawName] = true
}
return dups
}

// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
// to be used as is in client code, but rather as an intermediate struct which
// enforces compile time type safety and naming convention opposed to having to
Expand Down Expand Up @@ -121,6 +136,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
callIdentifiers = make(map[string]bool)
transactIdentifiers = make(map[string]bool)
eventIdentifiers = make(map[string]bool)
dups = duplicates(evmABI.Methods)
)

for _, input := range evmABI.Constructor.Inputs {
Expand All @@ -132,12 +148,16 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
for _, original := range evmABI.Methods {
// Normalize the method for capital cases and non-anonymous inputs/outputs
normalized := original
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
// Ensure there is no duplicated identifier
var identifiers = callIdentifiers
if !original.IsConstant() {
identifiers = transactIdentifiers
}
name := original.RawName
if dups[original.RawName] {
name = fmt.Sprintf("%s%x", original.RawName, original.ID)
}
normalizedName := methodNormalizer[lang](alias(aliases, name))
// Name shouldn't start with a digit. It will make the generated code invalid.
if len(normalizedName) > 0 && unicode.IsDigit(rune(normalizedName[0])) {
normalizedName = fmt.Sprintf("M%s", normalizedName)
Expand Down
4 changes: 2 additions & 2 deletions accounts/abi/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ var bindTests = []struct {
}
}
}()
contract.Foo(auth, big.NewInt(1), big.NewInt(2))
contract.Foo04bc52f8(auth, big.NewInt(1), big.NewInt(2))
sim.Commit()
select {
case n := <-resCh:
Expand All @@ -1497,7 +1497,7 @@ var bindTests = []struct {
t.Fatalf("Wait bar0 event timeout")
}

contract.Foo0(auth, big.NewInt(1))
contract.Foo2fbebd38(auth, big.NewInt(1))
sim.Commit()
select {
case n := <-resCh:
Expand Down
Loading