x/wasm
This module supports CosmWasm smart contracts.
Introduction
There are much information around CosmWasmo on the internet, here is a simple summary to provide some very high level overview of what is under the hood.
The overall design is as such:
- wasmvm: This is the Go code that imports cosmwasm-vm via cgo
- cosmwasm-vm is compiled lib (
.so,dylib,.dll,.a) to be linked via cgo - The APIs for this is in
internal/api
- cosmwasm-vm is compiled lib (
- cosmwasm-vm: This is written in rust uses wasmer embedded runtime to run the CosmWasm smart contracts
NewApp
When the node starts, along with creating all module keepers,
the wasmKeeper is also created.
This calls [newVM] which creates cosmwasm VM which has the wasmer engine,
the runtime for the CosmWasm contracts.
This VM is used for the execution / code store / query etc.
Transaction flow
On StoreCode the wasm file is compiled (into executable code).
Both the original wasm and the executable code are stored.
When there is a transaction to execute,
a contractInstance is created by loading the existing contract state / code.
Contract address and other relevant info along with prefixStore for that contract are then passed into the C code compiled from cosmwasm-vm for Execute in wasmvm -> CosmWasm-vm via the cgo import.