Skip to content

Commit 732b753

Browse files
authored
Merge pull request #3786 from fjl/compiler-metadata
common/compiler: add metadata output for solc > 0.4.6
2 parents 906378a + 728a299 commit 732b753

File tree

2 files changed

+56
-76
lines changed

2 files changed

+56
-76
lines changed

common/compiler/solidity.go

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,11 @@ import (
2525
"io/ioutil"
2626
"os/exec"
2727
"regexp"
28+
"strconv"
2829
"strings"
29-
30-
"github.com/ethereum/go-ethereum/common"
31-
"github.com/ethereum/go-ethereum/crypto"
3230
)
3331

34-
var (
35-
versionRegexp = regexp.MustCompile(`[0-9]+\.[0-9]+\.[0-9]+`)
36-
solcParams = []string{
37-
"--combined-json", "bin,abi,userdoc,devdoc",
38-
"--add-std", // include standard lib contracts
39-
"--optimize", // code optimizer switched on
40-
}
41-
)
32+
var versionRegexp = regexp.MustCompile(`([0-9]+)\.([0-9]+)\.([0-9]+)`)
4233

4334
type Contract struct {
4435
Code string `json:"code"`
@@ -54,17 +45,33 @@ type ContractInfo struct {
5445
AbiDefinition interface{} `json:"abiDefinition"`
5546
UserDoc interface{} `json:"userDoc"`
5647
DeveloperDoc interface{} `json:"developerDoc"`
48+
Metadata string `json:"metadata"`
5749
}
5850

5951
// Solidity contains information about the solidity compiler.
6052
type Solidity struct {
6153
Path, Version, FullVersion string
54+
Major, Minor, Patch int
6255
}
6356

6457
// --combined-output format
6558
type solcOutput struct {
66-
Contracts map[string]struct{ Bin, Abi, Devdoc, Userdoc string }
67-
Version string
59+
Contracts map[string]struct {
60+
Bin, Abi, Devdoc, Userdoc, Metadata string
61+
}
62+
Version string
63+
}
64+
65+
func (s *Solidity) makeArgs() []string {
66+
p := []string{
67+
"--combined-json", "bin,abi,userdoc,devdoc",
68+
"--add-std", // include standard lib contracts
69+
"--optimize", // code optimizer switched on
70+
}
71+
if s.Major > 0 || s.Minor > 4 || s.Patch > 6 {
72+
p[1] += ",metadata"
73+
}
74+
return p
6875
}
6976

7077
// SolidityVersion runs solc and parses its version output.
@@ -75,13 +82,23 @@ func SolidityVersion(solc string) (*Solidity, error) {
7582
var out bytes.Buffer
7683
cmd := exec.Command(solc, "--version")
7784
cmd.Stdout = &out
78-
if err := cmd.Run(); err != nil {
85+
err := cmd.Run()
86+
if err != nil {
87+
return nil, err
88+
}
89+
matches := versionRegexp.FindStringSubmatch(out.String())
90+
if len(matches) != 4 {
91+
return nil, fmt.Errorf("can't parse solc version %q", out.String())
92+
}
93+
s := &Solidity{Path: cmd.Path, FullVersion: out.String(), Version: matches[0]}
94+
if s.Major, err = strconv.Atoi(matches[1]); err != nil {
7995
return nil, err
8096
}
81-
s := &Solidity{
82-
Path: cmd.Path,
83-
FullVersion: out.String(),
84-
Version: versionRegexp.FindString(out.String()),
97+
if s.Minor, err = strconv.Atoi(matches[2]); err != nil {
98+
return nil, err
99+
}
100+
if s.Patch, err = strconv.Atoi(matches[3]); err != nil {
101+
return nil, err
85102
}
86103
return s, nil
87104
}
@@ -91,13 +108,14 @@ func CompileSolidityString(solc, source string) (map[string]*Contract, error) {
91108
if len(source) == 0 {
92109
return nil, errors.New("solc: empty source string")
93110
}
94-
if solc == "" {
95-
solc = "solc"
111+
s, err := SolidityVersion(solc)
112+
if err != nil {
113+
return nil, err
96114
}
97-
args := append(solcParams, "--")
98-
cmd := exec.Command(solc, append(args, "-")...)
115+
args := append(s.makeArgs(), "--")
116+
cmd := exec.Command(s.Path, append(args, "-")...)
99117
cmd.Stdin = strings.NewReader(source)
100-
return runsolc(cmd, source)
118+
return s.run(cmd, source)
101119
}
102120

103121
// CompileSolidity compiles all given Solidity source files.
@@ -109,15 +127,16 @@ func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract,
109127
if err != nil {
110128
return nil, err
111129
}
112-
if solc == "" {
113-
solc = "solc"
130+
s, err := SolidityVersion(solc)
131+
if err != nil {
132+
return nil, err
114133
}
115-
args := append(solcParams, "--")
116-
cmd := exec.Command(solc, append(args, sourcefiles...)...)
117-
return runsolc(cmd, source)
134+
args := append(s.makeArgs(), "--")
135+
cmd := exec.Command(s.Path, append(args, sourcefiles...)...)
136+
return s.run(cmd, source)
118137
}
119138

120-
func runsolc(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
139+
func (s *Solidity) run(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
121140
var stderr, stdout bytes.Buffer
122141
cmd.Stderr = &stderr
123142
cmd.Stdout = &stdout
@@ -128,7 +147,6 @@ func runsolc(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
128147
if err := json.Unmarshal(stdout.Bytes(), &output); err != nil {
129148
return nil, err
130149
}
131-
shortVersion := versionRegexp.FindString(output.Version)
132150

133151
// Compilation succeeded, assemble and return the contracts.
134152
contracts := make(map[string]*Contract)
@@ -151,12 +169,13 @@ func runsolc(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
151169
Info: ContractInfo{
152170
Source: source,
153171
Language: "Solidity",
154-
LanguageVersion: shortVersion,
155-
CompilerVersion: shortVersion,
156-
CompilerOptions: strings.Join(solcParams, " "),
172+
LanguageVersion: s.Version,
173+
CompilerVersion: s.Version,
174+
CompilerOptions: strings.Join(s.makeArgs(), " "),
157175
AbiDefinition: abi,
158176
UserDoc: userdoc,
159177
DeveloperDoc: devdoc,
178+
Metadata: info.Metadata,
160179
},
161180
}
162181
}
@@ -174,13 +193,3 @@ func slurpFiles(files []string) (string, error) {
174193
}
175194
return concat.String(), nil
176195
}
177-
178-
// SaveInfo serializes info to the given file and returns its Keccak256 hash.
179-
func SaveInfo(info *ContractInfo, filename string) (common.Hash, error) {
180-
infojson, err := json.Marshal(info)
181-
if err != nil {
182-
return common.Hash{}, err
183-
}
184-
contenthash := common.BytesToHash(crypto.Keccak256(infojson))
185-
return contenthash, ioutil.WriteFile(filename, infojson, 0600)
186-
}

common/compiler/solidity_test.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,8 @@
1717
package compiler
1818

1919
import (
20-
"encoding/json"
21-
"io/ioutil"
22-
"os"
2320
"os/exec"
24-
"path"
2521
"testing"
26-
27-
"github.com/ethereum/go-ethereum/common"
2822
)
2923

3024
const (
@@ -36,7 +30,6 @@ contract test {
3630
}
3731
}
3832
`
39-
testInfo = `{"source":"\ncontract test {\n /// @notice Will multiply ` + "`a`" + ` by 7.\n function multiply(uint a) returns(uint d) {\n return a * 7;\n }\n}\n","language":"Solidity","languageVersion":"0.1.1","compilerVersion":"0.1.1","compilerOptions":"--binary file --json-abi file --add-std 1","abiDefinition":[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}],"userDoc":{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}},"developerDoc":{"methods":{}}}`
4033
)
4134

4235
func skipWithoutSolc(t *testing.T) {
@@ -57,7 +50,10 @@ func TestCompiler(t *testing.T) {
5750
}
5851
c, ok := contracts["test"]
5952
if !ok {
60-
t.Fatal("info for contract 'test' not present in result")
53+
c, ok = contracts["<stdin>:test"]
54+
if !ok {
55+
t.Fatal("info for contract 'test' not present in result")
56+
}
6157
}
6258
if c.Code == "" {
6359
t.Error("empty code")
@@ -79,28 +75,3 @@ func TestCompileError(t *testing.T) {
7975
}
8076
t.Logf("error: %v", err)
8177
}
82-
83-
func TestSaveInfo(t *testing.T) {
84-
var cinfo ContractInfo
85-
err := json.Unmarshal([]byte(testInfo), &cinfo)
86-
if err != nil {
87-
t.Errorf("%v", err)
88-
}
89-
filename := path.Join(os.TempDir(), "solctest.info.json")
90-
os.Remove(filename)
91-
cinfohash, err := SaveInfo(&cinfo, filename)
92-
if err != nil {
93-
t.Errorf("error extracting info: %v", err)
94-
}
95-
got, err := ioutil.ReadFile(filename)
96-
if err != nil {
97-
t.Errorf("error reading '%v': %v", filename, err)
98-
}
99-
if string(got) != testInfo {
100-
t.Errorf("incorrect info.json extracted, expected:\n%s\ngot\n%s", testInfo, string(got))
101-
}
102-
wantHash := common.HexToHash("0x22450a77f0c3ff7a395948d07bc1456881226a1b6325f4189cb5f1254a824080")
103-
if cinfohash != wantHash {
104-
t.Errorf("content hash for info is incorrect. expected %v, got %v", wantHash.Hex(), cinfohash.Hex())
105-
}
106-
}

0 commit comments

Comments
 (0)