Skip to content

Commit

Permalink
fix(cosmos): remove extra types.Storage wrapping from x/swingset
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 2, 2021
1 parent da7c924 commit d716f96
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 49 deletions.
4 changes: 1 addition & 3 deletions golang/cosmos/x/swingset/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ type bootstrapBlockAction struct {

func InitGenesis(ctx sdk.Context, keeper Keeper, data *types.GenesisState) []abci.ValidatorUpdate {
// NONDETERMINISM: order of SetStorage is not deterministic
var storage types.Storage
for key, value := range data.Storage {
storage.Value = value
keeper.SetStorage(ctx, key, &storage)
keeper.SetStorage(ctx, key, value)
}

// Just run the SwingSet kernel to finish bootstrap and get ready to open for
Expand Down
10 changes: 5 additions & 5 deletions golang/cosmos/x/swingset/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func (k Querier) Storage(c context.Context, req *types.QueryStorageRequest) (*ty
}
ctx := sdk.UnwrapSDKContext(c)

storage := k.GetStorage(ctx, strings.Join(req.Path, "."))
value := k.GetStorage(ctx, strings.Join(req.Path, "."))

return &types.QueryStorageResponse{
Value: storage.Value,
Value: value,
}, nil
}

Expand Down Expand Up @@ -64,12 +64,12 @@ func (k Querier) Mailbox(c context.Context, req *types.QueryMailboxRequest) (*ty
}
ctx := sdk.UnwrapSDKContext(c)

mb := k.GetMailbox(ctx, req.Peer.String())
if mb.Value == "" {
value := k.GetMailbox(ctx, req.Peer.String())
if value == "" {
return nil, status.Error(codes.NotFound, "mailbox not found")
}

return &types.QueryStorageResponse{
Value: mb.Value,
Value: value,
}, nil
}
38 changes: 17 additions & 21 deletions golang/cosmos/x/swingset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ func (k Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) s
// GetEgress gets the entire egress struct for a peer
func (k Keeper) GetEgress(ctx sdk.Context, addr sdk.AccAddress) types.Egress {
path := "egress." + addr.String()
storage := k.GetStorage(ctx, path)
if storage.Value == "" {
value := k.GetStorage(ctx, path)
if value == "" {
return types.Egress{}
}

var egress types.Egress
err := json.Unmarshal([]byte(storage.Value), &egress)
err := json.Unmarshal([]byte(value), &egress)
if err != nil {
panic(err)
}
Expand All @@ -84,13 +84,12 @@ func (k Keeper) GetEgress(ctx sdk.Context, addr sdk.AccAddress) types.Egress {
func (k Keeper) SetEgress(ctx sdk.Context, egress *types.Egress) error {
path := "egress." + egress.Peer.String()

json, err := json.Marshal(egress)
bz, err := json.Marshal(egress)
if err != nil {
return err
}

storage := &types.Storage{Value: string(json)}
k.SetStorage(ctx, path, storage)
k.SetStorage(ctx, path, string(bz))

// Now make sure the corresponding account has been initialised.
if acc := k.accountKeeper.GetAccount(ctx, egress.Peer); acc != nil {
Expand Down Expand Up @@ -118,27 +117,24 @@ func (k Keeper) ExportStorage(ctx sdk.Context) map[string]string {
exported := make(map[string]string)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var storage types.Storage
keyStr := keyToString(iterator.Key())
k.cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &storage)
exported[keyStr] = storage.Value
exported[keyStr] = string(iterator.Value())
}
return exported
}

// GetStorage gets generic storage
func (k Keeper) GetStorage(ctx sdk.Context, path string) *types.Storage {
func (k Keeper) GetStorage(ctx sdk.Context, path string) string {
//fmt.Printf("GetStorage(%s)\n", path);
store := ctx.KVStore(k.storeKey)
dataStore := prefix.NewStore(store, types.DataPrefix)
dataKey := stringToKey(path)
if !dataStore.Has(dataKey) {
return &types.Storage{Value: ""}
return ""
}
bz := dataStore.Get(dataKey)
var storage types.Storage
k.cdc.MustUnmarshalLengthPrefixed(bz, &storage)
return &storage
value := string(bz)
return value
}

// GetKeys gets all storage child keys at a given path
Expand All @@ -156,21 +152,21 @@ func (k Keeper) GetKeys(ctx sdk.Context, path string) *types.Keys {
}

// SetStorage sets the entire generic storage for a path
func (k Keeper) SetStorage(ctx sdk.Context, path string, storage *types.Storage) {
func (k Keeper) SetStorage(ctx sdk.Context, path, value string) {
store := ctx.KVStore(k.storeKey)
dataStore := prefix.NewStore(store, types.DataPrefix)
keysStore := prefix.NewStore(store, types.KeysPrefix)

// Update the value.
pathKey := stringToKey(path)
if storage.Value == "" {
if value == "" {
dataStore.Delete(pathKey)
} else {
dataStore.Set(pathKey, k.cdc.MustMarshalLengthPrefixed(storage))
dataStore.Set(pathKey, []byte(value))
}

ctx.EventManager().EmitEvent(
types.NewStorageEvent(path, storage.Value),
types.NewStorageEvent(path, value),
)

// Update the parent keys.
Expand All @@ -188,7 +184,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, path string, storage *types.Storage)
}

// Decide if we need to add or remove the key.
if storage.Value == "" {
if value == "" {
if _, ok := keyMap[lastKeyStr]; !ok {
// If the key is already gone, we don't need to remove from the key lists.
return
Expand Down Expand Up @@ -229,13 +225,13 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
}

// GetMailbox gets the entire mailbox struct for a peer
func (k Keeper) GetMailbox(ctx sdk.Context, peer string) *types.Storage {
func (k Keeper) GetMailbox(ctx sdk.Context, peer string) string {
path := "mailbox." + peer
return k.GetStorage(ctx, path)
}

// SetMailbox sets the entire mailbox struct for a peer
func (k Keeper) SetMailbox(ctx sdk.Context, peer string, mailbox *types.Storage) {
func (k Keeper) SetMailbox(ctx sdk.Context, peer string, mailbox string) {
path := "mailbox." + peer
k.SetStorage(ctx, path, mailbox)
}
7 changes: 2 additions & 5 deletions golang/cosmos/x/swingset/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ func queryEgress(ctx sdk.Context, bech32 string, req abci.RequestQuery, keeper K

// nolint: unparam
func queryStorage(ctx sdk.Context, path string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, err error) {
storage := keeper.GetStorage(ctx, path)
value := storage.Value

value := keeper.GetStorage(ctx, path)
if value == "" {
return []byte{}, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "could not get storage %+v", path)
}
Expand Down Expand Up @@ -97,8 +95,7 @@ func queryKeys(ctx sdk.Context, path string, req abci.RequestQuery, keeper Keepe
func queryMailbox(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, err error) {
peer := path[0]

mailbox := keeper.GetMailbox(ctx, peer)
value := mailbox.Value
value := keeper.GetMailbox(ctx, peer)

if value == "" {
return []byte{}, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "could not get peer mailbox")
Expand Down
26 changes: 11 additions & 15 deletions golang/cosmos/x/swingset/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,25 @@ func (sh storageHandler) Receive(cctx *vm.ControllerContext, str string) (ret st
// Handle generic paths.
switch msg.Method {
case "set":
storage := NewStorage()
storage.Value = msg.Value
//fmt.Printf("giving Keeper.SetStorage(%s) %s\n", msg.Key, storage.Value)
keeper.SetStorage(cctx.Context, msg.Key, storage)
//fmt.Printf("giving Keeper.SetStorage(%s) %s\n", msg.Key, msg.Value)
keeper.SetStorage(cctx.Context, msg.Key, msg.Value)
return "true", nil

case "get":
storage := keeper.GetStorage(cctx.Context, msg.Key)
if storage.Value == "" {
value := keeper.GetStorage(cctx.Context, msg.Key)
if value == "" {
return "null", nil
}
//fmt.Printf("Keeper.GetStorage gave us %s\n", storage.Value)
s, err := json.Marshal(storage.Value)
//fmt.Printf("Keeper.GetStorage gave us %bz\n", value)
bz, err := json.Marshal(value)
if err != nil {
return "", err
}
return string(s), nil
return string(bz), nil

case "has":
storage := keeper.GetStorage(cctx.Context, msg.Key)
if storage.Value == "" {
value := keeper.GetStorage(cctx.Context, msg.Key)
if value == "" {
return "false", nil
}
return "true", nil
Expand All @@ -92,8 +90,7 @@ func (sh storageHandler) Receive(cctx *vm.ControllerContext, str string) (ret st
for i, key := range keys.Keys {
ents[i] = make([]string, 2)
ents[i][0] = key
storage := keeper.GetStorage(cctx.Context, fmt.Sprintf("%s.%s", msg.Key, key))
ents[i][1] = storage.Value
ents[i][i] = keeper.GetStorage(cctx.Context, fmt.Sprintf("%s.%s", msg.Key, key))
}
bytes, err := json.Marshal(ents)
if err != nil {
Expand All @@ -105,8 +102,7 @@ func (sh storageHandler) Receive(cctx *vm.ControllerContext, str string) (ret st
keys := keeper.GetKeys(cctx.Context, msg.Key)
vals := make([]string, len(keys.Keys))
for i, key := range keys.Keys {
storage := keeper.GetStorage(cctx.Context, fmt.Sprintf("%s.%s", msg.Key, key))
vals[i] = storage.Value
vals[i] = keeper.GetStorage(cctx.Context, fmt.Sprintf("%s.%s", msg.Key, key))
}
bytes, err := json.Marshal(vals)
if err != nil {
Expand Down

0 comments on commit d716f96

Please sign in to comment.