From ebe6369779c73f5053951aa419153c498f63f7c2 Mon Sep 17 00:00:00 2001 From: 0x19 Date: Sun, 20 Aug 2023 02:32:07 +0200 Subject: [PATCH] Sync speedup --- releases/releases.json | 2 +- solc.go | 34 +++++++--------------------------- syncer.go | 15 ++++++++------- syncer_test.go | 2 ++ 4 files changed, 18 insertions(+), 35 deletions(-) diff --git a/releases/releases.json b/releases/releases.json index 8207608..5731492 100644 --- a/releases/releases.json +++ b/releases/releases.json @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9fb6f1658da36f7c70bdeaf77561a4969b804b1a5a4c7cde21dc8ff814916623 +oid sha256:df6f80e7f72cfcee0df657421b98208e21307d4442c4c694ab79b785545e7795 size 965663 diff --git a/solc.go b/solc.go index 8c82c1e..10cf9da 100644 --- a/solc.go +++ b/solc.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "runtime" + "time" ) // Solc represents the main structure for interacting with the Solidity compiler. @@ -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") @@ -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 { diff --git a/syncer.go b/syncer.go index e2fdeb0..4a5a1cf 100644 --- a/syncer.go +++ b/syncer.go @@ -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) @@ -76,6 +82,7 @@ func (s *Solc) SyncReleases() ([]Version, error) { } s.localReleases = allVersions + s.lastSync = time.Now() return allVersions, nil } @@ -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)), diff --git a/syncer_test.go b/syncer_test.go index ef00607..33466c8 100644 --- a/syncer_test.go +++ b/syncer_test.go @@ -54,6 +54,7 @@ func TestSyncer(t *testing.T) { assert.NoError(t, err) } + assert.NotNil(t, s.LastSyncTime()) }) } } @@ -108,6 +109,7 @@ func TestSyncOnce(t *testing.T) { assert.NoError(t, err) } + assert.NotNil(t, s.LastSyncTime()) }) } }