@agoric/cosmos 0.34.2-dev-3573f79.0 → 0.34.2-dev-d6bb564.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.
@@ -0,0 +1,143 @@
1
+ package types
2
+
3
+ import (
4
+ "encoding/json"
5
+ "errors"
6
+ "strings"
7
+ "testing"
8
+ )
9
+
10
+ func checkEntry(t *testing.T, label string, entry KVEntry, isValidKey bool, expectedKey string, hasValue bool, expectedValue string) {
11
+ gotValidKey := entry.IsValidKey()
12
+ if gotValidKey != isValidKey {
13
+ t.Errorf("%s: valid key is %v, expected %v", label, gotValidKey, isValidKey)
14
+ }
15
+
16
+ gotKey := entry.Key()
17
+ if gotKey != expectedKey {
18
+ t.Errorf("%s: got %q, want %q", label, gotKey, expectedKey)
19
+ }
20
+
21
+ if entry.HasValue() {
22
+ if !hasValue {
23
+ t.Errorf("%s: expected has no value", label)
24
+ }
25
+
26
+ gotValue := *entry.Value()
27
+ if gotValue != expectedValue {
28
+ t.Errorf("%s: got %q, want %q", label, gotValue, expectedValue)
29
+ }
30
+ } else {
31
+ if hasValue {
32
+ t.Errorf("%s: expected has value", label)
33
+ }
34
+
35
+ gotValuePointer := entry.Value()
36
+ if gotValuePointer != nil {
37
+ t.Errorf("%s: got %#v, want nil", label, gotValuePointer)
38
+ }
39
+ }
40
+
41
+ gotValue := entry.StringValue()
42
+ if gotValue != expectedValue {
43
+ t.Errorf("%s: got %q, want %q", label, gotValue, expectedValue)
44
+ }
45
+ }
46
+
47
+ func TestKVEntry(t *testing.T) {
48
+ type testCase struct {
49
+ label string
50
+ entry KVEntry
51
+ isValidKey bool
52
+ expectedKey string
53
+ hasValue bool
54
+ expectedValue string
55
+ }
56
+ cases := []testCase{
57
+ {label: "normal", entry: NewKVEntry("foo", "bar"), isValidKey: true, expectedKey: "foo", hasValue: true, expectedValue: "bar"},
58
+ {label: "empty string value", entry: NewKVEntry("foo", ""), isValidKey: true, expectedKey: "foo", hasValue: true, expectedValue: ""},
59
+ {label: "no value", entry: NewKVEntryWithNoValue("foo"), isValidKey: true, expectedKey: "foo", hasValue: false, expectedValue: ""},
60
+ {label: "empty key", entry: NewKVEntryWithNoValue(""), isValidKey: false, expectedKey: "", hasValue: false, expectedValue: ""},
61
+ }
62
+ for _, desc := range cases {
63
+ checkEntry(t, desc.label, desc.entry, desc.isValidKey, desc.expectedKey, desc.hasValue, desc.expectedValue)
64
+ }
65
+ }
66
+
67
+ func TestKVEntryMarshall(t *testing.T) {
68
+ type testCase struct {
69
+ label string
70
+ entry KVEntry
71
+ expectedError error
72
+ expectedEncoding string
73
+ }
74
+ cases := []testCase{
75
+ {label: "normal", entry: NewKVEntry("foo", "bar"), expectedEncoding: `["foo","bar"]`},
76
+ {label: "empty string value", entry: NewKVEntry("foo", ""), expectedEncoding: `["foo",""]`},
77
+ {label: "no value", entry: NewKVEntryWithNoValue("foo"), expectedEncoding: `["foo"]`},
78
+ {label: "empty key", entry: NewKVEntryWithNoValue(""), expectedError: errors.New("cannot marshal invalid KVEntry")},
79
+ }
80
+ for _, desc := range cases {
81
+ marshalled, err := json.Marshal(desc.entry)
82
+ if desc.expectedError != nil && err == nil {
83
+ t.Errorf("%s: got nil error, expected marshal error: %q", desc.label, desc.expectedError.Error())
84
+ } else if err != nil {
85
+ if desc.expectedError == nil {
86
+ t.Errorf("%s: got error %v, expected no error", desc.label, err)
87
+ } else if !strings.Contains(err.Error(), desc.expectedError.Error()) {
88
+ t.Errorf("%s: got error %q, expected error %q", desc.label, err.Error(), desc.expectedError.Error())
89
+ }
90
+ continue
91
+ }
92
+ if string(marshalled) != desc.expectedEncoding {
93
+ t.Errorf("%s: got %q, want %q", desc.label, string(marshalled), desc.expectedEncoding)
94
+ }
95
+ }
96
+ }
97
+
98
+ func TestKVEntryUnmarshall(t *testing.T) {
99
+ type testCase struct {
100
+ label string
101
+ encoded string
102
+ expectedError error
103
+ expectedKey string
104
+ hasValue bool
105
+ expectedValue string
106
+ }
107
+ cases := []testCase{
108
+ {label: "normal", encoded: `["foo","bar"]`, expectedKey: "foo", hasValue: true, expectedValue: "bar"},
109
+ {label: "empty string value", encoded: `["foo",""]`, expectedKey: "foo", hasValue: true, expectedValue: ""},
110
+ {label: "no value", encoded: `["foo"]`, expectedKey: "foo", hasValue: false, expectedValue: ""},
111
+ {label: "null value", encoded: `["foo",null]`, expectedKey: "foo", hasValue: false, expectedValue: ""},
112
+ {label: "null", encoded: `null`, expectedError: errors.New("KVEntry cannot be null")},
113
+ {label: "string", encoded: `"foo"`, expectedError: errors.New("json")},
114
+ {label: "empty array", encoded: `[]`, expectedError: errors.New("KVEntry must be an array of length 1 or 2 (not 0)")},
115
+ {label: "[null, null] array", encoded: `[null,null]`, expectedError: errors.New("KVEntry key must be a non-empty string")},
116
+ {label: "invalid key array", encoded: `[42]`, expectedError: errors.New("json")},
117
+ {label: "empty key", encoded: `["",null]`, expectedError: errors.New("KVEntry key must be a non-empty string")},
118
+ {label: "too many entries array", encoded: `["foo","bar",null]`, expectedError: errors.New("KVEntry must be an array of length 1 or 2 (not 3)")},
119
+ {label: "invalid value array", encoded: `["foo",42]`, expectedError: errors.New("json")},
120
+ }
121
+ for _, desc := range cases {
122
+ unmarshalled := NewKVEntry("untouched", "untouched")
123
+ err := json.Unmarshal([]byte(desc.encoded), &unmarshalled)
124
+ if desc.expectedError != nil && err == nil {
125
+ t.Errorf("%s: got nil error, expected unmarshal error: %q", desc.label, desc.expectedError.Error())
126
+ } else if err != nil {
127
+ if unmarshalled.Key() != "untouched" {
128
+ t.Errorf("%s: expected error to not modify target key, got %s", desc.label, unmarshalled.Key())
129
+ }
130
+ if unmarshalled.StringValue() != "untouched" {
131
+ t.Errorf("%s: expected error to not modify target value, got %v", desc.label, unmarshalled.Value())
132
+ }
133
+ if desc.expectedError == nil {
134
+ t.Errorf("%s: got error %v, expected no error", desc.label, err)
135
+ } else if !strings.Contains(err.Error(), desc.expectedError.Error()) {
136
+ t.Errorf("%s: got error %q, expected error %q", desc.label, err.Error(), desc.expectedError.Error())
137
+ }
138
+ continue
139
+ }
140
+
141
+ checkEntry(t, desc.label, unmarshalled, true, desc.expectedKey, desc.hasValue, desc.expectedValue)
142
+ }
143
+ }
@@ -2,19 +2,16 @@ package keeper
2
2
 
3
3
  import (
4
4
  "bytes"
5
- "encoding/json"
6
5
  "errors"
7
6
  "fmt"
8
7
  "io"
9
8
  "math"
10
9
 
10
+ agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
11
11
  "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
12
- vstoragetypes "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
13
12
  "github.com/cosmos/cosmos-sdk/baseapp"
14
13
  snapshots "github.com/cosmos/cosmos-sdk/snapshots/types"
15
- sdk "github.com/cosmos/cosmos-sdk/types"
16
14
  "github.com/tendermint/tendermint/libs/log"
17
- tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
18
15
  )
19
16
 
20
17
  // This module implements a Cosmos ExtensionSnapshotter to capture and restore
@@ -67,30 +64,26 @@ type snapshotDetails struct {
67
64
  type ExtensionSnapshotter struct {
68
65
  isConfigured func() bool
69
66
  // takeAppSnapshot is called by OnExportStarted when creating a snapshot
70
- takeAppSnapshot func(height int64)
71
- newRestoreContext func(height int64) sdk.Context
72
- swingStoreExportsHandler *SwingStoreExportsHandler
73
- getSwingStoreExportDataShadowCopy func(ctx sdk.Context) []*vstoragetypes.DataEntry
74
- logger log.Logger
75
- activeSnapshot *snapshotDetails
67
+ takeAppSnapshot func(height int64)
68
+ swingStoreExportsHandler *SwingStoreExportsHandler
69
+ getSwingStoreExportDataShadowCopyReader func(height int64) agoric.KVEntryReader
70
+ logger log.Logger
71
+ activeSnapshot *snapshotDetails
76
72
  }
77
73
 
78
74
  // NewExtensionSnapshotter creates a new swingset ExtensionSnapshotter
79
75
  func NewExtensionSnapshotter(
80
76
  app *baseapp.BaseApp,
81
77
  swingStoreExportsHandler *SwingStoreExportsHandler,
82
- getSwingStoreExportDataShadowCopy func(ctx sdk.Context) []*vstoragetypes.DataEntry,
78
+ getSwingStoreExportDataShadowCopyReader func(height int64) agoric.KVEntryReader,
83
79
  ) *ExtensionSnapshotter {
84
80
  return &ExtensionSnapshotter{
85
- isConfigured: func() bool { return app.SnapshotManager() != nil },
86
- takeAppSnapshot: app.Snapshot,
87
- newRestoreContext: func(height int64) sdk.Context {
88
- return app.NewUncachedContext(false, tmproto.Header{Height: height})
89
- },
90
- logger: app.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName), "submodule", "extension snapshotter"),
91
- swingStoreExportsHandler: swingStoreExportsHandler,
92
- getSwingStoreExportDataShadowCopy: getSwingStoreExportDataShadowCopy,
93
- activeSnapshot: nil,
81
+ isConfigured: func() bool { return app.SnapshotManager() != nil },
82
+ takeAppSnapshot: app.Snapshot,
83
+ logger: app.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName), "submodule", "extension snapshotter"),
84
+ swingStoreExportsHandler: swingStoreExportsHandler,
85
+ getSwingStoreExportDataShadowCopyReader: getSwingStoreExportDataShadowCopyReader,
86
+ activeSnapshot: nil,
94
87
  }
95
88
  }
96
89
 
@@ -235,7 +228,7 @@ func (snapshotter *ExtensionSnapshotter) OnExportRetrieved(provider SwingStoreEx
235
228
  }
236
229
 
237
230
  for {
238
- artifact, err := provider.ReadArtifact()
231
+ artifact, err := provider.ReadNextArtifact()
239
232
  if err == io.EOF {
240
233
  break
241
234
  } else if err != nil {
@@ -248,13 +241,14 @@ func (snapshotter *ExtensionSnapshotter) OnExportRetrieved(provider SwingStoreEx
248
241
  }
249
242
  }
250
243
 
251
- swingStoreExportDataEntries, err := provider.GetExportData()
244
+ exportDataReader, err := provider.GetExportDataReader()
252
245
  if err != nil {
253
246
  return err
254
247
  }
255
- if len(swingStoreExportDataEntries) == 0 {
248
+ if exportDataReader == nil {
256
249
  return nil
257
250
  }
251
+ defer exportDataReader.Close()
258
252
 
259
253
  // For debugging, write out any retrieved export data as a single untrusted artifact
260
254
  // which has the same encoding as the internal SwingStore export data representation:
@@ -262,14 +256,9 @@ func (snapshotter *ExtensionSnapshotter) OnExportRetrieved(provider SwingStoreEx
262
256
  exportDataArtifact := types.SwingStoreArtifact{Name: UntrustedExportDataArtifactName}
263
257
 
264
258
  var encodedExportData bytes.Buffer
265
- encoder := json.NewEncoder(&encodedExportData)
266
- encoder.SetEscapeHTML(false)
267
- for _, dataEntry := range swingStoreExportDataEntries {
268
- entry := []string{dataEntry.Path, dataEntry.Value}
269
- err := encoder.Encode(entry)
270
- if err != nil {
271
- return err
272
- }
259
+ err = agoric.EncodeKVEntryReaderToJsonl(exportDataReader, &encodedExportData)
260
+ if err != nil {
261
+ return err
273
262
  }
274
263
  exportDataArtifact.Data = encodedExportData.Bytes()
275
264
 
@@ -298,13 +287,12 @@ func (snapshotter *ExtensionSnapshotter) RestoreExtension(blockHeight uint64, fo
298
287
  // At this point the content of the cosmos DB has been verified against the
299
288
  // AppHash, which means the SwingStore data it contains can be used as the
300
289
  // trusted root against which to validate the artifacts.
301
- getExportData := func() ([]*vstoragetypes.DataEntry, error) {
302
- ctx := snapshotter.newRestoreContext(height)
303
- exportData := snapshotter.getSwingStoreExportDataShadowCopy(ctx)
304
- return exportData, nil
290
+ getExportDataReader := func() (agoric.KVEntryReader, error) {
291
+ exportDataReader := snapshotter.getSwingStoreExportDataShadowCopyReader(height)
292
+ return exportDataReader, nil
305
293
  }
306
294
 
307
- readArtifact := func() (artifact types.SwingStoreArtifact, err error) {
295
+ readNextArtifact := func() (artifact types.SwingStoreArtifact, err error) {
308
296
  payloadBytes, err := payloadReader()
309
297
  if err != nil {
310
298
  return artifact, err
@@ -315,7 +303,7 @@ func (snapshotter *ExtensionSnapshotter) RestoreExtension(blockHeight uint64, fo
315
303
  }
316
304
 
317
305
  return snapshotter.swingStoreExportsHandler.RestoreExport(
318
- SwingStoreExportProvider{BlockHeight: blockHeight, GetExportData: getExportData, ReadArtifact: readArtifact},
306
+ SwingStoreExportProvider{BlockHeight: blockHeight, GetExportDataReader: getExportDataReader, ReadNextArtifact: readNextArtifact},
319
307
  SwingStoreRestoreOptions{IncludeHistorical: false},
320
308
  )
321
309
  }
@@ -4,7 +4,6 @@ import (
4
4
  "io"
5
5
  "testing"
6
6
 
7
- sdk "github.com/cosmos/cosmos-sdk/types"
8
7
  "github.com/tendermint/tendermint/libs/log"
9
8
  )
10
9
 
@@ -12,7 +11,6 @@ func newTestExtensionSnapshotter() *ExtensionSnapshotter {
12
11
  logger := log.NewNopLogger() // log.NewTMLogger(log.NewSyncWriter( /* os.Stdout*/ io.Discard)).With("module", "sdk/app")
13
12
  return &ExtensionSnapshotter{
14
13
  isConfigured: func() bool { return true },
15
- newRestoreContext: func(height int64) sdk.Context { return sdk.Context{} },
16
14
  logger: logger,
17
15
  swingStoreExportsHandler: newTestSwingStoreExportsHandler(),
18
16
  }
@@ -16,6 +16,7 @@ import (
16
16
  paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
17
17
 
18
18
  "github.com/Agoric/agoric-sdk/golang/cosmos/ante"
19
+ agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
19
20
  "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
20
21
  "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
21
22
  vstoragekeeper "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/keeper"
@@ -261,7 +262,7 @@ func getBeansOwingPathForAddress(addr sdk.AccAddress) string {
261
262
  func (k Keeper) GetBeansOwing(ctx sdk.Context, addr sdk.AccAddress) sdk.Uint {
262
263
  path := getBeansOwingPathForAddress(addr)
263
264
  entry := k.vstorageKeeper.GetEntry(ctx, path)
264
- if !entry.HasData() {
265
+ if !entry.HasValue() {
265
266
  return sdk.ZeroUint()
266
267
  }
267
268
  return sdk.NewUintFromString(entry.StringValue())
@@ -271,7 +272,7 @@ func (k Keeper) GetBeansOwing(ctx sdk.Context, addr sdk.AccAddress) sdk.Uint {
271
272
  // feeCollector but has not yet paid.
272
273
  func (k Keeper) SetBeansOwing(ctx sdk.Context, addr sdk.AccAddress, beans sdk.Uint) {
273
274
  path := getBeansOwingPathForAddress(addr)
274
- k.vstorageKeeper.SetStorage(ctx, vstoragetypes.NewStorageEntry(path, beans.String()))
275
+ k.vstorageKeeper.SetStorage(ctx, agoric.NewKVEntry(path, beans.String()))
275
276
  }
276
277
 
277
278
  // ChargeBeans charges the given address the given number of beans. It divides
@@ -375,7 +376,7 @@ func (k Keeper) ChargeForProvisioning(ctx sdk.Context, submitter, addr sdk.AccAd
375
376
  func (k Keeper) GetEgress(ctx sdk.Context, addr sdk.AccAddress) types.Egress {
376
377
  path := StoragePathEgress + "." + addr.String()
377
378
  entry := k.vstorageKeeper.GetEntry(ctx, path)
378
- if !entry.HasData() {
379
+ if !entry.HasValue() {
379
380
  return types.Egress{}
380
381
  }
381
382
 
@@ -398,7 +399,7 @@ func (k Keeper) SetEgress(ctx sdk.Context, egress *types.Egress) error {
398
399
  }
399
400
 
400
401
  // FIXME: We should use just SetStorageAndNotify here, but solo needs legacy for now.
401
- k.vstorageKeeper.LegacySetStorageAndNotify(ctx, vstoragetypes.NewStorageEntry(path, string(bz)))
402
+ k.vstorageKeeper.LegacySetStorageAndNotify(ctx, agoric.NewKVEntry(path, string(bz)))
402
403
 
403
404
  // Now make sure the corresponding account has been initialised.
404
405
  if acc := k.accountKeeper.GetAccount(ctx, egress.Peer); acc != nil {
@@ -431,7 +432,7 @@ func (k Keeper) GetMailbox(ctx sdk.Context, peer string) string {
431
432
  func (k Keeper) SetMailbox(ctx sdk.Context, peer string, mailbox string) {
432
433
  path := StoragePathMailbox + "." + peer
433
434
  // FIXME: We should use just SetStorageAndNotify here, but solo needs legacy for now.
434
- k.vstorageKeeper.LegacySetStorageAndNotify(ctx, vstoragetypes.NewStorageEntry(path, mailbox))
435
+ k.vstorageKeeper.LegacySetStorageAndNotify(ctx, agoric.NewKVEntry(path, mailbox))
435
436
  }
436
437
 
437
438
  func (k Keeper) ExportSwingStore(ctx sdk.Context) []*vstoragetypes.DataEntry {
@@ -80,7 +80,7 @@ func queryMailbox(ctx sdk.Context, path []string, req abci.RequestQuery, keeper
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
82
  entry := keeper.vstorageKeeper.GetEntry(ctx, path)
83
- if !entry.HasData() {
83
+ if !entry.HasValue() {
84
84
  return []byte{}, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "could not get swingset %+v", path)
85
85
  }
86
86
 
@@ -9,9 +9,9 @@ import (
9
9
  "path/filepath"
10
10
  "regexp"
11
11
 
12
+ agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
12
13
  "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
13
14
  "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
14
- vstoragetypes "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
15
15
  sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
16
16
  "github.com/tendermint/tendermint/libs/log"
17
17
  )
@@ -177,8 +177,8 @@ type SwingStoreExportOptions struct {
177
177
  // packages/swing-store/src/swingStore.js makeSwingStoreExporter
178
178
  ExportMode string `json:"exportMode,omitempty"`
179
179
  // A flag indicating whether "export data" should be part of the swing-store export
180
- // If false, the resulting SwingStoreExportProvider's GetExportData will
181
- // return an empty list of "export data" entries.
180
+ // If false, the resulting SwingStoreExportProvider's GetExportDataReader
181
+ // will return nil
182
182
  IncludeExportData bool `json:"includeExportData,omitempty"`
183
183
  }
184
184
 
@@ -364,11 +364,12 @@ func checkNotActive() error {
364
364
  type SwingStoreExportProvider struct {
365
365
  // BlockHeight is the block height of the SwingStore export.
366
366
  BlockHeight uint64
367
- // GetExportData is a function to return the "export data" of the SwingStore export, if any.
368
- GetExportData func() ([]*vstoragetypes.DataEntry, error)
369
- // ReadArtifact is a function to return the next unread artifact in the SwingStore export.
370
- // It errors with io.EOF upon reaching the end of the artifact list.
371
- ReadArtifact func() (types.SwingStoreArtifact, error)
367
+ // GetExportDataReader returns a KVEntryReader for the "export data" of the
368
+ // SwingStore export, or nil if the "export data" is not part of this export.
369
+ GetExportDataReader func() (agoric.KVEntryReader, error)
370
+ // ReadNextArtifact is a function to return the next unread artifact in the SwingStore export.
371
+ // It errors with io.EOF upon reaching the end of the list of available artifacts.
372
+ ReadNextArtifact func() (types.SwingStoreArtifact, error)
372
373
  }
373
374
 
374
375
  // SwingStoreExportEventHandler is used to handle events that occur while generating
@@ -615,41 +616,22 @@ func (exportsHandler SwingStoreExportsHandler) retrieveExport(onExportRetrieved
615
616
  return fmt.Errorf("export manifest blockHeight (%d) doesn't match (%d)", manifest.BlockHeight, blockHeight)
616
617
  }
617
618
 
618
- getExportData := func() ([]*vstoragetypes.DataEntry, error) {
619
- entries := []*vstoragetypes.DataEntry{}
619
+ getExportDataReader := func() (agoric.KVEntryReader, error) {
620
620
  if manifest.Data == "" {
621
- return entries, nil
621
+ return nil, nil
622
622
  }
623
623
 
624
624
  dataFile, err := os.Open(filepath.Join(exportDir, manifest.Data))
625
625
  if err != nil {
626
626
  return nil, err
627
627
  }
628
- defer dataFile.Close()
629
-
630
- decoder := json.NewDecoder(dataFile)
631
- for {
632
- var jsonEntry []string
633
- err = decoder.Decode(&jsonEntry)
634
- if err == io.EOF {
635
- break
636
- } else if err != nil {
637
- return nil, err
638
- }
639
-
640
- if len(jsonEntry) != 2 {
641
- return nil, fmt.Errorf("invalid export data entry (length %d)", len(jsonEntry))
642
- }
643
- entry := vstoragetypes.DataEntry{Path: jsonEntry[0], Value: jsonEntry[1]}
644
- entries = append(entries, &entry)
645
- }
646
-
647
- return entries, nil
628
+ exportDataReader := agoric.NewJsonlKVEntryDecoderReader(dataFile)
629
+ return exportDataReader, nil
648
630
  }
649
631
 
650
632
  nextArtifact := 0
651
633
 
652
- readArtifact := func() (artifact types.SwingStoreArtifact, err error) {
634
+ readNextArtifact := func() (artifact types.SwingStoreArtifact, err error) {
653
635
  if nextArtifact == len(manifest.Artifacts) {
654
636
  return artifact, io.EOF
655
637
  } else if nextArtifact > len(manifest.Artifacts) {
@@ -670,7 +652,7 @@ func (exportsHandler SwingStoreExportsHandler) retrieveExport(onExportRetrieved
670
652
  return artifact, err
671
653
  }
672
654
 
673
- err = onExportRetrieved(SwingStoreExportProvider{BlockHeight: manifest.BlockHeight, GetExportData: getExportData, ReadArtifact: readArtifact})
655
+ err = onExportRetrieved(SwingStoreExportProvider{BlockHeight: manifest.BlockHeight, GetExportDataReader: getExportDataReader, ReadNextArtifact: readNextArtifact})
674
656
  if err != nil {
675
657
  return err
676
658
  }
@@ -724,12 +706,14 @@ func (exportsHandler SwingStoreExportsHandler) RestoreExport(provider SwingStore
724
706
  BlockHeight: blockHeight,
725
707
  }
726
708
 
727
- exportDataEntries, err := provider.GetExportData()
709
+ exportDataReader, err := provider.GetExportDataReader()
728
710
  if err != nil {
729
711
  return err
730
712
  }
731
713
 
732
- if len(exportDataEntries) > 0 {
714
+ if exportDataReader != nil {
715
+ defer exportDataReader.Close()
716
+
733
717
  manifest.Data = exportDataFilename
734
718
  exportDataFile, err := os.OpenFile(filepath.Join(exportDir, exportDataFilename), os.O_CREATE|os.O_WRONLY, exportedFilesMode)
735
719
  if err != nil {
@@ -737,14 +721,9 @@ func (exportsHandler SwingStoreExportsHandler) RestoreExport(provider SwingStore
737
721
  }
738
722
  defer exportDataFile.Close()
739
723
 
740
- encoder := json.NewEncoder(exportDataFile)
741
- encoder.SetEscapeHTML(false)
742
- for _, dataEntry := range exportDataEntries {
743
- entry := []string{dataEntry.Path, dataEntry.Value}
744
- err := encoder.Encode(entry)
745
- if err != nil {
746
- return err
747
- }
724
+ err = agoric.EncodeKVEntryReaderToJsonl(exportDataReader, exportDataFile)
725
+ if err != nil {
726
+ return err
748
727
  }
749
728
 
750
729
  err = exportDataFile.Sync()
@@ -758,7 +737,7 @@ func (exportsHandler SwingStoreExportsHandler) RestoreExport(provider SwingStore
758
737
  }
759
738
 
760
739
  for {
761
- artifact, err := provider.ReadArtifact()
740
+ artifact, err := provider.ReadNextArtifact()
762
741
  if err == io.EOF {
763
742
  break
764
743
  } else if err != nil {
@@ -31,7 +31,7 @@ func newTestSwingStoreEventHandler() testSwingStoreEventHandler {
31
31
  },
32
32
  onExportRetrieved: func(provider SwingStoreExportProvider) error {
33
33
  for {
34
- _, err := provider.ReadArtifact()
34
+ _, err := provider.ReadNextArtifact()
35
35
  if err == io.EOF {
36
36
  return nil
37
37
  } else if err != nil {
@@ -200,7 +200,7 @@ func (k Querier) CapData(c context.Context, req *types.QueryCapDataRequest) (*ty
200
200
 
201
201
  // Read data, auto-upgrading a standalone value to a single-value StreamCell.
202
202
  entry := k.GetEntry(ctx, req.Path)
203
- if !entry.HasData() {
203
+ if !entry.HasValue() {
204
204
  return nil, status.Error(codes.FailedPrecondition, "no data")
205
205
  }
206
206
  value := entry.StringValue()
@@ -35,7 +35,7 @@ type ProposedChange struct {
35
35
  }
36
36
 
37
37
  type ChangeManager interface {
38
- Track(ctx sdk.Context, k Keeper, entry types.StorageEntry, isLegacy bool)
38
+ Track(ctx sdk.Context, k Keeper, entry agoric.KVEntry, isLegacy bool)
39
39
  EmitEvents(ctx sdk.Context, k Keeper)
40
40
  Rollback(ctx sdk.Context)
41
41
  }
@@ -65,8 +65,8 @@ type Keeper struct {
65
65
  storeKey sdk.StoreKey
66
66
  }
67
67
 
68
- func (bcm *BatchingChangeManager) Track(ctx sdk.Context, k Keeper, entry types.StorageEntry, isLegacy bool) {
69
- path := entry.Path()
68
+ func (bcm *BatchingChangeManager) Track(ctx sdk.Context, k Keeper, entry agoric.KVEntry, isLegacy bool) {
69
+ path := entry.Key()
70
70
  // TODO: differentiate between deletion and setting empty string?
71
71
  // Using empty string for deletion for backwards compatibility
72
72
  value := entry.StringValue()
@@ -177,7 +177,7 @@ func (k Keeper) ImportStorage(ctx sdk.Context, entries []*types.DataEntry) {
177
177
  for _, entry := range entries {
178
178
  // This set does the bookkeeping for us in case the entries aren't a
179
179
  // complete tree.
180
- k.SetStorage(ctx, types.NewStorageEntry(entry.Path, entry.Value))
180
+ k.SetStorage(ctx, agoric.NewKVEntry(entry.Path, entry.Value))
181
181
  }
182
182
  }
183
183
 
@@ -205,22 +205,22 @@ func (k Keeper) EmitChange(ctx sdk.Context, change *ProposedChange) {
205
205
  }
206
206
 
207
207
  // GetEntry gets generic storage. The default value is an empty string.
208
- func (k Keeper) GetEntry(ctx sdk.Context, path string) types.StorageEntry {
208
+ func (k Keeper) GetEntry(ctx sdk.Context, path string) agoric.KVEntry {
209
209
  //fmt.Printf("GetEntry(%s)\n", path);
210
210
  store := ctx.KVStore(k.storeKey)
211
211
  encodedKey := types.PathToEncodedKey(path)
212
212
  rawValue := store.Get(encodedKey)
213
213
  if len(rawValue) == 0 {
214
- return types.NewStorageEntryWithNoData(path)
214
+ return agoric.NewKVEntryWithNoValue(path)
215
215
  }
216
216
  if bytes.Equal(rawValue, types.EncodedNoDataValue) {
217
- return types.NewStorageEntryWithNoData(path)
217
+ return agoric.NewKVEntryWithNoValue(path)
218
218
  }
219
219
  value, hasPrefix := cutPrefix(rawValue, types.EncodedDataPrefix)
220
220
  if !hasPrefix {
221
221
  panic(fmt.Errorf("value at path %q starts with unexpected prefix", path))
222
222
  }
223
- return types.NewStorageEntry(path, string(value))
223
+ return agoric.NewKVEntry(path, string(value))
224
224
  }
225
225
 
226
226
  func (k Keeper) getKeyIterator(ctx sdk.Context, path string) db.Iterator {
@@ -249,7 +249,7 @@ func (k Keeper) GetChildren(ctx sdk.Context, path string) *types.Children {
249
249
  // (just an empty string) and exist only to provide linkage to subnodes with
250
250
  // data.
251
251
  func (k Keeper) HasStorage(ctx sdk.Context, path string) bool {
252
- return k.GetEntry(ctx, path).HasData()
252
+ return k.GetEntry(ctx, path).HasValue()
253
253
  }
254
254
 
255
255
  // HasEntry tells if a given path has either subnodes or data.
@@ -278,12 +278,12 @@ func (k Keeper) FlushChangeEvents(ctx sdk.Context) {
278
278
  k.changeManager.Rollback(ctx)
279
279
  }
280
280
 
281
- func (k Keeper) SetStorageAndNotify(ctx sdk.Context, entry types.StorageEntry) {
281
+ func (k Keeper) SetStorageAndNotify(ctx sdk.Context, entry agoric.KVEntry) {
282
282
  k.changeManager.Track(ctx, k, entry, false)
283
283
  k.SetStorage(ctx, entry)
284
284
  }
285
285
 
286
- func (k Keeper) LegacySetStorageAndNotify(ctx sdk.Context, entry types.StorageEntry) {
286
+ func (k Keeper) LegacySetStorageAndNotify(ctx sdk.Context, entry agoric.KVEntry) {
287
287
  k.changeManager.Track(ctx, k, entry, true)
288
288
  k.SetStorage(ctx, entry)
289
289
  }
@@ -308,7 +308,7 @@ func (k Keeper) AppendStorageValueAndNotify(ctx sdk.Context, path, value string)
308
308
  if err != nil {
309
309
  return err
310
310
  }
311
- k.SetStorageAndNotify(ctx, types.NewStorageEntry(path, string(bz)))
311
+ k.SetStorageAndNotify(ctx, agoric.NewKVEntry(path, string(bz)))
312
312
  return nil
313
313
  }
314
314
 
@@ -320,12 +320,12 @@ func componentsToPath(components []string) string {
320
320
  //
321
321
  // Maintains the invariant: path entries exist if and only if self or some
322
322
  // descendant has non-empty storage
323
- func (k Keeper) SetStorage(ctx sdk.Context, entry types.StorageEntry) {
323
+ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
324
324
  store := ctx.KVStore(k.storeKey)
325
- path := entry.Path()
325
+ path := entry.Key()
326
326
  encodedKey := types.PathToEncodedKey(path)
327
327
 
328
- if !entry.HasData() {
328
+ if !entry.HasValue() {
329
329
  if !k.HasChildren(ctx, path) {
330
330
  // We have no children, can delete.
331
331
  store.Delete(encodedKey)
@@ -340,7 +340,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry types.StorageEntry) {
340
340
 
341
341
  // Update our other parent children.
342
342
  pathComponents := strings.Split(path, types.PathSeparator)
343
- if !entry.HasData() {
343
+ if !entry.HasValue() {
344
344
  // delete placeholder ancestors if they're no longer needed
345
345
  for i := len(pathComponents) - 1; i >= 0; i-- {
346
346
  ancestor := componentsToPath(pathComponents[0:i])
@@ -381,7 +381,7 @@ func (k Keeper) GetNoDataValue() []byte {
381
381
 
382
382
  func (k Keeper) getIntValue(ctx sdk.Context, path string) (sdk.Int, error) {
383
383
  indexEntry := k.GetEntry(ctx, path)
384
- if !indexEntry.HasData() {
384
+ if !indexEntry.HasValue() {
385
385
  return sdk.NewInt(0), nil
386
386
  }
387
387
 
@@ -420,10 +420,10 @@ func (k Keeper) PushQueueItem(ctx sdk.Context, queuePath string, value string) e
420
420
 
421
421
  // Set the vstorage corresponding to the queue entry for the current tail.
422
422
  path := queuePath + "." + tail.String()
423
- k.SetStorage(ctx, types.NewStorageEntry(path, value))
423
+ k.SetStorage(ctx, agoric.NewKVEntry(path, value))
424
424
 
425
425
  // Update the tail to point to the next available entry.
426
426
  path = queuePath + ".tail"
427
- k.SetStorage(ctx, types.NewStorageEntry(path, nextTail.String()))
427
+ k.SetStorage(ctx, agoric.NewKVEntry(path, nextTail.String()))
428
428
  return nil
429
429
  }
@@ -9,6 +9,7 @@ import (
9
9
  grpcCodes "google.golang.org/grpc/codes"
10
10
  grpcStatus "google.golang.org/grpc/status"
11
11
 
12
+ agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
12
13
  "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/capdata"
13
14
  "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
14
15
 
@@ -271,9 +272,9 @@ func TestCapData(t *testing.T) {
271
272
  for _, desc := range testCases {
272
273
  desc.request.Path = "key"
273
274
  if desc.data == nil {
274
- keeper.SetStorage(ctx, types.NewStorageEntryWithNoData(desc.request.Path))
275
+ keeper.SetStorage(ctx, agoric.NewKVEntryWithNoValue(desc.request.Path))
275
276
  } else {
276
- keeper.SetStorage(ctx, types.NewStorageEntry(desc.request.Path, *desc.data))
277
+ keeper.SetStorage(ctx, agoric.NewKVEntry(desc.request.Path, *desc.data))
277
278
  }
278
279
  resp, err := querier.CapData(sdk.WrapSDKContext(ctx), &desc.request)
279
280
  if desc.errCode == grpcCodes.OK {