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 6, 2023
1 parent 517beb5 commit 4f47b09
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/Agoric/agoric-sdk/golang/cosmos/app/helpers"
gaiaappparams "github.com/Agoric/agoric-sdk/golang/cosmos/app/params"

appante "github.com/Agoric/agoric-sdk/golang/cosmos/ante"
Expand Down Expand Up @@ -803,14 +804,88 @@ 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) {
defer helpers.RecoverToError(&err)

var exportDataIterator sdk.Iterator
exportDataIterator, err = provider.GetExportDataIterator()
if err != nil {
return err
}
defer exportDataIterator.Close()

if !exportDataIterator.Valid() {
return fmt.Errorf("swing store export must provide some export data")
}

for ; exportDataIterator.Valid(); exportDataIterator.Next() {
eventHandler.swingStore.Set(exportDataIterator.Key(), exportDataIterator.Value())
}
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
savedBlockHeight := uint64(ctx.BlockHeight() - 1)

// First, repair swing-store metadata in case this node was state-synced
// This is done without checks on the block height as it's idempotent,
// and only missing historical metadata is imported
getExportDataIterator := func() (iter sdk.Iterator, err error) {
defer helpers.RecoverToError(&err)

return swingsetkeeper.NewExportDataIterator(swingsetkeeper.NewVstorageDataEntriesReader(
app.VstorageKeeper.ExportStorageFromPrefix(ctx, swingsetkeeper.StoragePathSwingStore),
)), nil
}

readArtifact := func() (artifact swingsettypes.SwingStoreArtifact, err error) {
return artifact, io.EOF
}

err := app.SwingStoreExportsHandler.RestoreExport(
swingsetkeeper.SwingStoreExportProvider{
BlockHeight: savedBlockHeight,
GetExportDataIterator: getExportDataIterator,
ReadArtifact: readArtifact,
},
swingsetkeeper.SwingStoreRestoreOptions{
ExportDataMode: swingsetkeeper.SwingStoreRepairMetadataExportDataMode,
ArtifactLevel: swingsetkeeper.SwingStoreNoneArtifactLevel,
},
)
if err != nil {
return nil, err
}

// Then migrate the swing-store shadow copy
app.VstorageKeeper.RemoveEntriesWithPrefix(ctx, swingsetkeeper.StoragePathSwingStore)
err = app.SwingStoreExportsHandler.InitiateExport(
savedBlockHeight,
swingStoreMigrationEventHandler{swingStore: app.SwingSetKeeper.GetSwingStore(ctx)},
swingsetkeeper.SwingStoreExportOptions{ArtifactLevel: "none", ExportDataMode: "all"},
)
if err != nil {
return nil, err
}

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 4f47b09

Please sign in to comment.