Skip to content

Commit 5598edf

Browse files
committed
accounts/abi/bind/v2: update tests
- add go:generate lines for updating the test bindings - move tests to bind_test package because the contracts now import bind/v2 - simplify some code in tests
1 parent 56502a9 commit 5598edf

File tree

2 files changed

+140
-160
lines changed

2 files changed

+140
-160
lines changed

accounts/abi/bind/v2/generate_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2024 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package bind_test
18+
19+
import (
20+
"encoding/json"
21+
"os"
22+
"path/filepath"
23+
"strings"
24+
"testing"
25+
26+
bind1 "github.com/ethereum/go-ethereum/accounts/abi/bind"
27+
"github.com/ethereum/go-ethereum/cmd/utils"
28+
"github.com/ethereum/go-ethereum/common/compiler"
29+
"github.com/ethereum/go-ethereum/crypto"
30+
)
31+
32+
//go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/db/combined-abi.json -type DBStats -pkg db -out internal/contracts/db/bindings.go
33+
//go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/events/combined-abi.json -type C -pkg events -out internal/contracts/events/bindings.go
34+
//go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/nested_libraries/combined-abi.json -type C1 -pkg nested_libraries -out internal/contracts/nested_libraries/bindings.go
35+
//go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/solc_errors/combined-abi.json -type C -pkg solc_errors -out internal/contracts/solc_errors/bindings.go
36+
//go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/uint256arrayreturn/combined-abi.json -type C -pkg uint256arrayreturn -out internal/contracts/uint256arrayreturn/bindings.go
37+
38+
// TestBindingGeneration tests that re-running generation of bindings does not result in
39+
// mutations to the binding code.
40+
func TestBindingGeneration(t *testing.T) {
41+
matches, _ := filepath.Glob("internal/contracts/*")
42+
var dirs []string
43+
for _, match := range matches {
44+
f, _ := os.Stat(match)
45+
if f.IsDir() {
46+
dirs = append(dirs, f.Name())
47+
}
48+
}
49+
50+
for _, dir := range dirs {
51+
var (
52+
abis []string
53+
bins []string
54+
types []string
55+
libs = make(map[string]string)
56+
)
57+
basePath := filepath.Join("internal/contracts", dir)
58+
combinedJsonPath := filepath.Join(basePath, "combined-abi.json")
59+
abiBytes, err := os.ReadFile(combinedJsonPath)
60+
if err != nil {
61+
t.Fatalf("error trying to read file %s: %v", combinedJsonPath, err)
62+
}
63+
contracts, err := compiler.ParseCombinedJSON(abiBytes, "", "", "", "")
64+
if err != nil {
65+
t.Fatalf("Failed to read contract information from json output: %v", err)
66+
}
67+
68+
for name, contract := range contracts {
69+
// fully qualified name is of the form <solFilePath>:<type>
70+
nameParts := strings.Split(name, ":")
71+
typeName := nameParts[len(nameParts)-1]
72+
abi, err := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
73+
if err != nil {
74+
utils.Fatalf("Failed to parse ABIs from compiler output: %v", err)
75+
}
76+
abis = append(abis, string(abi))
77+
bins = append(bins, contract.Code)
78+
types = append(types, typeName)
79+
80+
// Derive the library placeholder which is a 34 character prefix of the
81+
// hex encoding of the keccak256 hash of the fully qualified library name.
82+
// Note that the fully qualified library name is the path of its source
83+
// file and the library name separated by ":".
84+
libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36] // the first 2 chars are 0x
85+
libs[libPattern] = typeName
86+
}
87+
code, err := bind1.BindV2(types, abis, bins, dir, libs, make(map[string]string))
88+
if err != nil {
89+
t.Fatalf("error creating bindings for package %s: %v", dir, err)
90+
}
91+
92+
existingBindings, err := os.ReadFile(filepath.Join(basePath, "bindings.go"))
93+
if err != nil {
94+
t.Fatalf("ReadFile returned error: %v", err)
95+
}
96+
if code != string(existingBindings) {
97+
t.Fatalf("code mismatch for %s", dir)
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)