@agoric/cosmos 0.34.2-dev-5dc325b.0 → 0.35.0-getting-started-dev-26244e8.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,300 +0,0 @@
1
- package keeper
2
-
3
- import (
4
- "fmt"
5
- "reflect"
6
- "strings"
7
- "testing"
8
-
9
- grpcCodes "google.golang.org/grpc/codes"
10
- grpcStatus "google.golang.org/grpc/status"
11
-
12
- agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
13
- "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/capdata"
14
- "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
15
-
16
- sdk "github.com/cosmos/cosmos-sdk/types"
17
- )
18
-
19
- func ptr[T any](v T) *T {
20
- return &v
21
- }
22
-
23
- func mustJsonMarshal(val any) string {
24
- jsonText, err := capdata.JsonMarshal(val)
25
- if err != nil {
26
- panic(err)
27
- }
28
- return string(jsonText)
29
- }
30
-
31
- func mustMarshalStreamCell(blockHeight string, values []string) string {
32
- cell := map[string]any{
33
- "blockHeight": blockHeight,
34
- "values": values,
35
- }
36
- return mustJsonMarshal(cell)
37
- }
38
-
39
- func TestCapData(t *testing.T) {
40
- testKit := makeTestKit()
41
- ctx, keeper := testKit.ctx, testKit.vstorageKeeper
42
- querier := Querier{keeper}
43
-
44
- type testCase struct {
45
- label string
46
- data *string
47
- request types.QueryCapDataRequest
48
- expected types.QueryCapDataResponse
49
- errCode grpcCodes.Code
50
- errContains *string
51
- }
52
- testCases := []testCase{}
53
-
54
- // Test simple cases (various kinds of bad data up to simple flat JSON-compatible CapData).
55
- decodableSmallcaps := `{"body":"#true","slots":[]}`
56
- decodableLegacy := `{"body":"true","slots":[]}`
57
- testCases = append(testCases, []testCase{
58
- {label: "no data",
59
- data: nil,
60
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
61
- errCode: grpcCodes.FailedPrecondition,
62
- errContains: ptr("no data"),
63
- },
64
- {label: "zero-length data",
65
- data: ptr(""),
66
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
67
- errCode: grpcCodes.FailedPrecondition,
68
- errContains: ptr("JSON"),
69
- },
70
- {label: "cell with empty string",
71
- data: ptr(mustMarshalStreamCell("1", []string{decodableSmallcaps, "", decodableLegacy})),
72
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
73
- errCode: grpcCodes.FailedPrecondition,
74
- errContains: ptr("JSON"),
75
- },
76
- {label: "non-JSON data",
77
- data: ptr("foo"),
78
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
79
- errCode: grpcCodes.FailedPrecondition,
80
- errContains: ptr("invalid"),
81
- },
82
- {label: "cell with non-JSON",
83
- data: ptr(mustMarshalStreamCell("1", []string{decodableSmallcaps, "foo", decodableLegacy})),
84
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
85
- errCode: grpcCodes.FailedPrecondition,
86
- errContains: ptr("invalid"),
87
- },
88
- {label: "lone non-CapData",
89
- data: ptr("{}"),
90
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
91
- errCode: grpcCodes.FailedPrecondition,
92
- errContains: ptr("invalid CapData"),
93
- },
94
- {label: "cell with non-CapData",
95
- data: ptr(mustMarshalStreamCell("1", []string{decodableSmallcaps, "{}", decodableLegacy})),
96
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
97
- errCode: grpcCodes.FailedPrecondition,
98
- errContains: ptr("invalid CapData"),
99
- },
100
- {label: "lone smallcaps CapData",
101
- data: ptr(decodableSmallcaps),
102
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
103
- expected: types.QueryCapDataResponse{Value: `true`},
104
- },
105
- {label: "cell with smallcaps CapData",
106
- data: ptr(mustMarshalStreamCell("1", []string{decodableSmallcaps})),
107
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
108
- expected: types.QueryCapDataResponse{BlockHeight: "1", Value: `true`},
109
- },
110
- {label: "lone legacy CapData",
111
- data: ptr(decodableLegacy),
112
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
113
- expected: types.QueryCapDataResponse{Value: `true`},
114
- },
115
- {label: "cell with legacy CapData",
116
- data: ptr(mustMarshalStreamCell("1", []string{decodableLegacy})),
117
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
118
- expected: types.QueryCapDataResponse{BlockHeight: "1", Value: `true`},
119
- },
120
- }...)
121
-
122
- // Test option validation.
123
- testCases = append(testCases, []testCase{
124
- {label: "explicit JSON Lines",
125
- data: ptr(decodableSmallcaps),
126
- request: types.QueryCapDataRequest{MediaType: "JSON Lines", RemotableValueFormat: "string"},
127
- expected: types.QueryCapDataResponse{Value: `true`},
128
- },
129
- {label: "invalid media type",
130
- data: ptr(decodableSmallcaps),
131
- request: types.QueryCapDataRequest{MediaType: "JSONLines", RemotableValueFormat: "string"},
132
- errCode: grpcCodes.InvalidArgument,
133
- errContains: ptr("media_type"),
134
- },
135
- {label: "invalid item format",
136
- data: ptr(decodableSmallcaps),
137
- request: types.QueryCapDataRequest{ItemFormat: "deep", RemotableValueFormat: "string"},
138
- errCode: grpcCodes.InvalidArgument,
139
- errContains: ptr("item_format"),
140
- },
141
- {label: "missing remotable value format",
142
- data: ptr(decodableSmallcaps),
143
- request: types.QueryCapDataRequest{},
144
- errCode: grpcCodes.InvalidArgument,
145
- errContains: ptr("remotable_value_format"),
146
- },
147
- {label: "invalid remotable value format",
148
- data: ptr(decodableSmallcaps),
149
- request: types.QueryCapDataRequest{RemotableValueFormat: "foo"},
150
- errCode: grpcCodes.InvalidArgument,
151
- errContains: ptr("remotable_value_format"),
152
- },
153
- }...)
154
-
155
- // Test formatting options against sufficiently complex CapData,
156
- // deriving legacy encoding from smallcaps encoding to ensure equivalence
157
- // and deriving expectations from marshalling to avoid spurious mismatches
158
- // from Go's unpredictable field ordering (e.g., `{"a":0,"b":1}` vs. `{"b":1,"a":0}`).
159
- slots := []any{"a"}
160
- deepSmallcapsBody := `{"arr":[{"bigint":"+42","remotable":"$0.Alleged: Foo brand","ref2":"$0"}]}`
161
- deepLegacyBody := deepSmallcapsBody
162
- legacyFromSmallcaps := [][2]string{
163
- [2]string{`"+42"`, `{"@qclass":"bigint","digits":"42"}`},
164
- [2]string{`"$0.Alleged: Foo brand"`, `{"@qclass":"slot","index":0,"iface":"Alleged: Foo brand"}`},
165
- [2]string{`"$0"`, `{"@qclass":"slot","index":0}`},
166
- }
167
- for _, pair := range legacyFromSmallcaps {
168
- deepLegacyBody = strings.Replace(deepLegacyBody, pair[0], pair[1], -1)
169
- }
170
- cell := mustMarshalStreamCell("1", []string{
171
- mustJsonMarshal(map[string]any{"body": "#" + deepSmallcapsBody, "slots": slots}),
172
- mustJsonMarshal(map[string]any{"body": deepLegacyBody, "slots": slots}),
173
- })
174
- mustMarshalTwoLines := func(val any) string {
175
- line := mustJsonMarshal(val)
176
- return fmt.Sprintf("%s\n%s", line, line)
177
- }
178
- testCases = append(testCases, testCase{label: "remotables as strings",
179
- data: ptr(cell),
180
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
181
- expected: types.QueryCapDataResponse{
182
- BlockHeight: "1",
183
- Value: mustMarshalTwoLines(map[string]any{
184
- "arr": []any{
185
- map[string]any{
186
- "bigint": "42",
187
- "remotable": "[Alleged: Foo brand <a>]",
188
- "ref2": "[Alleged: Foo brand <a>]",
189
- },
190
- },
191
- }),
192
- },
193
- })
194
- testCases = append(testCases, testCase{label: "remotables as strings, flat",
195
- data: ptr(cell),
196
- request: types.QueryCapDataRequest{ItemFormat: "flat", RemotableValueFormat: "string"},
197
- expected: types.QueryCapDataResponse{
198
- BlockHeight: "1",
199
- Value: mustMarshalTwoLines(map[string]any{
200
- "arr-0-bigint": "42",
201
- "arr-0-remotable": "[Alleged: Foo brand <a>]",
202
- "arr-0-ref2": "[Alleged: Foo brand <a>]",
203
- }),
204
- },
205
- })
206
- testCases = append(testCases, testCase{label: "remotables as objects",
207
- data: ptr(cell),
208
- request: types.QueryCapDataRequest{RemotableValueFormat: "object"},
209
- expected: types.QueryCapDataResponse{
210
- BlockHeight: "1",
211
- Value: mustMarshalTwoLines(map[string]any{
212
- "arr": []any{
213
- map[string]any{
214
- "bigint": "42",
215
- "remotable": map[string]any{"id": "a", "allegedName": "Foo brand"},
216
- "ref2": map[string]any{"id": "a", "allegedName": "Foo brand"},
217
- },
218
- },
219
- }),
220
- },
221
- })
222
- testCases = append(testCases, testCase{label: "remotables as objects, flat",
223
- data: ptr(cell),
224
- request: types.QueryCapDataRequest{ItemFormat: "flat", RemotableValueFormat: "object"},
225
- expected: types.QueryCapDataResponse{
226
- BlockHeight: "1",
227
- Value: mustMarshalTwoLines(map[string]any{
228
- "arr-0-bigint": "42",
229
- "arr-0-remotable-id": "a",
230
- "arr-0-remotable-allegedName": "Foo brand",
231
- "arr-0-ref2-id": "a",
232
- "arr-0-ref2-allegedName": "Foo brand",
233
- }),
234
- },
235
- })
236
-
237
- // Test errors from CapData that includes unsupported values.
238
- expectNotImplemented := func(label, capdataBody string, slots []any) testCase {
239
- if slots == nil {
240
- slots = []any{}
241
- }
242
- serialized := mustJsonMarshal(map[string]any{
243
- "body": capdataBody,
244
- "slots": slots,
245
- })
246
- return testCase{
247
- label: label,
248
- data: ptr(serialized),
249
- request: types.QueryCapDataRequest{RemotableValueFormat: "string"},
250
- errCode: grpcCodes.FailedPrecondition,
251
- errContains: ptr("not implemented"),
252
- }
253
- }
254
- testCases = append(testCases, []testCase{
255
- expectNotImplemented("smallcaps undefined", `#"#undefined"`, nil),
256
- expectNotImplemented("smallcaps NaN", `#"#NaN"`, nil),
257
- expectNotImplemented("smallcaps infinity", `#"#Infinity"`, nil),
258
- expectNotImplemented("smallcaps negative infinity", `#"#-Infinity"`, nil),
259
- expectNotImplemented("smallcaps symbol", `#"%foo"`, nil),
260
- expectNotImplemented("smallcaps promise", `#"&0"`, []any{"a"}),
261
- expectNotImplemented("smallcaps tagged", `#{"#tag":"copySet","payload":[]}`, nil),
262
- expectNotImplemented("smallcaps error", `#{"#error":"foo","name":"Error"}`, nil),
263
- expectNotImplemented("legacy undefined", `{"@qclass":"undefined"}`, nil),
264
- expectNotImplemented("legacy NaN", `{"@qclass":"NaN"}`, nil),
265
- expectNotImplemented("legacy infinity", `{"@qclass":"Infinity"}`, nil),
266
- expectNotImplemented("legacy negative infinity", `{"@qclass":"-Infinity"}`, nil),
267
- expectNotImplemented("legacy symbol", `{"@qclass":"symbol","name":"foo"}`, nil),
268
- expectNotImplemented("smallcaps tagged", `{"@qclass":"tagged","tag":"copySet","payload":[]}`, nil),
269
- expectNotImplemented("smallcaps error", `{"@qclass":"error","message":"foo","name":"Error"}`, nil),
270
- expectNotImplemented("smallcaps Hilbert Hotel", `{"@qclass":"hilbert","original":"foo"}`, nil),
271
- }...)
272
- for _, desc := range testCases {
273
- desc.request.Path = "key"
274
- if desc.data == nil {
275
- keeper.SetStorage(ctx, agoric.NewKVEntryWithNoValue(desc.request.Path))
276
- } else {
277
- keeper.SetStorage(ctx, agoric.NewKVEntry(desc.request.Path, *desc.data))
278
- }
279
- resp, err := querier.CapData(sdk.WrapSDKContext(ctx), &desc.request)
280
- if desc.errCode == grpcCodes.OK {
281
- if err != nil {
282
- t.Errorf("%s: got unexpected error %v", desc.label, err)
283
- } else if reflect.DeepEqual(resp, &desc.expected) {
284
- continue
285
- }
286
- if resp.Value != desc.expected.Value {
287
- lines := strings.Split(resp.Value, "\n")
288
- t.Errorf("%s: wrong result value lines: %#q", desc.label, lines)
289
- } else {
290
- t.Errorf("%s: wrong result: %#v", desc.label, resp)
291
- }
292
- } else if err == nil {
293
- t.Errorf("%s: got no error, want error %q", desc.label, *desc.errContains)
294
- } else if code := grpcStatus.Code(err); code != desc.errCode {
295
- t.Errorf("%s: got error code %q, want %q", desc.label, code, desc.errCode)
296
- } else if desc.errContains != nil && !strings.Contains(err.Error(), *desc.errContains) {
297
- t.Errorf("%s: got error %v, want error %q", desc.label, err, *desc.errContains)
298
- }
299
- }
300
- }