Skip to content

Commit

Permalink
Merge pull request #9 from sei-protocol/tony-chen-mutex
Browse files Browse the repository at this point in the history
Add mutex around WasmVM write calls
  • Loading branch information
codchen authored Feb 6, 2023
2 parents 5212ee8 + 5a70b3e commit 0abacf0
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func NewKeeper(
keeper := &Keeper{
storeKey: storeKey,
cdc: cdc,
wasmVM: wasmer,
wasmVM: NewVMWrapper(wasmer),
accountKeeper: accountKeeper,
bank: NewBankCoinTransferrer(bankKeeper),
portKeeper: portKeeper,
Expand Down
122 changes: 122 additions & 0 deletions x/wasm/keeper/vm_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package keeper

import (
"sync"

"github.com/CosmWasm/wasmd/x/wasm/types"
wasmvm "github.com/CosmWasm/wasmvm"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
)

type VMWrapper struct {
types.WasmerEngine

mu *sync.Mutex
}

func NewVMWrapper(inner types.WasmerEngine) types.WasmerEngine {
return &VMWrapper{
inner,
&sync.Mutex{},
}
}

func (w *VMWrapper) Create(code wasmvm.WasmCode) (wasmvm.Checksum, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.WasmerEngine.Create(code)
}

func (w *VMWrapper) Instantiate(
checksum wasmvm.Checksum,
env wasmvmtypes.Env,
info wasmvmtypes.MessageInfo,
initMsg []byte,
store wasmvm.KVStore,
goapi wasmvm.GoAPI,
querier wasmvm.Querier,
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
) (*wasmvmtypes.Response, uint64, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.WasmerEngine.Instantiate(checksum, env, info, initMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
}

func (w *VMWrapper) Execute(
code wasmvm.Checksum,
env wasmvmtypes.Env,
info wasmvmtypes.MessageInfo,
executeMsg []byte,
store wasmvm.KVStore,
goapi wasmvm.GoAPI,
querier wasmvm.Querier,
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
) (*wasmvmtypes.Response, uint64, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.WasmerEngine.Execute(code, env, info, executeMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
}

func (w *VMWrapper) Migrate(
checksum wasmvm.Checksum,
env wasmvmtypes.Env,
migrateMsg []byte,
store wasmvm.KVStore,
goapi wasmvm.GoAPI,
querier wasmvm.Querier,
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
) (*wasmvmtypes.Response, uint64, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.WasmerEngine.Migrate(checksum, env, migrateMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
}

func (w *VMWrapper) Sudo(
checksum wasmvm.Checksum,
env wasmvmtypes.Env,
sudoMsg []byte,
store wasmvm.KVStore,
goapi wasmvm.GoAPI,
querier wasmvm.Querier,
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
) (*wasmvmtypes.Response, uint64, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.WasmerEngine.Sudo(checksum, env, sudoMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
}

func (w *VMWrapper) Reply(
checksum wasmvm.Checksum,
env wasmvmtypes.Env,
reply wasmvmtypes.Reply,
store wasmvm.KVStore,
goapi wasmvm.GoAPI,
querier wasmvm.Querier,
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
) (*wasmvmtypes.Response, uint64, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.WasmerEngine.Reply(checksum, env, reply, store, goapi, querier, gasMeter, gasLimit, deserCost)
}

func (w *VMWrapper) Unpin(checksum wasmvm.Checksum) error {
w.mu.Lock()
defer w.mu.Unlock()
return w.WasmerEngine.Unpin(checksum)
}

func (w *VMWrapper) Pin(checksum wasmvm.Checksum) error {
w.mu.Lock()
defer w.mu.Unlock()
return w.WasmerEngine.Pin(checksum)
}

0 comments on commit 0abacf0

Please sign in to comment.