Skip to content

Commit

Permalink
fixup! feat(cosmos): wire swingset RestoreExtension
Browse files Browse the repository at this point in the history
Refactor payloadReader loop
  • Loading branch information
mhofman committed Mar 28, 2023
1 parent 72bcc14 commit 95b8dab
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions golang/cosmos/x/swingset/keeper/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,27 @@ 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 {
break
} 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 {
Expand All @@ -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()
Expand All @@ -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
}
Expand Down

0 comments on commit 95b8dab

Please sign in to comment.