diff options
author | zelig <viktor.tron@gmail.com> | 2015-05-20 11:11:48 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2015-05-20 11:11:48 +0800 |
commit | b0ae84aa0dae65f00492f981bb61887331def2a5 (patch) | |
tree | 369d5c342c6df8b5486c386663806937a4d9def1 /common | |
parent | ea893aca8fbda0d821424ab7509a0a0cfef1f77d (diff) | |
download | dexon-b0ae84aa0dae65f00492f981bb61887331def2a5.tar.gz dexon-b0ae84aa0dae65f00492f981bb61887331def2a5.tar.zst dexon-b0ae84aa0dae65f00492f981bb61887331def2a5.zip |
multiple contract source for solidity compiler: returns contract array if multiple contracts. fixes #1023
Diffstat (limited to 'common')
-rw-r--r-- | common/compiler/solidity.go | 107 | ||||
-rw-r--r-- | common/compiler/solidity_test.go | 16 |
2 files changed, 65 insertions, 58 deletions
diff --git a/common/compiler/solidity.go b/common/compiler/solidity.go index 19d5849fb..0cfd41c95 100644 --- a/common/compiler/solidity.go +++ b/common/compiler/solidity.go @@ -92,7 +92,7 @@ func (sol *Solidity) Version() string { return sol.version } -func (sol *Solidity) Compile(source string) (contract *Contract, err error) { +func (sol *Solidity) Compile(source string) (contracts map[string]*Contract, err error) { if len(source) == 0 { err = fmt.Errorf("empty source") @@ -123,58 +123,61 @@ func (sol *Solidity) Compile(source string) (contract *Contract, err error) { err = fmt.Errorf("solc error: missing code output") return } - if len(matches) > 1 { - err = fmt.Errorf("multi-contract sources are not supported") - return - } - _, file := filepath.Split(matches[0]) - base := strings.Split(file, ".")[0] - - codeFile := filepath.Join(wd, base+".binary") - abiDefinitionFile := filepath.Join(wd, base+".abi") - userDocFile := filepath.Join(wd, base+".docuser") - developerDocFile := filepath.Join(wd, base+".docdev") - code, err := ioutil.ReadFile(codeFile) - if err != nil { - err = fmt.Errorf("error reading compiler output for code: %v", err) - return - } - abiDefinitionJson, err := ioutil.ReadFile(abiDefinitionFile) - if err != nil { - err = fmt.Errorf("error reading compiler output for abiDefinition: %v", err) - return - } - var abiDefinition interface{} - err = json.Unmarshal(abiDefinitionJson, &abiDefinition) - - userDocJson, err := ioutil.ReadFile(userDocFile) - if err != nil { - err = fmt.Errorf("error reading compiler output for userDoc: %v", err) - return - } - var userDoc interface{} - err = json.Unmarshal(userDocJson, &userDoc) - - developerDocJson, err := ioutil.ReadFile(developerDocFile) - if err != nil { - err = fmt.Errorf("error reading compiler output for developerDoc: %v", err) - return - } - var developerDoc interface{} - err = json.Unmarshal(developerDocJson, &developerDoc) - - contract = &Contract{ - Code: "0x" + string(code), - Info: ContractInfo{ - Source: source, - Language: "Solidity", - LanguageVersion: languageVersion, - CompilerVersion: sol.version, - AbiDefinition: abiDefinition, - UserDoc: userDoc, - DeveloperDoc: developerDoc, - }, + contracts = make(map[string]*Contract) + for _, path := range matches { + _, file := filepath.Split(path) + base := strings.Split(file, ".")[0] + + codeFile := filepath.Join(wd, base+".binary") + abiDefinitionFile := filepath.Join(wd, base+".abi") + userDocFile := filepath.Join(wd, base+".docuser") + developerDocFile := filepath.Join(wd, base+".docdev") + + var code, abiDefinitionJson, userDocJson, developerDocJson []byte + code, err = ioutil.ReadFile(codeFile) + if err != nil { + err = fmt.Errorf("error reading compiler output for code: %v", err) + return + } + abiDefinitionJson, err = ioutil.ReadFile(abiDefinitionFile) + if err != nil { + err = fmt.Errorf("error reading compiler output for abiDefinition: %v", err) + return + } + var abiDefinition interface{} + err = json.Unmarshal(abiDefinitionJson, &abiDefinition) + + userDocJson, err = ioutil.ReadFile(userDocFile) + if err != nil { + err = fmt.Errorf("error reading compiler output for userDoc: %v", err) + return + } + var userDoc interface{} + err = json.Unmarshal(userDocJson, &userDoc) + + developerDocJson, err = ioutil.ReadFile(developerDocFile) + if err != nil { + err = fmt.Errorf("error reading compiler output for developerDoc: %v", err) + return + } + var developerDoc interface{} + err = json.Unmarshal(developerDocJson, &developerDoc) + + contract := &Contract{ + Code: "0x" + string(code), + Info: ContractInfo{ + Source: source, + Language: "Solidity", + LanguageVersion: languageVersion, + CompilerVersion: sol.version, + AbiDefinition: abiDefinition, + UserDoc: userDoc, + DeveloperDoc: developerDoc, + }, + } + + contracts[base] = contract } return diff --git a/common/compiler/solidity_test.go b/common/compiler/solidity_test.go index 2b0ca148f..46f733e59 100644 --- a/common/compiler/solidity_test.go +++ b/common/compiler/solidity_test.go @@ -33,14 +33,18 @@ func TestCompiler(t *testing.T) { } else if sol.Version() != solcVersion { t.Logf("WARNING: a newer version of solc found (%v, expect %v)", sol.Version(), solcVersion) } - contract, err := sol.Compile(source) + contracts, err := sol.Compile(source) if err != nil { - t.Errorf("error compiling source. result %v: %v", contract, err) + t.Errorf("error compiling source. result %v: %v", contracts, err) return } - if contract.Code != code { - t.Errorf("wrong code, expected\n%s, got\n%s", code, contract.Code) + if len(contracts) != 1 { + t.Errorf("one contract expected, got\n%s", len(contracts)) + } + + if contracts["test"].Code != code { + t.Errorf("wrong code, expected\n%s, got\n%s", code, contracts["test"].Code) } } @@ -52,9 +56,9 @@ func TestCompileError(t *testing.T) { } else if sol.Version() != solcVersion { t.Logf("WARNING: a newer version of solc found (%v, expect %v)", sol.Version(), solcVersion) } - contract, err := sol.Compile(source[2:]) + contracts, err := sol.Compile(source[2:]) if err == nil { - t.Errorf("error expected compiling source. got none. result %v", contract) + t.Errorf("error expected compiling source. got none. result %v", contracts) return } } |