@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.
@@ -4,6 +4,7 @@ import (
4
4
  "reflect"
5
5
  "testing"
6
6
 
7
+ agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
7
8
  "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
8
9
 
9
10
  "github.com/cosmos/cosmos-sdk/store"
@@ -57,19 +58,19 @@ func TestStorage(t *testing.T) {
57
58
  ctx, keeper := testKit.ctx, testKit.vstorageKeeper
58
59
 
59
60
  // Test that we can store and retrieve a value.
60
- keeper.SetStorage(ctx, types.NewStorageEntry("inited", "initValue"))
61
+ keeper.SetStorage(ctx, agoric.NewKVEntry("inited", "initValue"))
61
62
  if got := keeper.GetEntry(ctx, "inited").StringValue(); got != "initValue" {
62
63
  t.Errorf("got %q, want %q", got, "initValue")
63
64
  }
64
65
 
65
66
  // Test that unknown children return empty string.
66
- if got := keeper.GetEntry(ctx, "unknown"); got.HasData() || got.StringValue() != "" {
67
+ if got := keeper.GetEntry(ctx, "unknown"); got.HasValue() || got.StringValue() != "" {
67
68
  t.Errorf("got %q, want no value", got.StringValue())
68
69
  }
69
70
 
70
71
  // 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() != "" {
72
+ keeper.SetStorage(ctx, agoric.NewKVEntry("inited", ""))
73
+ if got := keeper.GetEntry(ctx, "inited"); !got.HasValue() || got.StringValue() != "" {
73
74
  t.Errorf("got %q, want empty string", got.StringValue())
74
75
  }
75
76
 
@@ -78,18 +79,18 @@ func TestStorage(t *testing.T) {
78
79
  t.Errorf("got %q children, want [inited]", got.Children)
79
80
  }
80
81
 
81
- keeper.SetStorage(ctx, types.NewStorageEntry("key1", "value1"))
82
+ keeper.SetStorage(ctx, agoric.NewKVEntry("key1", "value1"))
82
83
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"inited", "key1"}) {
83
84
  t.Errorf("got %q children, want [inited,key1]", got.Children)
84
85
  }
85
86
 
86
87
  // Check alphabetical.
87
- keeper.SetStorage(ctx, types.NewStorageEntry("alpha2", "value2"))
88
+ keeper.SetStorage(ctx, agoric.NewKVEntry("alpha2", "value2"))
88
89
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "inited", "key1"}) {
89
90
  t.Errorf("got %q children, want [alpha2,inited,key1]", got.Children)
90
91
  }
91
92
 
92
- keeper.SetStorage(ctx, types.NewStorageEntry("beta3", "value3"))
93
+ keeper.SetStorage(ctx, agoric.NewKVEntry("beta3", "value3"))
93
94
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "beta3", "inited", "key1"}) {
94
95
  t.Errorf("got %q children, want [alpha2,beta3,inited,key1]", got.Children)
95
96
  }
@@ -99,7 +100,7 @@ func TestStorage(t *testing.T) {
99
100
  }
100
101
 
101
102
  // Check adding children.
102
- keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1", "value1child"))
103
+ keeper.SetStorage(ctx, agoric.NewKVEntry("key1.child1", "value1child"))
103
104
  if got := keeper.GetEntry(ctx, "key1.child1").StringValue(); got != "value1child" {
104
105
  t.Errorf("got %q, want %q", got, "value1child")
105
106
  }
@@ -109,7 +110,7 @@ func TestStorage(t *testing.T) {
109
110
  }
110
111
 
111
112
  // Add a grandchild.
112
- keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1.grandchild1", "value1grandchild"))
113
+ keeper.SetStorage(ctx, agoric.NewKVEntry("key1.child1.grandchild1", "value1grandchild"))
113
114
  if got := keeper.GetEntry(ctx, "key1.child1.grandchild1").StringValue(); got != "value1grandchild" {
114
115
  t.Errorf("got %q, want %q", got, "value1grandchild")
115
116
  }
@@ -119,7 +120,7 @@ func TestStorage(t *testing.T) {
119
120
  }
120
121
 
121
122
  // Delete the child's contents.
122
- keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1.child1"))
123
+ keeper.SetStorage(ctx, agoric.NewKVEntryWithNoValue("key1.child1"))
123
124
  if got := keeper.GetChildren(ctx, "key1"); !childrenEqual(got.Children, []string{"child1"}) {
124
125
  t.Errorf("got %q children, want [child1]", got.Children)
125
126
  }
@@ -129,7 +130,7 @@ func TestStorage(t *testing.T) {
129
130
  }
130
131
 
131
132
  // Delete the grandchild's contents.
132
- keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1.child1.grandchild1"))
133
+ keeper.SetStorage(ctx, agoric.NewKVEntryWithNoValue("key1.child1.grandchild1"))
133
134
  if got := keeper.GetChildren(ctx, "key1.child1"); !childrenEqual(got.Children, []string{}) {
134
135
  t.Errorf("got %q children, want []", got.Children)
135
136
  }
@@ -139,13 +140,13 @@ func TestStorage(t *testing.T) {
139
140
  }
140
141
 
141
142
  // See about deleting the parent.
142
- keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1"))
143
+ keeper.SetStorage(ctx, agoric.NewKVEntryWithNoValue("key1"))
143
144
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "beta3", "inited"}) {
144
145
  t.Errorf("got %q children, want [alpha2,beta3,inited]", got.Children)
145
146
  }
146
147
 
147
148
  // Do a deep set.
148
- keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2", "value2grandchild"))
149
+ keeper.SetStorage(ctx, agoric.NewKVEntry("key2.child2.grandchild2", "value2grandchild"))
149
150
  if got := keeper.GetChildren(ctx, ""); !childrenEqual(got.Children, []string{"alpha2", "beta3", "inited", "key2"}) {
150
151
  t.Errorf("got %q children, want [alpha2,beta3,inited,key2]", got.Children)
151
152
  }
@@ -157,7 +158,7 @@ func TestStorage(t *testing.T) {
157
158
  }
158
159
 
159
160
  // Do another deep set.
160
- keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2a", "value2grandchilda"))
161
+ keeper.SetStorage(ctx, agoric.NewKVEntry("key2.child2.grandchild2a", "value2grandchilda"))
161
162
  if got := keeper.GetChildren(ctx, "key2.child2"); !childrenEqual(got.Children, []string{"grandchild2", "grandchild2a"}) {
162
163
  t.Errorf("got %q children, want [grandchild2,grandchild2a]", got.Children)
163
164
  }
@@ -191,12 +192,12 @@ func TestStorageNotify(t *testing.T) {
191
192
  tk := makeTestKit()
192
193
  ctx, keeper := tk.ctx, tk.vstorageKeeper
193
194
 
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"))
195
+ keeper.SetStorageAndNotify(ctx, agoric.NewKVEntry("notify.noLegacy", "noLegacyValue"))
196
+ keeper.LegacySetStorageAndNotify(ctx, agoric.NewKVEntry("notify.legacy", "legacyValue"))
197
+ keeper.SetStorageAndNotify(ctx, agoric.NewKVEntry("notify.noLegacy2", "noLegacyValue2"))
198
+ keeper.SetStorageAndNotify(ctx, agoric.NewKVEntry("notify.legacy2", "legacyValue2"))
199
+ keeper.LegacySetStorageAndNotify(ctx, agoric.NewKVEntry("notify.legacy2", "legacyValue2b"))
200
+ keeper.SetStorageAndNotify(ctx, agoric.NewKVEntry("notify.noLegacy2", "noLegacyValue2b"))
200
201
 
201
202
  // Check the batched events.
202
203
  expectedBeforeFlushEvents := sdk.Events{}
@@ -35,7 +35,7 @@ func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier
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
37
  entry := keeper.GetEntry(ctx, path)
38
- if !entry.HasData() {
38
+ if !entry.HasValue() {
39
39
  return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "could not get vstorage path")
40
40
  }
41
41
 
@@ -1,10 +1,5 @@
1
1
  package types
2
2
 
3
- import (
4
- "encoding/json"
5
- "fmt"
6
- )
7
-
8
3
  func NewData() *Data {
9
4
  return &Data{}
10
5
  }
@@ -12,64 +7,3 @@ func NewData() *Data {
12
7
  func NewChildren() *Children {
13
8
  return &Children{}
14
9
  }
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) Value() *string {
66
- return se.value
67
- }
68
-
69
- func (se StorageEntry) StringValue() string {
70
- if se.value != nil {
71
- return *se.value
72
- } else {
73
- return ""
74
- }
75
- }
@@ -7,8 +7,8 @@ import (
7
7
 
8
8
  sdk "github.com/cosmos/cosmos-sdk/types"
9
9
 
10
+ agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
10
11
  "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
11
- "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
12
12
  )
13
13
 
14
14
  type vstorageHandler struct {
@@ -69,8 +69,8 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
69
69
  switch msg.Method {
70
70
  case "set":
71
71
  for _, arg := range msg.Args {
72
- var entry types.StorageEntry
73
- entry, err = types.UnmarshalStorageEntry(arg)
72
+ var entry agoric.KVEntry
73
+ err = json.Unmarshal(arg, &entry)
74
74
  if err != nil {
75
75
  return
76
76
  }
@@ -83,8 +83,8 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
83
83
  // FIXME: Use just "set" and remove this case.
84
84
  case "legacySet":
85
85
  for _, arg := range msg.Args {
86
- var entry types.StorageEntry
87
- entry, err = types.UnmarshalStorageEntry(arg)
86
+ var entry agoric.KVEntry
87
+ err = json.Unmarshal(arg, &entry)
88
88
  if err != nil {
89
89
  return
90
90
  }
@@ -95,8 +95,8 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
95
95
 
96
96
  case "setWithoutNotify":
97
97
  for _, arg := range msg.Args {
98
- var entry types.StorageEntry
99
- entry, err = types.UnmarshalStorageEntry(arg)
98
+ var entry agoric.KVEntry
99
+ err = json.Unmarshal(arg, &entry)
100
100
  if err != nil {
101
101
  return
102
102
  }
@@ -106,16 +106,16 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
106
106
 
107
107
  case "append":
108
108
  for _, arg := range msg.Args {
109
- var entry types.StorageEntry
110
- entry, err = types.UnmarshalStorageEntry(arg)
109
+ var entry agoric.KVEntry
110
+ err = json.Unmarshal(arg, &entry)
111
111
  if err != nil {
112
112
  return
113
113
  }
114
- if !entry.HasData() {
115
- err = fmt.Errorf("no value for append entry with path: %q", entry.Path())
114
+ if !entry.HasValue() {
115
+ err = fmt.Errorf("no value for append entry with path: %q", entry.Key())
116
116
  return
117
117
  }
118
- err = keeper.AppendStorageValueAndNotify(cctx.Context, entry.Path(), entry.StringValue())
118
+ err = keeper.AppendStorageValueAndNotify(cctx.Context, entry.Key(), entry.StringValue())
119
119
  if err != nil {
120
120
  return
121
121
  }
@@ -131,10 +131,7 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
131
131
  }
132
132
 
133
133
  entry := keeper.GetEntry(cctx.Context, path)
134
- if !entry.HasData() {
135
- return "null", nil
136
- }
137
- bz, err := json.Marshal(entry.StringValue())
134
+ bz, err := json.Marshal(entry.Value())
138
135
  if err != nil {
139
136
  return "", err
140
137
  }
@@ -194,13 +191,13 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
194
191
  return
195
192
  }
196
193
  children := keeper.GetChildren(cctx.Context, path)
197
- entries := make([][]interface{}, len(children.Children))
194
+ entries := make([]agoric.KVEntry, len(children.Children))
198
195
  for i, child := range children.Children {
199
196
  entry := keeper.GetEntry(cctx.Context, fmt.Sprintf("%s.%s", path, child))
200
- if !entry.HasData() {
201
- entries[i] = []interface{}{child}
197
+ if !entry.HasValue() {
198
+ entries[i] = agoric.NewKVEntryWithNoValue(child)
202
199
  } else {
203
- entries[i] = []interface{}{child, entry.Value()}
200
+ entries[i] = agoric.NewKVEntry(child, entry.StringValue())
204
201
  }
205
202
  }
206
203
  bytes, err := json.Marshal(entries)
@@ -216,9 +213,9 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
216
213
  return
217
214
  }
218
215
  children := keeper.GetChildren(cctx.Context, path)
219
- vals := make([]string, len(children.Children))
216
+ vals := make([]*string, len(children.Children))
220
217
  for i, child := range children.Children {
221
- vals[i] = keeper.GetEntry(cctx.Context, fmt.Sprintf("%s.%s", path, child)).StringValue()
218
+ vals[i] = keeper.GetEntry(cctx.Context, fmt.Sprintf("%s.%s", path, child)).Value()
222
219
  }
223
220
  bytes, err := json.Marshal(vals)
224
221
  if err != nil {
@@ -70,10 +70,10 @@ func TestGetAndHas(t *testing.T) {
70
70
  kit := makeTestKit()
71
71
  keeper, handler, ctx, cctx := kit.keeper, kit.handler, kit.ctx, kit.cctx
72
72
 
73
- keeper.SetStorage(ctx, types.NewStorageEntry("foo", "bar"))
74
- keeper.SetStorage(ctx, types.NewStorageEntry("empty", ""))
75
- keeper.SetStorage(ctx, types.NewStorageEntry("top.empty-non-terminal.leaf", ""))
76
- keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("top.empty-non-terminal"))
73
+ keeper.SetStorage(ctx, agorictypes.NewKVEntry("foo", "bar"))
74
+ keeper.SetStorage(ctx, agorictypes.NewKVEntry("empty", ""))
75
+ keeper.SetStorage(ctx, agorictypes.NewKVEntry("top.empty-non-terminal.leaf", ""))
76
+ keeper.SetStorage(ctx, agorictypes.NewKVEntryWithNoValue("top.empty-non-terminal"))
77
77
 
78
78
  type testCase struct {
79
79
  label string
@@ -153,7 +153,7 @@ func doTestSet(t *testing.T, method string, expectNotify bool) {
153
153
  // TODO: Fully validate input before making changes
154
154
  // args: []interface{}{[]string{"foo", "X"}, []interface{}{42, "new"}},
155
155
  args: []interface{}{[]interface{}{42, "new"}},
156
- errContains: ptr("path"),
156
+ errContains: ptr("json"),
157
157
  },
158
158
  {label: "non-string value",
159
159
  // TODO: Fully validate input before making changes
@@ -259,15 +259,15 @@ func TestEntries(t *testing.T) {
259
259
  kit := makeTestKit()
260
260
  keeper, handler, ctx, cctx := kit.keeper, kit.handler, kit.ctx, kit.cctx
261
261
 
262
- keeper.SetStorage(ctx, types.NewStorageEntry("key1", "value1"))
263
- keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1.grandchild1", "value1grandchild"))
264
- keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1.child1.grandchild2"))
265
- keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key1.child1"))
266
- keeper.SetStorage(ctx, types.NewStorageEntry("key1.child1.empty-non-terminal.leaf", ""))
267
- keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key2"))
268
- keeper.SetStorage(ctx, types.NewStorageEntryWithNoData("key2.child2"))
269
- keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2", "value2grandchild"))
270
- keeper.SetStorage(ctx, types.NewStorageEntry("key2.child2.grandchild2a", "value2grandchilda"))
262
+ keeper.SetStorage(ctx, agorictypes.NewKVEntry("key1", "value1"))
263
+ keeper.SetStorage(ctx, agorictypes.NewKVEntry("key1.child1.grandchild1", "value1grandchild"))
264
+ keeper.SetStorage(ctx, agorictypes.NewKVEntryWithNoValue("key1.child1.grandchild2"))
265
+ keeper.SetStorage(ctx, agorictypes.NewKVEntryWithNoValue("key1.child1"))
266
+ keeper.SetStorage(ctx, agorictypes.NewKVEntry("key1.child1.empty-non-terminal.leaf", ""))
267
+ keeper.SetStorage(ctx, agorictypes.NewKVEntryWithNoValue("key2"))
268
+ keeper.SetStorage(ctx, agorictypes.NewKVEntryWithNoValue("key2.child2"))
269
+ keeper.SetStorage(ctx, agorictypes.NewKVEntry("key2.child2.grandchild2", "value2grandchild"))
270
+ keeper.SetStorage(ctx, agorictypes.NewKVEntry("key2.child2.grandchild2a", "value2grandchilda"))
271
271
 
272
272
  type testCase struct {
273
273
  path string