Skip to content

Commit

Permalink
Sync speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Aug 20, 2023
1 parent fdaccab commit ebe6369
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 35 deletions.
2 changes: 1 addition & 1 deletion releases/releases.json
Git LFS file not shown
34 changes: 7 additions & 27 deletions solc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"runtime"
"time"
)

// Solc represents the main structure for interacting with the Solidity compiler.
Expand All @@ -15,18 +16,10 @@ type Solc struct {
client *http.Client
gOOSFunc func() string
localReleases []Version
lastSync time.Time
}

// New initializes and returns a new instance of the Solc structure.
// It requires a context and a configuration to be provided.
//
// Parameters:
// - ctx: The context for the Solc instance.
// - config: The configuration settings for the Solc instance.
//
// Returns:
// - A pointer to the initialized Solc instance.
// - An error if there's any issue during initialization.
func New(ctx context.Context, config *Config) (*Solc, error) {
if config == nil {
return nil, fmt.Errorf("config needs to be provided")
Expand All @@ -47,39 +40,26 @@ func New(ctx context.Context, config *Config) (*Solc, error) {
}

// GetContext retrieves the context associated with the Solc instance.
//
// Returns:
// - The context.Context of the Solc instance.
func (s *Solc) GetContext() context.Context {
return s.ctx
}

// LastSyncTime retrieves the last time the Solc instance was synced.
func (s *Solc) LastSyncTime() time.Time {
return s.lastSync
}

// GetConfig retrieves the configuration associated with the Solc instance.
//
// Returns:
// - A pointer to the Config of the Solc instance.
func (s *Solc) GetConfig() *Config {
return s.config
}

// GetHTTPClient retrieves the HTTP client associated with the Solc instance.
//
// Returns:
// - A pointer to the http.Client of the Solc instance.
func (s *Solc) GetHTTPClient() *http.Client {
return s.client
}

// Compile compiles the provided Solidity source code using the specified compiler configuration.
//
// Parameters:
// - ctx: The context for the compilation process.
// - source: The Solidity source code to be compiled.
// - config: The configuration settings for the compiler.
//
// Returns:
// - A pointer to the CompilerResults containing the results of the compilation.
// - An error if there's any issue during the compilation process.
func (s *Solc) Compile(ctx context.Context, source string, config *CompilerConfig) (*CompilerResults, error) {
compiler, err := NewCompiler(ctx, s, config, source)
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func (s *Solc) SyncReleases() ([]Version, error) {
var allVersions []Version
page := 1

// Sync maximum 4 times per day in order to increase the speed of the sync process when there's really
// no need to sync more often than that.
if time.Since(s.lastSync) < time.Duration(6*time.Hour) {
return s.localReleases, nil
}

for {
url := fmt.Sprintf("%s?page=%d", s.config.GetReleasesUrl(), page)
req, err := http.NewRequest("GET", url, nil)
Expand Down Expand Up @@ -76,6 +82,7 @@ func (s *Solc) SyncReleases() ([]Version, error) {
}

s.localReleases = allVersions
s.lastSync = time.Now()
return allVersions, nil
}

Expand Down Expand Up @@ -108,16 +115,10 @@ func (s *Solc) SyncBinaries(versions []Version, limitVersion string) error {
filename += ".exe"
}

zap.L().Debug(
"Checking if solc asset needs to be downloaded",
zap.String("version", versionTag),
zap.String("asset_name", asset.Name),
)

if _, err := os.Stat(filename); os.IsNotExist(err) {
totalDownloads++
zap.L().Debug(
"Downloading solc asset",
"Downloading solc release",
zap.String("version", versionTag),
zap.String("asset_name", asset.Name),
zap.String("asset_local_filename", filepath.Base(filename)),
Expand Down
2 changes: 2 additions & 0 deletions syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestSyncer(t *testing.T) {
assert.NoError(t, err)
}

assert.NotNil(t, s.LastSyncTime())
})
}
}
Expand Down Expand Up @@ -108,6 +109,7 @@ func TestSyncOnce(t *testing.T) {
assert.NoError(t, err)
}

assert.NotNil(t, s.LastSyncTime())
})
}
}

0 comments on commit ebe6369

Please sign in to comment.