Skip to content

Commit 17b7297

Browse files
committed
Solidity Compiler - solc new API
* adapt to new compiler versioning * use compiler version as language version * implement new solc API for versions >= 0.1.[2-9][0-9]* fixes #1770 * add optimize=1 to options * backward compatibility (for now) for <= 0.1.1, and old versions (0.[2-9][0-9]*.[0-9]+) * introduce compilerOptions to ContractInfo * clean up flair, include full version string to version line and ContractInfo
1 parent 55ed8d1 commit 17b7297

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

common/compiler/solidity.go

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,10 @@ import (
3434
"github.com/ethereum/go-ethereum/logger/glog"
3535
)
3636

37-
const (
38-
// flair = "Christian <[email protected]> and Lefteris <[email protected]> (c) 2014-2015"
39-
flair = ""
40-
languageVersion = "0"
41-
)
42-
4337
var (
44-
versionRegExp = regexp.MustCompile("[0-9]+.[0-9]+.[0-9]+")
45-
params = []string{
38+
versionRegExp = regexp.MustCompile("[0-9]+\\.[0-9]+\\.[0-9]+")
39+
newAPIRegexp = regexp.MustCompile("0\\.1\\.[2-9][0-9]*")
40+
paramsLegacy = []string{
4641
"--binary", // Request to output the contract in binary (hexadecimal).
4742
"file", //
4843
"--json-abi", // Request to output the contract's JSON ABI interface.
@@ -54,6 +49,15 @@ var (
5449
"--add-std",
5550
"1",
5651
}
52+
paramsNew = []string{
53+
"--bin", // Request to output the contract in binary (hexadecimal).
54+
"--abi", // Request to output the contract's JSON ABI interface.
55+
"--userdoc", // Request to output the contract's Natspec user documentation.
56+
"--devdoc", // Request to output the contract's Natspec developer documentation.
57+
"--add-std", // include standard lib contracts
58+
"--optimize=1", // code optimizer switched on
59+
"-o", // output directory
60+
}
5761
)
5862

5963
type Contract struct {
@@ -66,14 +70,17 @@ type ContractInfo struct {
6670
Language string `json:"language"`
6771
LanguageVersion string `json:"languageVersion"`
6872
CompilerVersion string `json:"compilerVersion"`
73+
CompilerOptions string `json:"compilerOptions"`
6974
AbiDefinition interface{} `json:"abiDefinition"`
7075
UserDoc interface{} `json:"userDoc"`
7176
DeveloperDoc interface{} `json:"developerDoc"`
7277
}
7378

7479
type Solidity struct {
75-
solcPath string
76-
version string
80+
solcPath string
81+
version string
82+
fullVersion string
83+
legacy bool
7784
}
7885

7986
func New(solcPath string) (sol *Solidity, err error) {
@@ -94,17 +101,22 @@ func New(solcPath string) (sol *Solidity, err error) {
94101
return
95102
}
96103

97-
version := versionRegExp.FindString(out.String())
104+
fullVersion := out.String()
105+
version := versionRegExp.FindString(fullVersion)
106+
legacy := !newAPIRegexp.MatchString(version)
107+
98108
sol = &Solidity{
99-
solcPath: solcPath,
100-
version: version,
109+
solcPath: solcPath,
110+
version: version,
111+
fullVersion: fullVersion,
112+
legacy: legacy,
101113
}
102114
glog.V(logger.Info).Infoln(sol.Info())
103115
return
104116
}
105117

106118
func (sol *Solidity) Info() string {
107-
return fmt.Sprintf("solc v%s\nSolidity Compiler: %s\n%s", sol.version, sol.solcPath, flair)
119+
return fmt.Sprintf("%s\npath: %s", sol.fullVersion, sol.solcPath)
108120
}
109121

110122
func (sol *Solidity) Version() string {
@@ -127,6 +139,15 @@ func (sol *Solidity) Compile(source string) (map[string]*Contract, error) {
127139
// Assemble the compiler command, change to the temp folder and capture any errors
128140
stderr := new(bytes.Buffer)
129141

142+
var params []string
143+
if sol.legacy {
144+
params = paramsLegacy
145+
} else {
146+
params = paramsNew
147+
params = append(params, wd)
148+
}
149+
compilerOptions := strings.Join(params, " ")
150+
130151
cmd := exec.Command(sol.solcPath, params...)
131152
cmd.Dir = wd
132153
cmd.Stdin = strings.NewReader(source)
@@ -136,7 +157,7 @@ func (sol *Solidity) Compile(source string) (map[string]*Contract, error) {
136157
return nil, fmt.Errorf("solc: %v\n%s", err, string(stderr.Bytes()))
137158
}
138159
// Sanity check that something was actually built
139-
matches, _ := filepath.Glob(wd + "/*.binary")
160+
matches, _ := filepath.Glob(wd + "/*\\.bin*")
140161
if len(matches) < 1 {
141162
return nil, fmt.Errorf("solc: no build results found")
142163
}
@@ -148,7 +169,11 @@ func (sol *Solidity) Compile(source string) (map[string]*Contract, error) {
148169

149170
// Parse the individual compilation results (code binary, ABI definitions, user and dev docs)
150171
var binary []byte
151-
if binary, err = ioutil.ReadFile(filepath.Join(wd, base+".binary")); err != nil {
172+
binext := ".bin"
173+
if sol.legacy {
174+
binext = ".binary"
175+
}
176+
if binary, err = ioutil.ReadFile(filepath.Join(wd, base+binext)); err != nil {
152177
return nil, fmt.Errorf("solc: error reading compiler output for code: %v", err)
153178
}
154179

@@ -178,8 +203,9 @@ func (sol *Solidity) Compile(source string) (map[string]*Contract, error) {
178203
Info: ContractInfo{
179204
Source: source,
180205
Language: "Solidity",
181-
LanguageVersion: languageVersion,
206+
LanguageVersion: sol.version,
182207
CompilerVersion: sol.version,
208+
CompilerOptions: compilerOptions,
183209
AbiDefinition: abi,
184210
UserDoc: userdoc,
185211
DeveloperDoc: devdoc,

common/compiler/solidity_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/common"
2727
)
2828

29-
const solcVersion = "0.9.23"
29+
const solcVersion = "0.1.1"
3030

3131
var (
3232
source = `
@@ -37,16 +37,16 @@ contract test {
3737
}
3838
}
3939
`
40-
code = "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"
41-
info = `{"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","compilerVersion":"0.9.23","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":{}}}`
40+
code = "0x6060604052606d8060116000396000f30060606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b6046600480359060200150605c565b6040518082815260200191505060405180910390f35b60006007820290506068565b91905056"
41+
info = `{"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 --natspec-user file --natspec-dev 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":{}}}`
4242

43-
infohash = common.HexToHash("0xea782f674eb898e477c20e8a7cf11c2c28b09fa68b5278732104f7a101aed255")
43+
infohash = common.HexToHash("0x9f3803735e7f16120c5a140ab3f02121fd3533a9655c69b33a10e78752cc49b0")
4444
)
4545

4646
func TestCompiler(t *testing.T) {
4747
sol, err := New("")
4848
if err != nil {
49-
t.Skip("solc not found: skip")
49+
t.Skip("solc not found: skip: %v", err)
5050
} else if sol.Version() != solcVersion {
5151
t.Skip("WARNING: skipping due to a newer version of solc found (%v, expect %v)", sol.Version(), solcVersion)
5252
}
@@ -111,4 +111,4 @@ func TestSaveInfo(t *testing.T) {
111111
if cinfohash != infohash {
112112
t.Errorf("content hash for info is incorrect. expected %v, got %v", infohash.Hex(), cinfohash.Hex())
113113
}
114-
}
114+
}

0 commit comments

Comments
 (0)