@agoric/cosmos 0.34.2-dev-3573f79.0 → 0.34.2-dev-fed2b5e.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.
@@ -1,10 +1,17 @@
1
1
  package keeper
2
2
 
3
3
  import (
4
+ "fmt"
5
+ "reflect"
4
6
  "testing"
5
7
 
6
8
  "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
9
+ "github.com/cosmos/cosmos-sdk/store"
10
+ prefixstore "github.com/cosmos/cosmos-sdk/store/prefix"
11
+ storetypes "github.com/cosmos/cosmos-sdk/store/types"
7
12
  sdk "github.com/cosmos/cosmos-sdk/types"
13
+
14
+ dbm "github.com/tendermint/tm-db"
8
15
  )
9
16
 
10
17
  func mkcoin(denom string) func(amt int64) sdk.Coin {
@@ -181,3 +188,73 @@ func Test_calculateFees(t *testing.T) {
181
188
  })
182
189
  }
183
190
  }
191
+
192
+ var (
193
+ swingsetStoreKey = storetypes.NewKVStoreKey(types.StoreKey)
194
+ )
195
+
196
+ func makeTestStore() sdk.KVStore {
197
+ db := dbm.NewMemDB()
198
+ ms := store.NewCommitMultiStore(db)
199
+ ms.MountStoreWithDB(swingsetStoreKey, sdk.StoreTypeIAVL, db)
200
+ err := ms.LoadLatestVersion()
201
+ if err != nil {
202
+ panic(err)
203
+ }
204
+ kvStore := ms.GetKVStore(swingsetStoreKey)
205
+ prefixStore := prefixstore.NewStore(kvStore, []byte("swingStore."))
206
+ return prefixStore
207
+ }
208
+
209
+ func TestSwingStore(t *testing.T) {
210
+ store := makeTestStore()
211
+
212
+ // Test that we can store and retrieve a value.
213
+ store.Set([]byte("someKey"), []byte("someValue"))
214
+ if got := string(store.Get([]byte("someKey"))); got != "someValue" {
215
+ t.Errorf("got %q, want %q", got, "someValue")
216
+ }
217
+
218
+ // Test that we can update and retrieve an updated value.
219
+ store.Set([]byte("someKey"), []byte("someNewValue"))
220
+ if got := string(store.Get([]byte("someKey"))); got != "someNewValue" {
221
+ t.Errorf("got %q, want %q", got, "someNewValue")
222
+ }
223
+
224
+ // Test that we can store and retrieve empty values
225
+ store.Set([]byte("someEmptyKey"), []byte(""))
226
+ if got := store.Get([]byte("someEmptyKey")); got == nil || string(got) != "" {
227
+ t.Errorf("got %#v, want empty string", got)
228
+ }
229
+
230
+ // Test that we can store and delete values.
231
+ store.Set([]byte("someOtherKey"), []byte("someOtherValue"))
232
+ store.Delete([]byte("someOtherKey"))
233
+ if store.Has([]byte("someOtherKey")) {
234
+ t.Errorf("has value, expected not")
235
+ }
236
+
237
+ // Test that we can delete non existing keys (e.g. delete twice)
238
+ store.Delete([]byte("someMissingKey"))
239
+
240
+ // Check the iterated values
241
+ expectedEntries := [][2]string{
242
+ {"someEmptyKey", "[]byte{}"},
243
+ {"someKey", "[]byte{0x73, 0x6f, 0x6d, 0x65, 0x4e, 0x65, 0x77, 0x56, 0x61, 0x6c, 0x75, 0x65}"},
244
+ }
245
+
246
+ iter := store.Iterator(nil, nil)
247
+ gotEntries := [][2]string{}
248
+ for ; iter.Valid(); iter.Next() {
249
+ entry := [2]string{
250
+ string(iter.Key()),
251
+ fmt.Sprintf("%#v", iter.Value()),
252
+ }
253
+ gotEntries = append(gotEntries, entry)
254
+ }
255
+ iter.Close()
256
+
257
+ if !reflect.DeepEqual(gotEntries, expectedEntries) {
258
+ t.Errorf("got export %q, want %q", gotEntries, expectedEntries)
259
+ }
260
+ }
@@ -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 {
@@ -0,0 +1,71 @@
1
+ package swingset
2
+
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "io"
7
+
8
+ agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
9
+ "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
10
+ sdk "github.com/cosmos/cosmos-sdk/types"
11
+ )
12
+
13
+ // portHandler implements vm.PortHandler
14
+ // for processing inbound messages from Swingset.
15
+ type portHandler struct {
16
+ keeper Keeper
17
+ }
18
+
19
+ type swingsetMessage struct {
20
+ Method string `json:"method"`
21
+ Args []json.RawMessage `json:"args"`
22
+ }
23
+
24
+ const (
25
+ SwingStoreUpdateExportData = "swingStoreUpdateExportData"
26
+ )
27
+
28
+ // NewPortHandler returns a port handler for a swingset Keeper.
29
+ func NewPortHandler(k Keeper) vm.PortHandler {
30
+ return portHandler{keeper: k}
31
+ }
32
+
33
+ // Receive implements the vm.PortHandler method.
34
+ // It receives and processes an inbound message, returning the
35
+ // JSON-serialized response or an error.
36
+ func (ph portHandler) Receive(ctx *vm.ControllerContext, str string) (string, error) {
37
+ var msg swingsetMessage
38
+ err := json.Unmarshal([]byte(str), &msg)
39
+ if err != nil {
40
+ return "", err
41
+ }
42
+
43
+ switch msg.Method {
44
+ case SwingStoreUpdateExportData:
45
+ return ph.handleSwingStoreUpdateExportData(ctx.Context, msg.Args)
46
+
47
+ default:
48
+ return "", fmt.Errorf("unrecognized swingset method %s", msg.Method)
49
+ }
50
+ }
51
+
52
+ func (ph portHandler) handleSwingStoreUpdateExportData(ctx sdk.Context, entries []json.RawMessage) (ret string, err error) {
53
+ store := ph.keeper.GetSwingStore(ctx)
54
+ exportDataReader := agoric.NewJsonRawMessageKVEntriesReader(entries)
55
+ defer exportDataReader.Close()
56
+ for {
57
+ entry, err := exportDataReader.Read()
58
+ if err == io.EOF {
59
+ return "true", nil
60
+ } else if err != nil {
61
+ return ret, err
62
+ }
63
+
64
+ key := []byte(entry.Key())
65
+ if !entry.HasValue() {
66
+ store.Delete(key)
67
+ } else {
68
+ store.Set(key, []byte(entry.StringValue()))
69
+ }
70
+ }
71
+ }