Skip to content

Commit

Permalink
WASM snapshotter: write directly to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
assafmo committed Oct 31, 2022
1 parent 7c9c966 commit 5546d19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"os"
"path"
"path/filepath"

"github.com/scrtlabs/SecretNetwork/app/keepers"
Expand Down Expand Up @@ -379,7 +380,7 @@ func NewSecretNetworkApp(

if manager := app.BaseApp.SnapshotManager(); manager != nil {
err := manager.RegisterExtensions(
compute.NewWasmSnapshotter(app.BaseApp.CommitMultiStore(), app.AppKeepers.ComputeKeeper),
compute.NewWasmSnapshotter(app.BaseApp.CommitMultiStore(), app.AppKeepers.ComputeKeeper, path.Join(homePath, ".compute", "wasm", "wasm")),
)
if err != nil {
panic(fmt.Errorf("failed to register snapshot extension: %s", err))
Expand Down
30 changes: 18 additions & 12 deletions x/compute/internal/keeper/wasm_snapshotter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package keeper

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"

snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -52,14 +54,16 @@ type ExtensionSnapshotter interface {
var _ snapshottypes.ExtensionSnapshotter = (*WasmSnapshotter)(nil)

type WasmSnapshotter struct {
cms storetypes.MultiStore
keeper *Keeper
cms storetypes.MultiStore
keeper *Keeper
wasmDirectory string
}

func NewWasmSnapshotter(cms storetypes.MultiStore, keeper *Keeper) *WasmSnapshotter {
func NewWasmSnapshotter(cms storetypes.MultiStore, keeper *Keeper, wasmDirectory string) *WasmSnapshotter {
return &WasmSnapshotter{
cms: cms,
keeper: keeper,
cms: cms,
keeper: keeper,
wasmDirectory: wasmDirectory,
}
}

Expand Down Expand Up @@ -144,15 +148,17 @@ func (ws *WasmSnapshotter) Restore(
return item, nil
}

wasmCode := payload.Payload
wasmBytes := payload.Payload

fmt.Println("Writing wasm file to disk")
hash := sha256.Sum256(wasmBytes)

// Store the WASM bytes using the existing API
// FIXME: check which codeIDs the checksum matches??
_, err = ws.keeper.wasmer.Create(wasmCode)
wasmFileName := hex.EncodeToString(hash[:])

wasmFilePath := filepath.Join(ws.wasmDirectory, wasmFileName)

err = os.WriteFile(wasmFilePath, wasmBytes, 0o600 /* -rw-rw-r-- */)
if err != nil {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
return snapshottypes.SnapshotItem{}, sdkerrors.Wrapf(err, "failed to write wasm file '%v' to disk", wasmFilePath)
}
}
}

0 comments on commit 5546d19

Please sign in to comment.