Commit e0fde022 authored by Felix Lange's avatar Felix Lange Committed by GitHub

common/compiler: remove workaround for solc 0.3.5 stdin bug (#3522)

The crash when compiling stdin was fixed in solc 0.3.6 (released
2016-08-10). While here, simplify the test so it runs with any solc
version.

Fixes #3484. The byte code was different for each run because recent
solc embeds the swarm hash of contract metadata into the code. When
compiling from stdin the name in the metadata is constant.
parent 59b8245b
...@@ -22,9 +22,7 @@ import ( ...@@ -22,9 +22,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os"
"os/exec" "os/exec"
"regexp" "regexp"
"strings" "strings"
...@@ -96,27 +94,16 @@ func CompileSolidityString(solc, source string) (map[string]*Contract, error) { ...@@ -96,27 +94,16 @@ func CompileSolidityString(solc, source string) (map[string]*Contract, error) {
if solc == "" { if solc == "" {
solc = "solc" solc = "solc"
} }
// Write source to a temporary file. Compiling stdin used to be supported args := append(solcParams, "--")
// but seems to produce an exception with solc 0.3.5. cmd := exec.Command(solc, append(args, "-")...)
infile, err := ioutil.TempFile("", "geth-compile-solidity") cmd.Stdin = strings.NewReader(source)
if err != nil { return runsolc(cmd, source)
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
}
return CompileSolidity(solc, infile.Name())
} }
// CompileSolidity compiles all given Solidity source files. // CompileSolidity compiles all given Solidity source files.
func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract, error) { func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract, error) {
if len(sourcefiles) == 0 { if len(sourcefiles) == 0 {
return nil, errors.New("solc: no source ") return nil, errors.New("solc: no source files")
} }
source, err := slurpFiles(sourcefiles) source, err := slurpFiles(sourcefiles)
if err != nil { if err != nil {
...@@ -125,10 +112,13 @@ func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract, ...@@ -125,10 +112,13 @@ func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract,
if solc == "" { if solc == "" {
solc = "solc" solc = "solc"
} }
var stderr, stdout bytes.Buffer
args := append(solcParams, "--") args := append(solcParams, "--")
cmd := exec.Command(solc, append(args, sourcefiles...)...) cmd := exec.Command(solc, append(args, sourcefiles...)...)
return runsolc(cmd, source)
}
func runsolc(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
var stderr, stdout bytes.Buffer
cmd.Stderr = &stderr cmd.Stderr = &stderr
cmd.Stdout = &stdout cmd.Stdout = &stdout
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
......
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"path" "path"
"testing" "testing"
...@@ -27,8 +28,7 @@ import ( ...@@ -27,8 +28,7 @@ import (
) )
const ( const (
supportedSolcVersion = "0.3.5" testSource = `
testSource = `
contract test { contract test {
/// @notice Will multiply ` + "`a`" + ` by 7. /// @notice Will multiply ` + "`a`" + ` by 7.
function multiply(uint a) returns(uint d) { function multiply(uint a) returns(uint d) {
...@@ -36,23 +36,18 @@ contract test { ...@@ -36,23 +36,18 @@ contract test {
} }
} }
` `
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":{}}}` 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 skipUnsupported(t *testing.T) { func skipWithoutSolc(t *testing.T) {
sol, err := SolidityVersion("") if _, err := exec.LookPath("solc"); err != nil {
if err != nil {
t.Skip(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) { func TestCompiler(t *testing.T) {
skipUnsupported(t) skipWithoutSolc(t)
contracts, err := CompileSolidityString("", testSource) contracts, err := CompileSolidityString("", testSource)
if err != nil { if err != nil {
t.Fatalf("error compiling source. result %v: %v", contracts, err) t.Fatalf("error compiling source. result %v: %v", contracts, err)
...@@ -64,19 +59,20 @@ func TestCompiler(t *testing.T) { ...@@ -64,19 +59,20 @@ func TestCompiler(t *testing.T) {
if !ok { if !ok {
t.Fatal("info for contract 'test' not present in result") t.Fatal("info for contract 'test' not present in result")
} }
if c.Code != testCode { if c.Code == "" {
t.Errorf("wrong code: expected\n%s, got\n%s", testCode, c.Code) t.Error("empty code")
} }
if c.Info.Source != testSource { if c.Info.Source != testSource {
t.Error("wrong source") t.Error("wrong source")
} }
if c.Info.CompilerVersion != supportedSolcVersion { if c.Info.CompilerVersion == "" {
t.Errorf("wrong version: expected %q, got %q", supportedSolcVersion, c.Info.CompilerVersion) t.Error("empty version")
} }
} }
func TestCompileError(t *testing.T) { func TestCompileError(t *testing.T) {
skipUnsupported(t) skipWithoutSolc(t)
contracts, err := CompileSolidityString("", testSource[4:]) contracts, err := CompileSolidityString("", testSource[4:])
if err == nil { if err == nil {
t.Errorf("error expected compiling source. got none. result %v", contracts) t.Errorf("error expected compiling source. got none. result %v", contracts)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment