Skip to content

Commit

Permalink
Merge pull request #7 from 0x19/pre-release-fixes
Browse files Browse the repository at this point in the history
Pre release v1.0.1 fixes
  • Loading branch information
0x19 committed Aug 28, 2023
2 parents 01bb1be + bfa6649 commit cf6af4b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
71 changes: 38 additions & 33 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ func (v *Compiler) Compile() ([]*CompilerResults, error) {
zap.String("version", compilerVersion),
zap.String("stderr", stderr.String()),
)
var errors []string
var warnings []string
var errors []CompilationError
var warnings []CompilationError

// Parsing the error message to extract line and column information.
errorMessage := stderr.String()
if strings.Contains(errorMessage, "Error:") {
errors = append(errors, errorMessage)
errors = append(errors, CompilationError{Message: errorMessage})
} else if strings.HasPrefix(errorMessage, "Warning:") {
warnings = append(warnings, errorMessage)
warnings = append(warnings, CompilationError{Message: errorMessage})
}

// Construct the CompilerResults structure with errors and warnings.
Expand Down Expand Up @@ -159,12 +159,12 @@ func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) (
}

// Separate errors and warnings
var errors, warnings []string
var errors, warnings []CompilationError
for _, msg := range compilationOutput.Errors {
if strings.Contains(msg, "Warning:") {
warnings = append(warnings, msg)
warnings = append(warnings, CompilationError{Message: msg})
} else {
errors = append(errors, msg)
errors = append(errors, CompilationError{Message: msg})
}
}

Expand Down Expand Up @@ -216,24 +216,14 @@ func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) ([]
} `json:"evm"`
Metadata string `json:"metadata"`
} `json:"contracts"`
Errors []string `json:"errors"`
Version string `json:"version"`
Errors []CompilationError `json:"errors"`
Version string `json:"version"`
}

if err := json.Unmarshal(out.Bytes(), &compilationOutput); err != nil {
return nil, err
}

// Separate errors and warnings
var errors, warnings []string
for _, msg := range compilationOutput.Errors {
if strings.Contains(msg, "Warning:") {
warnings = append(warnings, msg)
} else {
errors = append(errors, msg)
}
}

var results []*CompilerResults

for key := range compilationOutput.Contracts {
Expand All @@ -255,28 +245,43 @@ func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) ([]
ABI: string(abi),
Opcodes: output.Evm.Bytecode.Opcodes,
ContractName: key,
Errors: errors,
Warnings: warnings,
Errors: compilationOutput.Errors,
Metadata: output.Metadata,
})
}
}

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

return results, nil
}

// CompilationError represents a compilation error.
type CompilationError struct {
Component string `json:"component"`
Formatted string `json:"formattedMessage"`
Message string `json:"message"`
Severity string `json:"severity"`
Type string `json:"type"`
}

// CompilerResults represents the results of a solc compilation.
type CompilerResults 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"`
ABI string `json:"abi"`
Opcodes string `json:"opcodes"`
Metadata string `json:"metadata"`
Errors []string `json:"errors"`
Warnings []string `json:"warnings"`
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"`
ABI string `json:"abi"`
Opcodes string `json:"opcodes"`
Metadata string `json:"metadata"`
Errors []CompilationError `json:"errors"`
Warnings []CompilationError `json:"warnings"`
}

// IsEntry returns true if the compiled contract is the entry contract.
Expand Down Expand Up @@ -313,12 +318,12 @@ func (v *CompilerResults) HasWarnings() bool {
}

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

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

Expand Down
15 changes: 8 additions & 7 deletions compiler_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,20 @@ func NewCompilerConfigFromJSON(compilerVersion string, entrySourceName string, c
toReturn := &CompilerConfig{
EntrySourceName: entrySourceName,
CompilerVersion: compilerVersion,
Arguments: []string{
"--standard-json", // Output to stdout.
},
JsonConfig: config,
Arguments: []string{"--standard-json"},
JsonConfig: config,
}

if _, err := toReturn.SanitizeArguments(toReturn.Arguments); err != nil {
return nil, err
}

/* if err := toReturn.Validate(); err != nil {
return nil, err
} */
/*
TODO: Validation at this point for the JSON config is not done.
It's assumed that the caller has already validated the JSON config.
if err := toReturn.Validate(); err != nil {
return nil, err
} */

return toReturn, nil
}
Expand Down
3 changes: 3 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,13 @@ func TestCompilerWithJSON(t *testing.T) {
assert.NotNil(t, compilerResults)

for _, result := range compilerResults {
assert.NotNil(t, result.IsEntry())
assert.NotEmpty(t, result.GetRequestedVersion())
assert.NotEmpty(t, result.GetBytecode())
assert.NotEmpty(t, result.GetABI())
assert.NotEmpty(t, result.GetContractName())
assert.NotEmpty(t, result.GetOpcodes())
assert.NotEmpty(t, result.GetMetadata())
assert.GreaterOrEqual(t, len(result.GetWarnings()), 0)
assert.GreaterOrEqual(t, len(result.GetErrors()), 0)
}
Expand Down
2 changes: 1 addition & 1 deletion releases/releases.json
Git LFS file not shown

0 comments on commit cf6af4b

Please sign in to comment.