diff options
author | Felix Lange <fjl@twurst.com> | 2016-06-15 06:36:31 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2016-08-17 23:39:04 +0800 |
commit | 1a9e66915b415cb1ca2bc2680f8fb4ff1883787c (patch) | |
tree | 849620e85ab94fccf1e49a9a2ea9602d700ea7c1 /common/compiler | |
parent | 84d11c19fd246e245906ca7e498a67f6e0c55e1e (diff) | |
download | go-tangerine-1a9e66915b415cb1ca2bc2680f8fb4ff1883787c.tar go-tangerine-1a9e66915b415cb1ca2bc2680f8fb4ff1883787c.tar.gz go-tangerine-1a9e66915b415cb1ca2bc2680f8fb4ff1883787c.tar.bz2 go-tangerine-1a9e66915b415cb1ca2bc2680f8fb4ff1883787c.tar.lz go-tangerine-1a9e66915b415cb1ca2bc2680f8fb4ff1883787c.tar.xz go-tangerine-1a9e66915b415cb1ca2bc2680f8fb4ff1883787c.tar.zst go-tangerine-1a9e66915b415cb1ca2bc2680f8fb4ff1883787c.zip |
common/compiler: simplify solc wrapper
Support for legacy version 0.9.x is gone. The compiler version is no
longer cached. Compilation results (and the version) are read directly
from stdout using the --combined-json flag. As a workaround for
ethereum/solidity#651, source code is written to a temporary file before
compilation.
Integration of solc in package ethapi and cmd/abigen is now much simpler
because the compiler wrapper is no longer passed around as a pointer.
Fixes #2806, accidentally
Diffstat (limited to 'common/compiler')
-rw-r--r-- | common/compiler/solidity.go | 215 | ||||
-rw-r--r-- | common/compiler/solidity_test.go | 82 |
2 files changed, 132 insertions, 165 deletions
diff --git a/common/compiler/solidity.go b/common/compiler/solidity.go index 6a5bfecd8..b682107d9 100644 --- a/common/compiler/solidity.go +++ b/common/compiler/solidity.go @@ -14,6 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. +// Package compiler wraps the Solidity compiler executable (solc). package compiler import ( @@ -21,42 +22,23 @@ import ( "encoding/json" "errors" "fmt" + "io" "io/ioutil" "os" "os/exec" - "path/filepath" "regexp" "strings" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/logger/glog" ) var ( versionRegexp = regexp.MustCompile("[0-9]+\\.[0-9]+\\.[0-9]+") - legacyRegexp = regexp.MustCompile("0\\.(9\\..*|1\\.[01])") - paramsLegacy = []string{ - "--binary", // Request to output the contract in binary (hexadecimal). - "file", // - "--json-abi", // Request to output the contract's JSON ABI interface. - "file", // - "--natspec-user", // Request to output the contract's Natspec user documentation. - "file", // - "--natspec-dev", // Request to output the contract's Natspec developer documentation. - "file", - "--add-std", - "1", - } - paramsNew = []string{ - "--bin", // Request to output the contract in binary (hexadecimal). - "--abi", // Request to output the contract's JSON ABI interface. - "--userdoc", // Request to output the contract's Natspec user documentation. - "--devdoc", // Request to output the contract's Natspec developer documentation. + solcParams = []string{ + "--combined-json", "bin,abi,userdoc,devdoc", "--add-std", // include standard lib contracts "--optimize", // code optimizer switched on - "-o", // output directory } ) @@ -76,135 +58,112 @@ type ContractInfo struct { DeveloperDoc interface{} `json:"developerDoc"` } +// Solidity contains information about the solidity compiler. type Solidity struct { - solcPath string - version string - fullVersion string - legacy bool + Path, Version, FullVersion string } -func New(solcPath string) (sol *Solidity, err error) { - // set default solc - if len(solcPath) == 0 { - solcPath = "solc" - } - solcPath, err = exec.LookPath(solcPath) - if err != nil { - return - } +// --combined-output format +type solcOutput struct { + Contracts map[string]struct{ Bin, Abi, Devdoc, Userdoc string } + Version string +} - cmd := exec.Command(solcPath, "--version") +// SolidityVersion runs solc and parses its version output. +func SolidityVersion(solc string) (*Solidity, error) { + if solc == "" { + solc = "solc" + } var out bytes.Buffer + cmd := exec.Command(solc, "--version") cmd.Stdout = &out - err = cmd.Run() - if err != nil { - return + if err := cmd.Run(); err != nil { + return nil, err } - - fullVersion := out.String() - version := versionRegexp.FindString(fullVersion) - legacy := legacyRegexp.MatchString(version) - - sol = &Solidity{ - solcPath: solcPath, - version: version, - fullVersion: fullVersion, - legacy: legacy, + s := &Solidity{ + Path: cmd.Path, + FullVersion: out.String(), + Version: versionRegexp.FindString(out.String()), } - glog.V(logger.Info).Infoln(sol.Info()) - return -} - -func (sol *Solidity) Info() string { - return fmt.Sprintf("%s\npath: %s", sol.fullVersion, sol.solcPath) -} - -func (sol *Solidity) Version() string { - return sol.version + return s, nil } -// Compile builds and returns all the contracts contained within a source string. -func (sol *Solidity) Compile(source string) (map[string]*Contract, error) { - // Short circuit if no source code was specified +// CompileSolidityString builds and returns all the contracts contained within a source string. +func CompileSolidityString(solc, source string) (map[string]*Contract, error) { if len(source) == 0 { return nil, errors.New("solc: empty source string") } - // Create a safe place to dump compilation output - wd, err := ioutil.TempDir("", "solc") + if solc == "" { + solc = "solc" + } + // Write source to a temporary file. Compiling stdin used to be supported + // but seems to produce an exception with solc 0.3.5. + infile, err := ioutil.TempFile("", "geth-compile-solidity") if err != nil { - return nil, fmt.Errorf("solc: failed to create temporary build folder: %v", err) + return nil, err + } + defer os.Remove(infile.Name()) + if _, err := io.WriteString(infile, source); err != nil { + return nil, err + } + if err := infile.Close(); err != nil { + return nil, err } - defer os.RemoveAll(wd) - // Assemble the compiler command, change to the temp folder and capture any errors - stderr := new(bytes.Buffer) + return CompileSolidity(solc, infile.Name()) +} - var params []string - if sol.legacy { - params = paramsLegacy - } else { - params = paramsNew - params = append(params, wd) +// CompileSolidity compiles all given Solidity source files. +func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract, error) { + if len(sourcefiles) == 0 { + return nil, errors.New("solc: no source ") + } + source, err := slurpFiles(sourcefiles) + if err != nil { + return nil, err + } + if solc == "" { + solc = "solc" } - compilerOptions := strings.Join(params, " ") - - cmd := exec.Command(sol.solcPath, params...) - cmd.Stdin = strings.NewReader(source) - cmd.Stderr = stderr + var stderr, stdout bytes.Buffer + args := append(solcParams, "--") + cmd := exec.Command(solc, append(args, sourcefiles...)...) + cmd.Stderr = &stderr + cmd.Stdout = &stdout if err := cmd.Run(); err != nil { - return nil, fmt.Errorf("solc: %v\n%s", err, string(stderr.Bytes())) + return nil, fmt.Errorf("solc: %v\n%s", err, stderr.Bytes()) } - // Sanity check that something was actually built - matches, _ := filepath.Glob(filepath.Join(wd, "*.bin*")) - if len(matches) < 1 { - return nil, fmt.Errorf("solc: no build results found") + var output solcOutput + if err := json.Unmarshal(stdout.Bytes(), &output); err != nil { + return nil, err } - // Compilation succeeded, assemble and return the contracts - contracts := make(map[string]*Contract) - for _, path := range matches { - _, file := filepath.Split(path) - base := strings.Split(file, ".")[0] - - // Parse the individual compilation results (code binary, ABI definitions, user and dev docs) - var binary []byte - binext := ".bin" - if sol.legacy { - binext = ".binary" - } - if binary, err = ioutil.ReadFile(filepath.Join(wd, base+binext)); err != nil { - return nil, fmt.Errorf("solc: error reading compiler output for code: %v", err) - } + shortVersion := versionRegexp.FindString(output.Version) + // Compilation succeeded, assemble and return the contracts. + contracts := make(map[string]*Contract) + for name, info := range output.Contracts { + // Parse the individual compilation results. var abi interface{} - if blob, err := ioutil.ReadFile(filepath.Join(wd, base+".abi")); err != nil { - return nil, fmt.Errorf("solc: error reading abi definition: %v", err) - } else if err = json.Unmarshal(blob, &abi); err != nil { - return nil, fmt.Errorf("solc: error parsing abi definition: %v", err) + if err := json.Unmarshal([]byte(info.Abi), &abi); err != nil { + return nil, fmt.Errorf("solc: error reading abi definition (%v)", err) } - var userdoc interface{} - if blob, err := ioutil.ReadFile(filepath.Join(wd, base+".docuser")); err != nil { + if err := json.Unmarshal([]byte(info.Userdoc), &userdoc); err != nil { return nil, fmt.Errorf("solc: error reading user doc: %v", err) - } else if err = json.Unmarshal(blob, &userdoc); err != nil { - return nil, fmt.Errorf("solc: error parsing user doc: %v", err) } - var devdoc interface{} - if blob, err := ioutil.ReadFile(filepath.Join(wd, base+".docdev")); err != nil { + if err := json.Unmarshal([]byte(info.Devdoc), &devdoc); err != nil { return nil, fmt.Errorf("solc: error reading dev doc: %v", err) - } else if err = json.Unmarshal(blob, &devdoc); err != nil { - return nil, fmt.Errorf("solc: error parsing dev doc: %v", err) } - // Assemble the final contract - contracts[base] = &Contract{ - Code: "0x" + string(binary), + contracts[name] = &Contract{ + Code: "0x" + info.Bin, Info: ContractInfo{ Source: source, Language: "Solidity", - LanguageVersion: sol.version, - CompilerVersion: sol.version, - CompilerOptions: compilerOptions, + LanguageVersion: shortVersion, + CompilerVersion: shortVersion, + CompilerOptions: strings.Join(solcParams, " "), AbiDefinition: abi, UserDoc: userdoc, DeveloperDoc: devdoc, @@ -214,12 +173,24 @@ func (sol *Solidity) Compile(source string) (map[string]*Contract, error) { return contracts, nil } -func SaveInfo(info *ContractInfo, filename string) (contenthash common.Hash, err error) { +func slurpFiles(files []string) (string, error) { + var concat bytes.Buffer + for _, file := range files { + content, err := ioutil.ReadFile(file) + if err != nil { + return "", err + } + concat.Write(content) + } + return concat.String(), nil +} + +// SaveInfo serializes info to the given file and returns its Keccak256 hash. +func SaveInfo(info *ContractInfo, filename string) (common.Hash, error) { infojson, err := json.Marshal(info) if err != nil { - return + return common.Hash{}, err } - contenthash = common.BytesToHash(crypto.Keccak256(infojson)) - err = ioutil.WriteFile(filename, infojson, 0600) - return + contenthash := common.BytesToHash(crypto.Keccak256(infojson)) + return contenthash, ioutil.WriteFile(filename, infojson, 0600) } diff --git a/common/compiler/solidity_test.go b/common/compiler/solidity_test.go index 7109b1ec4..e4d96bd01 100644 --- a/common/compiler/solidity_test.go +++ b/common/compiler/solidity_test.go @@ -26,10 +26,9 @@ import ( "github.com/ethereum/go-ethereum/common" ) -const solcVersion = "0.1.1" - -var ( - source = ` +const ( + supportedSolcVersion = "0.3.5" + testSource = ` contract test { /// @notice Will multiply ` + "`a`" + ` by 7. function multiply(uint a) returns(uint d) { @@ -37,61 +36,57 @@ contract test { } } ` - code = "0x6060604052606d8060116000396000f30060606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b6046600480359060200150605c565b6040518082815260200191505060405180910390f35b60006007820290506068565b91905056" - 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":{}}}` - - infohash = common.HexToHash("0x9f3803735e7f16120c5a140ab3f02121fd3533a9655c69b33a10e78752cc49b0") + testCode = "0x6060604052602a8060106000396000f3606060405260e060020a6000350463c6888fa18114601a575b005b6007600435026060908152602090f3" + 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 --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":{}}}` ) -func TestCompiler(t *testing.T) { - sol, err := New("") +func skipUnsupported(t *testing.T) { + sol, err := SolidityVersion("") if err != nil { - t.Skipf("solc not found: %v", err) - } else if sol.Version() != solcVersion { - t.Skipf("WARNING: a newer version of solc found (%v, expect %v)", sol.Version(), solcVersion) - } - contracts, err := sol.Compile(source) - if err != nil { - t.Errorf("error compiling source. result %v: %v", contracts, err) + t.Skip(err) return } + if sol.Version != supportedSolcVersion { + t.Skipf("unsupported version of solc found (%v, expect %v)", sol.Version, supportedSolcVersion) + } +} +func TestCompiler(t *testing.T) { + skipUnsupported(t) + contracts, err := CompileSolidityString("", testSource) + if err != nil { + t.Fatalf("error compiling source. result %v: %v", contracts, err) + } if len(contracts) != 1 { t.Errorf("one contract expected, got %d", len(contracts)) } - - if contracts["test"].Code != code { - t.Errorf("wrong code, expected\n%s, got\n%s", code, contracts["test"].Code) + c, ok := contracts["test"] + if !ok { + t.Fatal("info for contract 'test' not present in result") + } + if c.Code != testCode { + t.Errorf("wrong code: expected\n%s, got\n%s", testCode, c.Code) + } + if c.Info.Source != testSource { + t.Error("wrong source") + } + if c.Info.CompilerVersion != supportedSolcVersion { + t.Errorf("wrong version: expected %q, got %q", supportedSolcVersion, c.Info.CompilerVersion) } - } func TestCompileError(t *testing.T) { - sol, err := New("") - if err != nil || sol.version != solcVersion { - t.Skip("solc not found: skip") - } else if sol.Version() != solcVersion { - t.Skip("WARNING: skipping due to a newer version of solc found (%v, expect %v)", sol.Version(), solcVersion) - } - contracts, err := sol.Compile(source[2:]) + skipUnsupported(t) + contracts, err := CompileSolidityString("", testSource[4:]) if err == nil { t.Errorf("error expected compiling source. got none. result %v", contracts) - return - } -} - -func TestNoCompiler(t *testing.T) { - _, err := New("/path/to/solc") - if err != nil { - t.Logf("solidity quits with error: %v", err) - } else { - t.Errorf("no solc installed, but got no error") } + t.Logf("error: %v", err) } func TestSaveInfo(t *testing.T) { var cinfo ContractInfo - err := json.Unmarshal([]byte(info), &cinfo) + err := json.Unmarshal([]byte(testInfo), &cinfo) if err != nil { t.Errorf("%v", err) } @@ -105,10 +100,11 @@ func TestSaveInfo(t *testing.T) { if err != nil { t.Errorf("error reading '%v': %v", filename, err) } - if string(got) != info { - t.Errorf("incorrect info.json extracted, expected:\n%s\ngot\n%s", info, string(got)) + if string(got) != testInfo { + t.Errorf("incorrect info.json extracted, expected:\n%s\ngot\n%s", testInfo, string(got)) } - if cinfohash != infohash { - t.Errorf("content hash for info is incorrect. expected %v, got %v", infohash.Hex(), cinfohash.Hex()) + wantHash := common.HexToHash("0x9f3803735e7f16120c5a140ab3f02121fd3533a9655c69b33a10e78752cc49b0") + if cinfohash != wantHash { + t.Errorf("content hash for info is incorrect. expected %v, got %v", wantHash.Hex(), cinfohash.Hex()) } } |