Skip to content

Commit

Permalink
feat(cosmos): wire swingset RestoreExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman committed Mar 31, 2023
1 parent 301286e commit 4b8e650
Showing 1 changed file with 96 additions and 2 deletions.
98 changes: 96 additions & 2 deletions golang/cosmos/x/swingset/keeper/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"

"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
Expand All @@ -23,6 +25,8 @@ var _ snapshots.ExtensionSnapshotter = &SwingsetSnapshotter{}
const SnapshotFormat = 1

const ExportManifestFilename = "export-manifest.json"
const ExportDataFilename = "export-data.jsonl"
const ExportedFilesMode = 0644

type activeSnapshot struct {
height int64
Expand Down Expand Up @@ -266,7 +270,97 @@ func (snapshotter *SwingsetSnapshotter) RestoreExtension(height uint64, format u

ctx := snapshotter.app.NewUncachedContext(false, tmproto.Header{Height: int64(height)})

_ = snapshotter.exporter.ExportSwingStore(ctx)
exportDir, err := os.MkdirTemp("", fmt.Sprintf("agd-state-sync-restore-%d-*", height))
if err != nil {
return err
}
defer os.RemoveAll(exportDir)

manifest := exportManifest{
BlockHeight: height,
Data: ExportDataFilename,
}

exportDataFile, err := os.OpenFile(filepath.Join(exportDir, ExportDataFilename), os.O_CREATE|os.O_WRONLY, ExportedFilesMode)
if err != nil {
return err
}
defer exportDataFile.Close()

swingStoreEntries := snapshotter.exporter.ExportSwingStore(ctx)

if len(swingStoreEntries) > 0 {
encoder := json.NewEncoder(exportDataFile)
encoder.SetEscapeHTML(false)
for _, dataEntry := range swingStoreEntries {
entry := []string{dataEntry.Path, dataEntry.Value}
err := encoder.Encode(entry)
if err != nil {
return err
}
}
}

artifactNameSanitizer := regexp.MustCompile(`[^-_.a-zA-Z0-9]`)

return errors.New("not implemented")
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{}
if err = payload.Unmarshal(payloadBytes); err != nil {
return err
}

filename := artifactNameSanitizer.ReplaceAllString(filepath.Base(payload.Name), "-")
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 err != nil {
return err
}
}

err = exportDataFile.Sync()
if err != nil {
return err
}
exportDataFile.Close()

manifestBytes, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return err
}
err = writeExportFile(ExportManifestFilename, manifestBytes)
if err != nil {
return err
}

encodedExportDir, err := json.Marshal(exportDir)
if err != nil {
return err
}

action := &snapshotAction{
Type: "COSMOS_SNAPSHOT",
BlockHeight: int64(height),
Request: "restore",
Args: []json.RawMessage{encodedExportDir},
}

_, err = snapshotter.blockingSend(action)
if err != nil {
return err
}

return nil
}

0 comments on commit 4b8e650

Please sign in to comment.