@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.
- package/app/app.go +41 -23
- package/git-revision.txt +1 -1
- package/package.json +2 -2
- package/proto/agoric/swingset/genesis.proto +10 -0
- package/types/kv_entry.go +114 -0
- package/types/kv_entry_helpers.go +234 -0
- package/types/kv_entry_helpers_test.go +237 -0
- package/types/kv_entry_test.go +143 -0
- package/x/swingset/genesis.go +30 -8
- package/x/swingset/keeper/extension_snapshotter.go +25 -37
- package/x/swingset/keeper/extension_snapshotter_test.go +0 -2
- package/x/swingset/keeper/keeper.go +14 -9
- package/x/swingset/keeper/keeper_test.go +77 -0
- package/x/swingset/keeper/querier.go +1 -1
- package/x/swingset/keeper/swing_store_exports_handler.go +23 -44
- package/x/swingset/keeper/swing_store_exports_handler_test.go +1 -1
- package/x/swingset/swingset.go +71 -0
- package/x/swingset/types/genesis.pb.go +308 -18
- package/x/vstorage/keeper/grpc_query.go +1 -1
- package/x/vstorage/keeper/keeper.go +19 -19
- package/x/vstorage/keeper/keeper_grpc_test.go +3 -2
- package/x/vstorage/keeper/keeper_test.go +21 -20
- package/x/vstorage/keeper/querier.go +1 -1
- package/x/vstorage/types/types.go +0 -66
- package/x/vstorage/vstorage.go +19 -22
- package/x/vstorage/vstorage_test.go +14 -14
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
package types
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"errors"
|
|
6
|
+
"io"
|
|
7
|
+
"strings"
|
|
8
|
+
"testing"
|
|
9
|
+
|
|
10
|
+
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
func toKVEntryIdentity(entry KVEntry) (KVEntry, error) {
|
|
14
|
+
return entry, nil
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
func toKVEntryError(err error) (KVEntry, error) {
|
|
18
|
+
return KVEntry{}, err
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
func checkSameKVEntry(t *testing.T, got KVEntry, expected KVEntry) {
|
|
22
|
+
if got.key != expected.key {
|
|
23
|
+
t.Errorf("got key %s, expected key %s", got.key, expected.key)
|
|
24
|
+
}
|
|
25
|
+
if got.value == nil && expected.value != nil {
|
|
26
|
+
t.Errorf("got nil value, expected string %s", *expected.value)
|
|
27
|
+
} else if got.value != nil && expected.value == nil {
|
|
28
|
+
t.Errorf("got string value %s, expected nil", *got.value)
|
|
29
|
+
} else if got.value != nil && expected.value != nil {
|
|
30
|
+
if *got.value != *expected.value {
|
|
31
|
+
t.Errorf("got string value %s, expected %s", *got.value, *expected.value)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
func TestKVEntriesReaderNormal(t *testing.T) {
|
|
37
|
+
source := []KVEntry{NewKVEntry("foo", "bar"), NewKVEntryWithNoValue("baz")}
|
|
38
|
+
reader := kvEntriesReader[KVEntry]{entries: source, toKVEntry: toKVEntryIdentity}
|
|
39
|
+
|
|
40
|
+
got1, err := reader.Read()
|
|
41
|
+
if err != nil {
|
|
42
|
+
t.Errorf("unexpected error: %v", err)
|
|
43
|
+
}
|
|
44
|
+
checkSameKVEntry(t, got1, source[0])
|
|
45
|
+
|
|
46
|
+
got2, err := reader.Read()
|
|
47
|
+
if err != nil {
|
|
48
|
+
t.Errorf("unexpected error: %v", err)
|
|
49
|
+
}
|
|
50
|
+
checkSameKVEntry(t, got2, source[1])
|
|
51
|
+
|
|
52
|
+
_, err = reader.Read()
|
|
53
|
+
if err != io.EOF {
|
|
54
|
+
t.Errorf("expected error io.EOF, got %v", err)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_, err = reader.Read()
|
|
58
|
+
if err == nil || !strings.Contains(err.Error(), "bounds") {
|
|
59
|
+
t.Errorf("expected out of bounds error, got %v", err)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
err = reader.Close()
|
|
63
|
+
if err != nil {
|
|
64
|
+
t.Errorf("unexpected error: %v", err)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_, err = reader.Read()
|
|
68
|
+
if err == nil || !strings.Contains(err.Error(), "reader closed") {
|
|
69
|
+
t.Errorf("expected reader closed error, got %v", err)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
func TestKVEntriesReaderErrors(t *testing.T) {
|
|
74
|
+
source := []error{errors.New("foo"), errors.New("bar")}
|
|
75
|
+
reader := kvEntriesReader[error]{entries: source, toKVEntry: toKVEntryError}
|
|
76
|
+
|
|
77
|
+
_, err := reader.Read()
|
|
78
|
+
if err != source[0] {
|
|
79
|
+
t.Errorf("got error %v, expected error %v", err, source[0])
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Nothing in the reader prevents reading after previous errors
|
|
83
|
+
_, err = reader.Read()
|
|
84
|
+
if err != source[1] {
|
|
85
|
+
t.Errorf("got error %v, expected error %v", err, source[1])
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
_, err = reader.Read()
|
|
89
|
+
if err != io.EOF {
|
|
90
|
+
t.Errorf("expected error io.EOF, got %v", err)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type kvEntryReaderIterator struct {
|
|
95
|
+
reader KVEntryReader
|
|
96
|
+
current KVEntry
|
|
97
|
+
err error
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// newKVEntryReaderIterator creates an iterator over a KVEntryReader.
|
|
101
|
+
// KVEntry keys and values are reported as []byte from the reader in order.
|
|
102
|
+
func newKVEntryReaderIterator(reader KVEntryReader) sdk.Iterator {
|
|
103
|
+
iter := &kvEntryReaderIterator{
|
|
104
|
+
reader: reader,
|
|
105
|
+
}
|
|
106
|
+
iter.Next()
|
|
107
|
+
return iter
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Domain implements sdk.Iterator
|
|
111
|
+
func (iter *kvEntryReaderIterator) Domain() (start []byte, end []byte) {
|
|
112
|
+
return nil, nil
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Valid returns whether the current iterator is valid. Once invalid, the
|
|
116
|
+
// Iterator remains invalid forever.
|
|
117
|
+
func (iter *kvEntryReaderIterator) Valid() bool {
|
|
118
|
+
if iter.err == io.EOF {
|
|
119
|
+
return false
|
|
120
|
+
} else if iter.err != nil {
|
|
121
|
+
panic(iter.err)
|
|
122
|
+
}
|
|
123
|
+
return true
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// checkValid implements the validity invariants of sdk.Iterator methods.
|
|
127
|
+
func (iter *kvEntryReaderIterator) checkValid() {
|
|
128
|
+
if !iter.Valid() {
|
|
129
|
+
panic("invalid iterator")
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Next moves the iterator to the next entry from the reader.
|
|
134
|
+
// If Valid() returns false, this method will panic.
|
|
135
|
+
func (iter *kvEntryReaderIterator) Next() {
|
|
136
|
+
iter.checkValid()
|
|
137
|
+
|
|
138
|
+
iter.current, iter.err = iter.reader.Read()
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Key returns the key at the current position. Panics if the iterator is invalid.
|
|
142
|
+
// CONTRACT: key readonly []byte
|
|
143
|
+
func (iter *kvEntryReaderIterator) Key() (key []byte) {
|
|
144
|
+
iter.checkValid()
|
|
145
|
+
|
|
146
|
+
return []byte(iter.current.Key())
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Value returns the value at the current position. Panics if the iterator is invalid.
|
|
150
|
+
// CONTRACT: value readonly []byte
|
|
151
|
+
func (iter *kvEntryReaderIterator) Value() (value []byte) {
|
|
152
|
+
iter.checkValid()
|
|
153
|
+
|
|
154
|
+
if !iter.current.HasValue() {
|
|
155
|
+
return nil
|
|
156
|
+
} else {
|
|
157
|
+
return []byte(iter.current.StringValue())
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Error returns the last error encountered by the iterator, if any.
|
|
162
|
+
func (iter *kvEntryReaderIterator) Error() error {
|
|
163
|
+
err := iter.err
|
|
164
|
+
if err == io.EOF {
|
|
165
|
+
return nil
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return err
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Close closes the iterator, releasing any allocated resources.
|
|
172
|
+
func (iter *kvEntryReaderIterator) Close() error {
|
|
173
|
+
return iter.reader.Close()
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
func TestKVIteratorReader(t *testing.T) {
|
|
177
|
+
source := []KVEntry{NewKVEntry("foo", "bar"), NewKVEntryWithNoValue("baz")}
|
|
178
|
+
iterator := newKVEntryReaderIterator(&kvEntriesReader[KVEntry]{entries: source, toKVEntry: toKVEntryIdentity})
|
|
179
|
+
reader := NewKVIteratorReader(iterator)
|
|
180
|
+
|
|
181
|
+
got1, err := reader.Read()
|
|
182
|
+
if err != nil {
|
|
183
|
+
t.Errorf("unexpected error: %v", err)
|
|
184
|
+
}
|
|
185
|
+
checkSameKVEntry(t, got1, source[0])
|
|
186
|
+
|
|
187
|
+
got2, err := reader.Read()
|
|
188
|
+
if err != nil {
|
|
189
|
+
t.Errorf("unexpected error: %v", err)
|
|
190
|
+
}
|
|
191
|
+
checkSameKVEntry(t, got2, source[1])
|
|
192
|
+
|
|
193
|
+
_, err = reader.Read()
|
|
194
|
+
if err != io.EOF {
|
|
195
|
+
t.Errorf("expected error io.EOF, got %v", err)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
err = reader.Close()
|
|
199
|
+
if err != nil {
|
|
200
|
+
t.Errorf("unexpected error: %v", err)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
func TestJsonlEncodeAndReadBack(t *testing.T) {
|
|
205
|
+
source := []KVEntry{NewKVEntry("foo", "bar"), NewKVEntryWithNoValue("baz")}
|
|
206
|
+
sourceReader := &kvEntriesReader[KVEntry]{entries: source, toKVEntry: toKVEntryIdentity}
|
|
207
|
+
|
|
208
|
+
var encodedKVEntries bytes.Buffer
|
|
209
|
+
err := EncodeKVEntryReaderToJsonl(sourceReader, &encodedKVEntries)
|
|
210
|
+
if err != nil {
|
|
211
|
+
t.Errorf("unexpected encode error %v", err)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
jsonlReader := NewJsonlKVEntryDecoderReader(io.NopCloser(&encodedKVEntries))
|
|
215
|
+
|
|
216
|
+
got1, err := jsonlReader.Read()
|
|
217
|
+
if err != nil {
|
|
218
|
+
t.Errorf("unexpected error: %v", err)
|
|
219
|
+
}
|
|
220
|
+
checkSameKVEntry(t, got1, source[0])
|
|
221
|
+
|
|
222
|
+
got2, err := jsonlReader.Read()
|
|
223
|
+
if err != nil {
|
|
224
|
+
t.Errorf("unexpected error: %v", err)
|
|
225
|
+
}
|
|
226
|
+
checkSameKVEntry(t, got2, source[1])
|
|
227
|
+
|
|
228
|
+
_, err = jsonlReader.Read()
|
|
229
|
+
if err != io.EOF {
|
|
230
|
+
t.Errorf("expected error io.EOF, got %v", err)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
err = jsonlReader.Close()
|
|
234
|
+
if err != nil {
|
|
235
|
+
t.Errorf("unexpected error: %v", err)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
package types
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"errors"
|
|
6
|
+
"strings"
|
|
7
|
+
"testing"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
func checkEntry(t *testing.T, label string, entry KVEntry, isValidKey bool, expectedKey string, hasValue bool, expectedValue string) {
|
|
11
|
+
gotValidKey := entry.IsValidKey()
|
|
12
|
+
if gotValidKey != isValidKey {
|
|
13
|
+
t.Errorf("%s: valid key is %v, expected %v", label, gotValidKey, isValidKey)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
gotKey := entry.Key()
|
|
17
|
+
if gotKey != expectedKey {
|
|
18
|
+
t.Errorf("%s: got %q, want %q", label, gotKey, expectedKey)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if entry.HasValue() {
|
|
22
|
+
if !hasValue {
|
|
23
|
+
t.Errorf("%s: expected has no value", label)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
gotValue := *entry.Value()
|
|
27
|
+
if gotValue != expectedValue {
|
|
28
|
+
t.Errorf("%s: got %q, want %q", label, gotValue, expectedValue)
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
if hasValue {
|
|
32
|
+
t.Errorf("%s: expected has value", label)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
gotValuePointer := entry.Value()
|
|
36
|
+
if gotValuePointer != nil {
|
|
37
|
+
t.Errorf("%s: got %#v, want nil", label, gotValuePointer)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
gotValue := entry.StringValue()
|
|
42
|
+
if gotValue != expectedValue {
|
|
43
|
+
t.Errorf("%s: got %q, want %q", label, gotValue, expectedValue)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
func TestKVEntry(t *testing.T) {
|
|
48
|
+
type testCase struct {
|
|
49
|
+
label string
|
|
50
|
+
entry KVEntry
|
|
51
|
+
isValidKey bool
|
|
52
|
+
expectedKey string
|
|
53
|
+
hasValue bool
|
|
54
|
+
expectedValue string
|
|
55
|
+
}
|
|
56
|
+
cases := []testCase{
|
|
57
|
+
{label: "normal", entry: NewKVEntry("foo", "bar"), isValidKey: true, expectedKey: "foo", hasValue: true, expectedValue: "bar"},
|
|
58
|
+
{label: "empty string value", entry: NewKVEntry("foo", ""), isValidKey: true, expectedKey: "foo", hasValue: true, expectedValue: ""},
|
|
59
|
+
{label: "no value", entry: NewKVEntryWithNoValue("foo"), isValidKey: true, expectedKey: "foo", hasValue: false, expectedValue: ""},
|
|
60
|
+
{label: "empty key", entry: NewKVEntryWithNoValue(""), isValidKey: false, expectedKey: "", hasValue: false, expectedValue: ""},
|
|
61
|
+
}
|
|
62
|
+
for _, desc := range cases {
|
|
63
|
+
checkEntry(t, desc.label, desc.entry, desc.isValidKey, desc.expectedKey, desc.hasValue, desc.expectedValue)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
func TestKVEntryMarshall(t *testing.T) {
|
|
68
|
+
type testCase struct {
|
|
69
|
+
label string
|
|
70
|
+
entry KVEntry
|
|
71
|
+
expectedError error
|
|
72
|
+
expectedEncoding string
|
|
73
|
+
}
|
|
74
|
+
cases := []testCase{
|
|
75
|
+
{label: "normal", entry: NewKVEntry("foo", "bar"), expectedEncoding: `["foo","bar"]`},
|
|
76
|
+
{label: "empty string value", entry: NewKVEntry("foo", ""), expectedEncoding: `["foo",""]`},
|
|
77
|
+
{label: "no value", entry: NewKVEntryWithNoValue("foo"), expectedEncoding: `["foo"]`},
|
|
78
|
+
{label: "empty key", entry: NewKVEntryWithNoValue(""), expectedError: errors.New("cannot marshal invalid KVEntry")},
|
|
79
|
+
}
|
|
80
|
+
for _, desc := range cases {
|
|
81
|
+
marshalled, err := json.Marshal(desc.entry)
|
|
82
|
+
if desc.expectedError != nil && err == nil {
|
|
83
|
+
t.Errorf("%s: got nil error, expected marshal error: %q", desc.label, desc.expectedError.Error())
|
|
84
|
+
} else if err != nil {
|
|
85
|
+
if desc.expectedError == nil {
|
|
86
|
+
t.Errorf("%s: got error %v, expected no error", desc.label, err)
|
|
87
|
+
} else if !strings.Contains(err.Error(), desc.expectedError.Error()) {
|
|
88
|
+
t.Errorf("%s: got error %q, expected error %q", desc.label, err.Error(), desc.expectedError.Error())
|
|
89
|
+
}
|
|
90
|
+
continue
|
|
91
|
+
}
|
|
92
|
+
if string(marshalled) != desc.expectedEncoding {
|
|
93
|
+
t.Errorf("%s: got %q, want %q", desc.label, string(marshalled), desc.expectedEncoding)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
func TestKVEntryUnmarshall(t *testing.T) {
|
|
99
|
+
type testCase struct {
|
|
100
|
+
label string
|
|
101
|
+
encoded string
|
|
102
|
+
expectedError error
|
|
103
|
+
expectedKey string
|
|
104
|
+
hasValue bool
|
|
105
|
+
expectedValue string
|
|
106
|
+
}
|
|
107
|
+
cases := []testCase{
|
|
108
|
+
{label: "normal", encoded: `["foo","bar"]`, expectedKey: "foo", hasValue: true, expectedValue: "bar"},
|
|
109
|
+
{label: "empty string value", encoded: `["foo",""]`, expectedKey: "foo", hasValue: true, expectedValue: ""},
|
|
110
|
+
{label: "no value", encoded: `["foo"]`, expectedKey: "foo", hasValue: false, expectedValue: ""},
|
|
111
|
+
{label: "null value", encoded: `["foo",null]`, expectedKey: "foo", hasValue: false, expectedValue: ""},
|
|
112
|
+
{label: "null", encoded: `null`, expectedError: errors.New("KVEntry cannot be null")},
|
|
113
|
+
{label: "string", encoded: `"foo"`, expectedError: errors.New("json")},
|
|
114
|
+
{label: "empty array", encoded: `[]`, expectedError: errors.New("KVEntry must be an array of length 1 or 2 (not 0)")},
|
|
115
|
+
{label: "[null, null] array", encoded: `[null,null]`, expectedError: errors.New("KVEntry key must be a non-empty string")},
|
|
116
|
+
{label: "invalid key array", encoded: `[42]`, expectedError: errors.New("json")},
|
|
117
|
+
{label: "empty key", encoded: `["",null]`, expectedError: errors.New("KVEntry key must be a non-empty string")},
|
|
118
|
+
{label: "too many entries array", encoded: `["foo","bar",null]`, expectedError: errors.New("KVEntry must be an array of length 1 or 2 (not 3)")},
|
|
119
|
+
{label: "invalid value array", encoded: `["foo",42]`, expectedError: errors.New("json")},
|
|
120
|
+
}
|
|
121
|
+
for _, desc := range cases {
|
|
122
|
+
unmarshalled := NewKVEntry("untouched", "untouched")
|
|
123
|
+
err := json.Unmarshal([]byte(desc.encoded), &unmarshalled)
|
|
124
|
+
if desc.expectedError != nil && err == nil {
|
|
125
|
+
t.Errorf("%s: got nil error, expected unmarshal error: %q", desc.label, desc.expectedError.Error())
|
|
126
|
+
} else if err != nil {
|
|
127
|
+
if unmarshalled.Key() != "untouched" {
|
|
128
|
+
t.Errorf("%s: expected error to not modify target key, got %s", desc.label, unmarshalled.Key())
|
|
129
|
+
}
|
|
130
|
+
if unmarshalled.StringValue() != "untouched" {
|
|
131
|
+
t.Errorf("%s: expected error to not modify target value, got %v", desc.label, unmarshalled.Value())
|
|
132
|
+
}
|
|
133
|
+
if desc.expectedError == nil {
|
|
134
|
+
t.Errorf("%s: got error %v, expected no error", desc.label, err)
|
|
135
|
+
} else if !strings.Contains(err.Error(), desc.expectedError.Error()) {
|
|
136
|
+
t.Errorf("%s: got error %q, expected error %q", desc.label, err.Error(), desc.expectedError.Error())
|
|
137
|
+
}
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
checkEntry(t, desc.label, unmarshalled, true, desc.expectedKey, desc.hasValue, desc.expectedValue)
|
|
142
|
+
}
|
|
143
|
+
}
|
package/x/swingset/genesis.go
CHANGED
|
@@ -8,10 +8,6 @@ import (
|
|
|
8
8
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
9
9
|
)
|
|
10
10
|
|
|
11
|
-
func NewGenesisState() *types.GenesisState {
|
|
12
|
-
return &types.GenesisState{}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
11
|
func ValidateGenesis(data *types.GenesisState) error {
|
|
16
12
|
if data == nil {
|
|
17
13
|
return fmt.Errorf("swingset genesis data cannot be nil")
|
|
@@ -24,7 +20,9 @@ func ValidateGenesis(data *types.GenesisState) error {
|
|
|
24
20
|
|
|
25
21
|
func DefaultGenesisState() *types.GenesisState {
|
|
26
22
|
return &types.GenesisState{
|
|
27
|
-
Params:
|
|
23
|
+
Params: types.DefaultParams(),
|
|
24
|
+
State: types.State{},
|
|
25
|
+
SwingStoreExportData: []*types.SwingStoreExportDataEntry{},
|
|
28
26
|
}
|
|
29
27
|
}
|
|
30
28
|
|
|
@@ -34,13 +32,37 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data *types.GenesisState) bool
|
|
|
34
32
|
keeper.SetParams(ctx, data.GetParams())
|
|
35
33
|
keeper.SetState(ctx, data.GetState())
|
|
36
34
|
|
|
35
|
+
swingStoreExportData := data.GetSwingStoreExportData()
|
|
36
|
+
if len(swingStoreExportData) > 0 {
|
|
37
|
+
// See https://github.com/Agoric/agoric-sdk/issues/6527
|
|
38
|
+
panic("genesis with swing-store state not implemented")
|
|
39
|
+
}
|
|
40
|
+
|
|
37
41
|
// TODO: bootstrap only if not restoring swing-store from genesis state
|
|
38
42
|
return true
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
func ExportGenesis(ctx sdk.Context, k Keeper) *types.GenesisState {
|
|
42
|
-
gs :=
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
gs := &types.GenesisState{
|
|
47
|
+
Params: k.GetParams(ctx),
|
|
48
|
+
State: k.GetState(ctx),
|
|
49
|
+
SwingStoreExportData: []*types.SwingStoreExportDataEntry{},
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Only export the swing-store shadow copy for now
|
|
53
|
+
// TODO:
|
|
54
|
+
// - perform state-sync export with check blockHeight (figure out how to
|
|
55
|
+
// handle export of historical height),
|
|
56
|
+
// - include swing-store artifacts in genesis state
|
|
57
|
+
// See https://github.com/Agoric/agoric-sdk/issues/6527
|
|
58
|
+
exportDataIterator := k.GetSwingStore(ctx).Iterator(nil, nil)
|
|
59
|
+
defer exportDataIterator.Close()
|
|
60
|
+
for ; exportDataIterator.Valid(); exportDataIterator.Next() {
|
|
61
|
+
entry := types.SwingStoreExportDataEntry{
|
|
62
|
+
Key: string(exportDataIterator.Key()),
|
|
63
|
+
Value: string(exportDataIterator.Value()),
|
|
64
|
+
}
|
|
65
|
+
gs.SwingStoreExportData = append(gs.SwingStoreExportData, &entry)
|
|
66
|
+
}
|
|
45
67
|
return gs
|
|
46
68
|
}
|
|
@@ -2,19 +2,16 @@ package keeper
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"bytes"
|
|
5
|
-
"encoding/json"
|
|
6
5
|
"errors"
|
|
7
6
|
"fmt"
|
|
8
7
|
"io"
|
|
9
8
|
"math"
|
|
10
9
|
|
|
10
|
+
agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
|
|
11
11
|
"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
|
|
12
|
-
vstoragetypes "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
|
|
13
12
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
14
13
|
snapshots "github.com/cosmos/cosmos-sdk/snapshots/types"
|
|
15
|
-
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
16
14
|
"github.com/tendermint/tendermint/libs/log"
|
|
17
|
-
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
18
15
|
)
|
|
19
16
|
|
|
20
17
|
// This module implements a Cosmos ExtensionSnapshotter to capture and restore
|
|
@@ -67,30 +64,26 @@ type snapshotDetails struct {
|
|
|
67
64
|
type ExtensionSnapshotter struct {
|
|
68
65
|
isConfigured func() bool
|
|
69
66
|
// takeAppSnapshot is called by OnExportStarted when creating a snapshot
|
|
70
|
-
takeAppSnapshot
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
activeSnapshot *snapshotDetails
|
|
67
|
+
takeAppSnapshot func(height int64)
|
|
68
|
+
swingStoreExportsHandler *SwingStoreExportsHandler
|
|
69
|
+
getSwingStoreExportDataShadowCopyReader func(height int64) agoric.KVEntryReader
|
|
70
|
+
logger log.Logger
|
|
71
|
+
activeSnapshot *snapshotDetails
|
|
76
72
|
}
|
|
77
73
|
|
|
78
74
|
// NewExtensionSnapshotter creates a new swingset ExtensionSnapshotter
|
|
79
75
|
func NewExtensionSnapshotter(
|
|
80
76
|
app *baseapp.BaseApp,
|
|
81
77
|
swingStoreExportsHandler *SwingStoreExportsHandler,
|
|
82
|
-
|
|
78
|
+
getSwingStoreExportDataShadowCopyReader func(height int64) agoric.KVEntryReader,
|
|
83
79
|
) *ExtensionSnapshotter {
|
|
84
80
|
return &ExtensionSnapshotter{
|
|
85
|
-
isConfigured:
|
|
86
|
-
takeAppSnapshot:
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
swingStoreExportsHandler: swingStoreExportsHandler,
|
|
92
|
-
getSwingStoreExportDataShadowCopy: getSwingStoreExportDataShadowCopy,
|
|
93
|
-
activeSnapshot: nil,
|
|
81
|
+
isConfigured: func() bool { return app.SnapshotManager() != nil },
|
|
82
|
+
takeAppSnapshot: app.Snapshot,
|
|
83
|
+
logger: app.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName), "submodule", "extension snapshotter"),
|
|
84
|
+
swingStoreExportsHandler: swingStoreExportsHandler,
|
|
85
|
+
getSwingStoreExportDataShadowCopyReader: getSwingStoreExportDataShadowCopyReader,
|
|
86
|
+
activeSnapshot: nil,
|
|
94
87
|
}
|
|
95
88
|
}
|
|
96
89
|
|
|
@@ -235,7 +228,7 @@ func (snapshotter *ExtensionSnapshotter) OnExportRetrieved(provider SwingStoreEx
|
|
|
235
228
|
}
|
|
236
229
|
|
|
237
230
|
for {
|
|
238
|
-
artifact, err := provider.
|
|
231
|
+
artifact, err := provider.ReadNextArtifact()
|
|
239
232
|
if err == io.EOF {
|
|
240
233
|
break
|
|
241
234
|
} else if err != nil {
|
|
@@ -248,13 +241,14 @@ func (snapshotter *ExtensionSnapshotter) OnExportRetrieved(provider SwingStoreEx
|
|
|
248
241
|
}
|
|
249
242
|
}
|
|
250
243
|
|
|
251
|
-
|
|
244
|
+
exportDataReader, err := provider.GetExportDataReader()
|
|
252
245
|
if err != nil {
|
|
253
246
|
return err
|
|
254
247
|
}
|
|
255
|
-
if
|
|
248
|
+
if exportDataReader == nil {
|
|
256
249
|
return nil
|
|
257
250
|
}
|
|
251
|
+
defer exportDataReader.Close()
|
|
258
252
|
|
|
259
253
|
// For debugging, write out any retrieved export data as a single untrusted artifact
|
|
260
254
|
// which has the same encoding as the internal SwingStore export data representation:
|
|
@@ -262,14 +256,9 @@ func (snapshotter *ExtensionSnapshotter) OnExportRetrieved(provider SwingStoreEx
|
|
|
262
256
|
exportDataArtifact := types.SwingStoreArtifact{Name: UntrustedExportDataArtifactName}
|
|
263
257
|
|
|
264
258
|
var encodedExportData bytes.Buffer
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
entry := []string{dataEntry.Path, dataEntry.Value}
|
|
269
|
-
err := encoder.Encode(entry)
|
|
270
|
-
if err != nil {
|
|
271
|
-
return err
|
|
272
|
-
}
|
|
259
|
+
err = agoric.EncodeKVEntryReaderToJsonl(exportDataReader, &encodedExportData)
|
|
260
|
+
if err != nil {
|
|
261
|
+
return err
|
|
273
262
|
}
|
|
274
263
|
exportDataArtifact.Data = encodedExportData.Bytes()
|
|
275
264
|
|
|
@@ -298,13 +287,12 @@ func (snapshotter *ExtensionSnapshotter) RestoreExtension(blockHeight uint64, fo
|
|
|
298
287
|
// At this point the content of the cosmos DB has been verified against the
|
|
299
288
|
// AppHash, which means the SwingStore data it contains can be used as the
|
|
300
289
|
// trusted root against which to validate the artifacts.
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
return exportData, nil
|
|
290
|
+
getExportDataReader := func() (agoric.KVEntryReader, error) {
|
|
291
|
+
exportDataReader := snapshotter.getSwingStoreExportDataShadowCopyReader(height)
|
|
292
|
+
return exportDataReader, nil
|
|
305
293
|
}
|
|
306
294
|
|
|
307
|
-
|
|
295
|
+
readNextArtifact := func() (artifact types.SwingStoreArtifact, err error) {
|
|
308
296
|
payloadBytes, err := payloadReader()
|
|
309
297
|
if err != nil {
|
|
310
298
|
return artifact, err
|
|
@@ -315,7 +303,7 @@ func (snapshotter *ExtensionSnapshotter) RestoreExtension(blockHeight uint64, fo
|
|
|
315
303
|
}
|
|
316
304
|
|
|
317
305
|
return snapshotter.swingStoreExportsHandler.RestoreExport(
|
|
318
|
-
SwingStoreExportProvider{BlockHeight: blockHeight,
|
|
306
|
+
SwingStoreExportProvider{BlockHeight: blockHeight, GetExportDataReader: getExportDataReader, ReadNextArtifact: readNextArtifact},
|
|
319
307
|
SwingStoreRestoreOptions{IncludeHistorical: false},
|
|
320
308
|
)
|
|
321
309
|
}
|
|
@@ -4,7 +4,6 @@ import (
|
|
|
4
4
|
"io"
|
|
5
5
|
"testing"
|
|
6
6
|
|
|
7
|
-
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
8
7
|
"github.com/tendermint/tendermint/libs/log"
|
|
9
8
|
)
|
|
10
9
|
|
|
@@ -12,7 +11,6 @@ func newTestExtensionSnapshotter() *ExtensionSnapshotter {
|
|
|
12
11
|
logger := log.NewNopLogger() // log.NewTMLogger(log.NewSyncWriter( /* os.Stdout*/ io.Discard)).With("module", "sdk/app")
|
|
13
12
|
return &ExtensionSnapshotter{
|
|
14
13
|
isConfigured: func() bool { return true },
|
|
15
|
-
newRestoreContext: func(height int64) sdk.Context { return sdk.Context{} },
|
|
16
14
|
logger: logger,
|
|
17
15
|
swingStoreExportsHandler: newTestSwingStoreExportsHandler(),
|
|
18
16
|
}
|
|
@@ -11,15 +11,16 @@ import (
|
|
|
11
11
|
|
|
12
12
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
13
13
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
14
|
+
"github.com/cosmos/cosmos-sdk/store/prefix"
|
|
14
15
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
15
16
|
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
|
16
17
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
|
17
18
|
|
|
18
19
|
"github.com/Agoric/agoric-sdk/golang/cosmos/ante"
|
|
20
|
+
agoric "github.com/Agoric/agoric-sdk/golang/cosmos/types"
|
|
19
21
|
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
|
|
20
22
|
"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
|
|
21
23
|
vstoragekeeper "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/keeper"
|
|
22
|
-
vstoragetypes "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
|
|
23
24
|
)
|
|
24
25
|
|
|
25
26
|
// Top-level paths for chain storage should remain synchronized with
|
|
@@ -36,7 +37,10 @@ const (
|
|
|
36
37
|
StoragePathSwingStore = "swingStore"
|
|
37
38
|
)
|
|
38
39
|
|
|
39
|
-
const
|
|
40
|
+
const (
|
|
41
|
+
stateKey = "state"
|
|
42
|
+
swingStoreKeyPrefix = "swingStore."
|
|
43
|
+
)
|
|
40
44
|
|
|
41
45
|
// Contextual information about the message source of an action on an inbound queue.
|
|
42
46
|
// This context should be unique per inboundQueueRecord.
|
|
@@ -261,7 +265,7 @@ func getBeansOwingPathForAddress(addr sdk.AccAddress) string {
|
|
|
261
265
|
func (k Keeper) GetBeansOwing(ctx sdk.Context, addr sdk.AccAddress) sdk.Uint {
|
|
262
266
|
path := getBeansOwingPathForAddress(addr)
|
|
263
267
|
entry := k.vstorageKeeper.GetEntry(ctx, path)
|
|
264
|
-
if !entry.
|
|
268
|
+
if !entry.HasValue() {
|
|
265
269
|
return sdk.ZeroUint()
|
|
266
270
|
}
|
|
267
271
|
return sdk.NewUintFromString(entry.StringValue())
|
|
@@ -271,7 +275,7 @@ func (k Keeper) GetBeansOwing(ctx sdk.Context, addr sdk.AccAddress) sdk.Uint {
|
|
|
271
275
|
// feeCollector but has not yet paid.
|
|
272
276
|
func (k Keeper) SetBeansOwing(ctx sdk.Context, addr sdk.AccAddress, beans sdk.Uint) {
|
|
273
277
|
path := getBeansOwingPathForAddress(addr)
|
|
274
|
-
k.vstorageKeeper.SetStorage(ctx,
|
|
278
|
+
k.vstorageKeeper.SetStorage(ctx, agoric.NewKVEntry(path, beans.String()))
|
|
275
279
|
}
|
|
276
280
|
|
|
277
281
|
// ChargeBeans charges the given address the given number of beans. It divides
|
|
@@ -375,7 +379,7 @@ func (k Keeper) ChargeForProvisioning(ctx sdk.Context, submitter, addr sdk.AccAd
|
|
|
375
379
|
func (k Keeper) GetEgress(ctx sdk.Context, addr sdk.AccAddress) types.Egress {
|
|
376
380
|
path := StoragePathEgress + "." + addr.String()
|
|
377
381
|
entry := k.vstorageKeeper.GetEntry(ctx, path)
|
|
378
|
-
if !entry.
|
|
382
|
+
if !entry.HasValue() {
|
|
379
383
|
return types.Egress{}
|
|
380
384
|
}
|
|
381
385
|
|
|
@@ -398,7 +402,7 @@ func (k Keeper) SetEgress(ctx sdk.Context, egress *types.Egress) error {
|
|
|
398
402
|
}
|
|
399
403
|
|
|
400
404
|
// FIXME: We should use just SetStorageAndNotify here, but solo needs legacy for now.
|
|
401
|
-
k.vstorageKeeper.LegacySetStorageAndNotify(ctx,
|
|
405
|
+
k.vstorageKeeper.LegacySetStorageAndNotify(ctx, agoric.NewKVEntry(path, string(bz)))
|
|
402
406
|
|
|
403
407
|
// Now make sure the corresponding account has been initialised.
|
|
404
408
|
if acc := k.accountKeeper.GetAccount(ctx, egress.Peer); acc != nil {
|
|
@@ -431,11 +435,12 @@ func (k Keeper) GetMailbox(ctx sdk.Context, peer string) string {
|
|
|
431
435
|
func (k Keeper) SetMailbox(ctx sdk.Context, peer string, mailbox string) {
|
|
432
436
|
path := StoragePathMailbox + "." + peer
|
|
433
437
|
// FIXME: We should use just SetStorageAndNotify here, but solo needs legacy for now.
|
|
434
|
-
k.vstorageKeeper.LegacySetStorageAndNotify(ctx,
|
|
438
|
+
k.vstorageKeeper.LegacySetStorageAndNotify(ctx, agoric.NewKVEntry(path, mailbox))
|
|
435
439
|
}
|
|
436
440
|
|
|
437
|
-
func (k Keeper)
|
|
438
|
-
|
|
441
|
+
func (k Keeper) GetSwingStore(ctx sdk.Context) sdk.KVStore {
|
|
442
|
+
store := ctx.KVStore(k.storeKey)
|
|
443
|
+
return prefix.NewStore(store, []byte(swingStoreKeyPrefix))
|
|
439
444
|
}
|
|
440
445
|
|
|
441
446
|
func (k Keeper) PathToEncodedKey(path string) []byte {
|