-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from sei-protocol/tony-chen-mutex
Add mutex around WasmVM write calls
- Loading branch information
Showing
2 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |