@agoric/cosmos 0.33.0 → 0.33.1-dev-800c2bb.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.
Files changed (43) hide show
  1. package/Makefile +1 -1
  2. package/app/app.go +109 -16
  3. package/app/const.go +2 -2
  4. package/daemon/cmd/root.go +8 -13
  5. package/package.json +2 -2
  6. package/proto/agoric/swingset/msgs.proto +10 -0
  7. package/proto/agoric/swingset/swingset.proto +14 -0
  8. package/x/swingset/abci.go +24 -17
  9. package/x/swingset/alias.go +1 -0
  10. package/x/swingset/client/cli/tx.go +18 -0
  11. package/x/swingset/genesis.go +11 -6
  12. package/x/swingset/keeper/keeper.go +131 -56
  13. package/x/swingset/keeper/migrations.go +27 -0
  14. package/x/swingset/keeper/msg_server.go +23 -6
  15. package/x/swingset/keeper/proposal.go +1 -1
  16. package/x/swingset/keeper/querier.go +3 -3
  17. package/x/swingset/keeper/snapshotter.go +491 -0
  18. package/x/swingset/keeper/snapshotter_test.go +185 -0
  19. package/x/swingset/legacy/v32/params.go +37 -0
  20. package/x/swingset/legacy/v32/params_test.go +133 -0
  21. package/x/swingset/module.go +6 -1
  22. package/x/swingset/types/default-params.go +5 -2
  23. package/x/swingset/types/expected_keepers.go +1 -0
  24. package/x/swingset/types/msgs.go +156 -10
  25. package/x/swingset/types/msgs.pb.go +142 -47
  26. package/x/swingset/types/msgs_test.go +174 -0
  27. package/x/swingset/types/swingset.pb.go +280 -51
  28. package/x/swingset/types/types.go +1 -1
  29. package/x/vbank/README.md +1 -1
  30. package/x/vbank/keeper/keeper.go +15 -1
  31. package/x/vbank/module.go +13 -1
  32. package/x/vbank/types/expected_keepers.go +3 -1
  33. package/x/vbank/vbank.go +8 -0
  34. package/x/vbank/vbank_test.go +70 -9
  35. package/x/vstorage/keeper/grpc_query.go +2 -2
  36. package/x/vstorage/keeper/keeper.go +269 -38
  37. package/x/vstorage/keeper/keeper_test.go +162 -17
  38. package/x/vstorage/keeper/querier.go +4 -4
  39. package/x/vstorage/module.go +2 -0
  40. package/x/vstorage/types/path_keys.go +10 -3
  41. package/x/vstorage/types/types.go +66 -0
  42. package/x/vstorage/vstorage.go +118 -25
  43. package/x/vstorage/vstorage_test.go +297 -0
@@ -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,10 @@ package keeper
3
3
  import (
4
4
  "bytes"
5
5
  "encoding/json"
6
+ "errors"
7
+ "fmt"
8
+ "math/big"
9
+ "sort"
6
10
  "strconv"
7
11
  "strings"
8
12
 
@@ -23,31 +27,147 @@ type StreamCell struct {
23
27
  Values []string `json:"values"`
24
28
  }
25
29
 
30
+ type ProposedChange struct {
31
+ Path string
32
+ ValueFromLastBlock string
33
+ NewValue string
34
+ LegacyEvents bool
35
+ }
36
+
37
+ type ChangeManager interface {
38
+ Track(ctx sdk.Context, k Keeper, entry types.StorageEntry, isLegacy bool)
39
+ EmitEvents(ctx sdk.Context, k Keeper)
40
+ Rollback(ctx sdk.Context)
41
+ }
42
+
43
+ type BatchingChangeManager struct {
44
+ // Map from storage path to proposed change.
45
+ changes map[string]*ProposedChange
46
+ }
47
+
48
+ var _ ChangeManager = (*BatchingChangeManager)(nil)
49
+
50
+ // 2 ** 256 - 1
51
+ var MaxSDKInt = sdk.NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1)))
52
+
53
+ // TODO: Use bytes.CutPrefix once we can rely upon go >= 1.20.
54
+ func cutPrefix(s, prefix []byte) (after []byte, found bool) {
55
+ if !bytes.HasPrefix(s, prefix) {
56
+ return s, false
57
+ }
58
+ return s[len(prefix):], true
59
+ }
60
+
26
61
  // Keeper maintains the link to data storage and exposes getter/setter methods
27
62
  // for the various parts of the state machine
28
63
  type Keeper struct {
29
- storeKey sdk.StoreKey
64
+ changeManager ChangeManager
65
+ storeKey sdk.StoreKey
66
+ }
67
+
68
+ func (bcm *BatchingChangeManager) Track(ctx sdk.Context, k Keeper, entry types.StorageEntry, isLegacy bool) {
69
+ path := entry.Path()
70
+ // TODO: differentiate between deletion and setting empty string?
71
+ // Using empty string for deletion for backwards compatibility
72
+ value := entry.StringValue()
73
+ if change, ok := bcm.changes[path]; ok {
74
+ change.NewValue = value
75
+ if isLegacy {
76
+ change.LegacyEvents = true
77
+ }
78
+ return
79
+ }
80
+ bcm.changes[path] = &ProposedChange{
81
+ Path: path,
82
+ NewValue: value,
83
+ ValueFromLastBlock: k.GetEntry(ctx, path).StringValue(),
84
+ LegacyEvents: isLegacy,
85
+ }
86
+ }
87
+
88
+ func (bcm *BatchingChangeManager) Rollback(ctx sdk.Context) {
89
+ bcm.changes = make(map[string]*ProposedChange)
90
+ }
91
+
92
+ // EmitEvents emits events for all actual changes.
93
+ // This does not clear the cache, so the caller must call Rollback() to do so.
94
+ func (bcm *BatchingChangeManager) EmitEvents(ctx sdk.Context, k Keeper) {
95
+ changes := bcm.changes
96
+ if len(changes) == 0 {
97
+ return
98
+ }
99
+
100
+ // Deterministic order.
101
+ sortedPaths := make([]string, 0, len(changes))
102
+ for path := range changes {
103
+ sortedPaths = append(sortedPaths, path)
104
+ }
105
+ sort.Strings(sortedPaths)
106
+
107
+ for _, path := range sortedPaths {
108
+ change := bcm.changes[path]
109
+ k.EmitChange(ctx, change)
110
+ }
111
+ }
112
+
113
+ // The BatchingChangeManager needs to be a pointer because its state is mutated.
114
+ func NewBatchingChangeManager() *BatchingChangeManager {
115
+ bcm := BatchingChangeManager{changes: make(map[string]*ProposedChange)}
116
+ return &bcm
30
117
  }
31
118
 
32
119
  func NewKeeper(storeKey sdk.StoreKey) Keeper {
33
- return Keeper{storeKey}
120
+ return Keeper{
121
+ storeKey: storeKey,
122
+ changeManager: NewBatchingChangeManager(),
123
+ }
34
124
  }
35
125
 
36
126
  // ExportStorage fetches all storage
37
127
  func (k Keeper) ExportStorage(ctx sdk.Context) []*types.DataEntry {
128
+ return k.ExportStorageFromPrefix(ctx, "")
129
+ }
130
+
131
+ // ExportStorageFromPrefix fetches storage only under the supplied pathPrefix.
132
+ func (k Keeper) ExportStorageFromPrefix(ctx sdk.Context, pathPrefix string) []*types.DataEntry {
38
133
  store := ctx.KVStore(k.storeKey)
39
134
 
135
+ if len(pathPrefix) > 0 {
136
+ if err := types.ValidatePath(pathPrefix); err != nil {
137
+ panic(err)
138
+ }
139
+ pathPrefix = pathPrefix + types.PathSeparator
140
+ }
141
+
142
+ // since vstorage encodes keys with a prefix indicating the number of path
143
+ // elements, exporting all entries under a given path cannot use a prefix
144
+ // iterator. Instead we iterate over the whole vstorage content and check
145
+ // whether each entry matches the path prefix. This choice assumes most
146
+ // entries will be exported. An alternative implementation would be to
147
+ // recursively list all children under the pathPrefix, and export them.
148
+
40
149
  iterator := sdk.KVStorePrefixIterator(store, nil)
41
150
 
42
151
  exported := []*types.DataEntry{}
43
152
  defer iterator.Close()
44
153
  for ; iterator.Valid(); iterator.Next() {
154
+ rawValue := iterator.Value()
155
+ if len(rawValue) == 0 {
156
+ continue
157
+ }
158
+ if bytes.Equal(rawValue, types.EncodedNoDataValue) {
159
+ continue
160
+ }
45
161
  path := types.EncodedKeyToPath(iterator.Key())
46
- value := string(bytes.TrimPrefix(iterator.Value(), types.EncodedDataPrefix))
47
- if len(value) == 0 {
162
+ if !strings.HasPrefix(path, pathPrefix) {
48
163
  continue
49
164
  }
50
- entry := types.DataEntry{Path: path, Value: value}
165
+ value, hasPrefix := cutPrefix(rawValue, types.EncodedDataPrefix)
166
+ if !hasPrefix {
167
+ panic(fmt.Errorf("value at path %q starts with unexpected prefix", path))
168
+ }
169
+ path = path[len(pathPrefix):]
170
+ entry := types.DataEntry{Path: path, Value: string(value)}
51
171
  exported = append(exported, &entry)
52
172
  }
53
173
  return exported
@@ -57,18 +177,74 @@ func (k Keeper) ImportStorage(ctx sdk.Context, entries []*types.DataEntry) {
57
177
  for _, entry := range entries {
58
178
  // This set does the bookkeeping for us in case the entries aren't a
59
179
  // complete tree.
60
- k.SetStorage(ctx, entry.Path, entry.Value)
180
+ k.SetStorage(ctx, types.NewStorageEntry(entry.Path, entry.Value))
61
181
  }
62
182
  }
63
183
 
64
- // GetData gets generic storage. The default value is an empty string.
65
- func (k Keeper) GetData(ctx sdk.Context, path string) string {
66
- //fmt.Printf("GetData(%s)\n", path);
184
+ func (k Keeper) MigrateNoDataPlaceholders(ctx sdk.Context) {
185
+ store := ctx.KVStore(k.storeKey)
186
+
187
+ iterator := sdk.KVStorePrefixIterator(store, nil)
188
+
189
+ // Copy empty keys first since cosmos stores do not support writing keys
190
+ // while an iterator is open over the domain
191
+ emptyKeys := [][]byte{}
192
+ for ; iterator.Valid(); iterator.Next() {
193
+ rawValue := iterator.Value()
194
+ if bytes.Equal(rawValue, types.EncodedDataPrefix) {
195
+ key := iterator.Key()
196
+ clonedKey := make([]byte, len(key))
197
+ copy(clonedKey, key)
198
+ emptyKeys = append(emptyKeys, clonedKey)
199
+ }
200
+ }
201
+ iterator.Close()
202
+
203
+ for _, key := range emptyKeys {
204
+ store.Set(key, types.EncodedNoDataValue)
205
+ }
206
+ }
207
+
208
+ func (k Keeper) EmitChange(ctx sdk.Context, change *ProposedChange) {
209
+ if change.NewValue == change.ValueFromLastBlock {
210
+ // No change.
211
+ return
212
+ }
213
+
214
+ if change.LegacyEvents {
215
+ // Emit the legacy change event.
216
+ ctx.EventManager().EmitEvent(
217
+ types.NewLegacyStorageEvent(change.Path, change.NewValue),
218
+ )
219
+ }
220
+
221
+ // Emit the new state change event.
222
+ ctx.EventManager().EmitEvent(
223
+ agoric.NewStateChangeEvent(
224
+ k.GetStoreName(),
225
+ k.PathToEncodedKey(change.Path),
226
+ []byte(change.NewValue),
227
+ ),
228
+ )
229
+ }
230
+
231
+ // GetEntry gets generic storage. The default value is an empty string.
232
+ func (k Keeper) GetEntry(ctx sdk.Context, path string) types.StorageEntry {
233
+ //fmt.Printf("GetEntry(%s)\n", path);
67
234
  store := ctx.KVStore(k.storeKey)
68
235
  encodedKey := types.PathToEncodedKey(path)
69
- bz := bytes.TrimPrefix(store.Get(encodedKey), types.EncodedDataPrefix)
70
- value := string(bz)
71
- return value
236
+ rawValue := store.Get(encodedKey)
237
+ if len(rawValue) == 0 {
238
+ return types.NewStorageEntryWithNoData(path)
239
+ }
240
+ if bytes.Equal(rawValue, types.EncodedNoDataValue) {
241
+ return types.NewStorageEntryWithNoData(path)
242
+ }
243
+ value, hasPrefix := cutPrefix(rawValue, types.EncodedDataPrefix)
244
+ if !hasPrefix {
245
+ panic(fmt.Errorf("value at path %q starts with unexpected prefix", path))
246
+ }
247
+ return types.NewStorageEntry(path, string(value))
72
248
  }
73
249
 
74
250
  func (k Keeper) getKeyIterator(ctx sdk.Context, path string) db.Iterator {
@@ -97,7 +273,7 @@ func (k Keeper) GetChildren(ctx sdk.Context, path string) *types.Children {
97
273
  // (just an empty string) and exist only to provide linkage to subnodes with
98
274
  // data.
99
275
  func (k Keeper) HasStorage(ctx sdk.Context, path string) bool {
100
- return k.GetData(ctx, path) != ""
276
+ return k.GetEntry(ctx, path).HasData()
101
277
  }
102
278
 
103
279
  // HasEntry tells if a given path has either subnodes or data.
@@ -117,26 +293,23 @@ func (k Keeper) HasChildren(ctx sdk.Context, path string) bool {
117
293
  return iterator.Valid()
118
294
  }
119
295
 
120
- func (k Keeper) LegacySetStorageAndNotify(ctx sdk.Context, path, value string) {
121
- k.SetStorage(ctx, path, value)
296
+ func (k Keeper) NewChangeBatch(ctx sdk.Context) {
297
+ k.changeManager.Rollback(ctx)
298
+ }
122
299
 
123
- // Emit the legacy change event.
124
- ctx.EventManager().EmitEvent(
125
- types.NewLegacyStorageEvent(path, value),
126
- )
300
+ func (k Keeper) FlushChangeEvents(ctx sdk.Context) {
301
+ k.changeManager.EmitEvents(ctx, k)
302
+ k.changeManager.Rollback(ctx)
127
303
  }
128
304
 
129
- func (k Keeper) SetStorageAndNotify(ctx sdk.Context, path, value string) {
130
- k.LegacySetStorageAndNotify(ctx, path, value)
305
+ func (k Keeper) SetStorageAndNotify(ctx sdk.Context, entry types.StorageEntry) {
306
+ k.changeManager.Track(ctx, k, entry, false)
307
+ k.SetStorage(ctx, entry)
308
+ }
131
309
 
132
- // Emit the new state change event.
133
- ctx.EventManager().EmitEvent(
134
- agoric.NewStateChangeEvent(
135
- k.GetStoreName(),
136
- k.PathToEncodedKey(path),
137
- []byte(value),
138
- ),
139
- )
310
+ func (k Keeper) LegacySetStorageAndNotify(ctx sdk.Context, entry types.StorageEntry) {
311
+ k.changeManager.Track(ctx, k, entry, true)
312
+ k.SetStorage(ctx, entry)
140
313
  }
141
314
 
142
315
  func (k Keeper) AppendStorageValueAndNotify(ctx sdk.Context, path, value string) error {
@@ -144,7 +317,7 @@ func (k Keeper) AppendStorageValueAndNotify(ctx sdk.Context, path, value string)
144
317
 
145
318
  // Preserve correctly-formatted data within the current block,
146
319
  // otherwise initialize a blank cell.
147
- currentData := k.GetData(ctx, path)
320
+ currentData := k.GetEntry(ctx, path).StringValue()
148
321
  var cell StreamCell
149
322
  _ = json.Unmarshal([]byte(currentData), &cell)
150
323
  if cell.BlockHeight != blockHeight {
@@ -159,7 +332,7 @@ func (k Keeper) AppendStorageValueAndNotify(ctx sdk.Context, path, value string)
159
332
  if err != nil {
160
333
  return err
161
334
  }
162
- k.SetStorageAndNotify(ctx, path, string(bz))
335
+ k.SetStorageAndNotify(ctx, types.NewStorageEntry(path, string(bz)))
163
336
  return nil
164
337
  }
165
338
 
@@ -171,22 +344,27 @@ func componentsToPath(components []string) string {
171
344
  //
172
345
  // Maintains the invariant: path entries exist if and only if self or some
173
346
  // descendant has non-empty storage
174
- func (k Keeper) SetStorage(ctx sdk.Context, path, value string) {
347
+ func (k Keeper) SetStorage(ctx sdk.Context, entry types.StorageEntry) {
175
348
  store := ctx.KVStore(k.storeKey)
349
+ path := entry.Path()
176
350
  encodedKey := types.PathToEncodedKey(path)
177
351
 
178
- if value == "" && !k.HasChildren(ctx, path) {
179
- // We have no children, can delete.
180
- store.Delete(encodedKey)
352
+ if !entry.HasData() {
353
+ if !k.HasChildren(ctx, path) {
354
+ // We have no children, can delete.
355
+ store.Delete(encodedKey)
356
+ } else {
357
+ store.Set(encodedKey, types.EncodedNoDataValue)
358
+ }
181
359
  } else {
182
360
  // Update the value.
183
- bz := bytes.Join([][]byte{types.EncodedDataPrefix, []byte(value)}, []byte{})
361
+ bz := bytes.Join([][]byte{types.EncodedDataPrefix, []byte(entry.StringValue())}, []byte{})
184
362
  store.Set(encodedKey, bz)
185
363
  }
186
364
 
187
365
  // Update our other parent children.
188
366
  pathComponents := strings.Split(path, types.PathSeparator)
189
- if value == "" {
367
+ if !entry.HasData() {
190
368
  // delete placeholder ancestors if they're no longer needed
191
369
  for i := len(pathComponents) - 1; i >= 0; i-- {
192
370
  ancestor := componentsToPath(pathComponents[0:i])
@@ -204,7 +382,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, path, value string) {
204
382
  // The ancestor exists, implying all further ancestors exist, so we can break.
205
383
  break
206
384
  }
207
- store.Set(types.PathToEncodedKey(ancestor), types.EncodedDataPrefix)
385
+ store.Set(types.PathToEncodedKey(ancestor), types.EncodedNoDataValue)
208
386
  }
209
387
  }
210
388
  }
@@ -220,3 +398,56 @@ func (k Keeper) GetStoreName() string {
220
398
  func (k Keeper) GetDataPrefix() []byte {
221
399
  return types.EncodedDataPrefix
222
400
  }
401
+
402
+ func (k Keeper) GetNoDataValue() []byte {
403
+ return types.EncodedNoDataValue
404
+ }
405
+
406
+ func (k Keeper) getIntValue(ctx sdk.Context, path string) (sdk.Int, error) {
407
+ indexEntry := k.GetEntry(ctx, path)
408
+ if !indexEntry.HasData() {
409
+ return sdk.NewInt(0), nil
410
+ }
411
+
412
+ index, ok := sdk.NewIntFromString(indexEntry.StringValue())
413
+ if !ok {
414
+ return index, fmt.Errorf("couldn't parse %s as Int: %s", path, indexEntry.StringValue())
415
+ }
416
+ return index, nil
417
+ }
418
+
419
+ func (k Keeper) GetQueueLength(ctx sdk.Context, queuePath string) (sdk.Int, error) {
420
+ head, err := k.getIntValue(ctx, queuePath+".head")
421
+ if err != nil {
422
+ return sdk.NewInt(0), err
423
+ }
424
+ tail, err := k.getIntValue(ctx, queuePath+".tail")
425
+ if err != nil {
426
+ return sdk.NewInt(0), err
427
+ }
428
+ // The tail index is exclusive
429
+ return tail.Sub(head), nil
430
+ }
431
+
432
+ func (k Keeper) PushQueueItem(ctx sdk.Context, queuePath string, value string) error {
433
+ // Get the current queue tail, defaulting to zero if its vstorage doesn't exist.
434
+ // The `tail` is the value of the next index to be inserted
435
+ tail, err := k.getIntValue(ctx, queuePath+".tail")
436
+ if err != nil {
437
+ return err
438
+ }
439
+
440
+ if tail.Equal(MaxSDKInt) {
441
+ return errors.New(queuePath + " overflow")
442
+ }
443
+ nextTail := tail.Add(sdk.NewInt(1))
444
+
445
+ // Set the vstorage corresponding to the queue entry for the current tail.
446
+ path := queuePath + "." + tail.String()
447
+ k.SetStorage(ctx, types.NewStorageEntry(path, value))
448
+
449
+ // Update the tail to point to the next available entry.
450
+ path = queuePath + ".tail"
451
+ k.SetStorage(ctx, types.NewStorageEntry(path, nextTail.String()))
452
+ return nil
453
+ }
@@ -10,6 +10,7 @@ import (
10
10
  storetypes "github.com/cosmos/cosmos-sdk/store/types"
11
11
  sdk "github.com/cosmos/cosmos-sdk/types"
12
12
 
13
+ abci "github.com/tendermint/tendermint/abci/types"
13
14
  "github.com/tendermint/tendermint/libs/log"
14
15
  tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
15
16
  dbm "github.com/tendermint/tm-db"
@@ -56,14 +57,20 @@ func TestStorage(t *testing.T) {
56
57
  ctx, keeper := testKit.ctx, testKit.vstorageKeeper
57
58
 
58
59
  // Test that we can store and retrieve a value.
59
- keeper.SetStorage(ctx, "inited", "initValue")
60
- 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" {
61
62
  t.Errorf("got %q, want %q", got, "initValue")
62
63
  }
63
64
 
64
65
  // Test that unknown children return empty string.
65
- if got := keeper.GetData(ctx, "unknown"); got != "" {
66
- 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())
67
74
  }
68
75
 
69
76
  // Check that our children are updated as expected.
@@ -71,18 +78,18 @@ func TestStorage(t *testing.T) {
71
78
  t.Errorf("got %q children, want [inited]", got.Children)
72
79
  }
73
80
 
74
- keeper.SetStorage(ctx, "key1", "value1")
81
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1", "value1"))
75
82
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"inited", "key1"}) {
76
83
  t.Errorf("got %q children, want [inited,key1]", got.Children)
77
84
  }
78
85
 
79
86
  // Check alphabetical.
80
- keeper.SetStorage(ctx, "alpha2", "value2")
87
+ keeper.SetStorage(ctx, types.NewStorageEntry("alpha2", "value2"))
81
88
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "inited", "key1"}) {
82
89
  t.Errorf("got %q children, want [alpha2,inited,key1]", got.Children)
83
90
  }
84
91
 
85
- keeper.SetStorage(ctx, "beta3", "value3")
92
+ keeper.SetStorage(ctx, types.NewStorageEntry("beta3", "value3"))
86
93
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "beta3", "inited", "key1"}) {
87
94
  t.Errorf("got %q children, want [alpha2,beta3,inited,key1]", got.Children)
88
95
  }
@@ -92,8 +99,8 @@ func TestStorage(t *testing.T) {
92
99
  }
93
100
 
94
101
  // Check adding children.
95
- keeper.SetStorage(ctx, "key1.child1", "value1child")
96
- 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" {
97
104
  t.Errorf("got %q, want %q", got, "value1child")
98
105
  }
99
106
 
@@ -102,8 +109,8 @@ func TestStorage(t *testing.T) {
102
109
  }
103
110
 
104
111
  // Add a grandchild.
105
- keeper.SetStorage(ctx, "key1.child1.grandchild1", "value1grandchild")
106
- 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" {
107
114
  t.Errorf("got %q, want %q", got, "value1grandchild")
108
115
  }
109
116
 
@@ -112,7 +119,7 @@ func TestStorage(t *testing.T) {
112
119
  }
113
120
 
114
121
  // Delete the child's contents.
115
- keeper.SetStorage(ctx, "key1.child1", "")
122
+ keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1.child1"))
116
123
  if got := keeper.GetChildren(ctx, "key1"); !childrenEqual(got.Children, []string{"child1"}) {
117
124
  t.Errorf("got %q children, want [child1]", got.Children)
118
125
  }
@@ -122,7 +129,7 @@ func TestStorage(t *testing.T) {
122
129
  }
123
130
 
124
131
  // Delete the grandchild's contents.
125
- keeper.SetStorage(ctx, "key1.child1.grandchild1", "")
132
+ keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1.child1.grandchild1"))
126
133
  if got := keeper.GetChildren(ctx, "key1.child1"); !childrenEqual(got.Children, []string{}) {
127
134
  t.Errorf("got %q children, want []", got.Children)
128
135
  }
@@ -132,13 +139,13 @@ func TestStorage(t *testing.T) {
132
139
  }
133
140
 
134
141
  // See about deleting the parent.
135
- keeper.SetStorage(ctx, "key1", "")
142
+ keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1"))
136
143
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "beta3", "inited"}) {
137
144
  t.Errorf("got %q children, want [alpha2,beta3,inited]", got.Children)
138
145
  }
139
146
 
140
147
  // Do a deep set.
141
- keeper.SetStorage(ctx, "key2.child2.grandchild2", "value2grandchild")
148
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2", "value2grandchild"))
142
149
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "beta3", "inited", "key2"}) {
143
150
  t.Errorf("got %q children, want [alpha2,beta3,inited,key2]", got.Children)
144
151
  }
@@ -150,7 +157,7 @@ func TestStorage(t *testing.T) {
150
157
  }
151
158
 
152
159
  // Do another deep set.
153
- keeper.SetStorage(ctx, "key2.child2.grandchild2a", "value2grandchilda")
160
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2a", "value2grandchilda"))
154
161
  if got := keeper.GetChildren(ctx, "key2.child2"); !childrenEqual(got.Children, []string{"grandchild2", "grandchild2a"}) {
155
162
  t.Errorf("got %q children, want [grandchild2,grandchild2a]", got.Children)
156
163
  }
@@ -159,7 +166,145 @@ func TestStorage(t *testing.T) {
159
166
  expectedExport := []*types.DataEntry{
160
167
  {Path: "alpha2", Value: "value2"},
161
168
  {Path: "beta3", Value: "value3"},
162
- {Path: "inited", Value: "initValue"},
169
+ {Path: "inited", Value: ""},
170
+ {Path: "key2.child2.grandchild2", Value: "value2grandchild"},
171
+ {Path: "key2.child2.grandchild2a", Value: "value2grandchilda"},
172
+ }
173
+ gotExport := keeper.ExportStorage(ctx)
174
+ if !reflect.DeepEqual(gotExport, expectedExport) {
175
+ t.Errorf("got export %q, want %q", gotExport, expectedExport)
176
+ }
177
+
178
+ // Check the export.
179
+ expectedKey2Export := []*types.DataEntry{
180
+ {Path: "child2.grandchild2", Value: "value2grandchild"},
181
+ {Path: "child2.grandchild2a", Value: "value2grandchilda"},
182
+ }
183
+ if got := keeper.ExportStorageFromPrefix(ctx, "key2"); !reflect.DeepEqual(got, expectedKey2Export) {
184
+ t.Errorf("got export %q, want %q", got, expectedKey2Export)
185
+ }
186
+
187
+ keeper.ImportStorage(ctx, gotExport)
188
+ }
189
+
190
+ func TestStorageNotify(t *testing.T) {
191
+ tk := makeTestKit()
192
+ ctx, keeper := tk.ctx, tk.vstorageKeeper
193
+
194
+ keeper.SetStorageAndNotify(ctx, types.NewStorageEntry("notify.noLegacy", "noLegacyValue"))
195
+ keeper.LegacySetStorageAndNotify(ctx, types.NewStorageEntry("notify.legacy", "legacyValue"))
196
+ keeper.SetStorageAndNotify(ctx, types.NewStorageEntry("notify.noLegacy2", "noLegacyValue2"))
197
+ keeper.SetStorageAndNotify(ctx, types.NewStorageEntry("notify.legacy2", "legacyValue2"))
198
+ keeper.LegacySetStorageAndNotify(ctx, types.NewStorageEntry("notify.legacy2", "legacyValue2b"))
199
+ keeper.SetStorageAndNotify(ctx, types.NewStorageEntry("notify.noLegacy2", "noLegacyValue2b"))
200
+
201
+ // Check the batched events.
202
+ expectedBeforeFlushEvents := sdk.Events{}
203
+ if got := ctx.EventManager().Events(); !reflect.DeepEqual(got, expectedBeforeFlushEvents) {
204
+ t.Errorf("got before flush events %#v, want %#v", got, expectedBeforeFlushEvents)
205
+ }
206
+
207
+ expectedAfterFlushEvents := sdk.Events{
208
+ {
209
+ Type: "storage",
210
+ Attributes: []abci.EventAttribute{
211
+ {Key: []byte("path"), Value: []byte("notify.legacy")},
212
+ {Key: []byte("value"), Value: []byte("legacyValue")},
213
+ },
214
+ },
215
+ {
216
+ Type: "state_change",
217
+ Attributes: []abci.EventAttribute{
218
+ {Key: []byte("store"), Value: []byte("vstorage")},
219
+ {Key: []byte("key"), Value: []byte("2\x00notify\x00legacy")},
220
+ {Key: []byte("anckey"), Value: []byte("\x012\x00notify\x00legacy\x01")},
221
+ {Key: []byte("value"), Value: []byte("legacyValue")},
222
+ },
223
+ },
224
+ {
225
+ Type: "storage",
226
+ Attributes: []abci.EventAttribute{
227
+ {Key: []byte("path"), Value: []byte("notify.legacy2")},
228
+ {Key: []byte("value"), Value: []byte("legacyValue2b")},
229
+ },
230
+ },
231
+ {
232
+ Type: "state_change",
233
+ Attributes: []abci.EventAttribute{
234
+ {Key: []byte("store"), Value: []byte("vstorage")},
235
+ {Key: []byte("key"), Value: []byte("2\x00notify\x00legacy2")},
236
+ {Key: []byte("anckey"), Value: []byte("\x012\x00notify\x00legacy2\x01")},
237
+ {Key: []byte("value"), Value: []byte("legacyValue2b")},
238
+ },
239
+ },
240
+ {
241
+ Type: "state_change",
242
+ Attributes: []abci.EventAttribute{
243
+ {Key: []byte("store"), Value: []byte("vstorage")},
244
+ {Key: []byte("key"), Value: []byte("2\x00notify\x00noLegacy")},
245
+ {Key: []byte("anckey"), Value: []byte("\x012\x00notify\x00noLegacy\x01")},
246
+ {Key: []byte("value"), Value: []byte("noLegacyValue")},
247
+ },
248
+ },
249
+ {
250
+ Type: "state_change",
251
+ Attributes: []abci.EventAttribute{
252
+ {Key: []byte("store"), Value: []byte("vstorage")},
253
+ {Key: []byte("key"), Value: []byte("2\x00notify\x00noLegacy2")},
254
+ {Key: []byte("anckey"), Value: []byte("\x012\x00notify\x00noLegacy2\x01")},
255
+ {Key: []byte("value"), Value: []byte("noLegacyValue2b")},
256
+ },
257
+ },
258
+ }
259
+
260
+ keeper.FlushChangeEvents(ctx)
261
+ if got := ctx.EventManager().Events(); !reflect.DeepEqual(got, expectedAfterFlushEvents) {
262
+ for _, e := range got {
263
+ t.Logf("got event: %s", e.Type)
264
+ for _, a := range e.Attributes {
265
+ t.Logf("got attr: %s = %s", a.Key, a.Value)
266
+ }
267
+ }
268
+ t.Errorf("got after flush events %#v, want %#v", got, expectedAfterFlushEvents)
269
+ }
270
+
271
+ keeper.FlushChangeEvents(ctx)
272
+ if got := ctx.EventManager().Events(); !reflect.DeepEqual(got, expectedAfterFlushEvents) {
273
+ t.Errorf("got after second flush events %#v, want %#v", got, expectedAfterFlushEvents)
274
+ }
275
+ }
276
+
277
+ func TestStorageMigrate(t *testing.T) {
278
+ testKit := makeTestKit()
279
+ ctx, keeper := testKit.ctx, testKit.vstorageKeeper
280
+
281
+ // Simulate a pre-migration storage with empty string as placeholders
282
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1", "value1"))
283
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1.grandchild1", "value1grandchild"))
284
+ keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1", ""))
285
+
286
+ // Do a deep set.
287
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2", "value2grandchild"))
288
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2a", "value2grandchilda"))
289
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2", ""))
290
+ keeper.SetStorage(ctx, types.NewStorageEntry("key2", ""))
291
+
292
+ keeper.MigrateNoDataPlaceholders(ctx)
293
+
294
+ if keeper.HasStorage(ctx, "key1.child1") {
295
+ t.Errorf("has key1.child1, want no value")
296
+ }
297
+ if keeper.HasStorage(ctx, "key2.child2") {
298
+ t.Errorf("has key2.child2, want no value")
299
+ }
300
+ if keeper.HasStorage(ctx, "key2") {
301
+ t.Errorf("has key2, want no value")
302
+ }
303
+
304
+ // Check the export.
305
+ expectedExport := []*types.DataEntry{
306
+ {Path: "key1", Value: "value1"},
307
+ {Path: "key1.child1.grandchild1", Value: "value1grandchild"},
163
308
  {Path: "key2.child2.grandchild2", Value: "value2grandchild"},
164
309
  {Path: "key2.child2.grandchild2a", Value: "value2grandchilda"},
165
310
  }