@agoric/cosmos 0.32.3-dev-7a740b6.0 → 0.32.3-dev-88491d2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/cosmos",
3
- "version": "0.32.3-dev-7a740b6.0+7a740b6",
3
+ "version": "0.32.3-dev-88491d2.0+88491d2",
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": "7a740b61e5f12ba737ac213cc184e34e37adb91a"
50
+ "gitHead": "88491d27ec41fc74ac29bce479da083840b4ecbf"
51
51
  }
@@ -62,6 +62,10 @@ func (se StorageEntry) Path() string {
62
62
  return se.path
63
63
  }
64
64
 
65
+ func (se StorageEntry) Value() *string {
66
+ return se.value
67
+ }
68
+
65
69
  func (se StorageEntry) StringValue() string {
66
70
  if se.value != nil {
67
71
  return *se.value
@@ -194,13 +194,12 @@ func (sh vstorageHandler) Receive(cctx *vm.ControllerContext, str string) (ret s
194
194
  return
195
195
  }
196
196
  children := keeper.GetChildren(cctx.Context, path)
197
- ents := make([][]string, len(children.Children))
197
+ entries := make([][]interface{}, len(children.Children))
198
198
  for i, child := range children.Children {
199
- ents[i] = make([]string, 2)
200
- ents[i][0] = child
201
- ents[i][i] = keeper.GetEntry(cctx.Context, fmt.Sprintf("%s.%s", path, child)).StringValue()
199
+ entry := keeper.GetEntry(cctx.Context, fmt.Sprintf("%s.%s", path, child))
200
+ entries[i] = []interface{}{child, entry.Value()}
202
201
  }
203
- bytes, err := json.Marshal(ents)
202
+ bytes, err := json.Marshal(entries)
204
203
  if err != nil {
205
204
  return "", err
206
205
  }
@@ -0,0 +1,297 @@
1
+ package vstorage
2
+
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "reflect"
7
+ "strings"
8
+ "testing"
9
+
10
+ "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
11
+
12
+ "github.com/cosmos/cosmos-sdk/store"
13
+ storetypes "github.com/cosmos/cosmos-sdk/store/types"
14
+ sdk "github.com/cosmos/cosmos-sdk/types"
15
+
16
+ agorictypes "github.com/Agoric/agoric-sdk/golang/cosmos/types"
17
+ "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
18
+ "github.com/tendermint/tendermint/libs/log"
19
+ tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
20
+ dbm "github.com/tendermint/tm-db"
21
+ )
22
+
23
+ var (
24
+ storeKey = storetypes.NewKVStoreKey(types.StoreKey)
25
+ )
26
+
27
+ func ptr[T any](v T) *T {
28
+ return &v
29
+ }
30
+
31
+ type testKit struct {
32
+ keeper Keeper
33
+ handler vstorageHandler
34
+ ctx sdk.Context
35
+ cctx *vm.ControllerContext
36
+ }
37
+
38
+ func makeTestKit() testKit {
39
+ keeper := NewKeeper(storeKey)
40
+ db := dbm.NewMemDB()
41
+ ms := store.NewCommitMultiStore(db)
42
+ ms.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db)
43
+ err := ms.LoadLatestVersion()
44
+ if err != nil {
45
+ panic(err)
46
+ }
47
+ ctx := sdk.NewContext(ms, tmproto.Header{}, false, log.NewNopLogger())
48
+ cctx := &vm.ControllerContext{Context: ctx}
49
+ handler := vstorageHandler{keeper}
50
+ return testKit{keeper, handler, ctx, cctx}
51
+ }
52
+
53
+ func callReceive(
54
+ handler vstorageHandler,
55
+ cctx *vm.ControllerContext,
56
+ method string,
57
+ args []interface{},
58
+ ) (string, error) {
59
+ var rawArgs []json.RawMessage
60
+ for _, arg := range args {
61
+ rawArg, _ := json.Marshal(arg)
62
+ rawArgs = append(rawArgs, json.RawMessage(rawArg))
63
+ }
64
+ req := vstorageMessage{method, rawArgs}
65
+ reqBytes, _ := json.Marshal(req)
66
+ return handler.Receive(cctx, string(reqBytes))
67
+ }
68
+
69
+ func TestGetAndHas(t *testing.T) {
70
+ kit := makeTestKit()
71
+ keeper, handler, ctx, cctx := kit.keeper, kit.handler, kit.ctx, kit.cctx
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"))
77
+
78
+ type testCase struct {
79
+ label string
80
+ args []interface{}
81
+ want string
82
+ errContains *string
83
+ }
84
+ cases := []testCase{
85
+ {label: "nonempty value", args: []interface{}{"foo"}, want: `"bar"`},
86
+ {label: "empty string value", args: []interface{}{"empty"}, want: `""`},
87
+ {label: "deep empty string", args: []interface{}{"top.empty-non-terminal.leaf"}, want: `""`},
88
+ {label: "empty non-terminal", args: []interface{}{"top.empty-non-terminal"}, want: `null`},
89
+ {label: "no entry", args: []interface{}{"nosuchpath"}, want: `null`},
90
+ {label: "empty args", args: []interface{}{}, errContains: ptr(`missing`)},
91
+ {label: "non-string arg", args: []interface{}{42}, errContains: ptr(`json`)},
92
+ {label: "extra args", args: []interface{}{"foo", "bar"}, errContains: ptr(`extra`)},
93
+ }
94
+ for _, desc := range cases {
95
+ got, err := callReceive(handler, cctx, "get", desc.args)
96
+ has, hasErr := callReceive(handler, cctx, "has", desc.args)
97
+
98
+ // Verify get/has error agreement.
99
+ if (err == nil) != (hasErr == nil) {
100
+ t.Errorf("%s: get/has error mismatch, %v vs. %v", desc.label, err, hasErr)
101
+ } else if err != nil && hasErr.Error() != err.Error() {
102
+ t.Errorf("%s: get/has error message mismatch, %v vs. %v", desc.label, err, hasErr)
103
+ }
104
+
105
+ if got != desc.want {
106
+ t.Errorf("%s: got %q; want %q", desc.label, got, desc.want)
107
+ }
108
+ if desc.errContains == nil {
109
+ if err != nil {
110
+ t.Errorf("%s: got unexpected error %v", desc.label, err)
111
+ }
112
+
113
+ // Verify that has returns false iff get returns null.
114
+ noData := desc.want == `null`
115
+ if (noData && has != `false`) || (!noData && has != `true`) {
116
+ t.Errorf("%s: got has %v; want %v", desc.label, has, !noData)
117
+ }
118
+ } else if err == nil {
119
+ t.Errorf("%s: got no error, want error %q", desc.label, *desc.errContains)
120
+ } else if !strings.Contains(err.Error(), *desc.errContains) {
121
+ t.Errorf("%s: got error %v, want error %q", desc.label, err, *desc.errContains)
122
+ }
123
+ }
124
+ }
125
+
126
+ func doTestSet(t *testing.T, method string, expectNotify bool) {
127
+ kit := makeTestKit()
128
+ keeper, handler, ctx, cctx := kit.keeper, kit.handler, kit.ctx, kit.cctx
129
+
130
+ type testCase struct {
131
+ label string
132
+ args []interface{}
133
+ errContains *string
134
+ skipReadBack map[int]bool
135
+ }
136
+ cases := []testCase{
137
+ {label: "single value",
138
+ args: []interface{}{[]string{"foo", "bar"}},
139
+ },
140
+ {label: "other single value",
141
+ args: []interface{}{[]string{"bar", "baz"}},
142
+ },
143
+ {label: "multi-value",
144
+ args: []interface{}{[]string{"baz.a", "qux"}, []string{"baz.b", "qux"}},
145
+ },
146
+ {label: "other multi-value",
147
+ args: []interface{}{[]string{"qux", "A"}, []string{"quux", "B"}},
148
+ },
149
+ {label: "overwrites",
150
+ args: []interface{}{[]string{"bar"}, []string{"quux", "new"}},
151
+ },
152
+ {label: "non-string path",
153
+ // TODO: Fully validate input before making changes
154
+ // args: []interface{}{[]string{"foo", "X"}, []interface{}{42, "new"}},
155
+ args: []interface{}{[]interface{}{42, "new"}},
156
+ errContains: ptr("path"),
157
+ },
158
+ {label: "non-string value",
159
+ // TODO: Fully validate input before making changes
160
+ // args: []interface{}{[]string{"foo", "X"}, []interface{}{"foo", true}},
161
+ args: []interface{}{[]interface{}{"foo", true}},
162
+ errContains: ptr("value"),
163
+ },
164
+ {label: "self-overwrite",
165
+ args: []interface{}{[]string{"final.final.corge", "grault"}, []string{"final.final.corge", "garply"}},
166
+ skipReadBack: map[int]bool{0: true},
167
+ },
168
+ }
169
+ stateChangeEvent := func(path, value string) sdk.Event {
170
+ return agorictypes.NewStateChangeEvent(
171
+ keeper.GetStoreName(),
172
+ keeper.PathToEncodedKey(path),
173
+ []byte(value),
174
+ )
175
+ }
176
+ // Expect events to be alphabetized by key.
177
+ expectedFlushEvents := sdk.Events{
178
+ stateChangeEvent("baz.a", "qux"),
179
+ stateChangeEvent("baz.b", "qux"),
180
+ stateChangeEvent("final.final.corge", "garply"),
181
+ stateChangeEvent("foo", "bar"),
182
+ stateChangeEvent("quux", "new"),
183
+ stateChangeEvent("qux", "A"),
184
+ }
185
+ for _, desc := range cases {
186
+ got, err := callReceive(handler, cctx, method, desc.args)
187
+
188
+ if desc.errContains == nil {
189
+ if err != nil {
190
+ t.Errorf("%s %s: got unexpected error %v", method, desc.label, err)
191
+ } else if got != "true" {
192
+ t.Errorf("%s %s: got unexpected response %q; want %q", method, desc.label, got, "true")
193
+ }
194
+
195
+ // Read the data back.
196
+ for i, arg := range desc.args {
197
+ entry, ok := arg.([]string)
198
+ if desc.skipReadBack[i] {
199
+ continue
200
+ } else if !ok {
201
+ t.Errorf("%s %s: cannot convert %#v to []string!", method, desc.label, arg)
202
+ continue
203
+ }
204
+ path := entry[0]
205
+ var value *string
206
+ if len(entry) > 1 {
207
+ value = &entry[1]
208
+ }
209
+ wantBackBytes, _ := json.Marshal(value)
210
+ wantBack := string(wantBackBytes)
211
+ gotBack, err := callReceive(handler, cctx, "get", []interface{}{path})
212
+ if err != nil {
213
+ t.Errorf("%s %s read back %q: got unexpected error %v", method, desc.label, path, err)
214
+ } else if gotBack != wantBack {
215
+ t.Errorf("%s %s read back %q: got %q; want %q", method, desc.label, path, gotBack, wantBack)
216
+ }
217
+ }
218
+ } else if err == nil {
219
+ t.Errorf("%s %s: got no error, want error %q", method, desc.label, *desc.errContains)
220
+ } else if !strings.Contains(err.Error(), *desc.errContains) {
221
+ t.Errorf("%s %s: got error %v, want error %q", method, desc.label, err, *desc.errContains)
222
+ }
223
+ }
224
+
225
+ // Verify corresponding events.
226
+ if got := ctx.EventManager().Events(); !reflect.DeepEqual(got, sdk.Events{}) {
227
+ t.Errorf("%s got unexpected events before flush %#v", method, got)
228
+ }
229
+ keeper.FlushChangeEvents(ctx)
230
+ if !expectNotify {
231
+ if got := ctx.EventManager().Events(); !reflect.DeepEqual(got, sdk.Events{}) {
232
+ t.Errorf("%s got unexpected events after flush %#v", method, got)
233
+ }
234
+ } else if got := ctx.EventManager().Events(); !reflect.DeepEqual(got, expectedFlushEvents) {
235
+ for _, evt := range got {
236
+ attrs := make([]string, len(evt.Attributes))
237
+ for i, attr := range evt.Attributes {
238
+ attrs[i] = fmt.Sprintf("%s=%q", attr.Key, attr.Value)
239
+ }
240
+ t.Logf("%s got event %s<%s>", method, evt.Type, strings.Join(attrs, ", "))
241
+ }
242
+ t.Errorf("%s got after flush events %#v; want %#v", method, got, expectedFlushEvents)
243
+ }
244
+ }
245
+
246
+ func TestSet(t *testing.T) {
247
+ doTestSet(t, "set", true)
248
+ }
249
+
250
+ func TestSetWithoutNotify(t *testing.T) {
251
+ doTestSet(t, "setWithoutNotify", false)
252
+ }
253
+
254
+ // TODO: TestAppend
255
+
256
+ // TODO: TestChildrenAndSize
257
+
258
+ func TestEntries(t *testing.T) {
259
+ kit := makeTestKit()
260
+ keeper, handler, ctx, cctx := kit.keeper, kit.handler, kit.ctx, kit.cctx
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"))
271
+
272
+ type testCase struct {
273
+ path string
274
+ want string
275
+ }
276
+ cases := []testCase{
277
+ {path: "key1", want: `[["child1",null]]`},
278
+ {path: "key1.child1",
279
+ // Empty non-terminals are included, empty leaves are not.
280
+ want: `[["empty-non-terminal",null],["grandchild1","value1grandchild"]]`},
281
+ {path: "key1.child1.grandchild1", want: `[]`},
282
+ {path: "key1.child1.empty-non-terminal", want: `[["leaf",""]]`},
283
+ {path: "key2", want: `[["child2",null]]`},
284
+ {path: "key2.child2",
285
+ want: `[["grandchild2","value2grandchild"],["grandchild2a","value2grandchilda"]]`},
286
+ {path: "nosuchkey", want: `[]`},
287
+ }
288
+ for _, desc := range cases {
289
+ got, err := callReceive(handler, cctx, "entries", []interface{}{desc.path})
290
+ if got != desc.want {
291
+ t.Errorf("%s: got %q; want %q", desc.path, got, desc.want)
292
+ }
293
+ if err != nil {
294
+ t.Errorf("%s: got unexpected error %v", desc.path, err)
295
+ }
296
+ }
297
+ }