From 91169eaa5c4ffdadf4e74714785cdf801551b1a9 Mon Sep 17 00:00:00 2001 From: ltacker Date: Mon, 1 Feb 2021 13:44:06 +0100 Subject: [PATCH 1/2] Not saving checksum when it is checked for modification --- starport/pkg/dirchange/dirchange.go | 21 +++++++++++---------- starport/pkg/dirchange/dirchange_test.go | 13 ++----------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/starport/pkg/dirchange/dirchange.go b/starport/pkg/dirchange/dirchange.go index 9c0c641993..068c4f69a5 100644 --- a/starport/pkg/dirchange/dirchange.go +++ b/starport/pkg/dirchange/dirchange.go @@ -30,23 +30,24 @@ func SaveDirChecksum(workdir string, paths []string, checksumSavePath string, ch // HasDirChecksumChanged computes the md5 checksum of the provided paths (directories or files) // and compare it with the current saved checksum -// If the checksum is different, the new checksum is saved -// Return true if the checksum file doesn't exist yet and if checksumSavePath directory doesn't exist, it is created +// Return true if the checksum file doesn't exist yet // paths are relative to workdir, if workdir is empty string paths are absolute func HasDirChecksumChanged(workdir string, paths []string, checksumSavePath string, checksumName string) (bool, error) { - checksum, err := checksumFromPaths(workdir, paths) - if err != nil { - return false, err - } - // create directory if needed if err := os.MkdirAll(checksumSavePath, 0700); err != nil && !os.IsExist(err) { return false, err } - checksumFilePath := filepath.Join(checksumSavePath, checksumName) if _, err := os.Stat(checksumFilePath); os.IsNotExist(err) { - return true, ioutil.WriteFile(checksumFilePath, checksum, 0644) + return true, nil + } else if err != nil { + return false, nil + } + + // Compute checksum + checksum, err := checksumFromPaths(workdir, paths) + if err != nil { + return false, err } // Compare checksums @@ -59,7 +60,7 @@ func HasDirChecksumChanged(workdir string, paths []string, checksumSavePath stri } // The checksum has changed - return true, ioutil.WriteFile(checksumFilePath, checksum, 0644) + return true, nil } // checksumFromPaths computes the md5 checksum from the provided paths diff --git a/starport/pkg/dirchange/dirchange_test.go b/starport/pkg/dirchange/dirchange_test.go index 6c58846a63..13a1c64936 100644 --- a/starport/pkg/dirchange/dirchange_test.go +++ b/starport/pkg/dirchange/dirchange_test.go @@ -126,27 +126,18 @@ func TestHasDirChecksumChanged(t *testing.T) { require.NoError(t, err) require.False(t, changed) - // Return true and create the checksum file if it doesn't exist + // Return true if it doesn't exist newSaveDir, err := ioutil.TempDir(tempDir, TmpPattern) require.NoError(t, err) defer os.RemoveAll(newSaveDir) changed, err = HasDirChecksumChanged("", paths, newSaveDir, ChecksumFile) require.NoError(t, err) require.True(t, changed) - require.FileExists(t, filepath.Join(newSaveDir, ChecksumFile)) - fileContent, err = ioutil.ReadFile(filepath.Join(newSaveDir, ChecksumFile)) - require.NoError(t, err) - require.Equal(t, newChecksum, fileContent) - // Return true and rewrite the checksum if it has been changed + // Return true if it has been changed err = ioutil.WriteFile(filepath.Join(dir21, "bar"), randomBytes(20), 0644) require.NoError(t, err) changed, err = HasDirChecksumChanged("", paths, saveDir, ChecksumFile) require.NoError(t, err) require.True(t, changed) - fileContent, err = ioutil.ReadFile(filepath.Join(saveDir, ChecksumFile)) - require.NoError(t, err) - newChecksum, err = checksumFromPaths("", paths) - require.NoError(t, err) - require.Equal(t, newChecksum, fileContent) } From aba235df7b8f9ee3867b9aef73688f691df3a007 Mon Sep 17 00:00:00 2001 From: ltacker Date: Mon, 1 Feb 2021 14:02:14 +0100 Subject: [PATCH 2/2] change cehcksum workflow --- starport/services/chain/serve.go | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/starport/services/chain/serve.go b/starport/services/chain/serve.go index e7ab301d51..731e5e6891 100644 --- a/starport/services/chain/serve.go +++ b/starport/services/chain/serve.go @@ -9,7 +9,6 @@ import ( "net/http" "os" "os/exec" - "os/signal" "path" "path/filepath" @@ -198,26 +197,6 @@ If the new code is no longer compatible with the saved state, you can reset the return c.watchAppBackend(ctx) }) - // save the source checksum on exit - // this goroutine is not in the waiting group to not block the program in case the blockchain start fails - go func() { - interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, os.Interrupt) - <-interrupt - - fmt.Fprintf(c.stdLog(logStarport).out, "\nExiting...\n") - saveDir, err := c.chainSavePath() - if err != nil { - fmt.Fprintf(c.stdLog(logStarport).err, "Failed to save source checksum:%s \n", err.Error()) - } - if err := dirchange.SaveDirChecksum(c.app.Path, appBackendSourceWatchPaths, saveDir, sourceChecksum); err != nil { - fmt.Fprintf(c.stdLog(logStarport).err, "Failed to save source checksum:%s \n", err.Error()) - } - if err := dirchange.SaveDirChecksum(c.app.Path, appBackendConfigWatchPaths, saveDir, configChecksum); err != nil { - fmt.Fprintf(c.stdLog(logStarport).err, "Failed to save config checksum:%s \n", err.Error()) - } - }() - return g.Wait() } @@ -381,6 +360,14 @@ func (c *Chain) serve(ctx context.Context, forceReset bool) error { fmt.Fprintln(c.stdLog(logStarport).out, "▶️ Restarting existing app...") } + // save source and config checksum + if err := dirchange.SaveDirChecksum(c.app.Path, appBackendConfigWatchPaths, saveDir, configChecksum); err != nil { + return err + } + if err := dirchange.SaveDirChecksum(c.app.Path, appBackendSourceWatchPaths, saveDir, sourceChecksum); err != nil { + return err + } + // start the blockchain return c.start(ctx, conf) }