Skip to content

Commit

Permalink
feat(cosmos): fix and migrate swing-store
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman committed Aug 10, 2023
1 parent 1da112d commit 316fbf3
Showing 1 changed file with 94 additions and 1 deletion.
95 changes: 94 additions & 1 deletion golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import (
gaiaappparams "github.com/Agoric/agoric-sdk/golang/cosmos/app/params"

appante "github.com/Agoric/agoric-sdk/golang/cosmos/ante"
agorictypes "github.com/Agoric/agoric-sdk/golang/cosmos/types"
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/lien"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset"
Expand Down Expand Up @@ -803,14 +804,106 @@ func NewAgoricApp(
return app
}

type swingStoreMigrationEventHandler struct {
swingStore sdk.KVStore
}

func (eventHandler swingStoreMigrationEventHandler) OnExportStarted(height uint64, retrieveSwingStoreExport func() error) error {
return retrieveSwingStoreExport()
}

func (eventHandler swingStoreMigrationEventHandler) OnExportRetrieved(provider swingsetkeeper.SwingStoreExportProvider) (err error) {
exportDataReader, err := provider.GetExportDataReader()
if err != nil {
return err
}
defer exportDataReader.Close()

var hasExportData bool

for {
entry, err := exportDataReader.Read()
if err == io.EOF {
break
} else if err != nil {
return err
}
hasExportData = true
if !entry.HasValue() {
return fmt.Errorf("no value for export data key %s", entry.Key())
}
eventHandler.swingStore.Set([]byte(entry.Key()), []byte(entry.StringValue()))
}
if !hasExportData {
return fmt.Errorf("export data had no entries")
}
return nil
}

// upgrade11Handler performs standard upgrade actions plus custom actions for upgrade-11.
func upgrade11Handler(app *GaiaApp, targetUpgrade string) func(sdk.Context, upgradetypes.Plan, module.VersionMap) (module.VersionMap, error) {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVm module.VersionMap) (module.VersionMap, error) {
app.CheckControllerInited(false)
// Record the plan to send to SwingSet
app.upgradePlan = &plan

// TODO: Migrate x/vstorage swingStore to x/swingset SwingStore
// Perform swing-store migrations. We do this in the app upgrade handler
// since it involved multile modules (x/vstorage and x/swingset) which
// don't strictly have a version change on their own.

// We are at the begining of the upgrade block, so all stores are commited
// as of the end of the previous block
savedBlockHeight := uint64(ctx.BlockHeight() - 1)

// First, repair swing-store metadata in case this node was state-synced
// This is done with a check on the block height to catch any hangover early
// Only entries related to missing historical metadata are imported, but we
// don't know what these look like here, so we provide it all.
getSwingStoreExportDataFromVstorage := func() (reader agorictypes.KVEntryReader, err error) {
return agorictypes.NewVstorageDataEntriesReader(
app.VstorageKeeper.ExportStorageFromPrefix(ctx, swingsetkeeper.StoragePathSwingStore),
), nil
}

// We're not restoring any artifact to swing-store, nor have any to provide
readNoArtifact := func() (artifact swingsettypes.SwingStoreArtifact, err error) {
return artifact, io.EOF
}

err := app.SwingStoreExportsHandler.RestoreExport(
swingsetkeeper.SwingStoreExportProvider{
BlockHeight: savedBlockHeight,
GetExportDataReader: getSwingStoreExportDataFromVstorage,
ReadArtifact: readNoArtifact,
},
swingsetkeeper.SwingStoreRestoreOptions{
ArtifactMode: swingsetkeeper.SwingStoreArtifactModeNone,
ExportDataMode: swingsetkeeper.SwingStoreRepairMetadataExportDataMode,
},
)
if err != nil {
return nil, err
}

// Then migrate the swing-store shadow copy:
// 1. Remove the swing-store "export data" shadow-copy entries from vstorage.
// 2. Export swing-store "export-data" (as of the previous block) through a
// handler that writes every entry into the swingset module's new Store.
app.VstorageKeeper.RemoveEntriesWithPrefix(ctx, swingsetkeeper.StoragePathSwingStore)
err = app.SwingStoreExportsHandler.InitiateExport(
savedBlockHeight,
swingStoreMigrationEventHandler{swingStore: app.SwingSetKeeper.GetSwingStore(ctx)},
swingsetkeeper.SwingStoreExportOptions{
ArtifactMode: swingsetkeeper.SwingStoreArtifactModeNone,
ExportDataMode: swingsetkeeper.SwingStoreExportDataModeAll,
},
)
if err == nil {
err = swingsetkeeper.WaitUntilSwingStoreExportDone()
}
if err != nil {
return nil, err
}

// Always run module migrations
mvm, err := app.mm.RunMigrations(ctx, app.configurator, fromVm)
Expand Down

0 comments on commit 316fbf3

Please sign in to comment.