Skip to content

Commit

Permalink
feat(x/swingset): import swing store from genesis state
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman committed Aug 19, 2023
1 parent ecc53b0 commit f936fbe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
6 changes: 6 additions & 0 deletions golang/cosmos/daemon/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/viper"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
Expand Down Expand Up @@ -243,6 +244,11 @@ func (ac appCreator) newApp(

homePath := cast.ToString(appOpts.Get(flags.FlagHome))

viper, ok := appOpts.(*viper.Viper)
if ok && cast.ToString(appOpts.Get(gaia.FlagSwingStoreExportDir)) == "" {
viper.Set(gaia.FlagSwingStoreExportDir, filepath.Join(homePath, "config", ExportedSwingStoreDirectoryName))
}

snapshotDir := filepath.Join(homePath, "data", "snapshots")
snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDir)
if err != nil {
Expand Down
40 changes: 32 additions & 8 deletions golang/cosmos/x/swingset/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,42 @@ func DefaultGenesisState() *types.GenesisState {

// InitGenesis initializes the (Cosmos-side) SwingSet state from the GenesisState.
// Returns whether the app should send a bootstrap action to the controller.
func InitGenesis(ctx sdk.Context, keeper Keeper, data *types.GenesisState) bool {
keeper.SetParams(ctx, data.GetParams())
keeper.SetState(ctx, data.GetState())
func InitGenesis(ctx sdk.Context, k Keeper, swingStoreExportsHandler *SwingStoreExportsHandler, swingStoreExportDir string, data *types.GenesisState) bool {
k.SetParams(ctx, data.GetParams())
k.SetState(ctx, data.GetState())

swingStoreExportData := data.GetSwingStoreExportData()
if len(swingStoreExportData) > 0 {
// See https://github.com/Agoric/agoric-sdk/issues/6527
panic("genesis with swing-store state not implemented")
if len(swingStoreExportData) == 0 {
return true
}

// TODO: bootstrap only if not restoring swing-store from genesis state
return true
artifactProvider, err := keeper.OpenSwingStoreExportDirectory(swingStoreExportDir)
if err != nil {
panic(err)
}

swingStore := k.GetSwingStore(ctx)

for _, entry := range swingStoreExportData {
swingStore.Set([]byte(entry.Key), []byte(entry.Value))
}

snapshotHeight := uint64(ctx.BlockHeight())

getExportDataReader := func() (agoric.KVEntryReader, error) {
exportDataIterator := swingStore.Iterator(nil, nil)
return agoric.NewKVIteratorReader(exportDataIterator), nil
}

err = swingStoreExportsHandler.RestoreExport(
keeper.SwingStoreExportProvider{BlockHeight: snapshotHeight, GetExportDataReader: getExportDataReader, ReadNextArtifact: artifactProvider.ReadNextArtifact},
keeper.SwingStoreRestoreOptions{ArtifactMode: keeper.SwingStoreArtifactModeReplay, ExportDataMode: keeper.SwingStoreExportDataModeAll},
)
if err != nil {
panic(err)
}

return false
}

func ExportGenesis(ctx sdk.Context, k Keeper, swingStoreExportsHandler *SwingStoreExportsHandler, swingStoreExportDir string) *types.GenesisState {
Expand Down
3 changes: 2 additions & 1 deletion golang/cosmos/x/swingset/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ func (am AppModule) checkSwingStoreExportSetup() {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
bootstrapNeeded := InitGenesis(ctx, am.keeper, &genesisState)
am.checkSwingStoreExportSetup()
bootstrapNeeded := InitGenesis(ctx, am.keeper, am.swingStoreExportsHandler, am.swingStoreExportDir, &genesisState)
if bootstrapNeeded {
am.setBootstrapNeeded()
}
Expand Down

0 comments on commit f936fbe

Please sign in to comment.