Skip to content

Commit e7835c3

Browse files
committed
Use go-ethereum in compiler
1 parent 2e83c14 commit e7835c3

File tree

2 files changed

+7
-135
lines changed

2 files changed

+7
-135
lines changed

solc/compiler.go

Lines changed: 5 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,25 @@ package solc
22

33
import (
44
"bytes"
5-
"encoding/json"
65
"errors"
76
"fmt"
87
"os/exec"
98
"regexp"
109
"strconv"
1110
"strings"
11+
12+
"github.com/ethereum/go-ethereum/common/compiler"
1213
)
1314

1415
var versionRegexp = regexp.MustCompile(`([0-9]+)\.([0-9]+)\.([0-9]+)`)
1516

16-
// Contract contains information about a compiled contract, alongside its code and runtime code.
17-
type Contract struct {
18-
Code string `json:"code"`
19-
RuntimeCode string `json:"runtime-code"`
20-
Info ContractInfo `json:"info"`
21-
Hashes map[string]string `json:"hashes"`
22-
}
23-
24-
// ContractInfo contains information about a compiled contract, including access
25-
// to the ABI definition, source mapping, user and developer docs, and metadata.
26-
//
27-
// Depending on the source, language version, compiler version, and compiler
28-
// options will provide information about how the contract was compiled.
29-
type ContractInfo struct {
30-
Source string `json:"source"`
31-
Language string `json:"language"`
32-
LanguageVersion string `json:"languageVersion"`
33-
CompilerVersion string `json:"compilerVersion"`
34-
CompilerOptions string `json:"compilerOptions"`
35-
SrcMap interface{} `json:"srcMap"`
36-
SrcMapRuntime string `json:"srcMapRuntime"`
37-
AbiDefinition interface{} `json:"abiDefinition"`
38-
UserDoc interface{} `json:"userDoc"`
39-
DeveloperDoc interface{} `json:"developerDoc"`
40-
Metadata string `json:"metadata"`
41-
}
4217

4318
// Solidity contains information about the solidity compiler.
4419
type Solidity struct {
4520
Path, Version, FullVersion string
4621
Major, Minor, Patch int
4722
}
4823

49-
// --combined-output format
50-
type solcOutput struct {
51-
Contracts map[string]struct {
52-
BinRuntime string `json:"bin-runtime"`
53-
SrcMapRuntime string `json:"srcmap-runtime"`
54-
Bin, SrcMap, Abi, Devdoc, Userdoc, Metadata string
55-
Hashes map[string]string
56-
}
57-
Version string
58-
}
59-
60-
// solidity v.0.8 changes the way ABI, Devdoc and Userdoc are serialized
61-
type solcOutputV8 struct {
62-
Contracts map[string]struct {
63-
BinRuntime string `json:"bin-runtime"`
64-
SrcMapRuntime string `json:"srcmap-runtime"`
65-
Bin, SrcMap, Metadata string
66-
Abi interface{}
67-
Devdoc interface{}
68-
Userdoc interface{}
69-
Hashes map[string]string
70-
}
71-
Version string
72-
}
7324

7425
func (s *Solidity) makeArgs() []string {
7526
p := []string{
@@ -113,7 +64,7 @@ func SolidityVersion(solc string) (*Solidity, error) {
11364
}
11465

11566
// compileSolidityString builds and returns all the contracts contained within a source string.
116-
func compileSolidityString(solc, source string) (map[string]*Contract, error) {
67+
func compileSolidityString(solc, source string) (map[string]*compiler.Contract, error) {
11768
if len(source) == 0 {
11869
return nil, errors.New("solc: empty source string")
11970
}
@@ -127,93 +78,13 @@ func compileSolidityString(solc, source string) (map[string]*Contract, error) {
12778
return s.run(cmd, source)
12879
}
12980

130-
func (s *Solidity) run(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
81+
func (s *Solidity) run(cmd *exec.Cmd, source string) (map[string]*compiler.Contract, error) {
13182
var stderr, stdout bytes.Buffer
13283
cmd.Stderr = &stderr
13384
cmd.Stdout = &stdout
13485
if err := cmd.Run(); err != nil {
13586
return nil, fmt.Errorf("solc: %v\n%s", err, stderr.Bytes())
13687
}
137-
return ParseCombinedJSON(stdout.Bytes(), source, s.Version, s.Version, strings.Join(s.makeArgs(), " "))
88+
return compiler.ParseCombinedJSON(stdout.Bytes(), source, s.Version, s.Version, strings.Join(s.makeArgs(), " "))
13889
}
13990

140-
// ParseCombinedJSON takes the direct output of a solc --combined-output run and
141-
// parses it into a map of string contract name to Contract structs. The
142-
// provided source, language and compiler version, and compiler options are all
143-
// passed through into the Contract structs.
144-
//
145-
// The solc output is expected to contain ABI, source mapping, user docs, and dev docs.
146-
//
147-
// Returns an error if the JSON is malformed or missing data, or if the JSON
148-
// embedded within the JSON is malformed.
149-
func ParseCombinedJSON(combinedJSON []byte, source string, languageVersion string, compilerVersion string, compilerOptions string) (map[string]*Contract, error) {
150-
var output solcOutput
151-
if err := json.Unmarshal(combinedJSON, &output); err != nil {
152-
// Try to parse the output with the new solidity v.0.8.0 rules
153-
return parseCombinedJSONV8(combinedJSON, source, languageVersion, compilerVersion, compilerOptions)
154-
}
155-
// Compilation succeeded, assemble and return the contracts.
156-
contracts := make(map[string]*Contract)
157-
for name, info := range output.Contracts {
158-
// Parse the individual compilation results.
159-
var abi interface{}
160-
if err := json.Unmarshal([]byte(info.Abi), &abi); err != nil {
161-
return nil, fmt.Errorf("solc: error reading abi definition (%v)", err)
162-
}
163-
var userdoc, devdoc interface{}
164-
json.Unmarshal([]byte(info.Userdoc), &userdoc)
165-
json.Unmarshal([]byte(info.Devdoc), &devdoc)
166-
167-
contracts[name] = &Contract{
168-
Code: "0x" + info.Bin,
169-
RuntimeCode: "0x" + info.BinRuntime,
170-
Hashes: info.Hashes,
171-
Info: ContractInfo{
172-
Source: source,
173-
Language: "Solidity",
174-
LanguageVersion: languageVersion,
175-
CompilerVersion: compilerVersion,
176-
CompilerOptions: compilerOptions,
177-
SrcMap: info.SrcMap,
178-
SrcMapRuntime: info.SrcMapRuntime,
179-
AbiDefinition: abi,
180-
UserDoc: userdoc,
181-
DeveloperDoc: devdoc,
182-
Metadata: info.Metadata,
183-
},
184-
}
185-
}
186-
return contracts, nil
187-
}
188-
189-
// parseCombinedJSONV8 parses the direct output of solc --combined-output
190-
// and parses it using the rules from solidity v.0.8.0 and later.
191-
func parseCombinedJSONV8(combinedJSON []byte, source string, languageVersion string, compilerVersion string, compilerOptions string) (map[string]*Contract, error) {
192-
var output solcOutputV8
193-
if err := json.Unmarshal(combinedJSON, &output); err != nil {
194-
return nil, err
195-
}
196-
// Compilation succeeded, assemble and return the contracts.
197-
contracts := make(map[string]*Contract)
198-
for name, info := range output.Contracts {
199-
contracts[name] = &Contract{
200-
Code: "0x" + info.Bin,
201-
RuntimeCode: "0x" + info.BinRuntime,
202-
Hashes: info.Hashes,
203-
Info: ContractInfo{
204-
Source: source,
205-
Language: "Solidity",
206-
LanguageVersion: languageVersion,
207-
CompilerVersion: compilerVersion,
208-
CompilerOptions: compilerOptions,
209-
SrcMap: info.SrcMap,
210-
SrcMapRuntime: info.SrcMapRuntime,
211-
AbiDefinition: info.Abi,
212-
UserDoc: info.Userdoc,
213-
DeveloperDoc: info.Devdoc,
214-
Metadata: info.Metadata,
215-
},
216-
}
217-
}
218-
return contracts, nil
219-
}

solc/solc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sort"
1515

1616
"github.com/Masterminds/semver/v3"
17+
"github.com/ethereum/go-ethereum/common/compiler"
1718
"github.com/shibukawa/configdir"
1819
"github.com/withtally/synceth/parser"
1920
)
@@ -178,7 +179,7 @@ func resolve(vc *parser.VersionConstraint) (Build, error) {
178179
return Build{}, fmt.Errorf("cant satisfy version constraint: %s", vc.String())
179180
}
180181

181-
func CompileSolidityString(src string) (map[string]*Contract, error) {
182+
func CompileSolidityString(src string) (map[string]*compiler.Contract, error) {
182183
c, err := parser.NewVersionConstraint(src)
183184
if err != nil {
184185
return nil, fmt.Errorf("parsing solidity version: %w", err)

0 commit comments

Comments
 (0)