/
sol.go
358 lines (300 loc) · 9.04 KB
/
sol.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Package sol provides a convenient interface for calling the 'solc' Solidity Compiler from Go.
package sol
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/tidwall/sjson"
)
type Contract struct {
Name string
SourcePath string
AllPaths []string
CompilerVersion string
Address common.Address
Coverage bool
Statements [][]int
ABI []byte
Bin string
}
type Compiler interface {
SetAllowPaths(paths []string) Compiler
Compile(prefix, path string, optimize int) (map[string]*Contract, error)
CompileWithCoverage(prefix, path string) (map[string]*Contract, error)
}
func NewSolCompiler(solcPath string) (Compiler, error) {
s := &solCompiler{
solcPath: solcPath,
}
if err := s.verify(); err != nil {
return nil, err
}
return s, nil
}
type solCompiler struct {
solcPath string
allowPaths []string
}
func (s *solCompiler) verify() error {
out, err := exec.Command(s.solcPath, "--version").CombinedOutput()
if err != nil {
err = fmt.Errorf("solc verify: failed to exec solc: %v", err)
return err
}
hasPrefix := strings.HasPrefix(string(out), "solc, the solidity compiler")
if !hasPrefix {
err := fmt.Errorf("solc verify: executable output was unexpected (output: %s)", out)
return err
}
return nil
}
func (s *solCompiler) SetAllowPaths(paths []string) Compiler {
s.allowPaths = paths
return s
}
type solcContract struct {
ABI json.RawMessage `json:"abi"`
Bin string `json:"bin"`
}
type solcSource struct {
AST json.RawMessage `json:"AST,omitempty"`
}
type solcOutput struct {
Contracts map[string]solcContract `json:"contracts"`
Sources map[string]solcSource `json:"sources,omitempty"`
SourceList []string `json:"sourceList,omitempty"`
Version string `json:"version"`
}
func (s *solCompiler) Compile(prefix, path string, optimize int) (map[string]*Contract, error) {
args := []string{s.solcPath}
if len(s.allowPaths) > 0 {
args = append(args, "--allow-paths", strings.Join(s.allowPaths, ","))
}
args = append(args, "--combined-json", "bin,abi,ast,compact-format", filepath.Join(prefix, path))
if optimize > 0 {
args = append(args, "--optimize", fmt.Sprintf("--optimize-runs=%d", optimize))
}
cmd := exec.Cmd{
Path: s.solcPath,
Args: args,
Dir: prefix,
Stderr: os.Stderr,
}
out, err := cmd.Output()
if err != nil {
err = fmt.Errorf("solc: failed to compile contract: %v", err)
return nil, err
}
var result solcOutput
if err := json.Unmarshal(out, &result); err != nil {
err = fmt.Errorf("solc: failed to unmarshal Solc output: %v", err)
return nil, err
}
if len(result.Contracts) == 0 {
err := errors.New("solc: no contracts compiled")
return nil, err
} else if len(result.Sources) == 0 {
err := errors.New("solc: no source paths collected")
return nil, err
}
contractPathsByName := make(map[string]string, len(result.SourceList))
contractNamesOrdered := make([]string, len(result.SourceList))
contractFilePaths := make([]string, 0, len(result.SourceList))
for id := range result.Contracts {
name, sourcePath, err := idToNameAndSourcePath(id)
if err != nil {
return nil, err
}
contractPathsByName[name] = sourcePath
}
for name, sourcePath := range contractPathsByName {
for idx, src := range result.SourceList {
if src == sourcePath {
contractNamesOrdered[idx] = name
break
}
}
}
seenPaths := make(map[string]struct{}, len(contractPathsByName))
for _, contractName := range contractNamesOrdered {
filePath := contractPathsByName[contractName]
if _, ok := seenPaths[filePath]; ok {
continue
} else {
seenPaths[filePath] = struct{}{}
}
contractFilePaths = append(contractFilePaths, filePath)
}
contracts := make(map[string]*Contract, len(result.Contracts))
for id, c := range result.Contracts {
name, sourcePath, err := idToNameAndSourcePath(id)
if err != nil {
return nil, err
}
contracts[name] = &Contract{
Name: name,
SourcePath: sourcePath,
AllPaths: contractFilePaths,
CompilerVersion: result.Version,
Coverage: false,
ABI: []byte(c.ABI),
Bin: c.Bin,
}
}
return contracts, nil
}
func (s *solCompiler) CompileWithCoverage(prefix, path string) (map[string]*Contract, error) {
args := []string{s.solcPath}
if len(s.allowPaths) > 0 {
args = append(args, "--allow-paths", strings.Join(s.allowPaths, ","))
}
args = append(args, "--optimize", "--combined-json", "ast,compact-format", filepath.Join(prefix, path))
cmd := exec.Cmd{
Path: s.solcPath,
Args: args,
Dir: prefix,
Stderr: os.Stderr,
}
out, err := cmd.Output()
if err != nil {
err = fmt.Errorf("solc: failed to compile contract: %v", err)
return nil, err
}
var result solcOutput
if err := json.Unmarshal(out, &result); err != nil {
err = fmt.Errorf("solc: failed to unmarshal Solc output: %v", err)
return nil, err
}
if len(result.Contracts) == 0 {
err := errors.New("solc: no contracts compiled")
return nil, err
} else if len(result.Sources) == 0 {
err := errors.New("solc: source paths collected")
return nil, err
}
contractPathsByName := make(map[string]string, len(result.SourceList))
contractNamesOrdered := make([]string, len(result.SourceList))
contractFilePaths := make([]string, 0, len(result.SourceList))
for id := range result.Contracts {
name, sourcePath, err := idToNameAndSourcePath(id)
if err != nil {
return nil, err
}
contractPathsByName[name] = sourcePath
}
for name, sourcePath := range contractPathsByName {
for idx, src := range result.SourceList {
if src == sourcePath {
contractNamesOrdered[idx] = name
break
}
}
}
seenPaths := make(map[string]struct{}, len(contractPathsByName))
contractStatements := make([][]int, 0, len(contractFilePaths))
for fileIdx, contractName := range contractNamesOrdered {
filePath := contractPathsByName[contractName]
if _, ok := seenPaths[filePath]; ok {
err := errors.Errorf("multiple contracts in the same file is a big no-no. Please refactor %s", filePath)
return nil, err
} else {
seenPaths[filePath] = struct{}{}
}
source := result.Sources[filePath]
modifiedAST, statements, err := addCoverageMarkers(fileIdx, contractName, source.AST)
if err != nil {
err = errors.Wrapf(err, "failed to orchestrate %s source with coverage markers", filePath)
return nil, err
}
contractFilePaths = append(contractFilePaths, filePath)
contractStatements = append(contractStatements, statements...)
escapedPath := strings.Replace(filePath, ".", "\\.", -1)
out, err = sjson.SetBytes(out, fmt.Sprintf("sources.%s.AST", escapedPath), modifiedAST)
if err != nil {
err = errors.Wrap(err, "failed to update solc output with added coverage")
return nil, err
}
}
tmp, err := ioutil.TempFile("", "*_sol_coverage.json")
if err != nil {
err = errors.Wrap(err, "failed to open temp file for orchestrated AST output")
return nil, err
}
// fmt.Println("Staging coverage into:", tmp.Name())
if _, err := io.Copy(tmp, bytes.NewReader(out)); err != nil {
err = errors.Wrap(err, "failed to write temp file with orchestrated AST output")
return nil, err
}
_ = tmp.Close()
defer func() {
_ = os.Remove(tmp.Name())
}()
// now just re-import the patched AST
args = []string{s.solcPath}
args = append(args, "--import-ast", "--optimize", "--combined-json", "bin,abi", tmp.Name())
errOut := new(bytes.Buffer)
finalCmd := exec.Cmd{
Path: s.solcPath,
Args: args,
Dir: prefix,
Stderr: errOut,
}
finalOutput, err := finalCmd.Output()
if err != nil {
_, _ = io.Copy(os.Stderr, errOut)
err = fmt.Errorf("solc: failed to compile contract: %v", err)
return nil, err
}
var finalResult solcOutput
if err := json.Unmarshal(finalOutput, &finalResult); err != nil {
err = fmt.Errorf("solc: failed to unmarshal Solc output: %v", err)
return nil, err
}
if len(finalResult.Contracts) == 0 {
err := errors.New("solc: no contracts compiled")
return nil, err
}
contracts := make(map[string]*Contract, len(finalResult.Contracts))
for id, c := range finalResult.Contracts {
name, sourcePath, err := idToNameAndSourcePath(id)
if err != nil {
return nil, err
}
contracts[name] = &Contract{
Name: name,
SourcePath: sourcePath,
AllPaths: contractFilePaths,
CompilerVersion: finalResult.Version,
Coverage: true,
Statements: contractStatements,
ABI: []byte(c.ABI),
Bin: c.Bin,
}
}
return contracts, nil
}
func idToNameAndSourcePath(id string) (name, sourcePath string, err error) {
idParts := strings.Split(id, ":")
if len(idParts) == 1 {
err = errors.Errorf("solc: found an unnamed contract in output: %s", id)
return
}
name = idParts[len(idParts)-1]
sourcePath = idParts[0]
return name, sourcePath, nil
}
func WhichSolc() (string, error) {
out, err := exec.Command("which", "solc").Output()
if err != nil {
return "", errors.New("solc executable file not found in $PATH")
}
return string(bytes.TrimSpace(out)), nil
}