Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common/compiler: add metadata output for solc > 0.4.6 #3786

Merged
merged 1 commit into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 52 additions & 43 deletions common/compiler/solidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,11 @@ import (
"io/ioutil"
"os/exec"
"regexp"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

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

type Contract struct {
Code string `json:"code"`
Expand All @@ -54,17 +45,33 @@ type ContractInfo struct {
AbiDefinition interface{} `json:"abiDefinition"`
UserDoc interface{} `json:"userDoc"`
DeveloperDoc interface{} `json:"developerDoc"`
Metadata string `json:"metadata"`
}

// Solidity contains information about the solidity compiler.
type Solidity struct {
Path, Version, FullVersion string
Major, Minor, Patch int
}

// --combined-output format
type solcOutput struct {
Contracts map[string]struct{ Bin, Abi, Devdoc, Userdoc string }
Version string
Contracts map[string]struct {
Bin, Abi, Devdoc, Userdoc, Metadata string
}
Version string
}

func (s *Solidity) makeArgs() []string {
p := []string{
"--combined-json", "bin,abi,userdoc,devdoc",
"--add-std", // include standard lib contracts
"--optimize", // code optimizer switched on
}
if s.Major > 0 || s.Minor > 4 || s.Patch > 6 {
p[1] += ",metadata"
}
return p
}

// SolidityVersion runs solc and parses its version output.
Expand All @@ -75,13 +82,23 @@ func SolidityVersion(solc string) (*Solidity, error) {
var out bytes.Buffer
cmd := exec.Command(solc, "--version")
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
err := cmd.Run()
if err != nil {
return nil, err
}
matches := versionRegexp.FindStringSubmatch(out.String())
if len(matches) != 4 {
return nil, fmt.Errorf("can't parse solc version %q", out.String())
}
s := &Solidity{Path: cmd.Path, FullVersion: out.String(), Version: matches[0]}
if s.Major, err = strconv.Atoi(matches[1]); err != nil {
return nil, err
}
s := &Solidity{
Path: cmd.Path,
FullVersion: out.String(),
Version: versionRegexp.FindString(out.String()),
if s.Minor, err = strconv.Atoi(matches[2]); err != nil {
return nil, err
}
if s.Patch, err = strconv.Atoi(matches[3]); err != nil {
return nil, err
}
return s, nil
}
Expand All @@ -91,13 +108,14 @@ func CompileSolidityString(solc, source string) (map[string]*Contract, error) {
if len(source) == 0 {
return nil, errors.New("solc: empty source string")
}
if solc == "" {
solc = "solc"
s, err := SolidityVersion(solc)
if err != nil {
return nil, err
}
args := append(solcParams, "--")
cmd := exec.Command(solc, append(args, "-")...)
args := append(s.makeArgs(), "--")
cmd := exec.Command(s.Path, append(args, "-")...)
cmd.Stdin = strings.NewReader(source)
return runsolc(cmd, source)
return s.run(cmd, source)
}

// CompileSolidity compiles all given Solidity source files.
Expand All @@ -109,15 +127,16 @@ func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract,
if err != nil {
return nil, err
}
if solc == "" {
solc = "solc"
s, err := SolidityVersion(solc)
if err != nil {
return nil, err
}
args := append(solcParams, "--")
cmd := exec.Command(solc, append(args, sourcefiles...)...)
return runsolc(cmd, source)
args := append(s.makeArgs(), "--")
cmd := exec.Command(s.Path, append(args, sourcefiles...)...)
return s.run(cmd, source)
}

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

// Compilation succeeded, assemble and return the contracts.
contracts := make(map[string]*Contract)
Expand All @@ -151,12 +169,13 @@ func runsolc(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
Info: ContractInfo{
Source: source,
Language: "Solidity",
LanguageVersion: shortVersion,
CompilerVersion: shortVersion,
CompilerOptions: strings.Join(solcParams, " "),
LanguageVersion: s.Version,
CompilerVersion: s.Version,
CompilerOptions: strings.Join(s.makeArgs(), " "),
AbiDefinition: abi,
UserDoc: userdoc,
DeveloperDoc: devdoc,
Metadata: info.Metadata,
},
}
}
Expand All @@ -174,13 +193,3 @@ func slurpFiles(files []string) (string, error) {
}
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 common.Hash{}, err
}
contenthash := common.BytesToHash(crypto.Keccak256(infojson))
return contenthash, ioutil.WriteFile(filename, infojson, 0600)
}
37 changes: 4 additions & 33 deletions common/compiler/solidity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@
package compiler

import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path"
"testing"

"github.com/ethereum/go-ethereum/common"
)

const (
Expand All @@ -36,7 +30,6 @@ contract test {
}
}
`
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":{}}}`
)

func skipWithoutSolc(t *testing.T) {
Expand All @@ -57,7 +50,10 @@ func TestCompiler(t *testing.T) {
}
c, ok := contracts["test"]
if !ok {
t.Fatal("info for contract 'test' not present in result")
c, ok = contracts["<stdin>:test"]
if !ok {
t.Fatal("info for contract 'test' not present in result")
}
}
if c.Code == "" {
t.Error("empty code")
Expand All @@ -79,28 +75,3 @@ func TestCompileError(t *testing.T) {
}
t.Logf("error: %v", err)
}

func TestSaveInfo(t *testing.T) {
var cinfo ContractInfo
err := json.Unmarshal([]byte(testInfo), &cinfo)
if err != nil {
t.Errorf("%v", err)
}
filename := path.Join(os.TempDir(), "solctest.info.json")
os.Remove(filename)
cinfohash, err := SaveInfo(&cinfo, filename)
if err != nil {
t.Errorf("error extracting info: %v", err)
}
got, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("error reading '%v': %v", filename, err)
}
if string(got) != testInfo {
t.Errorf("incorrect info.json extracted, expected:\n%s\ngot\n%s", testInfo, string(got))
}
wantHash := common.HexToHash("0x22450a77f0c3ff7a395948d07bc1456881226a1b6325f4189cb5f1254a824080")
if cinfohash != wantHash {
t.Errorf("content hash for info is incorrect. expected %v, got %v", wantHash.Hex(), cinfohash.Hex())
}
}