github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/keeper/snapshotter.go (about) 1 package keeper 2 3 //TODO unsupport state sync snapshot 4 //import ( 5 // "encoding/hex" 6 // "io" 7 // 8 // protoio "github.com/gogo/protobuf/io" 9 // snapshot "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/snapshots/types" 10 // sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 11 // sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors" 12 // "github.com/tendermint/tendermint/libs/log" 13 // tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 14 // 15 // "github.com/fibonacci-chain/fbc/x/wasm/ioutils" 16 // "github.com/fibonacci-chain/fbc/x/wasm/types" 17 //) 18 // 19 //var _ snapshot.ExtensionSnapshotter = &WasmSnapshotter{} 20 // 21 //// SnapshotFormat format 1 is just gzipped wasm byte code for each item payload. No protobuf envelope, no metadata. 22 //const SnapshotFormat = 1 23 // 24 //type WasmSnapshotter struct { 25 // wasm *Keeper 26 // cms sdk.MultiStore 27 //} 28 // 29 //func NewWasmSnapshotter(cms sdk.MultiStore, wasm *Keeper) *WasmSnapshotter { 30 // return &WasmSnapshotter{ 31 // wasm: wasm, 32 // cms: cms, 33 // } 34 //} 35 // 36 //func (ws *WasmSnapshotter) SnapshotName() string { 37 // return types.ModuleName 38 //} 39 // 40 //func (ws *WasmSnapshotter) SnapshotFormat() uint32 { 41 // return SnapshotFormat 42 //} 43 // 44 //func (ws *WasmSnapshotter) SupportedFormats() []uint32 { 45 // // If we support older formats, add them here and handle them in Restore 46 // return []uint32{SnapshotFormat} 47 //} 48 // 49 //func (ws *WasmSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) error { 50 // cacheMS, err := ws.cms.CacheMultiStoreWithVersion(int64(height)) 51 // if err != nil { 52 // return err 53 // } 54 // 55 // ctx := sdk.NewContext(cacheMS, tmproto.Header{}, false, log.NewNopLogger()) 56 // seenBefore := make(map[string]bool) 57 // var rerr error 58 // 59 // ws.wasm.IterateCodeInfos(ctx, func(id uint64, info types.CodeInfo) bool { 60 // // Many code ids may point to the same code hash... only sync it once 61 // hexHash := hex.EncodeToString(info.CodeHash) 62 // // if seenBefore, just skip this one and move to the next 63 // if seenBefore[hexHash] { 64 // return false 65 // } 66 // seenBefore[hexHash] = true 67 // 68 // // load code and abort on error 69 // wasmBytes, err := ws.wasm.GetByteCode(ctx, id) 70 // if err != nil { 71 // rerr = err 72 // return true 73 // } 74 // 75 // compressedWasm, err := ioutils.GzipIt(wasmBytes) 76 // if err != nil { 77 // rerr = err 78 // return true 79 // } 80 // 81 // err = snapshot.WriteExtensionItem(protoWriter, compressedWasm) 82 // if err != nil { 83 // rerr = err 84 // return true 85 // } 86 // 87 // return false 88 // }) 89 // 90 // return rerr 91 //} 92 // 93 //func (ws *WasmSnapshotter) Restore( 94 // height uint64, format uint32, protoReader protoio.Reader, 95 //) (snapshot.SnapshotItem, error) { 96 // if format == SnapshotFormat { 97 // return ws.processAllItems(height, protoReader, restoreV1, finalizeV1) 98 // } 99 // return snapshot.SnapshotItem{}, snapshot.ErrUnknownFormat 100 //} 101 // 102 //func restoreV1(ctx sdk.Context, k *Keeper, compressedCode []byte) error { 103 // wasmCode, err := ioutils.Uncompress(compressedCode, uint64(types.MaxWasmSize)) 104 // if err != nil { 105 // return sdkerrors.Wrap(types.ErrCreateFailed, err.Error()) 106 // } 107 // 108 // // FIXME: check which codeIDs the checksum matches?? 109 // _, err = k.wasmVM.Create(wasmCode) 110 // if err != nil { 111 // return sdkerrors.Wrap(types.ErrCreateFailed, err.Error()) 112 // } 113 // return nil 114 //} 115 // 116 //func finalizeV1(ctx sdk.Context, k *Keeper) error { 117 // // FIXME: ensure all codes have been uploaded? 118 // return k.InitializePinnedCodes(ctx) 119 //} 120 // 121 //func (ws *WasmSnapshotter) processAllItems( 122 // height uint64, 123 // protoReader protoio.Reader, 124 // cb func(sdk.Context, *Keeper, []byte) error, 125 // finalize func(sdk.Context, *Keeper) error, 126 //) (snapshot.SnapshotItem, error) { 127 // ctx := sdk.NewContext(ws.cms, tmproto.Header{Height: int64(height)}, false, log.NewNopLogger()) 128 // 129 // // keep the last item here... if we break, it will either be empty (if we hit io.EOF) 130 // // or contain the last item (if we hit payload == nil) 131 // var item snapshot.SnapshotItem 132 // for { 133 // item = snapshot.SnapshotItem{} 134 // err := protoReader.ReadMsg(&item) 135 // if err == io.EOF { 136 // break 137 // } else if err != nil { 138 // return snapshot.SnapshotItem{}, sdkerrors.Wrap(err, "invalid protobuf message") 139 // } 140 // 141 // // if it is not another ExtensionPayload message, then it is not for us. 142 // // we should return it an let the manager handle this one 143 // payload := item.GetExtensionPayload() 144 // if payload == nil { 145 // break 146 // } 147 // 148 // if err := cb(ctx, ws.wasm, payload.Payload); err != nil { 149 // return snapshot.SnapshotItem{}, sdkerrors.Wrap(err, "processing snapshot item") 150 // } 151 // } 152 // 153 // return item, finalize(ctx, ws.wasm) 154 //}