@agoric/cosmos 0.33.0 → 0.33.1-dev-800c2bb.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/Makefile +1 -1
- package/app/app.go +109 -16
- package/app/const.go +2 -2
- package/daemon/cmd/root.go +8 -13
- package/package.json +2 -2
- package/proto/agoric/swingset/msgs.proto +10 -0
- package/proto/agoric/swingset/swingset.proto +14 -0
- package/x/swingset/abci.go +24 -17
- package/x/swingset/alias.go +1 -0
- package/x/swingset/client/cli/tx.go +18 -0
- package/x/swingset/genesis.go +11 -6
- package/x/swingset/keeper/keeper.go +131 -56
- package/x/swingset/keeper/migrations.go +27 -0
- package/x/swingset/keeper/msg_server.go +23 -6
- package/x/swingset/keeper/proposal.go +1 -1
- package/x/swingset/keeper/querier.go +3 -3
- package/x/swingset/keeper/snapshotter.go +491 -0
- package/x/swingset/keeper/snapshotter_test.go +185 -0
- package/x/swingset/legacy/v32/params.go +37 -0
- package/x/swingset/legacy/v32/params_test.go +133 -0
- package/x/swingset/module.go +6 -1
- package/x/swingset/types/default-params.go +5 -2
- package/x/swingset/types/expected_keepers.go +1 -0
- package/x/swingset/types/msgs.go +156 -10
- package/x/swingset/types/msgs.pb.go +142 -47
- package/x/swingset/types/msgs_test.go +174 -0
- package/x/swingset/types/swingset.pb.go +280 -51
- package/x/swingset/types/types.go +1 -1
- package/x/vbank/README.md +1 -1
- package/x/vbank/keeper/keeper.go +15 -1
- package/x/vbank/module.go +13 -1
- package/x/vbank/types/expected_keepers.go +3 -1
- package/x/vbank/vbank.go +8 -0
- package/x/vbank/vbank_test.go +70 -9
- package/x/vstorage/keeper/grpc_query.go +2 -2
- package/x/vstorage/keeper/keeper.go +269 -38
- package/x/vstorage/keeper/keeper_test.go +162 -17
- package/x/vstorage/keeper/querier.go +4 -4
- package/x/vstorage/module.go +2 -0
- package/x/vstorage/types/path_keys.go +10 -3
- package/x/vstorage/types/types.go +66 -0
- package/x/vstorage/vstorage.go +118 -25
- package/x/vstorage/vstorage_test.go +297 -0
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
package keeper
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"errors"
|
|
6
|
+
"fmt"
|
|
7
|
+
"io"
|
|
8
|
+
"os"
|
|
9
|
+
"path/filepath"
|
|
10
|
+
"regexp"
|
|
11
|
+
|
|
12
|
+
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
|
|
13
|
+
"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
|
|
14
|
+
vstoragetypes "github.com/Agoric/agoric-sdk/golang/cosmos/x/vstorage/types"
|
|
15
|
+
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
16
|
+
snapshots "github.com/cosmos/cosmos-sdk/snapshots/types"
|
|
17
|
+
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
18
|
+
"github.com/tendermint/tendermint/libs/log"
|
|
19
|
+
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
var _ snapshots.ExtensionSnapshotter = &SwingsetSnapshotter{}
|
|
23
|
+
|
|
24
|
+
// SnapshotFormat 1 is a proto message containing an artifact name, and the binary artifact data
|
|
25
|
+
const SnapshotFormat = 1
|
|
26
|
+
|
|
27
|
+
// The manifest filename must be synchronized with the JS export/import tooling
|
|
28
|
+
const ExportManifestFilename = "export-manifest.json"
|
|
29
|
+
const ExportDataFilename = "export-data.jsonl"
|
|
30
|
+
const UntrustedExportDataArtifactName = "UNTRUSTED-EXPORT-DATA"
|
|
31
|
+
const UntrustedExportDataFilename = "untrusted-export-data.jsonl"
|
|
32
|
+
const ExportedFilesMode = 0644
|
|
33
|
+
|
|
34
|
+
var disallowedArtifactNameChar = regexp.MustCompile(`[^-_.a-zA-Z0-9]`)
|
|
35
|
+
|
|
36
|
+
// sanitizeArtifactName searches a string for all characters
|
|
37
|
+
// other than ASCII alphanumerics, hyphens, underscores, and dots,
|
|
38
|
+
// and replaces each of them with a hyphen.
|
|
39
|
+
func sanitizeArtifactName(name string) string {
|
|
40
|
+
return disallowedArtifactNameChar.ReplaceAllString(name, "-")
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type activeSnapshot struct {
|
|
44
|
+
// The block height of the snapshot in progress
|
|
45
|
+
height int64
|
|
46
|
+
// The logger for this snapshot
|
|
47
|
+
logger log.Logger
|
|
48
|
+
// Use to synchronize the commit boundary
|
|
49
|
+
startedResult chan error
|
|
50
|
+
// Internal flag indicating whether the cosmos driven snapshot process completed
|
|
51
|
+
// Only read or written by the snapshot worker goroutine.
|
|
52
|
+
retrieved bool
|
|
53
|
+
// Closed when this snapshot is complete
|
|
54
|
+
done chan struct{}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type exportManifest struct {
|
|
58
|
+
BlockHeight uint64 `json:"blockHeight,omitempty"`
|
|
59
|
+
// The filename of the export data
|
|
60
|
+
Data string `json:"data,omitempty"`
|
|
61
|
+
// The list of artifact names and their corresponding filenames
|
|
62
|
+
Artifacts [][2]string `json:"artifacts"`
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
type SwingStoreExporter interface {
|
|
66
|
+
ExportSwingStore(ctx sdk.Context) []*vstoragetypes.DataEntry
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type SwingsetSnapshotter struct {
|
|
70
|
+
isConfigured func() bool
|
|
71
|
+
takeSnapshot func(height int64)
|
|
72
|
+
newRestoreContext func(height int64) sdk.Context
|
|
73
|
+
logger log.Logger
|
|
74
|
+
exporter SwingStoreExporter
|
|
75
|
+
blockingSend func(action vm.Jsonable) (string, error)
|
|
76
|
+
// Only modified by the main goroutine.
|
|
77
|
+
activeSnapshot *activeSnapshot
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
type snapshotAction struct {
|
|
81
|
+
Type string `json:"type"` // COSMOS_SNAPSHOT
|
|
82
|
+
BlockHeight int64 `json:"blockHeight"`
|
|
83
|
+
Request string `json:"request"` // "initiate", "discard", "retrieve", or "restore"
|
|
84
|
+
Args []json.RawMessage `json:"args,omitempty"`
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
func NewSwingsetSnapshotter(app *baseapp.BaseApp, exporter SwingStoreExporter, sendToController func(bool, string) (string, error)) SwingsetSnapshotter {
|
|
88
|
+
// The sendToController performed by this submodule are non-deterministic.
|
|
89
|
+
// This submodule will send messages to JS from goroutines at unpredictable
|
|
90
|
+
// times, but this is safe because when handling the messages, the JS side
|
|
91
|
+
// does not perform operations affecting consensus and ignores state changes
|
|
92
|
+
// since committing the previous block.
|
|
93
|
+
// Since this submodule implements block level commit synchronization, the
|
|
94
|
+
// processing and results are both insensitive to sub-block timing of messages.
|
|
95
|
+
|
|
96
|
+
blockingSend := func(action vm.Jsonable) (string, error) {
|
|
97
|
+
bz, err := json.Marshal(action)
|
|
98
|
+
if err != nil {
|
|
99
|
+
return "", err
|
|
100
|
+
}
|
|
101
|
+
return sendToController(true, string(bz))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return SwingsetSnapshotter{
|
|
105
|
+
isConfigured: func() bool { return app.SnapshotManager() != nil },
|
|
106
|
+
takeSnapshot: app.Snapshot,
|
|
107
|
+
newRestoreContext: func(height int64) sdk.Context {
|
|
108
|
+
return app.NewUncachedContext(false, tmproto.Header{Height: height})
|
|
109
|
+
},
|
|
110
|
+
logger: app.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName), "submodule", "snapshotter"),
|
|
111
|
+
exporter: exporter,
|
|
112
|
+
blockingSend: blockingSend,
|
|
113
|
+
activeSnapshot: nil,
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// InitiateSnapshot synchronously initiates a snapshot for the given height.
|
|
118
|
+
// If a snapshot is already in progress, or if no snapshot manager is configured,
|
|
119
|
+
// this will fail.
|
|
120
|
+
// The snapshot operation is performed in a goroutine, and synchronized with the
|
|
121
|
+
// main thread through the `WaitUntilSnapshotStarted` method.
|
|
122
|
+
func (snapshotter *SwingsetSnapshotter) InitiateSnapshot(height int64) error {
|
|
123
|
+
if snapshotter.activeSnapshot != nil {
|
|
124
|
+
select {
|
|
125
|
+
case <-snapshotter.activeSnapshot.done:
|
|
126
|
+
snapshotter.activeSnapshot = nil
|
|
127
|
+
default:
|
|
128
|
+
return fmt.Errorf("snapshot already in progress for height %d", snapshotter.activeSnapshot.height)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if !snapshotter.isConfigured() {
|
|
133
|
+
return fmt.Errorf("snapshot manager not configured")
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
logger := snapshotter.logger.With("height", height)
|
|
137
|
+
|
|
138
|
+
// Indicate that a snapshot has been initiated by setting `activeSnapshot`.
|
|
139
|
+
// This structure is used to synchronize with the goroutine spawned below.
|
|
140
|
+
// It's nilled-out before exiting (and is the only code that does so).
|
|
141
|
+
active := &activeSnapshot{
|
|
142
|
+
height: height,
|
|
143
|
+
logger: logger,
|
|
144
|
+
startedResult: make(chan error, 1),
|
|
145
|
+
retrieved: false,
|
|
146
|
+
done: make(chan struct{}),
|
|
147
|
+
}
|
|
148
|
+
snapshotter.activeSnapshot = active
|
|
149
|
+
|
|
150
|
+
go func() {
|
|
151
|
+
defer close(active.done)
|
|
152
|
+
|
|
153
|
+
action := &snapshotAction{
|
|
154
|
+
Type: "COSMOS_SNAPSHOT",
|
|
155
|
+
BlockHeight: height,
|
|
156
|
+
Request: "initiate",
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// blockingSend for COSMOS_SNAPSHOT action is safe to call from a goroutine
|
|
160
|
+
_, err := snapshotter.blockingSend(action)
|
|
161
|
+
|
|
162
|
+
if err != nil {
|
|
163
|
+
// First indicate a snapshot is no longer in progress if the call to
|
|
164
|
+
// `WaitUntilSnapshotStarted` has't happened yet.
|
|
165
|
+
// Then signal the current snapshot operation if a call to
|
|
166
|
+
// `WaitUntilSnapshotStarted` was already waiting.
|
|
167
|
+
active.startedResult <- err
|
|
168
|
+
close(active.startedResult)
|
|
169
|
+
logger.Error("failed to initiate swingset snapshot", "err", err)
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Signal that the snapshot operation has started in the goroutine. Calls to
|
|
174
|
+
// `WaitUntilSnapshotStarted` will no longer block.
|
|
175
|
+
close(active.startedResult)
|
|
176
|
+
|
|
177
|
+
// In production this should indirectly call SnapshotExtension().
|
|
178
|
+
snapshotter.takeSnapshot(height)
|
|
179
|
+
|
|
180
|
+
// Check whether the cosmos Snapshot() method successfully handled our extension
|
|
181
|
+
if active.retrieved {
|
|
182
|
+
return
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
logger.Error("failed to make swingset snapshot")
|
|
186
|
+
action = &snapshotAction{
|
|
187
|
+
Type: "COSMOS_SNAPSHOT",
|
|
188
|
+
BlockHeight: height,
|
|
189
|
+
Request: "discard",
|
|
190
|
+
}
|
|
191
|
+
_, err = snapshotter.blockingSend(action)
|
|
192
|
+
|
|
193
|
+
if err != nil {
|
|
194
|
+
logger.Error("failed to discard swingset snapshot", "err", err)
|
|
195
|
+
}
|
|
196
|
+
}()
|
|
197
|
+
|
|
198
|
+
return nil
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// WaitUntilSnapshotStarted synchronizes with a snapshot in progress, if any.
|
|
202
|
+
// The JS SwingStore export must have started before a new block is committed.
|
|
203
|
+
// The app must call this method before sending a commit action to SwingSet.
|
|
204
|
+
//
|
|
205
|
+
// Waits for a just initiated snapshot to have started in its goroutine.
|
|
206
|
+
// If no snapshot is in progress (`InitiateSnapshot` hasn't been called or
|
|
207
|
+
// already completed), or if we previously checked if the snapshot had started,
|
|
208
|
+
// returns immediately.
|
|
209
|
+
func (snapshotter *SwingsetSnapshotter) WaitUntilSnapshotStarted() error {
|
|
210
|
+
activeSnapshot := snapshotter.activeSnapshot
|
|
211
|
+
if activeSnapshot == nil {
|
|
212
|
+
return nil
|
|
213
|
+
}
|
|
214
|
+
// Block until the active snapshot has started, saving the result.
|
|
215
|
+
// The snapshot goroutine only produces a value in case of an error,
|
|
216
|
+
// and closes the channel once the snapshot has started or failed.
|
|
217
|
+
// Only the first call after a snapshot was initiated will report an error.
|
|
218
|
+
startErr := <-activeSnapshot.startedResult
|
|
219
|
+
|
|
220
|
+
// Check if the active snapshot is done, and if so, nil it out so future
|
|
221
|
+
// calls are faster.
|
|
222
|
+
select {
|
|
223
|
+
case <-activeSnapshot.done:
|
|
224
|
+
snapshotter.activeSnapshot = nil
|
|
225
|
+
default:
|
|
226
|
+
// don't wait for it to finish
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return startErr
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// SnapshotName returns the name of snapshotter, it should be unique in the manager.
|
|
233
|
+
// Implements ExtensionSnapshotter
|
|
234
|
+
func (snapshotter *SwingsetSnapshotter) SnapshotName() string {
|
|
235
|
+
return types.ModuleName
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// SnapshotFormat returns the default format the extension snapshotter uses to encode the
|
|
239
|
+
// payloads when taking a snapshot.
|
|
240
|
+
// It's defined within the extension, different from the global format for the whole state-sync snapshot.
|
|
241
|
+
// Implements ExtensionSnapshotter
|
|
242
|
+
func (snapshotter *SwingsetSnapshotter) SnapshotFormat() uint32 {
|
|
243
|
+
return SnapshotFormat
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// SupportedFormats returns a list of formats it can restore from.
|
|
247
|
+
// Implements ExtensionSnapshotter
|
|
248
|
+
func (snapshotter *SwingsetSnapshotter) SupportedFormats() []uint32 {
|
|
249
|
+
return []uint32{SnapshotFormat}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// SnapshotExtension writes extension payloads into the underlying protobuf stream.
|
|
253
|
+
// This operation is invoked by the snapshot manager in the goroutine started by
|
|
254
|
+
// `InitiateSnapshot`.
|
|
255
|
+
// Implements ExtensionSnapshotter
|
|
256
|
+
func (snapshotter *SwingsetSnapshotter) SnapshotExtension(height uint64, payloadWriter snapshots.ExtensionPayloadWriter) (err error) {
|
|
257
|
+
defer func() {
|
|
258
|
+
// Since the cosmos layers do a poor job of reporting errors, do our own reporting
|
|
259
|
+
// `err` will be set correctly regardless if it was explicitly assigned or
|
|
260
|
+
// a value was provided to a `return` statement.
|
|
261
|
+
// See https://go.dev/blog/defer-panic-and-recover for details
|
|
262
|
+
if err != nil {
|
|
263
|
+
var logger log.Logger
|
|
264
|
+
if snapshotter.activeSnapshot != nil {
|
|
265
|
+
logger = snapshotter.activeSnapshot.logger
|
|
266
|
+
} else {
|
|
267
|
+
logger = snapshotter.logger
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
logger.Error("swingset snapshot extension failed", "err", err)
|
|
271
|
+
}
|
|
272
|
+
}()
|
|
273
|
+
|
|
274
|
+
activeSnapshot := snapshotter.activeSnapshot
|
|
275
|
+
if activeSnapshot == nil {
|
|
276
|
+
// shouldn't happen, but return an error if it does
|
|
277
|
+
return errors.New("no active swingset snapshot")
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if activeSnapshot.height != int64(height) {
|
|
281
|
+
return fmt.Errorf("swingset snapshot requested for unexpected height %d (expected %d)", height, activeSnapshot.height)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
action := &snapshotAction{
|
|
285
|
+
Type: "COSMOS_SNAPSHOT",
|
|
286
|
+
BlockHeight: activeSnapshot.height,
|
|
287
|
+
Request: "retrieve",
|
|
288
|
+
}
|
|
289
|
+
out, err := snapshotter.blockingSend(action)
|
|
290
|
+
|
|
291
|
+
if err != nil {
|
|
292
|
+
return err
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
var exportDir string
|
|
296
|
+
err = json.Unmarshal([]byte(out), &exportDir)
|
|
297
|
+
if err != nil {
|
|
298
|
+
return err
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
defer os.RemoveAll(exportDir)
|
|
302
|
+
|
|
303
|
+
rawManifest, err := os.ReadFile(filepath.Join(exportDir, ExportManifestFilename))
|
|
304
|
+
if err != nil {
|
|
305
|
+
return err
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
var manifest exportManifest
|
|
309
|
+
err = json.Unmarshal(rawManifest, &manifest)
|
|
310
|
+
if err != nil {
|
|
311
|
+
return err
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if manifest.BlockHeight != height {
|
|
315
|
+
return fmt.Errorf("snapshot manifest blockHeight (%d) doesn't match (%d)", manifest.BlockHeight, height)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
writeFileToPayload := func(fileName string, artifactName string) error {
|
|
319
|
+
payload := types.ExtensionSnapshotterArtifactPayload{Name: artifactName}
|
|
320
|
+
|
|
321
|
+
payload.Data, err = os.ReadFile(filepath.Join(exportDir, fileName))
|
|
322
|
+
if err != nil {
|
|
323
|
+
return err
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
payloadBytes, err := payload.Marshal()
|
|
327
|
+
if err != nil {
|
|
328
|
+
return err
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
err = payloadWriter(payloadBytes)
|
|
332
|
+
if err != nil {
|
|
333
|
+
return err
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return nil
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if manifest.Data != "" {
|
|
340
|
+
err = writeFileToPayload(manifest.Data, UntrustedExportDataArtifactName)
|
|
341
|
+
if err != nil {
|
|
342
|
+
return err
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
for _, artifactInfo := range manifest.Artifacts {
|
|
347
|
+
artifactName := artifactInfo[0]
|
|
348
|
+
fileName := artifactInfo[1]
|
|
349
|
+
if artifactName == UntrustedExportDataArtifactName {
|
|
350
|
+
return fmt.Errorf("unexpected artifact name %s", artifactName)
|
|
351
|
+
}
|
|
352
|
+
err = writeFileToPayload(fileName, artifactName)
|
|
353
|
+
if err != nil {
|
|
354
|
+
return err
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
activeSnapshot.retrieved = true
|
|
359
|
+
activeSnapshot.logger.Info("retrieved snapshot", "exportDir", exportDir)
|
|
360
|
+
|
|
361
|
+
return nil
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// RestoreExtension restores an extension state snapshot,
|
|
365
|
+
// the payload reader returns `io.EOF` when it reaches the extension boundaries.
|
|
366
|
+
// Implements ExtensionSnapshotter
|
|
367
|
+
func (snapshotter *SwingsetSnapshotter) RestoreExtension(height uint64, format uint32, payloadReader snapshots.ExtensionPayloadReader) error {
|
|
368
|
+
if format != SnapshotFormat {
|
|
369
|
+
return snapshots.ErrUnknownFormat
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
ctx := snapshotter.newRestoreContext(int64(height))
|
|
373
|
+
|
|
374
|
+
exportDir, err := os.MkdirTemp("", fmt.Sprintf("agd-state-sync-restore-%d-*", height))
|
|
375
|
+
if err != nil {
|
|
376
|
+
return err
|
|
377
|
+
}
|
|
378
|
+
defer os.RemoveAll(exportDir)
|
|
379
|
+
|
|
380
|
+
manifest := exportManifest{
|
|
381
|
+
BlockHeight: height,
|
|
382
|
+
Data: ExportDataFilename,
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
exportDataFile, err := os.OpenFile(filepath.Join(exportDir, ExportDataFilename), os.O_CREATE|os.O_WRONLY, ExportedFilesMode)
|
|
386
|
+
if err != nil {
|
|
387
|
+
return err
|
|
388
|
+
}
|
|
389
|
+
defer exportDataFile.Close()
|
|
390
|
+
|
|
391
|
+
// Retrieve the SwingStore "ExportData" from the verified vstorage data.
|
|
392
|
+
// At this point the content of the cosmos DB has been verified against the
|
|
393
|
+
// AppHash, which means the SwingStore data it contains can be used as the
|
|
394
|
+
// trusted root against which to validate the artifacts.
|
|
395
|
+
swingStoreEntries := snapshotter.exporter.ExportSwingStore(ctx)
|
|
396
|
+
|
|
397
|
+
if len(swingStoreEntries) > 0 {
|
|
398
|
+
encoder := json.NewEncoder(exportDataFile)
|
|
399
|
+
encoder.SetEscapeHTML(false)
|
|
400
|
+
for _, dataEntry := range swingStoreEntries {
|
|
401
|
+
entry := []string{dataEntry.Path, dataEntry.Value}
|
|
402
|
+
err := encoder.Encode(entry)
|
|
403
|
+
if err != nil {
|
|
404
|
+
return err
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
writeExportFile := func(filename string, data []byte) error {
|
|
410
|
+
return os.WriteFile(filepath.Join(exportDir, filename), data, ExportedFilesMode)
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
for {
|
|
414
|
+
payloadBytes, err := payloadReader()
|
|
415
|
+
if err == io.EOF {
|
|
416
|
+
break
|
|
417
|
+
} else if err != nil {
|
|
418
|
+
return err
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
payload := types.ExtensionSnapshotterArtifactPayload{}
|
|
422
|
+
if err = payload.Unmarshal(payloadBytes); err != nil {
|
|
423
|
+
return err
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
switch {
|
|
427
|
+
case payload.Name != UntrustedExportDataArtifactName:
|
|
428
|
+
// Artifact verifiable on import from the export data
|
|
429
|
+
// Since we cannot trust the state-sync payload at this point, we generate
|
|
430
|
+
// a safe and unique filename from the artifact name we received, by
|
|
431
|
+
// substituting any non letters-digits-hyphen-underscore-dot by a hyphen,
|
|
432
|
+
// and prefixing with an incremented id.
|
|
433
|
+
// The filename is not used for any purpose in the snapshotting logic.
|
|
434
|
+
filename := sanitizeArtifactName(payload.Name)
|
|
435
|
+
filename = fmt.Sprintf("%d-%s", len(manifest.Artifacts), filename)
|
|
436
|
+
manifest.Artifacts = append(manifest.Artifacts, [2]string{payload.Name, filename})
|
|
437
|
+
err = writeExportFile(filename, payload.Data)
|
|
438
|
+
|
|
439
|
+
case len(swingStoreEntries) > 0:
|
|
440
|
+
// Pseudo artifact containing untrusted export data which may have been
|
|
441
|
+
// saved separately for debugging purposes (not referenced from the manifest)
|
|
442
|
+
err = writeExportFile(UntrustedExportDataFilename, payload.Data)
|
|
443
|
+
|
|
444
|
+
default:
|
|
445
|
+
// There is no trusted export data
|
|
446
|
+
err = errors.New("cannot restore from untrusted export data")
|
|
447
|
+
// snapshotter.logger.Info("using untrusted export data for swingstore restore")
|
|
448
|
+
// _, err = exportDataFile.Write(payload.Data)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if err != nil {
|
|
452
|
+
return err
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
err = exportDataFile.Sync()
|
|
457
|
+
if err != nil {
|
|
458
|
+
return err
|
|
459
|
+
}
|
|
460
|
+
exportDataFile.Close()
|
|
461
|
+
|
|
462
|
+
manifestBytes, err := json.MarshalIndent(manifest, "", " ")
|
|
463
|
+
if err != nil {
|
|
464
|
+
return err
|
|
465
|
+
}
|
|
466
|
+
err = writeExportFile(ExportManifestFilename, manifestBytes)
|
|
467
|
+
if err != nil {
|
|
468
|
+
return err
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
encodedExportDir, err := json.Marshal(exportDir)
|
|
472
|
+
if err != nil {
|
|
473
|
+
return err
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
action := &snapshotAction{
|
|
477
|
+
Type: "COSMOS_SNAPSHOT",
|
|
478
|
+
BlockHeight: int64(height),
|
|
479
|
+
Request: "restore",
|
|
480
|
+
Args: []json.RawMessage{encodedExportDir},
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
_, err = snapshotter.blockingSend(action)
|
|
484
|
+
if err != nil {
|
|
485
|
+
return err
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
snapshotter.logger.Info("restored snapshot", "exportDir", exportDir, "height", height)
|
|
489
|
+
|
|
490
|
+
return nil
|
|
491
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
package keeper
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"errors"
|
|
5
|
+
"testing"
|
|
6
|
+
|
|
7
|
+
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
|
|
8
|
+
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
9
|
+
"github.com/tendermint/tendermint/libs/log"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
func newTestSnapshotter() SwingsetSnapshotter {
|
|
13
|
+
logger := log.NewNopLogger() // log.NewTMLogger(log.NewSyncWriter( /* os.Stdout*/ io.Discard)).With("module", "sdk/app")
|
|
14
|
+
return SwingsetSnapshotter{
|
|
15
|
+
isConfigured: func() bool { return true },
|
|
16
|
+
takeSnapshot: func(height int64) {},
|
|
17
|
+
newRestoreContext: func(height int64) sdk.Context { return sdk.Context{} },
|
|
18
|
+
logger: logger,
|
|
19
|
+
blockingSend: func(action vm.Jsonable) (string, error) { return "", nil },
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
func TestSnapshotInProgress(t *testing.T) {
|
|
24
|
+
swingsetSnapshotter := newTestSnapshotter()
|
|
25
|
+
ch := make(chan struct{})
|
|
26
|
+
swingsetSnapshotter.takeSnapshot = func(height int64) {
|
|
27
|
+
<-ch
|
|
28
|
+
}
|
|
29
|
+
err := swingsetSnapshotter.InitiateSnapshot(123)
|
|
30
|
+
if err != nil {
|
|
31
|
+
t.Fatal(err)
|
|
32
|
+
}
|
|
33
|
+
err = swingsetSnapshotter.WaitUntilSnapshotStarted()
|
|
34
|
+
if err != nil {
|
|
35
|
+
t.Fatal(err)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
err = swingsetSnapshotter.InitiateSnapshot(456)
|
|
39
|
+
if err == nil {
|
|
40
|
+
t.Error("wanted error for snapshot in progress")
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
close(ch)
|
|
44
|
+
<-swingsetSnapshotter.activeSnapshot.done
|
|
45
|
+
err = swingsetSnapshotter.InitiateSnapshot(456)
|
|
46
|
+
if err != nil {
|
|
47
|
+
t.Fatal(err)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
func TestNotConfigured(t *testing.T) {
|
|
52
|
+
swingsetSnapshotter := newTestSnapshotter()
|
|
53
|
+
swingsetSnapshotter.isConfigured = func() bool { return false }
|
|
54
|
+
err := swingsetSnapshotter.InitiateSnapshot(123)
|
|
55
|
+
if err == nil {
|
|
56
|
+
t.Error("wanted error for unconfigured snapshot manager")
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
func TestSecondCommit(t *testing.T) {
|
|
61
|
+
swingsetSnapshotter := newTestSnapshotter()
|
|
62
|
+
|
|
63
|
+
// Use a channel to block the snapshot goroutine after it has started but before it exits.
|
|
64
|
+
ch := make(chan struct{})
|
|
65
|
+
swingsetSnapshotter.takeSnapshot = func(height int64) {
|
|
66
|
+
<-ch
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// First run through app.Commit()
|
|
70
|
+
err := swingsetSnapshotter.WaitUntilSnapshotStarted()
|
|
71
|
+
if err != nil {
|
|
72
|
+
t.Fatal(err)
|
|
73
|
+
}
|
|
74
|
+
err = swingsetSnapshotter.InitiateSnapshot(123)
|
|
75
|
+
if err != nil {
|
|
76
|
+
t.Fatal(err)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Second run through app.Commit() - should return right away
|
|
80
|
+
err = swingsetSnapshotter.WaitUntilSnapshotStarted()
|
|
81
|
+
if err != nil {
|
|
82
|
+
t.Fatal(err)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// close the signaling channel to let goroutine exit
|
|
86
|
+
close(ch)
|
|
87
|
+
<-swingsetSnapshotter.activeSnapshot.done
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
func TestInitiateFails(t *testing.T) {
|
|
91
|
+
swingsetSnapshotter := newTestSnapshotter()
|
|
92
|
+
swingsetSnapshotter.blockingSend = func(action vm.Jsonable) (string, error) {
|
|
93
|
+
if action.(*snapshotAction).Request == "initiate" {
|
|
94
|
+
return "", errors.New("initiate failed")
|
|
95
|
+
}
|
|
96
|
+
return "", nil
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
err := swingsetSnapshotter.InitiateSnapshot(123)
|
|
100
|
+
if err != nil {
|
|
101
|
+
t.Fatal(err)
|
|
102
|
+
}
|
|
103
|
+
err = swingsetSnapshotter.WaitUntilSnapshotStarted()
|
|
104
|
+
if err == nil {
|
|
105
|
+
t.Fatal("wanted initiation error")
|
|
106
|
+
}
|
|
107
|
+
if err.Error() != "initiate failed" {
|
|
108
|
+
t.Errorf(`wanted error "initiate failed", got "%s"`, err.Error())
|
|
109
|
+
}
|
|
110
|
+
// another wait should succeed without error
|
|
111
|
+
err = swingsetSnapshotter.WaitUntilSnapshotStarted()
|
|
112
|
+
if err != nil {
|
|
113
|
+
t.Error(err)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
func TestRetrievalFails(t *testing.T) {
|
|
118
|
+
swingsetSnapshotter := newTestSnapshotter()
|
|
119
|
+
swingsetSnapshotter.blockingSend = func(action vm.Jsonable) (string, error) {
|
|
120
|
+
if action.(*snapshotAction).Request == "retrieve" {
|
|
121
|
+
return "", errors.New("retrieve failed")
|
|
122
|
+
}
|
|
123
|
+
return "", nil
|
|
124
|
+
}
|
|
125
|
+
nilWriter := func(_ []byte) error { return nil }
|
|
126
|
+
var savedErr error
|
|
127
|
+
ch := make(chan struct{})
|
|
128
|
+
swingsetSnapshotter.takeSnapshot = func(height int64) {
|
|
129
|
+
// shortcut to the snapshot manager calling the extension
|
|
130
|
+
savedErr = swingsetSnapshotter.SnapshotExtension(uint64(height), nilWriter)
|
|
131
|
+
close(ch)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
err := swingsetSnapshotter.InitiateSnapshot(123)
|
|
135
|
+
if err != nil {
|
|
136
|
+
t.Fatal(err)
|
|
137
|
+
}
|
|
138
|
+
err = swingsetSnapshotter.WaitUntilSnapshotStarted()
|
|
139
|
+
if err != nil {
|
|
140
|
+
t.Fatal(err)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
<-ch
|
|
144
|
+
if savedErr == nil {
|
|
145
|
+
t.Fatal("wanted retrieval error")
|
|
146
|
+
}
|
|
147
|
+
if savedErr.Error() != "retrieve failed" {
|
|
148
|
+
t.Errorf(`wanted error "retrieve failed", got "%s"`, savedErr.Error())
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
func TestDiscard(t *testing.T) {
|
|
153
|
+
discardCalled := false
|
|
154
|
+
swingsetSnapshotter := newTestSnapshotter()
|
|
155
|
+
swingsetSnapshotter.blockingSend = func(action vm.Jsonable) (string, error) {
|
|
156
|
+
if action.(*snapshotAction).Request == "discard" {
|
|
157
|
+
discardCalled = true
|
|
158
|
+
}
|
|
159
|
+
return "", nil
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// simulate a normal Snapshot() call which calls SnapshotExtension()
|
|
163
|
+
swingsetSnapshotter.takeSnapshot = func(height int64) {
|
|
164
|
+
swingsetSnapshotter.activeSnapshot.retrieved = true
|
|
165
|
+
}
|
|
166
|
+
err := swingsetSnapshotter.InitiateSnapshot(123)
|
|
167
|
+
if err != nil {
|
|
168
|
+
t.Fatal(err)
|
|
169
|
+
}
|
|
170
|
+
<-swingsetSnapshotter.activeSnapshot.done
|
|
171
|
+
if discardCalled {
|
|
172
|
+
t.Error("didn't want discard called")
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// simulate a Snapshot() call which doesn't call SnapshotExtension()
|
|
176
|
+
swingsetSnapshotter.takeSnapshot = func(height int64) {}
|
|
177
|
+
err = swingsetSnapshotter.InitiateSnapshot(456)
|
|
178
|
+
if err != nil {
|
|
179
|
+
t.Fatal(err)
|
|
180
|
+
}
|
|
181
|
+
<-swingsetSnapshotter.activeSnapshot.done
|
|
182
|
+
if !discardCalled {
|
|
183
|
+
t.Error("wanted discard called")
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
package v32
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
|
|
6
|
+
"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
// UpdateParams performs the parameter updates to migrate to version 2,
|
|
10
|
+
// returning the updated params or an error.
|
|
11
|
+
func UpdateParams(params types.Params) (types.Params, error) {
|
|
12
|
+
newBpu, err := addStorageBeanCost(params.BeansPerUnit)
|
|
13
|
+
if err != nil {
|
|
14
|
+
return params, err
|
|
15
|
+
}
|
|
16
|
+
params.BeansPerUnit = newBpu
|
|
17
|
+
return params, nil
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// addStorageBeanCost adds the default beans per storage byte cost,
|
|
21
|
+
// if it's not in the list of bean costs already, returning the possibly-updated list.
|
|
22
|
+
// or an error if there's no default storage cost.
|
|
23
|
+
func addStorageBeanCost(bpu []types.StringBeans) ([]types.StringBeans, error) {
|
|
24
|
+
for _, ob := range bpu {
|
|
25
|
+
if ob.Key == types.BeansPerStorageByte {
|
|
26
|
+
// success if there's already an entry
|
|
27
|
+
return bpu, nil
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
defaultParams := types.DefaultParams()
|
|
31
|
+
for _, b := range defaultParams.BeansPerUnit {
|
|
32
|
+
if b.Key == types.BeansPerStorageByte {
|
|
33
|
+
return append(bpu, b), nil
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return bpu, fmt.Errorf("no beans per storage byte in default params %v", defaultParams)
|
|
37
|
+
}
|