Skip to content

Commit

Permalink
Download from external source early, and retyr when failed
Browse files Browse the repository at this point in the history
  • Loading branch information
faustocarva committed Mar 30, 2024
1 parent 443fb5e commit dc1dee9
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions pkg/solc/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/Masterminds/semver/v3"
"github.com/dogefuzz/dogefuzz/pkg/common"
Expand All @@ -25,23 +26,40 @@ var ErrContractNotFound = errors.New("the contract was not found in compiled cod

type solidityCompiler struct {
storageFolder string
versions []string
}

func NewSolidityCompiler(storageFolder string) *solidityCompiler {
return &solidityCompiler{storageFolder: storageFolder}
versions, err := getDescendingOrderedVersionsFromSolidyBinariesEndpoint()
if err != nil {
versions = []string{}
}
return &solidityCompiler{storageFolder: storageFolder, versions: versions}
}

func (c *solidityCompiler) CompileSource(contractName string, contractSource string) (*common.Contract, error) {
if len(contractSource) == 0 {
return nil, ErrEmptySourceFile
}

solcVersion, err := getIdealSolcVersionBasedOnSource(contractSource)
if err != nil {
return nil, err
var solcVersion *semver.Version
maxRetries := 5
var err error
for retries := 1; retries <= maxRetries; retries++ {
if retries == maxRetries {
return nil, err
}

solcVersion, err = c.getIdealSolcVersionBasedOnSource(contractSource)
if err != nil {
time.Sleep(100 * time.Millisecond)
continue
}
break
}

var solcBinaryLocation string

if location, ok := c.getSolcBinaryLocationIfExists(solcVersion); ok {
solcBinaryLocation = location
} else {
Expand Down Expand Up @@ -154,10 +172,14 @@ func run(cmd *exec.Cmd, source string, maxVersion *semver.Version) (map[string]*
return compiler.ParseCombinedJSON(stdout.Bytes(), source, maxVersion.String(), maxVersion.String(), strings.Join(buildArgs(maxVersion), " "))
}

func getIdealSolcVersionBasedOnSource(source string) (*semver.Version, error) {
versions, err := getDescendingOrderedVersionsFromSolidyBinariesEndpoint()
if err != nil {
return nil, err
func (c *solidityCompiler) getIdealSolcVersionBasedOnSource(source string) (*semver.Version, error) {
var versions = c.versions
if len(versions) == 0 {
var err error
versions, err = getDescendingOrderedVersionsFromSolidyBinariesEndpoint()
if err != nil {
return nil, err
}
}

versionConstraint, err := extractVersionConstraintFromSource(source)
Expand Down

0 comments on commit dc1dee9

Please sign in to comment.