Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp: adding 08-wasm build opts with libwasmvm linking disabled #5923

Merged
merged 25 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4dd9512
wip: messing with build options for wasm cgo
damiannolan Mar 4, 2024
96b2e9e
chore: mv type assertion to wasm_cgo with build flags
damiannolan Mar 4, 2024
37f38e6
chore: mv make target to build section
damiannolan Mar 4, 2024
446104f
Merge branch 'main' into damian/5903-wasm-build-no-cgo
damiannolan Mar 4, 2024
de635bb
chore: revert cgo enabled 0 build opt for testing
damiannolan Mar 4, 2024
21832c1
chore: rm unneeded file
damiannolan Mar 5, 2024
893b498
Merge branch 'main' into damian/5903-wasm-build-no-cgo
damiannolan Mar 5, 2024
6f9f2a7
Merge branch 'main' into damian/5903-wasm-build-no-cgo
damiannolan Mar 6, 2024
cbf297f
Merge branch 'main' into damian/5903-wasm-build-no-cgo
charleenfei Mar 13, 2024
441b32a
Merge branch 'main' into damian/5903-wasm-build-no-cgo
charleenfei Mar 14, 2024
d486f1e
update build tag
charleenfei Mar 14, 2024
d3de24e
linter
charleenfei Mar 14, 2024
5c97278
Merge branch 'main' into damian/5903-wasm-build-no-cgo
charleenfei Mar 18, 2024
a130f22
update panic message
charleenfei Mar 18, 2024
6f3015e
add Codec back
charleenfei Mar 18, 2024
aa2a1fb
Merge branch 'main' into damian/5903-wasm-build-no-cgo
damiannolan Mar 20, 2024
ee4ac4d
refactor: adapt build tags to match wasmvm
damiannolan Mar 20, 2024
003fa1b
Update modules/light-clients/08-wasm/doc.go
damiannolan Mar 20, 2024
22dab08
Update modules/light-clients/08-wasm/keeper/keeper_vm.go
damiannolan Mar 20, 2024
64a011a
chore: make lint-fix
damiannolan Mar 20, 2024
fffd115
chore: make lint-fix
damiannolan Mar 20, 2024
6ce9c96
Merge branch 'main' into damian/5903-wasm-build-no-cgo
chatton Mar 25, 2024
ca70123
Merge branch 'main' into damian/5903-wasm-build-no-cgo
damiannolan Apr 2, 2024
96753a3
Merge branch 'main' into damian/5903-wasm-build-no-cgo
damiannolan Apr 2, 2024
1a6878f
Merge branch 'main' into damian/5903-wasm-build-no-cgo
damiannolan Apr 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ clean:

.PHONY: distclean clean

#? build-docker-wasm: Build wasm simapp with specified tag.
build-docker-wasm:
./scripts/build-wasm-simapp-docker.sh $(tag)

.PHONY: build-docker-wasm

###############################################################################
### Tools & Dependencies ###
###############################################################################
Expand Down Expand Up @@ -385,7 +391,6 @@ proto-update-deps:

.PHONY: proto-all proto-gen proto-gen-any proto-swagger-gen proto-format proto-lint proto-check-breaking proto-update-deps


#? help: Get more info on make commands
help: Makefile
@echo " Choose a command run in "$(PROJECT_NAME)":"
Expand Down
7 changes: 7 additions & 0 deletions modules/light-clients/08-wasm/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ ClientMessage and types for the proxy light client module communicating
with underlying Wasm light clients.
This implementation is based off the ICS 08 specification
(https://github.com/cosmos/ibc/blob/main/spec/client/ics-008-wasm-client)

By default the 08-wasm module requires cgo and libwasmvm dependencies available on the system.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➕ 🙏

However, users of this module may want to depend only on types, without incurring the dependency on cgo or libwasmvm.
In this case, it is possible to build the code with either cgo disabled or a custom build directive: nolink_libwasmvm.
This allows disabling linking of libwasmvm and not forcing users to have specific libraries available on their systems.

Please refer to the 08-wasm module documentation for more information.
*/
package wasm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ WasmEngine = (*wasmvm.VM)(nil)

type WasmEngine interface {
// StoreCode will compile the Wasm code, and store the resulting compiled module
// as well as the original code. Both can be referenced later via Checksum.
Expand Down
7 changes: 7 additions & 0 deletions modules/light-clients/08-wasm/internal/ibcwasm/wasm_vm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build cgo && !nolink_libwasmvm

package ibcwasm

import wasmvm "github.com/CosmWasm/wasmvm/v2"

var _ WasmEngine = (*wasmvm.VM)(nil)
72 changes: 0 additions & 72 deletions modules/light-clients/08-wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package keeper
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"strings"

wasmvm "github.com/CosmWasm/wasmvm/v2"

Expand Down Expand Up @@ -34,75 +31,6 @@ type Keeper struct {
authority string
}

// NewKeeperWithVM creates a new Keeper instance with the provided Wasm VM.
// This constructor function is meant to be used when the chain uses x/wasm
// and the same Wasm VM instance should be shared with it.
func NewKeeperWithVM(
cdc codec.BinaryCodec,
storeService store.KVStoreService,
clientKeeper types.ClientKeeper,
authority string,
vm ibcwasm.WasmEngine,
queryRouter ibcwasm.QueryRouter,
opts ...Option,
) Keeper {
if clientKeeper == nil {
panic(errors.New("client keeper must not be nil"))
}

if vm == nil {
panic(errors.New("wasm VM must not be nil"))
}

if storeService == nil {
panic(errors.New("store service must not be nil"))
}

if strings.TrimSpace(authority) == "" {
panic(errors.New("authority must be non-empty"))
}

keeper := &Keeper{
cdc: cdc,
storeService: storeService,
clientKeeper: clientKeeper,
authority: authority,
}

// set query plugins to ensure there is a non-nil query plugin
// regardless of what options the user provides
ibcwasm.SetQueryPlugins(types.NewDefaultQueryPlugins())
for _, opt := range opts {
opt.apply(keeper)
}

ibcwasm.SetVM(vm)
ibcwasm.SetQueryRouter(queryRouter)
ibcwasm.SetupWasmStoreService(storeService)

return *keeper
}

// NewKeeperWithConfig creates a new Keeper instance with the provided Wasm configuration.
// This constructor function is meant to be used when the chain does not use x/wasm
// and a Wasm VM needs to be instantiated using the provided parameters.
func NewKeeperWithConfig(
cdc codec.BinaryCodec,
storeService store.KVStoreService,
clientKeeper types.ClientKeeper,
authority string,
wasmConfig types.WasmConfig,
queryRouter ibcwasm.QueryRouter,
opts ...Option,
) Keeper {
vm, err := wasmvm.NewVM(wasmConfig.DataDir, wasmConfig.SupportedCapabilities, types.ContractMemoryLimit, wasmConfig.ContractDebugMode, types.MemoryCacheSize)
if err != nil {
panic(fmt.Errorf("failed to instantiate new Wasm VM instance: %v", err))
}

return NewKeeperWithVM(cdc, storeService, clientKeeper, authority, vm, queryRouter, opts...)
}

// Codec returns the 08-wasm module's codec.
func (k Keeper) Codec() codec.BinaryCodec {
return k.cdc
Expand Down
44 changes: 44 additions & 0 deletions modules/light-clients/08-wasm/keeper/keeper_no_vm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build !cgo || nolink_libwasmvm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can rename these files to cgo & no_cgo again?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no_vm is more representative of the purpose of these build tags


package keeper

import (
storetypes "cosmossdk.io/core/store"

"github.com/cosmos/cosmos-sdk/codec"

"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
)

// NewKeeperWithVM creates a new Keeper instance with the provided Wasm VM.
// This constructor function is used when binaries are compiled with cgo disabled or the
// custom build directive: nolink_libwasmvm.
// This function is intended to panic and notify users that 08-wasm keeper functionality is not available.
func NewKeeperWithVM(
_ codec.BinaryCodec,
_ storetypes.KVStoreService,
_ types.ClientKeeper,
_ string,
_ ibcwasm.WasmEngine,
_ ibcwasm.QueryRouter,
_ ...Option,
) Keeper {
panic("not implemented, please build with cgo enabled or nolink_libwasmvm disabled")
}

// NewKeeperWithConfig creates a new Keeper instance with the provided Wasm configuration.
// This constructor function is used when binaries are compiled with cgo disabled or the
// custom build directive: nolink_libwasmvm.
// This function is intended to panic and notify users that 08-wasm keeper functionality is not available.
func NewKeeperWithConfig(
_ codec.BinaryCodec,
_ storetypes.KVStoreService,
_ types.ClientKeeper,
_ string,
_ types.WasmConfig,
_ ibcwasm.QueryRouter,
_ ...Option,
) Keeper {
panic("not implemented, please build with cgo enabled or nolink_libwasmvm disabled")
}
87 changes: 87 additions & 0 deletions modules/light-clients/08-wasm/keeper/keeper_vm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//go:build cgo && !nolink_libwasmvm

package keeper

import (
"errors"
"fmt"
"strings"

wasmvm "github.com/CosmWasm/wasmvm/v2"

"cosmossdk.io/core/store"

"github.com/cosmos/cosmos-sdk/codec"

"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
)

// NewKeeperWithVM creates a new Keeper instance with the provided Wasm VM.
// This constructor function is meant to be used when the chain uses x/wasm
// and the same Wasm VM instance should be shared with it.
func NewKeeperWithVM(
cdc codec.BinaryCodec,
storeService store.KVStoreService,
clientKeeper types.ClientKeeper,
authority string,
vm ibcwasm.WasmEngine,
queryRouter ibcwasm.QueryRouter,
opts ...Option,
) Keeper {
if clientKeeper == nil {
panic(errors.New("client keeper must not be nil"))
}

if vm == nil {
panic(errors.New("wasm VM must not be nil"))
}

if storeService == nil {
panic(errors.New("store service must not be nil"))
}

if strings.TrimSpace(authority) == "" {
panic(errors.New("authority must be non-empty"))
}

keeper := &Keeper{
cdc: cdc,
storeService: storeService,
clientKeeper: clientKeeper,
authority: authority,
}

// set query plugins to ensure there is a non-nil query plugin
// regardless of what options the user provides
ibcwasm.SetQueryPlugins(types.NewDefaultQueryPlugins())
for _, opt := range opts {
opt.apply(keeper)
}

ibcwasm.SetVM(vm)
ibcwasm.SetQueryRouter(queryRouter)
ibcwasm.SetupWasmStoreService(storeService)

return *keeper
}

// NewKeeperWithConfig creates a new Keeper instance with the provided Wasm configuration.
// This constructor function is meant to be used when the chain does not use x/wasm
// and a Wasm VM needs to be instantiated using the provided parameters.
func NewKeeperWithConfig(
cdc codec.BinaryCodec,
storeService store.KVStoreService,
clientKeeper types.ClientKeeper,
authority string,
wasmConfig types.WasmConfig,
queryRouter ibcwasm.QueryRouter,
opts ...Option,
) Keeper {
vm, err := wasmvm.NewVM(wasmConfig.DataDir, wasmConfig.SupportedCapabilities, types.ContractMemoryLimit, wasmConfig.ContractDebugMode, types.MemoryCacheSize)
if err != nil {
panic(fmt.Errorf("failed to instantiate new Wasm VM instance: %v", err))
}

return NewKeeperWithVM(cdc, storeService, clientKeeper, authority, vm, queryRouter, opts...)
}
16 changes: 16 additions & 0 deletions scripts/build-wasm-simapp-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -eou pipefail

# build_wasm_image extracts the correct libwasm version and checksum
# based on the go.mod and builds a docker image with the provided tag.
function build_wasm_image(){
local version="$(scripts/get-libwasm-version.py --get-version)"
local checksum="$(scripts/get-libwasm-version.py --get-checksum)"
docker build . -t "${1}" -f modules/light-clients/08-wasm/Dockerfile --build-arg LIBWASM_VERSION=${version} --build-arg LIBWASM_CHECKSUM=${checksum}
}

# default to latest if no tag is specified.
TAG="${1:-ibc-go-wasm-simd:latest}"

build_wasm_image "${TAG}"
Loading