-
Notifications
You must be signed in to change notification settings - Fork 212
/
vstorage.go
241 lines (218 loc) · 5.59 KB
/
vstorage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package vstorage
import (
"context"
"encoding/json"
"errors"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
)
type vstorageHandler struct {
keeper Keeper
}
type vstorageMessage struct {
Method string `json:"method"`
Args []json.RawMessage `json:"args"`
}
type vstorageStoreKey struct {
StoreName string `json:"storeName"`
StoreSubkey string `json:"storeSubkey"`
DataPrefixBytes string `json:"dataPrefixBytes"`
NoDataValue string `json:"noDataValue"`
}
func NewStorageHandler(keeper Keeper) vstorageHandler {
return vstorageHandler{keeper: keeper}
}
func unmarshalSinglePathFromArgs(args []json.RawMessage, path *string) error {
if len(args) == 0 {
return fmt.Errorf("missing 'path' argument")
}
if len(args) != 1 {
return fmt.Errorf("extra arguments after 'path'")
}
return json.Unmarshal(args[0], path)
}
func (sh vstorageHandler) Receive(cctx context.Context, str string) (ret string, err error) {
ctx := sdk.UnwrapSDKContext(cctx)
keeper := sh.keeper
msg := new(vstorageMessage)
err = json.Unmarshal([]byte(str), &msg)
if err != nil {
return
}
// Allow recovery from OutOfGas panics so that we don't crash
defer func() {
if r := recover(); r != nil {
switch rType := r.(type) {
case sdk.ErrorOutOfGas:
err = fmt.Errorf(
"out of gas in location: %v; gasUsed: %d",
rType.Descriptor, ctx.GasMeter().GasConsumed(),
)
default:
// Not ErrorOutOfGas, so panic again.
panic(r)
}
}
}()
// Handle generic paths.
switch msg.Method {
case "set":
for _, arg := range msg.Args {
var entry agoric.KVEntry
err = json.Unmarshal(arg, &entry)
if err != nil {
return
}
keeper.SetStorageAndNotify(ctx, entry)
}
return "true", nil
// We sometimes need to use LegacySetStorageAndNotify, because the solo's
// chain-cosmos-sdk.js consumes legacy events for `mailbox.*` and `egress.*`.
// FIXME: Use just "set" and remove this case.
case "legacySet":
for _, arg := range msg.Args {
var entry agoric.KVEntry
err = json.Unmarshal(arg, &entry)
if err != nil {
return
}
//fmt.Printf("giving Keeper.SetStorage(%s) %s\n", entry.Path(), entry.Value())
keeper.LegacySetStorageAndNotify(ctx, entry)
}
return "true", nil
case "setWithoutNotify":
for _, arg := range msg.Args {
var entry agoric.KVEntry
err = json.Unmarshal(arg, &entry)
if err != nil {
return
}
keeper.SetStorage(ctx, entry)
}
return "true", nil
case "append":
for _, arg := range msg.Args {
var entry agoric.KVEntry
err = json.Unmarshal(arg, &entry)
if err != nil {
return
}
if !entry.HasValue() {
err = fmt.Errorf("no value for append entry with path: %q", entry.Key())
return
}
err = keeper.AppendStorageValueAndNotify(ctx, entry.Key(), entry.StringValue())
if err != nil {
return
}
}
return "true", nil
case "get":
// Note that "get" does not (currently) unwrap a StreamCell.
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
if err != nil {
return
}
entry := keeper.GetEntry(ctx, path)
bz, err := json.Marshal(entry.Value())
if err != nil {
return "", err
}
return string(bz), nil
case "getStoreKey":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
if err != nil {
return
}
value := vstorageStoreKey{
StoreName: keeper.GetStoreName(),
StoreSubkey: string(keeper.PathToEncodedKey(path)),
DataPrefixBytes: string(keeper.GetDataPrefix()),
NoDataValue: string(keeper.GetNoDataValue()),
}
bz, err := json.Marshal(value)
if err != nil {
return "", err
}
return string(bz), nil
case "has":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
if err != nil {
return
}
value := keeper.HasStorage(ctx, path)
if !value {
return "false", nil
}
return "true", nil
// TODO: "keys" is deprecated
case "children", "keys":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
if err != nil {
return
}
children := keeper.GetChildren(ctx, path)
if children.Children == nil {
return "[]", nil
}
bytes, err := json.Marshal(children.Children)
if err != nil {
return "", err
}
return string(bytes), nil
case "entries":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
if err != nil {
return
}
children := keeper.GetChildren(ctx, path)
entries := make([]agoric.KVEntry, len(children.Children))
for i, child := range children.Children {
entry := keeper.GetEntry(ctx, fmt.Sprintf("%s.%s", path, child))
if !entry.HasValue() {
entries[i] = agoric.NewKVEntryWithNoValue(child)
} else {
entries[i] = agoric.NewKVEntry(child, entry.StringValue())
}
}
bytes, err := json.Marshal(entries)
if err != nil {
return "", err
}
return string(bytes), nil
case "values":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
if err != nil {
return
}
children := keeper.GetChildren(ctx, path)
vals := make([]*string, len(children.Children))
for i, child := range children.Children {
vals[i] = keeper.GetEntry(ctx, fmt.Sprintf("%s.%s", path, child)).Value()
}
bytes, err := json.Marshal(vals)
if err != nil {
return "", err
}
return string(bytes), nil
case "size":
var path string
err = unmarshalSinglePathFromArgs(msg.Args, &path)
if err != nil {
return
}
children := keeper.GetChildren(ctx, path)
if children.Children == nil {
return "0", nil
}
return fmt.Sprint(len(children.Children)), nil
}
return "", errors.New("Unrecognized msg.Method " + msg.Method)
}