@agoric/cosmos 0.34.2-dev-a29afed.0.a29afed → 0.34.2-dev-cb1866d.0.cb1866d
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/git-revision.txt +1 -1
- package/package.json +2 -2
- package/proto/agoric/swingset/msgs.proto +75 -6
- package/proto/agoric/swingset/query.proto +26 -0
- package/proto/agoric/swingset/swingset.proto +95 -0
- package/x/swingset/abci.go +5 -0
- package/x/swingset/keeper/chunked_artifact_id_test.go +168 -0
- package/x/swingset/keeper/grpc_query.go +24 -0
- package/x/swingset/keeper/keeper.go +210 -2
- package/x/swingset/keeper/msg_server.go +204 -1
- package/x/swingset/keeper/msg_server_test.go +297 -1
- package/x/swingset/types/default-params.go +9 -0
- package/x/swingset/types/list_tools.go +103 -0
- package/x/swingset/types/msgs.go +131 -21
- package/x/swingset/types/msgs.pb.go +876 -198
- package/x/swingset/types/msgs_test.go +11 -21
- package/x/swingset/types/params.go +63 -13
- package/x/swingset/types/params_test.go +56 -26
- package/x/swingset/types/query.pb.go +526 -32
- package/x/swingset/types/query.pb.gw.go +101 -0
- package/x/swingset/types/swingset.pb.go +1288 -123
|
@@ -45,8 +45,11 @@ const (
|
|
|
45
45
|
)
|
|
46
46
|
|
|
47
47
|
const (
|
|
48
|
-
stateKey
|
|
49
|
-
|
|
48
|
+
stateKey = "state"
|
|
49
|
+
pendingChunkDataKeyPrefix = "pendingChunkData."
|
|
50
|
+
pendingBundleInstallKeyPrefix = "pendingBundleInstall."
|
|
51
|
+
pendingNodeKeyPrefix = "pending."
|
|
52
|
+
swingStoreKeyPrefix = "swingStore."
|
|
50
53
|
)
|
|
51
54
|
|
|
52
55
|
// Keeper maintains the link to data vstorage and exposes getter/setter methods for the various parts of the state machine
|
|
@@ -489,6 +492,211 @@ func (k Keeper) SetMailbox(ctx sdk.Context, peer string, mailbox string) {
|
|
|
489
492
|
k.vstorageKeeper.LegacySetStorageAndNotify(ctx, agoric.NewKVEntry(path, mailbox))
|
|
490
493
|
}
|
|
491
494
|
|
|
495
|
+
func (k Keeper) GetPendingChunkData(ctx sdk.Context, chunkedArtifactId uint64, chunkIndex uint64) []byte {
|
|
496
|
+
kvstore := k.storeService.OpenKVStore(ctx)
|
|
497
|
+
store := runtime.KVStoreAdapter(kvstore)
|
|
498
|
+
pendingChunkData := prefix.NewStore(store, []byte(pendingChunkDataKeyPrefix))
|
|
499
|
+
|
|
500
|
+
key := append(sdk.Uint64ToBigEndian(chunkedArtifactId), sdk.Uint64ToBigEndian(chunkIndex)...)
|
|
501
|
+
if !pendingChunkData.Has(key) {
|
|
502
|
+
return nil
|
|
503
|
+
}
|
|
504
|
+
return pendingChunkData.Get(key)
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
func (k Keeper) SetPendingChunkData(ctx sdk.Context, chunkedArtifactId uint64, chunkIndex uint64, data []byte) {
|
|
508
|
+
kvstore := k.storeService.OpenKVStore(ctx)
|
|
509
|
+
store := runtime.KVStoreAdapter(kvstore)
|
|
510
|
+
pendingChunkData := prefix.NewStore(store, []byte(pendingChunkDataKeyPrefix))
|
|
511
|
+
|
|
512
|
+
key := append(sdk.Uint64ToBigEndian(chunkedArtifactId), sdk.Uint64ToBigEndian(chunkIndex)...)
|
|
513
|
+
if len(data) == 0 {
|
|
514
|
+
pendingChunkData.Delete(key)
|
|
515
|
+
return
|
|
516
|
+
}
|
|
517
|
+
pendingChunkData.Set(key, data)
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
func (k Keeper) GetPendingBundleInstall(ctx sdk.Context, chunkedArtifactId uint64) *types.MsgInstallBundle {
|
|
521
|
+
kvstore := k.storeService.OpenKVStore(ctx)
|
|
522
|
+
store := runtime.KVStoreAdapter(kvstore)
|
|
523
|
+
pendingStore := prefix.NewStore(store, []byte(pendingBundleInstallKeyPrefix))
|
|
524
|
+
key := sdk.Uint64ToBigEndian(chunkedArtifactId)
|
|
525
|
+
if !pendingStore.Has(key) {
|
|
526
|
+
return nil
|
|
527
|
+
}
|
|
528
|
+
bz := pendingStore.Get(key)
|
|
529
|
+
msg := &types.MsgInstallBundle{}
|
|
530
|
+
k.cdc.MustUnmarshal(bz, msg)
|
|
531
|
+
return msg
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
func (k Keeper) AddPendingBundleInstall(ctx sdk.Context, msg *types.MsgInstallBundle) (uint64, error) {
|
|
535
|
+
state := k.GetState(ctx)
|
|
536
|
+
if state.NextChunkedArtifactId < state.LastChunkedArtifactId {
|
|
537
|
+
// Handle legacy state that predates the monotonic counter.
|
|
538
|
+
state.NextChunkedArtifactId = state.LastChunkedArtifactId
|
|
539
|
+
}
|
|
540
|
+
state.NextChunkedArtifactId++
|
|
541
|
+
chunkedArtifactId := state.NextChunkedArtifactId
|
|
542
|
+
|
|
543
|
+
// Create and store the pending install node. The list is non-circular;
|
|
544
|
+
// PrevId/NextId use 0 to indicate the start/end, and State tracks endpoints
|
|
545
|
+
// separately from the monotonically increasing allocation counter.
|
|
546
|
+
node := &types.ChunkedArtifactNode{
|
|
547
|
+
ChunkedArtifactId: chunkedArtifactId,
|
|
548
|
+
StartTimeUnix: ctx.BlockTime().Unix(),
|
|
549
|
+
StartBlockHeight: ctx.BlockHeight(),
|
|
550
|
+
}
|
|
551
|
+
kvstore := k.storeService.OpenKVStore(ctx)
|
|
552
|
+
store := runtime.KVStoreAdapter(kvstore)
|
|
553
|
+
startStore := prefix.NewStore(store, []byte(pendingNodeKeyPrefix))
|
|
554
|
+
if state.FirstChunkedArtifactId == 0 {
|
|
555
|
+
if state.LastChunkedArtifactId != 0 {
|
|
556
|
+
return 0, fmt.Errorf("inconsistent chunked artifact list: first=0 last=%d", state.LastChunkedArtifactId)
|
|
557
|
+
}
|
|
558
|
+
state.FirstChunkedArtifactId = chunkedArtifactId
|
|
559
|
+
} else {
|
|
560
|
+
prevId := state.LastChunkedArtifactId
|
|
561
|
+
if prevId == 0 {
|
|
562
|
+
return 0, fmt.Errorf("missing last chunked artifact id for non-empty list")
|
|
563
|
+
}
|
|
564
|
+
prevKey := sdk.Uint64ToBigEndian(prevId)
|
|
565
|
+
if !startStore.Has(prevKey) {
|
|
566
|
+
return 0, fmt.Errorf("missing chunked artifact node id=%d during add", prevId)
|
|
567
|
+
}
|
|
568
|
+
prevNode := &types.ChunkedArtifactNode{}
|
|
569
|
+
k.cdc.MustUnmarshal(startStore.Get(prevKey), prevNode)
|
|
570
|
+
prevNode.NextId = chunkedArtifactId
|
|
571
|
+
startStore.Set(prevKey, k.cdc.MustMarshal(prevNode))
|
|
572
|
+
node.PrevId = prevId
|
|
573
|
+
}
|
|
574
|
+
state.LastChunkedArtifactId = chunkedArtifactId
|
|
575
|
+
k.SetState(ctx, state)
|
|
576
|
+
if err := k.SetPendingBundleInstall(ctx, chunkedArtifactId, msg); err != nil {
|
|
577
|
+
return 0, err
|
|
578
|
+
}
|
|
579
|
+
key := sdk.Uint64ToBigEndian(chunkedArtifactId)
|
|
580
|
+
startStore.Set(key, k.cdc.MustMarshal(node))
|
|
581
|
+
|
|
582
|
+
return chunkedArtifactId, nil
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// PruneExpiredBundleInstalls removes pending bundle installs that have passed
|
|
586
|
+
// their deadline, as set by the keeper parameters.
|
|
587
|
+
func (k Keeper) PruneExpiredBundleInstalls(ctx sdk.Context) error {
|
|
588
|
+
params := k.GetParams(ctx)
|
|
589
|
+
currentSeconds := ctx.BlockTime().Unix()
|
|
590
|
+
currentBlocks := ctx.BlockHeight()
|
|
591
|
+
deadlineSeconds := params.InstallationDeadlineSeconds
|
|
592
|
+
deadlineBlocks := params.InstallationDeadlineBlocks
|
|
593
|
+
|
|
594
|
+
kvstore := k.storeService.OpenKVStore(ctx)
|
|
595
|
+
store := runtime.KVStoreAdapter(kvstore)
|
|
596
|
+
startStore := prefix.NewStore(store, []byte(pendingNodeKeyPrefix))
|
|
597
|
+
|
|
598
|
+
state := k.GetState(ctx)
|
|
599
|
+
for state.FirstChunkedArtifactId != 0 {
|
|
600
|
+
chunkedArtifactId := state.FirstChunkedArtifactId
|
|
601
|
+
key := sdk.Uint64ToBigEndian(chunkedArtifactId)
|
|
602
|
+
bz := startStore.Get(key)
|
|
603
|
+
node := &types.ChunkedArtifactNode{}
|
|
604
|
+
k.cdc.MustUnmarshal(bz, node)
|
|
605
|
+
|
|
606
|
+
if deadlineSeconds < 0 || currentSeconds-node.StartTimeUnix < deadlineSeconds {
|
|
607
|
+
if deadlineBlocks < 0 || currentBlocks-node.StartBlockHeight < deadlineBlocks {
|
|
608
|
+
// Still alive. Stop the search.
|
|
609
|
+
break
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// This pending bundle install is dead. Remove it.
|
|
614
|
+
if err := k.SetPendingBundleInstall(ctx, chunkedArtifactId, nil); err != nil {
|
|
615
|
+
return err
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// Advance to the next node.
|
|
619
|
+
state.FirstChunkedArtifactId = node.NextId
|
|
620
|
+
if node.NextId == 0 {
|
|
621
|
+
state.LastChunkedArtifactId = 0
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
k.SetState(ctx, state)
|
|
626
|
+
return nil
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
func (k Keeper) makeListTools(ctx sdk.Context) *types.ListTools {
|
|
630
|
+
kvstore := k.storeService.OpenKVStore(ctx)
|
|
631
|
+
store := runtime.KVStoreAdapter(kvstore)
|
|
632
|
+
listStore := prefix.NewStore(store, []byte(pendingNodeKeyPrefix))
|
|
633
|
+
return types.NewListTools(ctx, listStore, k.cdc)
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
func (k Keeper) SetPendingBundleInstall(ctx sdk.Context, chunkedArtifactId uint64, newMsg *types.MsgInstallBundle) error {
|
|
637
|
+
kvstore := k.storeService.OpenKVStore(ctx)
|
|
638
|
+
store := runtime.KVStoreAdapter(kvstore)
|
|
639
|
+
pendingStore := prefix.NewStore(store, []byte(pendingBundleInstallKeyPrefix))
|
|
640
|
+
|
|
641
|
+
key := sdk.Uint64ToBigEndian(chunkedArtifactId)
|
|
642
|
+
if newMsg != nil {
|
|
643
|
+
bz := k.cdc.MustMarshal(newMsg)
|
|
644
|
+
pendingStore.Set(key, bz)
|
|
645
|
+
return nil
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
var msg types.MsgInstallBundle
|
|
649
|
+
k.cdc.MustUnmarshal(pendingStore.Get(key), &msg)
|
|
650
|
+
if msg.ChunkedArtifact != nil && len(msg.ChunkedArtifact.Chunks) > 0 {
|
|
651
|
+
// Remove the chunks.
|
|
652
|
+
for i := range msg.ChunkedArtifact.Chunks {
|
|
653
|
+
k.SetPendingChunkData(ctx, chunkedArtifactId, uint64(i), nil)
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
pendingStore.Delete(key)
|
|
657
|
+
|
|
658
|
+
// Remove auxilliary data structure entries.
|
|
659
|
+
if err := k.RemoveChunkedArtifactNode(ctx, chunkedArtifactId); err != nil {
|
|
660
|
+
return err
|
|
661
|
+
}
|
|
662
|
+
return nil
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// RemoveChunkedArtifactNode removes this pending install from the keeper's
|
|
666
|
+
// ordered linked list structures, and deletes it from the store.
|
|
667
|
+
func (k Keeper) RemoveChunkedArtifactNode(ctx sdk.Context, chunkedArtifactId uint64) error {
|
|
668
|
+
lt := k.makeListTools(ctx)
|
|
669
|
+
|
|
670
|
+
victimKey := lt.Key(chunkedArtifactId)
|
|
671
|
+
victimNode := lt.Fetch(victimKey)
|
|
672
|
+
if victimNode == nil {
|
|
673
|
+
return fmt.Errorf("missing chunked artifact node id=%d during unlink", chunkedArtifactId)
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
// Remove the victim from the linked list, keeping the structure intact.
|
|
677
|
+
if err := lt.Unlink(victimNode, func(firstp, lastp *uint64) {
|
|
678
|
+
state := k.GetState(ctx)
|
|
679
|
+
if firstp != nil {
|
|
680
|
+
state.FirstChunkedArtifactId = *firstp
|
|
681
|
+
}
|
|
682
|
+
if lastp != nil {
|
|
683
|
+
state.LastChunkedArtifactId = *lastp
|
|
684
|
+
}
|
|
685
|
+
k.SetState(ctx, state)
|
|
686
|
+
}); err != nil {
|
|
687
|
+
return err
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// Finally, delete the victim node's storage.
|
|
691
|
+
lt.Delete(victimKey)
|
|
692
|
+
return nil
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
func (k Keeper) GetChunkedArtifactNode(ctx sdk.Context, chunkedArtifactId uint64) *types.ChunkedArtifactNode {
|
|
696
|
+
lt := k.makeListTools(ctx)
|
|
697
|
+
return lt.Fetch(lt.Key(chunkedArtifactId))
|
|
698
|
+
}
|
|
699
|
+
|
|
492
700
|
func (k Keeper) GetSwingStore(ctx sdk.Context) storetypes.KVStore {
|
|
493
701
|
kvstore := k.storeService.OpenKVStore(ctx)
|
|
494
702
|
store := runtime.KVStoreAdapter(kvstore)
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
package keeper
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
+
"bytes"
|
|
4
5
|
"context"
|
|
6
|
+
"crypto/sha512"
|
|
7
|
+
"encoding/hex"
|
|
8
|
+
"fmt"
|
|
5
9
|
|
|
6
10
|
sdkioerrors "cosmossdk.io/errors"
|
|
7
11
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
@@ -203,12 +207,95 @@ type installBundleAction struct {
|
|
|
203
207
|
*types.MsgInstallBundle
|
|
204
208
|
}
|
|
205
209
|
|
|
206
|
-
func (keeper msgServer)
|
|
210
|
+
func (keeper msgServer) validateMsgInstallBundle(goCtx context.Context, msg *types.MsgInstallBundle) error {
|
|
211
|
+
if err := msg.ValidateBasic(); err != nil {
|
|
212
|
+
return err
|
|
213
|
+
}
|
|
214
|
+
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
215
|
+
params := keeper.GetParams(ctx)
|
|
216
|
+
if msg.UncompressedSize >= params.BundleUncompressedSizeLimitBytes {
|
|
217
|
+
return sdkioerrors.Wrap(sdkerrors.ErrUnknownRequest, "Uncompressed size out of range")
|
|
218
|
+
}
|
|
219
|
+
return keeper.validateChunkedArtifact(goCtx, msg.ChunkedArtifact)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
func (keeper msgServer) validateChunkedArtifact(goCtx context.Context, msg *types.ChunkedArtifact) error {
|
|
223
|
+
if msg == nil {
|
|
224
|
+
return nil
|
|
225
|
+
}
|
|
226
|
+
if err := msg.ValidateBasic(); err != nil {
|
|
227
|
+
return err
|
|
228
|
+
}
|
|
207
229
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
230
|
+
params := keeper.GetParams(ctx)
|
|
231
|
+
if msg.SizeBytes == 0 || int64(msg.SizeBytes) >= params.BundleUncompressedSizeLimitBytes {
|
|
232
|
+
return sdkioerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Bundle size out of range")
|
|
233
|
+
}
|
|
234
|
+
chunkIndexLimit := types.MaxArtifactChunksCount(params.BundleUncompressedSizeLimitBytes, params.ChunkSizeLimitBytes)
|
|
235
|
+
if int64(len(msg.Chunks)) > chunkIndexLimit {
|
|
236
|
+
return sdkioerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Number of bundle chunks must be less than %d", chunkIndexLimit)
|
|
237
|
+
}
|
|
238
|
+
for i, chunk := range msg.Chunks {
|
|
239
|
+
if chunk.SizeBytes == 0 || int64(chunk.SizeBytes) > params.ChunkSizeLimitBytes {
|
|
240
|
+
return sdkioerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Chunk %d size out of range", i)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return nil
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
func (keeper msgServer) validateMsgSendChunk(goCtx context.Context, msg *types.MsgSendChunk) error {
|
|
208
247
|
if err := msg.ValidateBasic(); err != nil {
|
|
248
|
+
return err
|
|
249
|
+
}
|
|
250
|
+
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
251
|
+
params := keeper.GetParams(ctx)
|
|
252
|
+
if int64(len(msg.ChunkData)) > params.ChunkSizeLimitBytes {
|
|
253
|
+
return sdkioerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Chunk size must be at most %d bytes", params.ChunkSizeLimitBytes)
|
|
254
|
+
}
|
|
255
|
+
chunkIndexLimit := types.MaxArtifactChunksCount(params.BundleUncompressedSizeLimitBytes, params.ChunkSizeLimitBytes)
|
|
256
|
+
if int64(msg.ChunkIndex) >= chunkIndexLimit {
|
|
257
|
+
return sdkioerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Chunk index must be less than %d", chunkIndexLimit)
|
|
258
|
+
}
|
|
259
|
+
return nil
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
func (keeper msgServer) InstallBundle(goCtx context.Context, msg *types.MsgInstallBundle) (*types.MsgInstallBundleResponse, error) {
|
|
263
|
+
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
264
|
+
if err := keeper.validateMsgInstallBundle(goCtx, msg); err != nil {
|
|
209
265
|
return nil, sdkioerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
|
|
210
266
|
}
|
|
211
267
|
|
|
268
|
+
if msg.ChunkedArtifact == nil || len(msg.ChunkedArtifact.Chunks) == 0 {
|
|
269
|
+
return keeper.InstallFinishedBundle(goCtx, msg)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Mark all the chunks as in-flight.
|
|
273
|
+
ca := *msg.ChunkedArtifact
|
|
274
|
+
chunks := make([]*types.ChunkInfo, len(ca.Chunks))
|
|
275
|
+
for i, chunk := range ca.Chunks {
|
|
276
|
+
if chunk == nil {
|
|
277
|
+
return nil, fmt.Errorf("chunk %d is nil", i)
|
|
278
|
+
}
|
|
279
|
+
ci := *chunk
|
|
280
|
+
if ci.State != types.ChunkState_CHUNK_STATE_UNSPECIFIED {
|
|
281
|
+
return nil, fmt.Errorf("chunk %d state must be unspecified", i)
|
|
282
|
+
}
|
|
283
|
+
ci.State = types.ChunkState_CHUNK_STATE_IN_FLIGHT
|
|
284
|
+
chunks[i] = &ci
|
|
285
|
+
}
|
|
286
|
+
ca.Chunks = chunks
|
|
287
|
+
msg.ChunkedArtifact = &ca
|
|
288
|
+
|
|
289
|
+
chunkedArtifactId, err := keeper.AddPendingBundleInstall(ctx, msg)
|
|
290
|
+
if err != nil {
|
|
291
|
+
return nil, err
|
|
292
|
+
}
|
|
293
|
+
return &types.MsgInstallBundleResponse{ChunkedArtifactId: chunkedArtifactId}, nil
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
func (keeper msgServer) InstallFinishedBundle(goCtx context.Context, msg *types.MsgInstallBundle) (*types.MsgInstallBundleResponse, error) {
|
|
297
|
+
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
298
|
+
|
|
212
299
|
if err := msg.Uncompress(); err != nil {
|
|
213
300
|
return nil, err
|
|
214
301
|
}
|
|
@@ -254,3 +341,119 @@ func (k msgServer) CoreEval(goCtx context.Context, msg *types.MsgCoreEval) (*typ
|
|
|
254
341
|
|
|
255
342
|
return &types.MsgCoreEvalResponse{}, nil
|
|
256
343
|
}
|
|
344
|
+
|
|
345
|
+
func (keeper msgServer) SendChunk(goCtx context.Context, msg *types.MsgSendChunk) (*types.MsgSendChunkResponse, error) {
|
|
346
|
+
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
347
|
+
|
|
348
|
+
if err := keeper.validateMsgSendChunk(goCtx, msg); err != nil {
|
|
349
|
+
return nil, err
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
inst := keeper.GetPendingBundleInstall(ctx, msg.ChunkedArtifactId)
|
|
353
|
+
if inst == nil {
|
|
354
|
+
return nil, fmt.Errorf("no upload in progress for chunked artifact identifier %d", msg.ChunkedArtifactId)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
ca := inst.ChunkedArtifact
|
|
358
|
+
|
|
359
|
+
if msg.ChunkIndex >= uint64(len(ca.Chunks)) {
|
|
360
|
+
return nil, fmt.Errorf("chunk index %d out of range for chunked artifact identifier %d", msg.ChunkIndex, msg.ChunkedArtifactId)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if ca.Chunks[msg.ChunkIndex].State != types.ChunkState_CHUNK_STATE_IN_FLIGHT {
|
|
364
|
+
return nil, fmt.Errorf("chunk %d must be in flight for chunked artifact id %d", msg.ChunkIndex, msg.ChunkedArtifactId)
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Verify the chunk data.
|
|
368
|
+
ci := ca.Chunks[msg.ChunkIndex]
|
|
369
|
+
if ci.SizeBytes != uint64(len(msg.ChunkData)) {
|
|
370
|
+
return nil, fmt.Errorf("chunk %d size mismatch for chunked artifact id %d", msg.ChunkIndex, msg.ChunkedArtifactId)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
expectedSha512, err := hex.DecodeString(ci.Sha512)
|
|
374
|
+
if err != nil {
|
|
375
|
+
return nil, fmt.Errorf("chunk %d cannot decode hash %s: %s", msg.ChunkIndex, ci.Sha512, err)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
actualSha512 := sha512.Sum512(msg.ChunkData)
|
|
379
|
+
if len(expectedSha512) != sha512.Size {
|
|
380
|
+
return nil, fmt.Errorf(
|
|
381
|
+
"chunk %d hash length mismatch; expected %d, got %d",
|
|
382
|
+
msg.ChunkIndex,
|
|
383
|
+
sha512.Size,
|
|
384
|
+
len(expectedSha512),
|
|
385
|
+
)
|
|
386
|
+
}
|
|
387
|
+
if !bytes.Equal(actualSha512[:], expectedSha512) {
|
|
388
|
+
return nil, fmt.Errorf("chunk %d hash mismatch; expected %x, got %x", msg.ChunkIndex, expectedSha512, actualSha512)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Data is valid, so store it.
|
|
392
|
+
keeper.SetPendingChunkData(ctx, msg.ChunkedArtifactId, msg.ChunkIndex, msg.ChunkData)
|
|
393
|
+
|
|
394
|
+
// Mark the chunk as received, and store the pending installation.
|
|
395
|
+
ci.State = types.ChunkState_CHUNK_STATE_RECEIVED
|
|
396
|
+
if err := keeper.SetPendingBundleInstall(ctx, msg.ChunkedArtifactId, inst); err != nil {
|
|
397
|
+
return nil, err
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
err = keeper.MaybeFinalizeBundle(ctx, msg.ChunkedArtifactId)
|
|
401
|
+
if err != nil {
|
|
402
|
+
return nil, err
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return &types.MsgSendChunkResponse{
|
|
406
|
+
ChunkedArtifactId: msg.ChunkedArtifactId,
|
|
407
|
+
Chunk: ci,
|
|
408
|
+
}, err
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
func (keeper msgServer) MaybeFinalizeBundle(ctx sdk.Context, chunkedArtifactId uint64) error {
|
|
412
|
+
msg := keeper.GetPendingBundleInstall(ctx, chunkedArtifactId)
|
|
413
|
+
if msg == nil {
|
|
414
|
+
return nil
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// If any chunks are not received, then bail (without error).
|
|
418
|
+
ca := msg.ChunkedArtifact
|
|
419
|
+
var totalSize uint64
|
|
420
|
+
for _, chunk := range ca.Chunks {
|
|
421
|
+
if chunk.State != types.ChunkState_CHUNK_STATE_RECEIVED {
|
|
422
|
+
return nil
|
|
423
|
+
}
|
|
424
|
+
totalSize += chunk.SizeBytes
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
chunkData := make([]byte, 0, totalSize)
|
|
428
|
+
for i := range ca.Chunks {
|
|
429
|
+
bz := keeper.GetPendingChunkData(ctx, chunkedArtifactId, uint64(i))
|
|
430
|
+
chunkData = append(chunkData, bz...)
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Verify the hash of the concatenated chunks.
|
|
434
|
+
actualSha512 := sha512.Sum512(chunkData)
|
|
435
|
+
expectedSha512, err := hex.DecodeString(ca.Sha512)
|
|
436
|
+
if err != nil {
|
|
437
|
+
return fmt.Errorf("cannot decode hash %s: %s", ca.Sha512, err)
|
|
438
|
+
}
|
|
439
|
+
if !bytes.Equal(expectedSha512, actualSha512[:]) {
|
|
440
|
+
return fmt.Errorf("bundle hash mismatch; expected %x, got %x", expectedSha512, actualSha512)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Is it compressed or not?
|
|
444
|
+
if msg.UncompressedSize > 0 {
|
|
445
|
+
msg.CompressedBundle = chunkData
|
|
446
|
+
} else {
|
|
447
|
+
msg.Bundle = string(chunkData)
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// Clean up the pending installation state.
|
|
451
|
+
msg.ChunkedArtifact = nil
|
|
452
|
+
if err := keeper.SetPendingBundleInstall(ctx, chunkedArtifactId, nil); err != nil {
|
|
453
|
+
return err
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Install the bundle now that all the chunks are processed.
|
|
457
|
+
_, err = keeper.InstallFinishedBundle(ctx, msg)
|
|
458
|
+
return err
|
|
459
|
+
}
|