-
Notifications
You must be signed in to change notification settings - Fork 656
/
manager.go
231 lines (196 loc) · 5.89 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package compilesc
import (
"fmt"
"io/fs"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/0xPolygonHermez/zkevm-node/log"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
)
const (
defaultAbigenImage = "ethereum/client-go:alltools-latest"
defaultSolcImage = "ethereum/solc"
containerBase = "/contracts"
)
// CompileUnit represents a single contract to be compiled.
type CompileUnit struct {
Name string `yaml:"name"`
SolcVersion string `yaml:"solcVersion"`
InputPath string `yaml:"inputPath"`
OutputPath string `yaml:"outputPath"`
}
// CompileUnits represents data for all the contracts to be compiled.
type CompileUnits struct {
Parallel []CompileUnit `yaml:"parallel"`
Sequential []CompileUnit `yaml:"sequential"`
}
type compileIndex struct {
CompileUnits CompileUnits `yaml:"compileUnits"`
}
// Manager handles smart contract compilation.
type Manager struct {
basePath string
absoluteBasePath string
currentUser *user.User
}
// NewManager is the Manager constructor.
func NewManager(basePath string) (*Manager, error) {
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "../../../")
absoluteBasePath := path.Join(dir, basePath)
currentUser, err := user.Current()
if err != nil {
return nil, err
}
return &Manager{
basePath: basePath,
absoluteBasePath: absoluteBasePath,
currentUser: currentUser,
}, nil
}
// Run executes the compilation of smart contracts and generation of golang
// bindings.
func (cm *Manager) Run() error {
yamlFile, err := os.ReadFile(path.Join(cm.absoluteBasePath, "index.yaml"))
if err != nil {
return err
}
ci := &compileIndex{}
err = yaml.Unmarshal(yamlFile, ci)
if err != nil {
return err
}
g := new(errgroup.Group)
g.Go(func() error {
for _, item := range ci.CompileUnits.Parallel {
err = cm.parallelActions(item)
if err != nil {
return err
}
}
return nil
})
g.Go(func() error {
for _, item := range ci.CompileUnits.Sequential {
err = cm.sequentialActions(item)
if err != nil {
return err
}
}
return nil
})
return g.Wait()
}
// parallelActions performs compile and generate actions in parallel for a given
// compile unit.
func (cm *Manager) parallelActions(item CompileUnit) error {
entryPoint := path.Join(cm.basePath, item.InputPath)
file, err := os.Open(entryPoint) // #nosec G304
if err != nil {
return err
}
defer func() {
if err := file.Close(); err != nil {
log.Errorf("Could not close file %q, %v", file.Name(), err)
}
}()
fileInfo, err := file.Stat()
if err != nil {
return err
}
if !fileInfo.IsDir() {
return cm.fileActions(path.Base(file.Name()), item.SolcVersion, item.InputPath, item.OutputPath)
}
g := new(errgroup.Group)
g.Go(func() error {
return filepath.WalkDir(file.Name(), func(target string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info == nil || info.IsDir() {
return nil
}
if filepath.Ext(target) != ".sol" {
return nil
}
fileName := strings.TrimSuffix(target, ".sol")
g.Go(func() error {
return cm.fileActions(path.Base(fileName), item.SolcVersion, item.InputPath, item.OutputPath)
})
return nil
})
})
return g.Wait()
}
// sequentialActions performs compile and generate actions sequentially for a given
// compile unit.
func (cm *Manager) sequentialActions(item CompileUnit) error {
entryPoint := path.Join(cm.basePath, item.InputPath, fmt.Sprintf("%s.sol", item.Name))
file, err := os.Open(entryPoint) // #nosec G304
if err != nil && err != os.ErrNotExist {
return err
}
defer func() {
if err := file.Close(); err != nil {
log.Errorf("Could not close file %q, %v", file.Name(), err)
}
}()
return cm.fileActions(item.Name, item.SolcVersion, item.InputPath, item.OutputPath)
}
func (cm *Manager) fileActions(name, solcVersion, inputPath, outputPath string) error {
err := cm.Compile(path.Base(name), solcVersion, inputPath, outputPath)
if err != nil {
return err
}
return cm.Abigen(path.Base(name), inputPath, outputPath)
}
// Compile invokes solc on the given sol file.
func (cm *Manager) Compile(name, solcVersion, inputPath, outputPath string) error {
log.Infof("Compiling %s.sol with version %s", path.Join(cm.basePath, inputPath, name), solcVersion)
solcImage := fmt.Sprintf("%s:%s", defaultSolcImage, solcVersion)
c := exec.Command(
"docker", "run", "--rm",
"--user", fmt.Sprintf("%s:%s", cm.currentUser.Uid, cm.currentUser.Gid),
"-v", fmt.Sprintf("%s:%s", cm.absoluteBasePath, containerBase),
solcImage,
"-",
fmt.Sprintf("%s.sol", path.Join(containerBase, inputPath, name)),
"-o", path.Join(containerBase, "bin", outputPath, name),
"--abi", "--bin", "--overwrite", "--optimize") // #nosec G204
envPath := os.Getenv("PATH")
c.Env = []string{fmt.Sprintf("PATH=%s", envPath)}
err := c.Run()
if err != nil {
return err
}
log.Infof("Compiler run successfully, artifacts can be found at %q", path.Join(cm.basePath, "bin", name))
return nil
}
// Abigen generates bindings for the given file
func (cm *Manager) Abigen(name, inputPath, outputPath string) error {
log.Infof("Generating go code for %q...", name)
c := exec.Command(
"docker", "run", "--rm",
"--user", fmt.Sprintf("%s:%s", cm.currentUser.Uid, cm.currentUser.Gid),
"-v", fmt.Sprintf("%s:%s", cm.absoluteBasePath, containerBase),
defaultAbigenImage,
"abigen",
"--bin", path.Join(containerBase, "bin", outputPath, name, fmt.Sprintf("%s.bin", name)),
"--abi", path.Join(containerBase, "bin", outputPath, name, fmt.Sprintf("%s.abi", name)),
"--pkg", name,
"--out", path.Join(containerBase, "bin", outputPath, name, fmt.Sprintf("%s.go", name))) // #nosec G204
envPath := os.Getenv("PATH")
c.Env = []string{fmt.Sprintf("PATH=%s", envPath)}
err := c.Run()
if err != nil {
return err
}
log.Infof("Code generated at %q", path.Join(cm.basePath, "bin", outputPath, name, fmt.Sprintf("%s.go", name)))
return nil
}