From 95b8dabc2f448ddb11fcc7aad39000b9eaa28796 Mon Sep 17 00:00:00 2001 From: Mathieu Hofman Date: Tue, 28 Mar 2023 20:13:12 +0000 Subject: [PATCH] fixup! feat(cosmos): wire swingset RestoreExtension Refactor payloadReader loop --- .../cosmos/x/swingset/keeper/snapshotter.go | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/golang/cosmos/x/swingset/keeper/snapshotter.go b/golang/cosmos/x/swingset/keeper/snapshotter.go index 7c8112e442c2..8d8244f29ab5 100644 --- a/golang/cosmos/x/swingset/keeper/snapshotter.go +++ b/golang/cosmos/x/swingset/keeper/snapshotter.go @@ -315,7 +315,10 @@ func (snapshotter *SwingsetSnapshotter) RestoreExtension(height uint64, format u artifactNameSanitizer := regexp.MustCompile(`[^-_.a-zA-Z0-9]`) - i := 0 + writeExportFile := func(filename string, data []byte) error { + return os.WriteFile(filepath.Join(exportDir, filename), data, ExportedFilesMode) + } + for { payloadBytes, err := payloadReader() if err == io.EOF { @@ -323,15 +326,16 @@ func (snapshotter *SwingsetSnapshotter) RestoreExtension(height uint64, format u } else if err != nil { return err } + payload := types.ExtensionSnapshotterArtifactPayload{} - err = payload.Unmarshal(payloadBytes) - if err != nil { + if err = payload.Unmarshal(payloadBytes); err != nil { return err } filename := artifactNameSanitizer.ReplaceAllString(filepath.Base(payload.Name), "-") - filename = fmt.Sprintf("%d-%s", i, filename) - i += 1 + filename = fmt.Sprintf("%d-%s", len(manifest.Artifacts), filename) + manifest.Artifacts = append(manifest.Artifacts, [2]string{payload.Name, filename}) + err = writeExportFile(filename, payload.Data) if payload.Name == UntrustedExportDataArtifactName { if len(swingStoreEntries) == 0 { @@ -346,14 +350,9 @@ func (snapshotter *SwingsetSnapshotter) RestoreExtension(height uint64, format u } } - err = os.WriteFile(filepath.Join(exportDir, filename), payload.Data, ExportedFilesMode) if err != nil { return err } - - if payload.Name != UntrustedExportDataArtifactName { - manifest.Artifacts = append(manifest.Artifacts, [2]string{payload.Name, filename}) - } } err = exportDataFile.Sync() @@ -366,7 +365,7 @@ func (snapshotter *SwingsetSnapshotter) RestoreExtension(height uint64, format u if err != nil { return err } - err = os.WriteFile(filepath.Join(exportDir, ExportManifestFilename), manifestBytes, ExportedFilesMode) + err = writeExportFile(ExportManifestFilename, manifestBytes) if err != nil { return err }