Skip to content

Commit

Permalink
fix: represent storage in same order in genesis state
Browse files Browse the repository at this point in the history
  • Loading branch information
JimLarson committed Dec 2, 2021
1 parent 8002489 commit f584cd1
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 146 deletions.
8 changes: 7 additions & 1 deletion golang/cosmos/proto/agoric/swingset/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ option go_package = "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types
message GenesisState {
option (gogoproto.equal) = false;

map<string, string> storage = 1 [
repeated StorageEntry storage = 1 [
(gogoproto.jsontag) = "storage",
(gogoproto.moretags) = "yaml:\"storage\""
];

Params params = 2 [(gogoproto.nullable) = false];
}

// A storage entry.
message StorageEntry {
string key = 1;
string value = 2;
}
9 changes: 4 additions & 5 deletions golang/cosmos/x/swingset/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func NewGenesisState() *types.GenesisState {
return &types.GenesisState{
Storage: make(map[string]string),
Storage: []*types.StorageEntry{},
}
}

Expand All @@ -31,7 +31,7 @@ func ValidateGenesis(data *types.GenesisState) error {
func DefaultGenesisState() *types.GenesisState {
return &types.GenesisState{
Params: types.DefaultParams(),
Storage: make(map[string]string),
Storage: []*types.StorageEntry{},
}
}

Expand All @@ -44,9 +44,8 @@ type bootstrapBlockAction struct {
func InitGenesis(ctx sdk.Context, keeper Keeper, data *types.GenesisState) []abci.ValidatorUpdate {
keeper.SetParams(ctx, data.GetParams())

// NONDETERMINISM: order of SetStorage is not deterministic
for key, value := range data.Storage {
keeper.SetStorage(ctx, key, value)
for _, entry := range data.Storage {
keeper.SetStorage(ctx, entry.Key, entry.Value)
}

// Just run the SwingSet kernel to finish bootstrap and get ready to open for
Expand Down
7 changes: 4 additions & 3 deletions golang/cosmos/x/swingset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,18 @@ func (k Keeper) SetEgress(ctx sdk.Context, egress *types.Egress) error {
}

// ExportStorage fetches all storage
func (k Keeper) ExportStorage(ctx sdk.Context) map[string]string {
func (k Keeper) ExportStorage(ctx sdk.Context) []*types.StorageEntry {
store := ctx.KVStore(k.storeKey)
dataStore := prefix.NewStore(store, types.DataPrefix)

iterator := sdk.KVStorePrefixIterator(dataStore, nil)

exported := make(map[string]string)
exported := []*types.StorageEntry{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
keyStr := keyToString(iterator.Key())
exported[keyStr] = string(iterator.Value())
entry := types.StorageEntry{Key: keyStr, Value: string(iterator.Value())}
exported = append(exported, &entry)
}
return exported
}
Expand Down
Loading

0 comments on commit f584cd1

Please sign in to comment.