@agoric/cosmos 0.32.3-dev-5a9b5fe.0 → 0.32.3-dev-5e88c8a.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app/app.go CHANGED
@@ -759,6 +759,8 @@ func NewAgoricApp(
759
759
 
760
760
  func upgrade10Handler(app *GaiaApp, targetUpgrade string) func(sdk.Context, upgradetypes.Plan, module.VersionMap) (module.VersionMap, error) {
761
761
  return func(ctx sdk.Context, plan upgradetypes.Plan, fromVm module.VersionMap) (module.VersionMap, error) {
762
+ // TODO: In next upgrade handler (bulldozer), run:
763
+ // app.VstorageKeeper.MigrateNoDataPlaceholders(ctx)
762
764
  return fromVm, nil
763
765
  }
764
766
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/cosmos",
3
- "version": "0.32.3-dev-5a9b5fe.0+5a9b5fe",
3
+ "version": "0.32.3-dev-5e88c8a.0+5e88c8a",
4
4
  "description": "Connect JS to the Cosmos blockchain SDK",
5
5
  "parsers": {
6
6
  "js": "mjs"
@@ -47,5 +47,5 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
- "gitHead": "5a9b5fef1b94b179182ab4beaa1c2578f3f9a4b4"
50
+ "gitHead": "5e88c8a074c5cd78364e1de16535bf26a232213b"
51
51
  }
@@ -17,6 +17,7 @@ import (
17
17
  "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
18
18
  "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
19
19
  vstoragekeeper "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/keeper"
20
+ vstoragetypes "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
20
21
  )
21
22
 
22
23
  // Top-level paths for chain storage should remain synchronized with
@@ -99,23 +100,25 @@ func (k Keeper) PushAction(ctx sdk.Context, action vm.Jsonable) error {
99
100
 
100
101
  // JS uses IEEE 754 floats so avoid overflowing integers
101
102
  if tail == MaxUint53 {
102
- return errors.New("actionQueue overflow")
103
+ return errors.New(StoragePathActionQueue + " overflow")
103
104
  }
104
105
 
105
106
  // Set the vstorage corresponding to the queue entry for the current tail.
106
- k.vstorageKeeper.SetStorage(ctx, fmt.Sprintf("actionQueue.%d", tail), string(bz))
107
+ path := StoragePathActionQueue + "." + strconv.FormatUint(tail, 10)
108
+ k.vstorageKeeper.SetStorage(ctx, vstoragetypes.NewStorageEntry(path, string(bz)))
107
109
 
108
110
  // Update the tail to point to the next available entry.
109
- k.vstorageKeeper.SetStorage(ctx, "actionQueue.tail", fmt.Sprintf("%d", tail+1))
111
+ path = StoragePathActionQueue + ".tail"
112
+ k.vstorageKeeper.SetStorage(ctx, vstoragetypes.NewStorageEntry(path, strconv.FormatUint(tail+1, 10)))
110
113
  return nil
111
114
  }
112
115
 
113
116
  func (k Keeper) actionQueueIndex(ctx sdk.Context, name string) (uint64, error) {
114
117
  index := uint64(0)
115
118
  var err error
116
- indexStr := k.vstorageKeeper.GetData(ctx, "actionQueue."+name)
117
- if indexStr != "" {
118
- index, err = strconv.ParseUint(indexStr, 10, 64)
119
+ indexEntry := k.vstorageKeeper.GetEntry(ctx, StoragePathActionQueue+"."+name)
120
+ if indexEntry.HasData() {
121
+ index, err = strconv.ParseUint(indexEntry.StringValue(), 10, 64)
119
122
  }
120
123
  return index, err
121
124
  }
@@ -190,18 +193,18 @@ func getBeansOwingPathForAddress(addr sdk.AccAddress) string {
190
193
  // the FeeAccount but has not yet paid.
191
194
  func (k Keeper) GetBeansOwing(ctx sdk.Context, addr sdk.AccAddress) sdk.Uint {
192
195
  path := getBeansOwingPathForAddress(addr)
193
- value := k.vstorageKeeper.GetData(ctx, path)
194
- if value == "" {
196
+ entry := k.vstorageKeeper.GetEntry(ctx, path)
197
+ if !entry.HasData() {
195
198
  return sdk.ZeroUint()
196
199
  }
197
- return sdk.NewUintFromString(value)
200
+ return sdk.NewUintFromString(entry.StringValue())
198
201
  }
199
202
 
200
203
  // SetBeansOwing sets the number of beans that the given address owes to the
201
204
  // feeCollector but has not yet paid.
202
205
  func (k Keeper) SetBeansOwing(ctx sdk.Context, addr sdk.AccAddress, beans sdk.Uint) {
203
206
  path := getBeansOwingPathForAddress(addr)
204
- k.vstorageKeeper.SetStorage(ctx, path, beans.String())
207
+ k.vstorageKeeper.SetStorage(ctx, vstoragetypes.NewStorageEntry(path, beans.String()))
205
208
  }
206
209
 
207
210
  // ChargeBeans charges the given address the given number of beans. It divides
@@ -304,13 +307,13 @@ func (k Keeper) ChargeForProvisioning(ctx sdk.Context, submitter, addr sdk.AccAd
304
307
  // GetEgress gets the entire egress struct for a peer
305
308
  func (k Keeper) GetEgress(ctx sdk.Context, addr sdk.AccAddress) types.Egress {
306
309
  path := StoragePathEgress + "." + addr.String()
307
- value := k.vstorageKeeper.GetData(ctx, path)
308
- if value == "" {
310
+ entry := k.vstorageKeeper.GetEntry(ctx, path)
311
+ if !entry.HasData() {
309
312
  return types.Egress{}
310
313
  }
311
314
 
312
315
  var egress types.Egress
313
- err := json.Unmarshal([]byte(value), &egress)
316
+ err := json.Unmarshal([]byte(entry.StringValue()), &egress)
314
317
  if err != nil {
315
318
  panic(err)
316
319
  }
@@ -328,7 +331,7 @@ func (k Keeper) SetEgress(ctx sdk.Context, egress *types.Egress) error {
328
331
  }
329
332
 
330
333
  // FIXME: We should use just SetStorageAndNotify here, but solo needs legacy for now.
331
- k.vstorageKeeper.LegacySetStorageAndNotify(ctx, path, string(bz))
334
+ k.vstorageKeeper.LegacySetStorageAndNotify(ctx, vstoragetypes.NewStorageEntry(path, string(bz)))
332
335
 
333
336
  // Now make sure the corresponding account has been initialised.
334
337
  if acc := k.accountKeeper.GetAccount(ctx, egress.Peer); acc != nil {
@@ -354,14 +357,14 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
354
357
  // GetMailbox gets the entire mailbox struct for a peer
355
358
  func (k Keeper) GetMailbox(ctx sdk.Context, peer string) string {
356
359
  path := StoragePathMailbox + "." + peer
357
- return k.vstorageKeeper.GetData(ctx, path)
360
+ return k.vstorageKeeper.GetEntry(ctx, path).StringValue()
358
361
  }
359
362
 
360
363
  // SetMailbox sets the entire mailbox struct for a peer
361
364
  func (k Keeper) SetMailbox(ctx sdk.Context, peer string, mailbox string) {
362
365
  path := StoragePathMailbox + "." + peer
363
366
  // FIXME: We should use just SetStorageAndNotify here, but solo needs legacy for now.
364
- k.vstorageKeeper.LegacySetStorageAndNotify(ctx, path, mailbox)
367
+ k.vstorageKeeper.LegacySetStorageAndNotify(ctx, vstoragetypes.NewStorageEntry(path, mailbox))
365
368
  }
366
369
 
367
370
  func (k Keeper) PathToEncodedKey(path string) []byte {
@@ -79,12 +79,12 @@ func queryMailbox(ctx sdk.Context, path []string, req abci.RequestQuery, keeper
79
79
 
80
80
  // nolint: unparam
81
81
  func legacyQueryStorage(ctx sdk.Context, path string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, err error) {
82
- value := keeper.vstorageKeeper.GetData(ctx, path)
83
- if value == "" {
82
+ entry := keeper.vstorageKeeper.GetEntry(ctx, path)
83
+ if !entry.HasData() {
84
84
  return []byte{}, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "could not get swingset %+v", path)
85
85
  }
86
86
 
87
- bz, err2 := codec.MarshalJSONIndent(legacyQuerierCdc, vstoragetypes.Data{Value: value})
87
+ bz, err2 := codec.MarshalJSONIndent(legacyQuerierCdc, vstoragetypes.Data{Value: entry.StringValue()})
88
88
  if err2 != nil {
89
89
  return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err2.Error())
90
90
  }
@@ -23,10 +23,10 @@ func (k Querier) Data(c context.Context, req *types.QueryDataRequest) (*types.Qu
23
23
  }
24
24
  ctx := sdk.UnwrapSDKContext(c)
25
25
 
26
- value := k.GetData(ctx, req.Path)
26
+ entry := k.GetEntry(ctx, req.Path)
27
27
 
28
28
  return &types.QueryDataResponse{
29
- Value: value,
29
+ Value: entry.StringValue(),
30
30
  }, nil
31
31
  }
32
32
 
@@ -3,6 +3,7 @@ package keeper
3
3
  import (
4
4
  "bytes"
5
5
  "encoding/json"
6
+ "fmt"
6
7
  "sort"
7
8
  "strconv"
8
9
  "strings"
@@ -32,7 +33,7 @@ type ProposedChange struct {
32
33
  }
33
34
 
34
35
  type ChangeManager interface {
35
- Track(ctx sdk.Context, k Keeper, path, value string, isLegacy bool)
36
+ Track(ctx sdk.Context, k Keeper, entry types.StorageEntry, isLegacy bool)
36
37
  EmitEvents(ctx sdk.Context, k Keeper)
37
38
  Rollback(ctx sdk.Context)
38
39
  }
@@ -44,6 +45,14 @@ type BatchingChangeManager struct {
44
45
 
45
46
  var _ ChangeManager = (*BatchingChangeManager)(nil)
46
47
 
48
+ // TODO: Use bytes.CutPrefix once we can rely upon go >= 1.20.
49
+ func cutPrefix(s, prefix []byte) (after []byte, found bool) {
50
+ if !bytes.HasPrefix(s, prefix) {
51
+ return s, false
52
+ }
53
+ return s[len(prefix):], true
54
+ }
55
+
47
56
  // Keeper maintains the link to data storage and exposes getter/setter methods
48
57
  // for the various parts of the state machine
49
58
  type Keeper struct {
@@ -51,7 +60,11 @@ type Keeper struct {
51
60
  storeKey sdk.StoreKey
52
61
  }
53
62
 
54
- func (bcm *BatchingChangeManager) Track(ctx sdk.Context, k Keeper, path, value string, isLegacy bool) {
63
+ func (bcm *BatchingChangeManager) Track(ctx sdk.Context, k Keeper, entry types.StorageEntry, isLegacy bool) {
64
+ path := entry.Path()
65
+ // TODO: differentiate between deletion and setting empty string?
66
+ // Using empty string for deletion for backwards compatibility
67
+ value := entry.StringValue()
55
68
  if change, ok := bcm.changes[path]; ok {
56
69
  change.NewValue = value
57
70
  if isLegacy {
@@ -62,7 +75,7 @@ func (bcm *BatchingChangeManager) Track(ctx sdk.Context, k Keeper, path, value s
62
75
  bcm.changes[path] = &ProposedChange{
63
76
  Path: path,
64
77
  NewValue: value,
65
- ValueFromLastBlock: k.GetData(ctx, path),
78
+ ValueFromLastBlock: k.GetEntry(ctx, path).StringValue(),
66
79
  LegacyEvents: isLegacy,
67
80
  }
68
81
  }
@@ -114,12 +127,19 @@ func (k Keeper) ExportStorage(ctx sdk.Context) []*types.DataEntry {
114
127
  exported := []*types.DataEntry{}
115
128
  defer iterator.Close()
116
129
  for ; iterator.Valid(); iterator.Next() {
117
- path := types.EncodedKeyToPath(iterator.Key())
118
- value := string(bytes.TrimPrefix(iterator.Value(), types.EncodedDataPrefix))
119
- if len(value) == 0 {
130
+ rawValue := iterator.Value()
131
+ if len(rawValue) == 0 {
120
132
  continue
121
133
  }
122
- entry := types.DataEntry{Path: path, Value: value}
134
+ if bytes.Equal(rawValue, types.EncodedNoDataValue) {
135
+ continue
136
+ }
137
+ path := types.EncodedKeyToPath(iterator.Key())
138
+ value, hasPrefix := cutPrefix(rawValue, types.EncodedDataPrefix)
139
+ if !hasPrefix {
140
+ panic(fmt.Errorf("value at path %q starts with unexpected prefix", path))
141
+ }
142
+ entry := types.DataEntry{Path: path, Value: string(value)}
123
143
  exported = append(exported, &entry)
124
144
  }
125
145
  return exported
@@ -129,7 +149,31 @@ func (k Keeper) ImportStorage(ctx sdk.Context, entries []*types.DataEntry) {
129
149
  for _, entry := range entries {
130
150
  // This set does the bookkeeping for us in case the entries aren't a
131
151
  // complete tree.
132
- k.SetStorage(ctx, entry.Path, entry.Value)
152
+ k.SetStorage(ctx, types.NewStorageEntry(entry.Path, entry.Value))
153
+ }
154
+ }
155
+
156
+ func (k Keeper) MigrateNoDataPlaceholders(ctx sdk.Context) {
157
+ store := ctx.KVStore(k.storeKey)
158
+
159
+ iterator := sdk.KVStorePrefixIterator(store, nil)
160
+
161
+ // Copy empty keys first since cosmos stores do not support writing keys
162
+ // while an iterator is open over the domain
163
+ emptyKeys := [][]byte{}
164
+ for ; iterator.Valid(); iterator.Next() {
165
+ rawValue := iterator.Value()
166
+ if bytes.Equal(rawValue, types.EncodedDataPrefix) {
167
+ key := iterator.Key()
168
+ clonedKey := make([]byte, len(key))
169
+ copy(clonedKey, key)
170
+ emptyKeys = append(emptyKeys, clonedKey)
171
+ }
172
+ }
173
+ iterator.Close()
174
+
175
+ for _, key := range emptyKeys {
176
+ store.Set(key, types.EncodedNoDataValue)
133
177
  }
134
178
  }
135
179
 
@@ -156,14 +200,23 @@ func (k Keeper) EmitChange(ctx sdk.Context, change *ProposedChange) {
156
200
  )
157
201
  }
158
202
 
159
- // GetData gets generic storage. The default value is an empty string.
160
- func (k Keeper) GetData(ctx sdk.Context, path string) string {
161
- //fmt.Printf("GetData(%s)\n", path);
203
+ // GetEntry gets generic storage. The default value is an empty string.
204
+ func (k Keeper) GetEntry(ctx sdk.Context, path string) types.StorageEntry {
205
+ //fmt.Printf("GetEntry(%s)\n", path);
162
206
  store := ctx.KVStore(k.storeKey)
163
207
  encodedKey := types.PathToEncodedKey(path)
164
- bz := bytes.TrimPrefix(store.Get(encodedKey), types.EncodedDataPrefix)
165
- value := string(bz)
166
- return value
208
+ rawValue := store.Get(encodedKey)
209
+ if len(rawValue) == 0 {
210
+ return types.NewStorageEntryWithNoData(path)
211
+ }
212
+ if bytes.Equal(rawValue, types.EncodedNoDataValue) {
213
+ return types.NewStorageEntryWithNoData(path)
214
+ }
215
+ value, hasPrefix := cutPrefix(rawValue, types.EncodedDataPrefix)
216
+ if !hasPrefix {
217
+ panic(fmt.Errorf("value at path %q starts with unexpected prefix", path))
218
+ }
219
+ return types.NewStorageEntry(path, string(value))
167
220
  }
168
221
 
169
222
  func (k Keeper) getKeyIterator(ctx sdk.Context, path string) db.Iterator {
@@ -192,7 +245,7 @@ func (k Keeper) GetChildren(ctx sdk.Context, path string) *types.Children {
192
245
  // (just an empty string) and exist only to provide linkage to subnodes with
193
246
  // data.
194
247
  func (k Keeper) HasStorage(ctx sdk.Context, path string) bool {
195
- return k.GetData(ctx, path) != ""
248
+ return k.GetEntry(ctx, path).HasData()
196
249
  }
197
250
 
198
251
  // HasEntry tells if a given path has either subnodes or data.
@@ -221,14 +274,14 @@ func (k Keeper) FlushChangeEvents(ctx sdk.Context) {
221
274
  k.changeManager.Rollback(ctx)
222
275
  }
223
276
 
224
- func (k Keeper) SetStorageAndNotify(ctx sdk.Context, path, value string) {
225
- k.changeManager.Track(ctx, k, path, value, false)
226
- k.SetStorage(ctx, path, value)
277
+ func (k Keeper) SetStorageAndNotify(ctx sdk.Context, entry types.StorageEntry) {
278
+ k.changeManager.Track(ctx, k, entry, false)
279
+ k.SetStorage(ctx, entry)
227
280
  }
228
281
 
229
- func (k Keeper) LegacySetStorageAndNotify(ctx sdk.Context, path, value string) {
230
- k.changeManager.Track(ctx, k, path, value, true)
231
- k.SetStorage(ctx, path, value)
282
+ func (k Keeper) LegacySetStorageAndNotify(ctx sdk.Context, entry types.StorageEntry) {
283
+ k.changeManager.Track(ctx, k, entry, true)
284
+ k.SetStorage(ctx, entry)
232
285
  }
233
286
 
234
287
  func (k Keeper) AppendStorageValueAndNotify(ctx sdk.Context, path, value string) error {
@@ -236,7 +289,7 @@ func (k Keeper) AppendStorageValueAndNotify(ctx sdk.Context, path, value string)
236
289
 
237
290
  // Preserve correctly-formatted data within the current block,
238
291
  // otherwise initialize a blank cell.
239
- currentData := k.GetData(ctx, path)
292
+ currentData := k.GetEntry(ctx, path).StringValue()
240
293
  var cell StreamCell
241
294
  _ = json.Unmarshal([]byte(currentData), &cell)
242
295
  if cell.BlockHeight != blockHeight {
@@ -251,7 +304,7 @@ func (k Keeper) AppendStorageValueAndNotify(ctx sdk.Context, path, value string)
251
304
  if err != nil {
252
305
  return err
253
306
  }
254
- k.SetStorageAndNotify(ctx, path, string(bz))
307
+ k.SetStorageAndNotify(ctx, types.NewStorageEntry(path, string(bz)))
255
308
  return nil
256
309
  }
257
310
 
@@ -263,22 +316,27 @@ func componentsToPath(components []string) string {
263
316
  //
264
317
  // Maintains the invariant: path entries exist if and only if self or some
265
318
  // descendant has non-empty storage
266
- func (k Keeper) SetStorage(ctx sdk.Context, path, value string) {
319
+ func (k Keeper) SetStorage(ctx sdk.Context, entry types.StorageEntry) {
267
320
  store := ctx.KVStore(k.storeKey)
321
+ path := entry.Path()
268
322
  encodedKey := types.PathToEncodedKey(path)
269
323
 
270
- if value == "" && !k.HasChildren(ctx, path) {
271
- // We have no children, can delete.
272
- store.Delete(encodedKey)
324
+ if !entry.HasData() {
325
+ if !k.HasChildren(ctx, path) {
326
+ // We have no children, can delete.
327
+ store.Delete(encodedKey)
328
+ } else {
329
+ store.Set(encodedKey, types.EncodedNoDataValue)
330
+ }
273
331
  } else {
274
332
  // Update the value.
275
- bz := bytes.Join([][]byte{types.EncodedDataPrefix, []byte(value)}, []byte{})
333
+ bz := bytes.Join([][]byte{types.EncodedDataPrefix, []byte(entry.StringValue())}, []byte{})
276
334
  store.Set(encodedKey, bz)
277
335
  }
278
336
 
279
337
  // Update our other parent children.
280
338
  pathComponents := strings.Split(path, types.PathSeparator)
281
- if value == "" {
339
+ if !entry.HasData() {
282
340
  // delete placeholder ancestors if they're no longer needed
283
341
  for i := len(pathComponents) - 1; i >= 0; i-- {
284
342
  ancestor := componentsToPath(pathComponents[0:i])
@@ -296,7 +354,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, path, value string) {
296
354
  // The ancestor exists, implying all further ancestors exist, so we can break.
297
355
  break
298
356
  }
299
- store.Set(types.PathToEncodedKey(ancestor), types.EncodedDataPrefix)
357
+ store.Set(types.PathToEncodedKey(ancestor), types.EncodedNoDataValue)
300
358
  }
301
359
  }
302
360
  }
@@ -312,3 +370,7 @@ func (k Keeper) GetStoreName() string {
312
370
  func (k Keeper) GetDataPrefix() []byte {
313
371
  return types.EncodedDataPrefix
314
372
  }
373
+
374
+ func (k Keeper) GetNoDataValue() []byte {
375
+ return types.EncodedNoDataValue
376
+ }
@@ -57,14 +57,20 @@ func TestStorage(t *testing.T) {
57
57
  ctx, keeper := testKit.ctx, testKit.vstorageKeeper
58
58
 
59
59
  // Test that we can store and retrieve a value.
60
- keeper.SetStorage(ctx, "inited", "initValue")
61
- if got := keeper.GetData(ctx, "inited"); got != "initValue" {
60
+ keeper.SetStorage(ctx, types.NewStorageEntry("inited", "initValue"))
61
+ if got := keeper.GetEntry(ctx, "inited").StringValue(); got != "initValue" {
62
62
  t.Errorf("got %q, want %q", got, "initValue")
63
63
  }
64
64
 
65
65
  // Test that unknown children return empty string.
66
- if got := keeper.GetData(ctx, "unknown"); got != "" {
67
- t.Errorf("got %q, want empty string", got)
66
+ if got := keeper.GetEntry(ctx, "unknown"); got.HasData() || got.StringValue() != "" {
67
+ t.Errorf("got %q, want no value", got.StringValue())
68
+ }
69
+
70
+ // Test that we can store and retrieve an empty string value.
71
+ keeper.SetStorage(ctx, types.NewStorageEntry("inited", ""))
72
+ if got := keeper.GetEntry(ctx, "inited"); !got.HasData() || got.StringValue() != "" {
73
+ t.Errorf("got %q, want empty string", got.StringValue())
68
74
  }
69
75
 
70
76
  // Check that our children are updated as expected.
@@ -72,18 +78,18 @@ func TestStorage(t *testing.T) {
72
78
  t.Errorf("got %q children, want [inited]", got.Children)
73
79
  }
74
80
 
75
- keeper.SetStorage(ctx, "key1", "value1")
81
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1", "value1"))
76
82
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"inited", "key1"}) {
77
83
  t.Errorf("got %q children, want [inited,key1]", got.Children)
78
84
  }
79
85
 
80
86
  // Check alphabetical.
81
- keeper.SetStorage(ctx, "alpha2", "value2")
87
+ keeper.SetStorage(ctx, types.NewStorageEntry("alpha2", "value2"))
82
88
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "inited", "key1"}) {
83
89
  t.Errorf("got %q children, want [alpha2,inited,key1]", got.Children)
84
90
  }
85
91
 
86
- keeper.SetStorage(ctx, "beta3", "value3")
92
+ keeper.SetStorage(ctx, types.NewStorageEntry("beta3", "value3"))
87
93
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "beta3", "inited", "key1"}) {
88
94
  t.Errorf("got %q children, want [alpha2,beta3,inited,key1]", got.Children)
89
95
  }
@@ -93,8 +99,8 @@ func TestStorage(t *testing.T) {
93
99
  }
94
100
 
95
101
  // Check adding children.
96
- keeper.SetStorage(ctx, "key1.child1", "value1child")
97
- if got := keeper.GetData(ctx, "key1.child1"); got != "value1child" {
102
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1", "value1child"))
103
+ if got := keeper.GetEntry(ctx, "key1.child1").StringValue(); got != "value1child" {
98
104
  t.Errorf("got %q, want %q", got, "value1child")
99
105
  }
100
106
 
@@ -103,8 +109,8 @@ func TestStorage(t *testing.T) {
103
109
  }
104
110
 
105
111
  // Add a grandchild.
106
- keeper.SetStorage(ctx, "key1.child1.grandchild1", "value1grandchild")
107
- if got := keeper.GetData(ctx, "key1.child1.grandchild1"); got != "value1grandchild" {
112
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1.grandchild1", "value1grandchild"))
113
+ if got := keeper.GetEntry(ctx, "key1.child1.grandchild1").StringValue(); got != "value1grandchild" {
108
114
  t.Errorf("got %q, want %q", got, "value1grandchild")
109
115
  }
110
116
 
@@ -113,7 +119,7 @@ func TestStorage(t *testing.T) {
113
119
  }
114
120
 
115
121
  // Delete the child's contents.
116
- keeper.SetStorage(ctx, "key1.child1", "")
122
+ keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1.child1"))
117
123
  if got := keeper.GetChildren(ctx, "key1"); !childrenEqual(got.Children, []string{"child1"}) {
118
124
  t.Errorf("got %q children, want [child1]", got.Children)
119
125
  }
@@ -123,7 +129,7 @@ func TestStorage(t *testing.T) {
123
129
  }
124
130
 
125
131
  // Delete the grandchild's contents.
126
- keeper.SetStorage(ctx, "key1.child1.grandchild1", "")
132
+ keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1.child1.grandchild1"))
127
133
  if got := keeper.GetChildren(ctx, "key1.child1"); !childrenEqual(got.Children, []string{}) {
128
134
  t.Errorf("got %q children, want []", got.Children)
129
135
  }
@@ -133,13 +139,13 @@ func TestStorage(t *testing.T) {
133
139
  }
134
140
 
135
141
  // See about deleting the parent.
136
- keeper.SetStorage(ctx, "key1", "")
142
+ keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1"))
137
143
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "beta3", "inited"}) {
138
144
  t.Errorf("got %q children, want [alpha2,beta3,inited]", got.Children)
139
145
  }
140
146
 
141
147
  // Do a deep set.
142
- keeper.SetStorage(ctx, "key2.child2.grandchild2", "value2grandchild")
148
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2", "value2grandchild"))
143
149
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "beta3", "inited", "key2"}) {
144
150
  t.Errorf("got %q children, want [alpha2,beta3,inited,key2]", got.Children)
145
151
  }
@@ -151,7 +157,7 @@ func TestStorage(t *testing.T) {
151
157
  }
152
158
 
153
159
  // Do another deep set.
154
- keeper.SetStorage(ctx, "key2.child2.grandchild2a", "value2grandchilda")
160
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2a", "value2grandchilda"))
155
161
  if got := keeper.GetChildren(ctx, "key2.child2"); !childrenEqual(got.Children, []string{"grandchild2", "grandchild2a"}) {
156
162
  t.Errorf("got %q children, want [grandchild2,grandchild2a]", got.Children)
157
163
  }
@@ -160,7 +166,7 @@ func TestStorage(t *testing.T) {
160
166
  expectedExport := []*types.DataEntry{
161
167
  {Path: "alpha2", Value: "value2"},
162
168
  {Path: "beta3", Value: "value3"},
163
- {Path: "inited", Value: "initValue"},
169
+ {Path: "inited", Value: ""},
164
170
  {Path: "key2.child2.grandchild2", Value: "value2grandchild"},
165
171
  {Path: "key2.child2.grandchild2a", Value: "value2grandchilda"},
166
172
  }
@@ -175,12 +181,12 @@ func TestStorageNotify(t *testing.T) {
175
181
  tk := makeTestKit()
176
182
  ctx, keeper := tk.ctx, tk.vstorageKeeper
177
183
 
178
- keeper.SetStorageAndNotify(ctx, "notify.noLegacy", "noLegacyValue")
179
- keeper.LegacySetStorageAndNotify(ctx, "notify.legacy", "legacyValue")
180
- keeper.SetStorageAndNotify(ctx, "notify.noLegacy2", "noLegacyValue2")
181
- keeper.SetStorageAndNotify(ctx, "notify.legacy2", "legacyValue2")
182
- keeper.LegacySetStorageAndNotify(ctx, "notify.legacy2", "legacyValue2b")
183
- keeper.SetStorageAndNotify(ctx, "notify.noLegacy2", "noLegacyValue2b")
184
+ keeper.SetStorageAndNotify(ctx, types.NewStorageEntry("notify.noLegacy", "noLegacyValue"))
185
+ keeper.LegacySetStorageAndNotify(ctx, types.NewStorageEntry("notify.legacy", "legacyValue"))
186
+ keeper.SetStorageAndNotify(ctx, types.NewStorageEntry("notify.noLegacy2", "noLegacyValue2"))
187
+ keeper.SetStorageAndNotify(ctx, types.NewStorageEntry("notify.legacy2", "legacyValue2"))
188
+ keeper.LegacySetStorageAndNotify(ctx, types.NewStorageEntry("notify.legacy2", "legacyValue2b"))
189
+ keeper.SetStorageAndNotify(ctx, types.NewStorageEntry("notify.noLegacy2", "noLegacyValue2b"))
184
190
 
185
191
  // Check the batched events.
186
192
  expectedBeforeFlushEvents := sdk.Events{}
@@ -257,3 +263,44 @@ func TestStorageNotify(t *testing.T) {
257
263
  t.Errorf("got after second flush events %#v, want %#v", got, expectedAfterFlushEvents)
258
264
  }
259
265
  }
266
+
267
+ func TestStorageMigrate(t *testing.T) {
268
+ testKit := makeTestKit()
269
+ ctx, keeper := testKit.ctx, testKit.vstorageKeeper
270
+
271
+ // Simulate a pre-migration storage with empty string as placeholders
272
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1", "value1"))
273
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1.grandchild1", "value1grandchild"))
274
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1", ""))
275
+
276
+ // Do a deep set.
277
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2", "value2grandchild"))
278
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2a", "value2grandchilda"))
279
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2", ""))
280
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2", ""))
281
+
282
+ keeper.MigrateNoDataPlaceholders(ctx)
283
+
284
+ if keeper.HasStorage(ctx, "key1.child1") {
285
+ t.Errorf("has key1.child1, want no value")
286
+ }
287
+ if keeper.HasStorage(ctx, "key2.child2") {
288
+ t.Errorf("has key2.child2, want no value")
289
+ }
290
+ if keeper.HasStorage(ctx, "key2") {
291
+ t.Errorf("has key2, want no value")
292
+ }
293
+
294
+ // Check the export.
295
+ expectedExport := []*types.DataEntry{
296
+ {Path: "key1", Value: "value1"},
297
+ {Path: "key1.child1.grandchild1", Value: "value1grandchild"},
298
+ {Path: "key2.child2.grandchild2", Value: "value2grandchild"},
299
+ {Path: "key2.child2.grandchild2a", Value: "value2grandchilda"},
300
+ }
301
+ got := keeper.ExportStorage(ctx)
302
+ if !reflect.DeepEqual(got, expectedExport) {
303
+ t.Errorf("got export %q, want %q", got, expectedExport)
304
+ }
305
+ keeper.ImportStorage(ctx, got)
306
+ }
@@ -34,12 +34,12 @@ func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier
34
34
 
35
35
  // nolint: unparam
36
36
  func queryData(ctx sdk.Context, path string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) (res []byte, err error) {
37
- value := keeper.GetData(ctx, path)
38
- if value == "" {
37
+ entry := keeper.GetEntry(ctx, path)
38
+ if !entry.HasData() {
39
39
  return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "could not get vstorage path")
40
40
  }
41
41
 
42
- bz, err2 := codec.MarshalJSONIndent(legacyQuerierCdc, types.Data{Value: value})
42
+ bz, err2 := codec.MarshalJSONIndent(legacyQuerierCdc, types.Data{Value: entry.StringValue()})
43
43
  if err2 != nil {
44
44
  return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err2.Error())
45
45
  }
@@ -15,15 +15,22 @@ import (
15
15
  // digits), separated by nul, followed by the path with dots replaced with nul.
16
16
  // So the path key for the empty path is `0\0`.
17
17
  //
18
- // - Store entries contain `\0`-prefixed data, (just `\0` if data is
18
+ // - Store entries exist if and only if self or some descendant has an entry
19
+ // with data.
20
+ //
21
+ // - Store entries with data contain `\0`-prefixed data, (just `\0` if data is
19
22
  // empty).
20
23
  //
21
- // - Store entries exist if and only if self or some descendant has a
22
- // non-empty data entry.
24
+ // - Placeholder store entries contain a single `\255` byte. These are used to
25
+ // indicate that the entry does not have any data (which is different from
26
+ // empty data). Placeholder entries are used when a descendant with data exists,
27
+ // similar to empty non-terminals in the DNS
28
+ // (cf. https://www.rfc-editor.org/rfc/rfc8499.html#section-7 ).
23
29
  var (
24
30
  EncodedKeySeparator = []byte{0}
25
31
  PathSeparator = "."
26
32
  EncodedDataPrefix = []byte{0}
33
+ EncodedNoDataValue = []byte{255}
27
34
  )
28
35
 
29
36
  // EncodedKeyToPath converts a byte slice key to a string path
@@ -1,5 +1,10 @@
1
1
  package types
2
2
 
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ )
7
+
3
8
  func NewData() *Data {
4
9
  return &Data{}
5
10
  }
@@ -7,3 +12,60 @@ func NewData() *Data {
7
12
  func NewChildren() *Children {
8
13
  return &Children{}
9
14
  }
15
+
16
+ type StorageEntry struct {
17
+ path string
18
+ value *string
19
+ }
20
+
21
+ func NewStorageEntry(path string, value string) StorageEntry {
22
+ return StorageEntry{path, &value}
23
+ }
24
+
25
+ func NewStorageEntryWithNoData(path string) StorageEntry {
26
+ return StorageEntry{path, nil}
27
+ }
28
+
29
+ // UnmarshalStorageEntry interprets its argument as a [key: string, value?: string | null]
30
+ // JSON array and returns a corresponding StorageEntry.
31
+ // The key must be a string, and the value (if present) must be a string or null.
32
+ func UnmarshalStorageEntry(msg json.RawMessage) (entry StorageEntry, err error) {
33
+ var generic [2]interface{}
34
+ err = json.Unmarshal(msg, &generic)
35
+
36
+ if err != nil {
37
+ return
38
+ }
39
+
40
+ path, ok := generic[0].(string)
41
+ if !ok {
42
+ err = fmt.Errorf("invalid storage entry path: %q", generic[0])
43
+ return
44
+ }
45
+
46
+ switch generic[1].(type) {
47
+ case string:
48
+ entry = NewStorageEntry(path, generic[1].(string))
49
+ case nil:
50
+ entry = NewStorageEntryWithNoData(path)
51
+ default:
52
+ err = fmt.Errorf("invalid storage entry value: %q", generic[1])
53
+ }
54
+ return
55
+ }
56
+
57
+ func (se StorageEntry) HasData() bool {
58
+ return se.value != nil
59
+ }
60
+
61
+ func (se StorageEntry) Path() string {
62
+ return se.path
63
+ }
64
+
65
+ func (se StorageEntry) StringValue() string {
66
+ if se.value != nil {
67
+ return *se.value
68
+ } else {
69
+ return ""
70
+ }
71
+ }
@@ -8,6 +8,7 @@ import (
8
8
  sdk "github.com/cosmos/cosmos-sdk/types"
9
9
 
10
10
  "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
11
+ "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
11
12
  )
12
13
 
13
14
  type vstorageHandler struct {
@@ -15,21 +16,31 @@ type vstorageHandler struct {
15
16
  }
16
17
 
17
18
  type vstorageMessage struct {
18
- Method string `json:"method"`
19
- Path string `json:"key"` // TODO: rename JSON to "path"
20
- Value string `json:"value"`
19
+ Method string `json:"method"`
20
+ Args []json.RawMessage `json:"args"`
21
21
  }
22
22
 
23
23
  type vstorageStoreKey struct {
24
24
  StoreName string `json:"storeName"`
25
25
  StoreSubkey string `json:"storeSubkey"`
26
26
  DataPrefixBytes string `json:"dataPrefixBytes"`
27
+ NoDataValue string `json:"noDataValue"`
27
28
  }
28
29
 
29
30
  func NewStorageHandler(keeper Keeper) vstorageHandler {
30
31
  return vstorageHandler{keeper: keeper}
31
32
  }
32
33
 
34
+ func unmarshalSinglePathFromArgs(args []json.RawMessage, path *string) error {
35
+ if len(args) == 0 {
36
+ return fmt.Errorf("missing 'path' argument")
37
+ }
38
+ if len(args) != 1 {
39
+ return fmt.Errorf("extra arguments after 'path'")
40
+ }
41
+ return json.Unmarshal(args[0], path)
42
+ }
43
+
33
44
  func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret string, err error) {
34
45
  keeper := sh.keeper
35
46
  msg := new(vstorageMessage)
@@ -57,46 +68,89 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
57
68
  // Handle generic paths.
58
69
  switch msg.Method {
59
70
  case "set":
60
- keeper.SetStorageAndNotify(cctx.Context, msg.Path, msg.Value)
71
+ for _, arg := range msg.Args {
72
+ var entry types.StorageEntry
73
+ entry, err = types.UnmarshalStorageEntry(arg)
74
+ if err != nil {
75
+ return
76
+ }
77
+ keeper.SetStorageAndNotify(cctx.Context, entry)
78
+ }
61
79
  return "true", nil
62
80
 
63
81
  // We sometimes need to use LegacySetStorageAndNotify, because the solo's
64
82
  // chain-cosmos-sdk.js consumes legacy events for `mailbox.*` and `egress.*`.
65
83
  // FIXME: Use just "set" and remove this case.
66
84
  case "legacySet":
67
- //fmt.Printf("giving Keeper.SetStorage(%s) %s\n", msg.Key, msg.Value)
68
- keeper.LegacySetStorageAndNotify(cctx.Context, msg.Path, msg.Value)
85
+ for _, arg := range msg.Args {
86
+ var entry types.StorageEntry
87
+ entry, err = types.UnmarshalStorageEntry(arg)
88
+ if err != nil {
89
+ return
90
+ }
91
+ //fmt.Printf("giving Keeper.SetStorage(%s) %s\n", entry.Path(), entry.Value())
92
+ keeper.LegacySetStorageAndNotify(cctx.Context, entry)
93
+ }
69
94
  return "true", nil
70
95
 
71
96
  case "setWithoutNotify":
72
- keeper.SetStorage(cctx.Context, msg.Path, msg.Value)
97
+ for _, arg := range msg.Args {
98
+ var entry types.StorageEntry
99
+ entry, err = types.UnmarshalStorageEntry(arg)
100
+ if err != nil {
101
+ return
102
+ }
103
+ keeper.SetStorage(cctx.Context, entry)
104
+ }
73
105
  return "true", nil
74
106
 
75
107
  case "append":
76
- err = keeper.AppendStorageValueAndNotify(cctx.Context, msg.Path, msg.Value)
77
- if err != nil {
78
- return "", err
108
+ for _, arg := range msg.Args {
109
+ var entry types.StorageEntry
110
+ entry, err = types.UnmarshalStorageEntry(arg)
111
+ if err != nil {
112
+ return
113
+ }
114
+ if !entry.HasData() {
115
+ err = fmt.Errorf("no value for append entry with path: %q", entry.Path())
116
+ return
117
+ }
118
+ err = keeper.AppendStorageValueAndNotify(cctx.Context, entry.Path(), entry.StringValue())
119
+ if err != nil {
120
+ return
121
+ }
79
122
  }
80
123
  return "true", nil
81
124
 
82
125
  case "get":
83
126
  // Note that "get" does not (currently) unwrap a StreamCell.
84
- value := keeper.GetData(cctx.Context, msg.Path)
85
- if value == "" {
127
+ var path string
128
+ err = unmarshalSinglePathFromArgs(msg.Args, &path)
129
+ if err != nil {
130
+ return
131
+ }
132
+
133
+ entry := keeper.GetEntry(cctx.Context, path)
134
+ if !entry.HasData() {
86
135
  return "null", nil
87
136
  }
88
- //fmt.Printf("Keeper.GetStorage gave us %bz\n", value)
89
- bz, err := json.Marshal(value)
137
+ bz, err := json.Marshal(entry.StringValue())
90
138
  if err != nil {
91
139
  return "", err
92
140
  }
93
141
  return string(bz), nil
94
142
 
95
143
  case "getStoreKey":
144
+ var path string
145
+ err = unmarshalSinglePathFromArgs(msg.Args, &path)
146
+ if err != nil {
147
+ return
148
+ }
96
149
  value := vstorageStoreKey{
97
150
  StoreName: keeper.GetStoreName(),
98
- StoreSubkey: string(keeper.PathToEncodedKey(msg.Path)),
151
+ StoreSubkey: string(keeper.PathToEncodedKey(path)),
99
152
  DataPrefixBytes: string(keeper.GetDataPrefix()),
153
+ NoDataValue: string(keeper.GetNoDataValue()),
100
154
  }
101
155
  bz, err := json.Marshal(value)
102
156
  if err != nil {
@@ -105,15 +159,25 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
105
159
  return string(bz), nil
106
160
 
107
161
  case "has":
108
- value := keeper.GetData(cctx.Context, msg.Path)
109
- if value == "" {
162
+ var path string
163
+ err = unmarshalSinglePathFromArgs(msg.Args, &path)
164
+ if err != nil {
165
+ return
166
+ }
167
+ value := keeper.HasStorage(cctx.Context, path)
168
+ if !value {
110
169
  return "false", nil
111
170
  }
112
171
  return "true", nil
113
172
 
114
173
  // TODO: "keys" is deprecated
115
174
  case "children", "keys":
116
- children := keeper.GetChildren(cctx.Context, msg.Path)
175
+ var path string
176
+ err = unmarshalSinglePathFromArgs(msg.Args, &path)
177
+ if err != nil {
178
+ return
179
+ }
180
+ children := keeper.GetChildren(cctx.Context, path)
117
181
  if children.Children == nil {
118
182
  return "[]", nil
119
183
  }
@@ -124,12 +188,17 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
124
188
  return string(bytes), nil
125
189
 
126
190
  case "entries":
127
- children := keeper.GetChildren(cctx.Context, msg.Path)
191
+ var path string
192
+ err = unmarshalSinglePathFromArgs(msg.Args, &path)
193
+ if err != nil {
194
+ return
195
+ }
196
+ children := keeper.GetChildren(cctx.Context, path)
128
197
  ents := make([][]string, len(children.Children))
129
198
  for i, child := range children.Children {
130
199
  ents[i] = make([]string, 2)
131
200
  ents[i][0] = child
132
- ents[i][i] = keeper.GetData(cctx.Context, fmt.Sprintf("%s.%s", msg.Path, child))
201
+ ents[i][i] = keeper.GetEntry(cctx.Context, fmt.Sprintf("%s.%s", path, child)).StringValue()
133
202
  }
134
203
  bytes, err := json.Marshal(ents)
135
204
  if err != nil {
@@ -138,10 +207,15 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
138
207
  return string(bytes), nil
139
208
 
140
209
  case "values":
141
- children := keeper.GetChildren(cctx.Context, msg.Path)
210
+ var path string
211
+ err = unmarshalSinglePathFromArgs(msg.Args, &path)
212
+ if err != nil {
213
+ return
214
+ }
215
+ children := keeper.GetChildren(cctx.Context, path)
142
216
  vals := make([]string, len(children.Children))
143
217
  for i, child := range children.Children {
144
- vals[i] = keeper.GetData(cctx.Context, fmt.Sprintf("%s.%s", msg.Path, child))
218
+ vals[i] = keeper.GetEntry(cctx.Context, fmt.Sprintf("%s.%s", path, child)).StringValue()
145
219
  }
146
220
  bytes, err := json.Marshal(vals)
147
221
  if err != nil {
@@ -150,7 +224,12 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
150
224
  return string(bytes), nil
151
225
 
152
226
  case "size":
153
- children := keeper.GetChildren(cctx.Context, msg.Path)
227
+ var path string
228
+ err = unmarshalSinglePathFromArgs(msg.Args, &path)
229
+ if err != nil {
230
+ return
231
+ }
232
+ children := keeper.GetChildren(cctx.Context, path)
154
233
  if children.Children == nil {
155
234
  return "0", nil
156
235
  }