Skip to content

Commit

Permalink
Probably need fix for code coverage...
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Aug 28, 2023
1 parent cf6af4b commit d94b610
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 52 deletions.
83 changes: 57 additions & 26 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (v *Compiler) GetSources() string {

// Compile compiles the Solidity sources using the configured compiler version and arguments.
// It returns the compilation results or an error if the compilation fails.
func (v *Compiler) Compile() ([]*CompilerResults, error) {
func (v *Compiler) Compile() (*CompilerResults, error) {
compilerVersion := v.GetCompilerVersion()
if compilerVersion == "" {
return nil, fmt.Errorf("no compiler version specified")
Expand Down Expand Up @@ -125,12 +125,12 @@ func (v *Compiler) Compile() ([]*CompilerResults, error) {
}

// Construct the CompilerResults structure with errors and warnings.
results := &CompilerResults{
results := &CompilerResult{
RequestedVersion: compilerVersion,
Errors: errors,
Warnings: warnings,
}
return []*CompilerResults{results}, err
return &CompilerResults{Results: []*CompilerResult{results}}, err
}

if v.config.JsonConfig != nil {
Expand All @@ -143,7 +143,7 @@ func (v *Compiler) Compile() ([]*CompilerResults, error) {
// resultsFromSimple parses the output from the solc compiler when the output is in a simple format.
// It extracts the compilation details such as bytecode, ABI, and any errors or warnings.
// The method returns a slice of CompilerResults or an error if the output cannot be parsed.
func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) ([]*CompilerResults, error) {
func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) (*CompilerResults, error) {
// Parse the output
var compilationOutput struct {
Contracts map[string]struct {
Expand All @@ -168,7 +168,7 @@ func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) (
}
}

var results []*CompilerResults
var results []*CompilerResult

for key, output := range compilationOutput.Contracts {
isEntryContract := false
Expand All @@ -181,7 +181,7 @@ func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) (
return nil, err
}

results = append(results, &CompilerResults{
results = append(results, &CompilerResult{
IsEntryContract: isEntryContract,
RequestedVersion: compilerVersion,
CompilerVersion: compilationOutput.Version,
Expand All @@ -193,15 +193,14 @@ func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) (
})
}

return results, nil
return &CompilerResults{Results: results}, nil
}

// resultsFromJson parses the output from the solc compiler when the output is in a JSON format.
// It extracts detailed compilation information including bytecode, ABI, opcodes, and metadata.
// Additionally, it separates any errors and warnings from the compilation process.
// The method returns a slice of CompilerResults or an error if the output cannot be parsed.
func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) ([]*CompilerResults, error) {
// Parse the output
func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) (*CompilerResults, error) {
var compilationOutput struct {
Contracts map[string]map[string]struct {
Abi interface{} `json:"abi"`
Expand All @@ -213,6 +212,13 @@ func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) ([]
Opcodes string `json:"opcodes"`
SourceMap string `json:"sourceMap"`
} `json:"bytecode"`
DeployedBytecode struct {
GeneratedSources []interface{} `json:"generatedSources"`
LinkReferences map[string]interface{} `json:"linkReferences"`
Object string `json:"object"`
Opcodes string `json:"opcodes"`
SourceMap string `json:"sourceMap"`
} `json:"deployedBytecode"`
} `json:"evm"`
Metadata string `json:"metadata"`
} `json:"contracts"`
Expand All @@ -224,7 +230,7 @@ func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) ([]
return nil, err
}

var results []*CompilerResults
var results []*CompilerResult

for key := range compilationOutput.Contracts {
for key, output := range compilationOutput.Contracts[key] {
Expand All @@ -238,10 +244,11 @@ func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) ([]
return nil, err
}

results = append(results, &CompilerResults{
results = append(results, &CompilerResult{
IsEntryContract: isEntryContract,
RequestedVersion: compilerVersion,
Bytecode: output.Evm.Bytecode.Object,
DeployedBytecode: output.Evm.DeployedBytecode.Object,
ABI: string(abi),
Opcodes: output.Evm.Bytecode.Opcodes,
ContractName: key,
Expand All @@ -252,13 +259,13 @@ func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) ([]
}

if len(compilationOutput.Errors) > 0 {
results = append(results, &CompilerResults{
results = append(results, &CompilerResult{
RequestedVersion: compilerVersion,
Errors: compilationOutput.Errors,
})
}

return results, nil
return &CompilerResults{Results: results}, nil
}

// CompilationError represents a compilation error.
Expand All @@ -270,13 +277,32 @@ type CompilationError struct {
Type string `json:"type"`
}

// CompilerResults represents the results of a solc compilation.
type CompilerResults struct {
Results []*CompilerResult `json:"results"`
}

func (cr *CompilerResults) GetResults() []*CompilerResult {
return cr.Results
}

func (cr *CompilerResults) GetEntryContract() *CompilerResult {
for _, result := range cr.Results {
if result.IsEntry() {
return result
}
}

return nil
}

// CompilerResults represents the results of a solc compilation.
type CompilerResult struct {
IsEntryContract bool `json:"is_entry_contract"`
RequestedVersion string `json:"requested_version"`
CompilerVersion string `json:"compiler_version"`
ContractName string `json:"contract_name"`
Bytecode string `json:"bytecode"`
DeployedBytecode string `json:"deployedBytecode"`
ABI string `json:"abi"`
Opcodes string `json:"opcodes"`
Metadata string `json:"metadata"`
Expand All @@ -285,22 +311,22 @@ type CompilerResults struct {
}

// IsEntry returns true if the compiled contract is the entry contract.
func (v *CompilerResults) IsEntry() bool {
func (v *CompilerResult) IsEntry() bool {
return v.IsEntryContract
}

// GetOpcodes returns the compiled contract's opcodes.
func (v *CompilerResults) GetOpcodes() string {
func (v *CompilerResult) GetOpcodes() string {
return v.Opcodes
}

// GetMetadata returns the compiled contract's metadata.
func (v *CompilerResults) GetMetadata() string {
func (v *CompilerResult) GetMetadata() string {
return v.Metadata
}

// HasErrors returns true if there are compilation errors.
func (v *CompilerResults) HasErrors() bool {
func (v *CompilerResult) HasErrors() bool {
if v == nil {
return false
}
Expand All @@ -309,7 +335,7 @@ func (v *CompilerResults) HasErrors() bool {
}

// HasWarnings returns true if there are compilation warnings.
func (v *CompilerResults) HasWarnings() bool {
func (v *CompilerResult) HasWarnings() bool {
if v == nil {
return false
}
Expand All @@ -318,36 +344,41 @@ func (v *CompilerResults) HasWarnings() bool {
}

// GetErrors returns the compilation errors.
func (v *CompilerResults) GetErrors() []CompilationError {
func (v *CompilerResult) GetErrors() []CompilationError {
return v.Errors
}

// GetWarnings returns the compilation warnings.
func (v *CompilerResults) GetWarnings() []CompilationError {
func (v *CompilerResult) GetWarnings() []CompilationError {
return v.Warnings
}

// GetABI returns the compiled contract's ABI (Application Binary Interface) in JSON format.
func (v *CompilerResults) GetABI() string {
func (v *CompilerResult) GetABI() string {
return v.ABI
}

// GetBytecode returns the compiled contract's bytecode.
func (v *CompilerResults) GetBytecode() string {
func (v *CompilerResult) GetBytecode() string {
return v.Bytecode
}

// GetDeployedBytecode returns the compiled contract's deployed bytecode.
func (v *CompilerResult) GetDeployedBytecode() string {
return v.DeployedBytecode
}

// GetContractName returns the name of the compiled contract.
func (v *CompilerResults) GetContractName() string {
func (v *CompilerResult) GetContractName() string {
return v.ContractName
}

// GetRequestedVersion returns the requested compiler version used for compilation.
func (v *CompilerResults) GetRequestedVersion() string {
func (v *CompilerResult) GetRequestedVersion() string {
return v.RequestedVersion
}

// GetCompilerVersion returns the actual compiler version used for compilation.
func (v *CompilerResults) GetCompilerVersion() string {
func (v *CompilerResult) GetCompilerVersion() string {
return v.CompilerVersion
}
42 changes: 18 additions & 24 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestCompiler(t *testing.T) {

compilerResults, err := compiler.Compile()
if testCase.wantCompileErr {
for _, result := range compilerResults {
for _, result := range compilerResults.GetResults() {
assert.True(t, result.HasErrors())
assert.False(t, result.HasWarnings())
assert.GreaterOrEqual(t, len(result.GetWarnings()), 0)
Expand All @@ -225,7 +225,7 @@ func TestCompiler(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, compilerResults)

for _, result := range compilerResults {
for _, result := range compilerResults.GetResults() {
assert.NotEmpty(t, result.GetRequestedVersion())
assert.NotEmpty(t, result.GetCompilerVersion())
assert.NotEmpty(t, result.GetBytecode())
Expand Down Expand Up @@ -293,12 +293,12 @@ func TestCompilerFromSolc(t *testing.T) {
name: "Invalid Source",
source: `// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedData;
function set(uint256 x) public {
}`,
wantCompileErr: true,
compilerConfig: func() *CompilerConfig {
Expand All @@ -314,14 +314,14 @@ func TestCompilerFromSolc(t *testing.T) {
name: "Invalid Compiler Version",
source: `// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
Expand All @@ -340,14 +340,14 @@ func TestCompilerFromSolc(t *testing.T) {
name: "Invalid Compiler Config Version",
source: `// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
Expand All @@ -364,14 +364,14 @@ func TestCompilerFromSolc(t *testing.T) {
name: "Invalid Compiler Solc Instance",
source: `// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
Expand Down Expand Up @@ -414,20 +414,14 @@ func TestCompilerFromSolc(t *testing.T) {

compilerResults, err := solc.Compile(context.TODO(), testCase.source, testCase.compilerConfig)
if testCase.wantCompileErr {
for _, result := range compilerResults {
assert.True(t, result.HasErrors())
assert.False(t, result.HasWarnings())
assert.GreaterOrEqual(t, len(result.GetWarnings()), 0)
assert.GreaterOrEqual(t, len(result.GetErrors()), 1)
}

assert.Nil(t, compilerResults)
return
}

assert.NoError(t, err)
assert.NotNil(t, compilerResults)

for _, result := range compilerResults {
for _, result := range compilerResults.GetResults() {
assert.NotEmpty(t, result.GetRequestedVersion())
assert.NotEmpty(t, result.GetCompilerVersion())
assert.NotEmpty(t, result.GetBytecode())
Expand Down Expand Up @@ -550,7 +544,7 @@ func TestCompilerWithJSON(t *testing.T) {

compilerResults, err := compiler.Compile()
if testCase.wantCompileErr {
for _, result := range compilerResults {
for _, result := range compilerResults.GetResults() {
assert.True(t, result.HasErrors())
assert.False(t, result.HasWarnings())
assert.GreaterOrEqual(t, len(result.GetWarnings()), 0)
Expand All @@ -563,7 +557,7 @@ func TestCompilerWithJSON(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, compilerResults)

for _, result := range compilerResults {
for _, result := range compilerResults.GetResults() {
assert.NotNil(t, result.IsEntry())
assert.NotEmpty(t, result.GetRequestedVersion())
assert.NotEmpty(t, result.GetBytecode())
Expand Down
2 changes: 1 addition & 1 deletion releases/releases.json
Git LFS file not shown
2 changes: 1 addition & 1 deletion solc.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *Solc) GetHTTPClient() *http.Client {
}

// Compile compiles the provided Solidity source code using the specified compiler configuration.
func (s *Solc) Compile(ctx context.Context, source string, config *CompilerConfig) ([]*CompilerResults, error) {
func (s *Solc) Compile(ctx context.Context, source string, config *CompilerConfig) (*CompilerResults, error) {
compiler, err := NewCompiler(ctx, s, config, source)
if err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ func (s *Solc) SyncBinaries(versions []Version, limitVersion string) error {
return nil
}

// IsSynced checks if the local cache is synced with the remote releases.
func (s *Solc) IsSynced() bool {
return time.Since(s.lastSync) < time.Duration(6*time.Hour)
}

// Sync fetches the available Solidity versions from GitHub, saves them to releases.json, reloads the local cache,
// and downloads all the binaries for the distribution for future use.
func (s *Solc) Sync() error {
Expand Down

0 comments on commit d94b610

Please sign in to comment.