Merge pull request #3786 from fjl/compiler-metadata

common/compiler: add metadata output for solc > 0.4.6
This commit is contained in:
Péter Szilágyi 2017-04-13 16:26:38 +03:00 committed by GitHub
commit 732b75325c
2 changed files with 56 additions and 76 deletions

@ -25,20 +25,11 @@ import (
"io/ioutil" "io/ioutil"
"os/exec" "os/exec"
"regexp" "regexp"
"strconv"
"strings" "strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
) )
var ( var versionRegexp = regexp.MustCompile(`([0-9]+)\.([0-9]+)\.([0-9]+)`)
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
}
)
type Contract struct { type Contract struct {
Code string `json:"code"` Code string `json:"code"`
@ -54,17 +45,33 @@ type ContractInfo struct {
AbiDefinition interface{} `json:"abiDefinition"` AbiDefinition interface{} `json:"abiDefinition"`
UserDoc interface{} `json:"userDoc"` UserDoc interface{} `json:"userDoc"`
DeveloperDoc interface{} `json:"developerDoc"` DeveloperDoc interface{} `json:"developerDoc"`
Metadata string `json:"metadata"`
} }
// Solidity contains information about the solidity compiler. // Solidity contains information about the solidity compiler.
type Solidity struct { type Solidity struct {
Path, Version, FullVersion string Path, Version, FullVersion string
Major, Minor, Patch int
} }
// --combined-output format // --combined-output format
type solcOutput struct { type solcOutput struct {
Contracts map[string]struct{ Bin, Abi, Devdoc, Userdoc string } Contracts map[string]struct {
Version string 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. // SolidityVersion runs solc and parses its version output.
@ -75,13 +82,23 @@ func SolidityVersion(solc string) (*Solidity, error) {
var out bytes.Buffer var out bytes.Buffer
cmd := exec.Command(solc, "--version") cmd := exec.Command(solc, "--version")
cmd.Stdout = &out cmd.Stdout = &out
if err := cmd.Run(); err != nil { err := cmd.Run()
if err != nil {
return nil, err return nil, err
} }
s := &Solidity{ matches := versionRegexp.FindStringSubmatch(out.String())
Path: cmd.Path, if len(matches) != 4 {
FullVersion: out.String(), return nil, fmt.Errorf("can't parse solc version %q", out.String())
Version: versionRegexp.FindString(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
}
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 return s, nil
} }
@ -91,13 +108,14 @@ func CompileSolidityString(solc, source string) (map[string]*Contract, error) {
if len(source) == 0 { if len(source) == 0 {
return nil, errors.New("solc: empty source string") return nil, errors.New("solc: empty source string")
} }
if solc == "" { s, err := SolidityVersion(solc)
solc = "solc" if err != nil {
return nil, err
} }
args := append(solcParams, "--") args := append(s.makeArgs(), "--")
cmd := exec.Command(solc, append(args, "-")...) cmd := exec.Command(s.Path, append(args, "-")...)
cmd.Stdin = strings.NewReader(source) cmd.Stdin = strings.NewReader(source)
return runsolc(cmd, source) return s.run(cmd, source)
} }
// CompileSolidity compiles all given Solidity source files. // CompileSolidity compiles all given Solidity source files.
@ -109,15 +127,16 @@ func CompileSolidity(solc string, sourcefiles ...string) (map[string]*Contract,
if err != nil { if err != nil {
return nil, err return nil, err
} }
if solc == "" { s, err := SolidityVersion(solc)
solc = "solc" if err != nil {
return nil, err
} }
args := append(solcParams, "--") args := append(s.makeArgs(), "--")
cmd := exec.Command(solc, append(args, sourcefiles...)...) cmd := exec.Command(s.Path, append(args, sourcefiles...)...)
return runsolc(cmd, source) 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 var stderr, stdout bytes.Buffer
cmd.Stderr = &stderr cmd.Stderr = &stderr
cmd.Stdout = &stdout cmd.Stdout = &stdout
@ -128,7 +147,6 @@ func runsolc(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
if err := json.Unmarshal(stdout.Bytes(), &output); err != nil { if err := json.Unmarshal(stdout.Bytes(), &output); err != nil {
return nil, err return nil, err
} }
shortVersion := versionRegexp.FindString(output.Version)
// Compilation succeeded, assemble and return the contracts. // Compilation succeeded, assemble and return the contracts.
contracts := make(map[string]*Contract) contracts := make(map[string]*Contract)
@ -151,12 +169,13 @@ func runsolc(cmd *exec.Cmd, source string) (map[string]*Contract, error) {
Info: ContractInfo{ Info: ContractInfo{
Source: source, Source: source,
Language: "Solidity", Language: "Solidity",
LanguageVersion: shortVersion, LanguageVersion: s.Version,
CompilerVersion: shortVersion, CompilerVersion: s.Version,
CompilerOptions: strings.Join(solcParams, " "), CompilerOptions: strings.Join(s.makeArgs(), " "),
AbiDefinition: abi, AbiDefinition: abi,
UserDoc: userdoc, UserDoc: userdoc,
DeveloperDoc: devdoc, DeveloperDoc: devdoc,
Metadata: info.Metadata,
}, },
} }
} }
@ -174,13 +193,3 @@ func slurpFiles(files []string) (string, error) {
} }
return concat.String(), nil 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)
}

@ -17,14 +17,8 @@
package compiler package compiler
import ( import (
"encoding/json"
"io/ioutil"
"os"
"os/exec" "os/exec"
"path"
"testing" "testing"
"github.com/ethereum/go-ethereum/common"
) )
const ( const (
@ -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) { func skipWithoutSolc(t *testing.T) {
@ -57,7 +50,10 @@ func TestCompiler(t *testing.T) {
} }
c, ok := contracts["test"] c, ok := contracts["test"]
if !ok { 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 == "" { if c.Code == "" {
t.Error("empty code") t.Error("empty code")
@ -79,28 +75,3 @@ func TestCompileError(t *testing.T) {
} }
t.Logf("error: %v", err) 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())
}
}