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

feat: integrate wasmd to support CosmWasm #135

Closed
wants to merge 12 commits into from

Conversation

queencre
Copy link
Contributor

@queencre queencre commented Dec 21, 2022

Description

This PR adds the wasmd module to support CosmWasm in Crescent blockchain.

closes: #132

Notes

This PR is intended to be merged for v5.

Tasks

  • Add wasmd v0.29.2
  • Add new module wasmd in StoreUpgrades for v5
  • Add UpgradeHandler for v5.
  • Resolve failing test cases
  • Update docs
  • Update CHANGELOG.md

Design Decision

  • Permissioned

References

Tests

  • Deploy sample wasm contract and interact with it

In order to use the following commands, you must spin up a full node with the config-test.toml using Ignite CLI in your local machine with this branch.

# Download `nameservice` sample contract from the cw-contracts repository
git clone -b main https://github.com/deus-labs/cw-contracts
cd cw-contracts/contracts/nameservice

# Compile the wasm contract with stable toolchain
rustup default stable
RUSTFLAGS='-C link-arg=-s' cargo wasm

# Run unit tests
RUST_BACKTRACE=1 cargo unit-test

# Optimized compilation
docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/rust-optimizer:0.12.6

# Ensure that it is not authorized to store code with transaction message
# Expected result is `can not create code: unauthorized`
crescentd tx wasm store artifacts/cw_nameservice.wasm \
--from alice \
--keyring-backend test \
--chain-id localnet \
--gas-prices 0.25stake \
--gas auto \
--gas-adjustment 1.3 \
--yes \
--output json | jq

# Submit a gov proposal to store code
crescentd tx gov submit-proposal wasm-store artifacts/cw_nameservice.wasm \
--from alice \
--keyring-backend test \
--chain-id localnet \
--title "Upload sample nameservice contract" \
--description "By voting YES..." \
--run-as $(crescentd keys show -a alice) \
--deposit 5000stake \
--gas 10000000 \
--yes \
--output json | jq

# Query
crescentd q gov proposals -o json | jq

# Vote
crescentd tx gov vote 3 yes \
--from alice \
--keyring-backend test \
--chain-id localnet \
--gas 2000000 \
--broadcast-mode block \
--yes \
--output json | jq

# Query a list of wasm contracts
crescentd query wasm list-code -o json | jq

# Prepare the instantiation message
INIT='{"purchase_price":{"amount":"100","denom":"uatom"},"transfer_price":{"amount":"999","denom":"uatom"}}'

# Instantiate the contract
crescentd tx wasm instantiate 1 "$INIT" \
--chain-id localnet \
--from alice \
--keyring-backend test \
--gas-prices 0.025stake \
--gas auto \
--gas-adjustment 1.3 \
--label "Sample Contract - Name Service" \
--yes \
--no-admin -o json | jq

# Reference the following guide to interact with the deployed contract
https://docs.cosmwasm.com/docs/1.0/getting-started/interact-with-contract

Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Appropriate labels applied
  • Targeted PR against correct branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards.
  • Wrote unit and integration
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Re-reviewed Files changed in the Github PR explorer
  • Review Codecov Report in the comment section below once CI passes

@github-actions github-actions bot added the build label Dec 21, 2022
@queencre queencre mentioned this pull request Dec 21, 2022
4 tasks
# Conflicts:
#	app/app_test.go
#	app/sim_test.go
#	app/test_helpers.go
#	app/upgrades/mainnet/v4/upgrade.go
#	cmd/crescentd/cmd/root.go
#	x/liquidfarming/client/testutil/suite.go
#	x/liquidity/client/testutil/suite.go
#	x/liquidstaking/client/testutil/suite.go
@queencre
Copy link
Contributor Author

queencre commented Jan 2, 2023

Problem (Fixed)

It is quite difficult to debug why this particular simulation test $ make test-sim-after-import is failing. the test comes from the sim_test.go in Cosmos SDK and it is not covered in the wasmd module.
If you take a look at the codes, it simply sets up simulation, runs randomized simulation, exports state, sets up new simulation, and runs another randomized simulation. The test runs well without any issue if the wasmd module is not registered in module.NewSimulationManager in app.go.

Debug

As you can see, panic: halting simulations occurs when running the simulation test and running the suggested line of code to reproduce, it runs without any error.

...
[W3] Spawned simulation with pid 11755 [seed=30123012 stdout=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-30123012-date-01-13-2023_111907.stdout stderr=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-30123012-date-01-13-2023_111907.stderr]
[W0] Spawned simulation with pid 11808 [seed=47284728 stdout=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-47284728-date-01-13-2023_111907.stdout stderr=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-47284728-date-01-13-2023_111907.stderr]
[W2] Spawned simulation with pid 11875 [seed=7601778 stdout=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-7601778-date-01-13-2023_111907.stdout stderr=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-7601778-date-01-13-2023_111907.stderr]
[W0] Spawned simulation with pid 11914 [seed=8090485 stdout=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-8090485-date-01-13-2023_111907.stdout stderr=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-8090485-date-01-13-2023_111907.stderr]
[W3] Spawned simulation with pid 11976 [seed=977367484 stdout=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-977367484-date-01-13-2023_111907.stdout stderr=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-977367484-date-01-13-2023_111907.stderr]
[W1] Spawned simulation with pid 12037 [seed=491163361 stdout=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-491163361-date-01-13-2023_111907.stdout stderr=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-491163361-date-01-13-2023_111907.stderr]
.
[W0] Spawned simulation with pid 12071 [seed=424254581 stdout=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-424254581-date-01-13-2023_111907.stdout stderr=/var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/app-simulation-seed-424254581-date-01-13-2023_111907.stderr]
exit status 1
[W3] Seed 977367484: FAILED
To reproduce run: go test ./app -run TestAppSimulationAfterImport -Enabled=true -NumBlocks=2 -Genesis= -Verbose=true -Commit=true -Seed=977367484 -Period=2 -ExportParamsPath /var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/sim_params-977367484.json -ExportStatePath /var/folders/wn/zwc2mf1n61z7sghz9g_1wl8h0000gn/T/sim-logs-015066938/sim_state-977367484.json -v -timeout 24h
ERROR OUTPUT 

exit status 1
panic: halting simulations

The panic comes from worker 3 [W3]. When I open the stdout file, the following logs can be found.

I[2023-01-13|11:22:20.843] asserting crisis invariants                  module=x/crisis inv=30/30 name=marketmaker/deposit-record
I[2023-01-13|11:22:20.843] asserted all invariants                      module=x/crisis duration=17.747004ms height=0
Starting the simulation from time Sat Jan  1 00:00:00 UTC 2022 (unixtime 1640995200)
^MSimulating... block 1/2, operation 0/0.^MSimulating... block 1/2, operation 0/584. I[2023-01-13|11:22:20.897] minted coins from module account             module=x/bank amount=10000000000000airdrop from=mint
^MSimulating... block 1/2, operation 50/584. I[2023-01-13|11:22:21.593] minted coins from module account             module=x/bank amount=1000000000000pool1 from=liquidity
D[2023-01-13|11:22:21.739] storing new contract                         module=x/wasm capabilities=cosmwasm_1_1,staking,stargate code_id=13339727724447516257
^MSimulating... block 1/2, operation 100/584. fatal error: unexpected signal during runtime execution
[signal SIGBUS: bus error code=0x2 addr=0x112a2e000 pc=0x1045f3510]

runtime stack:
runtime.throw({0x101b3e137?, 0x7ff8066a4abb?})
        /usr/local/Cellar/go/1.19.4/libexec/src/runtime/panic.go:1047 +0x5d fp=0x7000064caf60 sp=0x7000064caf30 pc=0x10003c93d
runtime.sigpanic()
        /usr/local/Cellar/go/1.19.4/libexec/src/runtime/signal_unix.go:819 +0x369 fp=0x7000064cafb0 sp=0x7000064caf60 pc=0x100053489

goroutine 53 [syscall]:
runtime.cgocall(0x1017104e0, 0xc003be40a0)
        /usr/local/Cellar/go/1.19.4/libexec/src/runtime/cgocall.go:158 +0x5c fp=0xc003be4008 sp=0xc003be3fd0 pc=0x100006e7c
github.com/CosmWasm/wasmvm/internal/api._C2func_instantiate(0x104a04410, {0x0, 0xc00301c2c0, 0x20}, {0x0, 0xc00038ab60, 0xc5}, {0x0, 0xc000158770, 0x6e}, ...)
        _cgo_gotypes.go:500 +0x85 fp=0xc003be40a0 sp=0xc003be4008 pc=0x100dd9685
github.com/CosmWasm/wasmvm/internal/api.Instantiate.func1({0xc0046b4fa0?}, {0x68?, 0xc00301c2c0?, 0xc003be4390?}, {0x4c?, 0xc00038ab60?, 0x101863200?}, {0x0, 0xc000158770, 0x6e}, ...)
        /Users/xxx/go/pkg/mod/github.com/!cosm!wasm/wasmvm@v1.1.1/internal/api/lib.go:171 +0x247 fp=0xc003be4230 sp=0xc003be40a0 pc=0x100ddfa47
github.com/CosmWasm/wasmvm/internal/api.Instantiate({0x0?}, {0xc00301c2c0?, 0xc003be45d8?, 0x203000?}, {0xc00038ab60?, 0x203000?, 0xc002b0c100?}, {0xc000158770, 0x6e, 0x70}, ...)
        /Users/xxx/go/pkg/mod/github.com/!cosm!wasm/wasmvm@v1.1.1/internal/api/lib.go:171 +0x6f1 fp=0xc003be4530 sp=0xc003be4230 pc=0x100ddf491
github.com/CosmWasm/wasmvm.(*VM).Instantiate(0xc0053d79d0, {0xc00301c2c0, 0x20, 0x20}, {{0x1, 0x16c5fc70a61f0000, {0x101af8bbb, 0xe}}, 0xc002c6a0c0, {{0xc004ad47d0, ...}}}, ...)
        /Users/xxx/go/pkg/mod/github.com/!cosm!wasm/wasmvm@v1.1.1/lib.go:138 +0x2e5 fp=0xc003be46b0 sp=0xc003be4530 pc=0x100de9e65
github.com/CosmWasm/wasmd/x/wasm/keeper.Keeper.instantiate({{0x102503828, 0xc0053d7910}, {0x10252faf8, 0xc004572660}, {0x10251ae80, 0xc0019b15f0}, {0x102500f40, 0xc0053d79f0}, {0x1024fe2c0, 0xc0003bc478}, ...}, ...)
        /Users/xxx/go/pkg/mod/github.com/!cosm!wasm/wasmd@v0.29.2/x/wasm/keeper/keeper.go:354 +0x15e2 fp=0xc003be61e8 sp=0xc003be46b0 pc=0x101145542
github.com/CosmWasm/wasmd/x/wasm/keeper.(*Keeper).instantiate(_, {{0x10251bff8, 0xc0046a9380}, {0x10252c920, 0xc0046b80c0}, {{0x0, 0x0}, {0x101af8bbb, 0xe}, 0x1, ...}, ...}, ...)
        <autogenerated>:1 +0x185 fp=0xc003be6710 sp=0xc003be61e8 pc=0x101170d25
github.com/CosmWasm/wasmd/x/wasm/keeper.PermissionedKeeper.Instantiate({{_, _}, {_, _}}, {{0x10251bff8, 0xc0046a9380}, {0x10252c920, 0xc0046b80c0}, {{0x0, 0x0}, ...}, ...}, ...)
        /Users/xxx/go/pkg/mod/github.com/!cosm!wasm/wasmd@v0.29.2/x/wasm/keeper/contract_keeper.go:67 +0x1b8 fp=0xc003be6a08 sp=0xc003be6710 pc=0x101137e38
github.com/CosmWasm/wasmd/x/wasm/keeper.(*PermissionedKeeper).Instantiate(_, {{0x10251bff8, 0xc0046a9380}, {0x10252c920, 0xc0046b80c0}, {{0x0, 0x0}, {0x101af8bbb, 0xe}, 0x1, ...}, ...}, ...)
        <autogenerated>:1 +0x14b fp=0xc003be6cd8 sp=0xc003be6a08 pc=0x1011747cb
github.com/CosmWasm/wasmd/x/wasm/keeper.msgServer.InstantiateContract({{0x10252b978?, 0xc005857a00?}}, {0x10251bff8, 0xc0046b4c90}, 0xc0045a7810)
        /Users/xxx/go/pkg/mod/github.com/!cosm!wasm/wasmd@v0.29.2/x/wasm/keeper/msg_server.go:73 +0x839 fp=0xc003be7a18 sp=0xc003be6cd8 pc=0x101159659
github.com/CosmWasm/wasmd/x/wasm/keeper.(*msgServer).InstantiateContract(0x10000e4ab?, {0x10251bff8?, 0xc0046b4c90?}, 0x101ad38e0?)
        <autogenerated>:1 +0x47 fp=0xc003be7a50 sp=0xc003be7a18 pc=0x101173ec7
github.com/CosmWasm/wasmd/x/wasm/types._Msg_InstantiateContract_Handler.func1({0x10251bff8, 0xc0046b4c90}, {0x101a6c300?, 0xc0045a7810})
        /Users/xxx/go/pkg/mod/github.com/!cosm!wasm/wasmd@v0.29.2/x/wasm/types/tx.pb.go:967 +0x78 fp=0xc003be7a90 sp=0xc003be7a50 pc=0x100e351b8
github.com/cosmos/cosmos-sdk/baseapp.(*MsgServiceRouter).RegisterService.func2.1({0x10251bff8, 0xc0046b4c60}, {0x100114466?, 0x10000e4ab?}, 0x101ad38e0?, 0xc004661e90)
        /Users/xxx/go/pkg/mod/github.com/crescent-network/cosmos-sdk@v1.1.4-sdk-0.45.10/baseapp/msg_service_router.go:113 +0xdb fp=0xc003be7d08 sp=0xc003be7a90 pc=0x100a3447b
github.com/CosmWasm/wasmd/x/wasm/types._Msg_InstantiateContract_Handler({0x10198d040?, 0xc004ed6430}, {0x10251bff8, 0xc0046b4c60}, 0x10226c960, 0xc002b87ce0)
        /Users/xxx/go/pkg/mod/github.com/!cosm!wasm/wasmd@v0.29.2/x/wasm/types/tx.pb.go:969 +0x138 fp=0xc003be7d60 sp=0xc003be7d08 pc=0x100e35078
github.com/cosmos/cosmos-sdk/baseapp.(*MsgServiceRouter).RegisterService.func2({{0x10251bff8, 0xc0046a9380}, {0x10252c920, 0xc0046b80c0}, {{0x0, 0x0}, {0x101af8bbb, 0xe}, 0x1, {0x0, ...}, ...}, ...}, ...)
...

goroutine 1 [chan receive]:
runtime.gopark(0x1?, 0xc000dcfaa8?, 0xff?, 0xb?, 0xc000dcfa58?)
        /usr/local/Cellar/go/1.19.4/libexec/src/runtime/proc.go:363 +0xd6 fp=0xc002353a28 sp=0xc002353a08 pc=0x10003f5d6
runtime.chanrecv(0xc000384460, 0xc000dcfb27, 0x1)
        /usr/local/Cellar/go/1.19.4/libexec/src/runtime/chan.go:583 +0x49b fp=0xc002353ab8 sp=0xc002353a28 pc=0x100009e7b
runtime.chanrecv1(0x101b13ee7?, 0x101b13ee7?)
        /usr/local/Cellar/go/1.19.4/libexec/src/runtime/chan.go:442 +0x18 fp=0xc002353ae0 sp=0xc002353ab8 pc=0x100009978
testing.(*T).Run(0xc001003a00, {0x101b13ee7?, 0x5979463534cc5?}, 0x10226dcd0)
        /usr/local/Cellar/go/1.19.4/libexec/src/testing/testing.go:1494 +0x37a fp=0xc002353b80 sp=0xc002353ae0 pc=0x100123f7a
testing.runTests.func1(0xc0010a1620?)
        /usr/local/Cellar/go/1.19.4/libexec/src/testing/testing.go:1846 +0x6e fp=0xc002353bd0 sp=0xc002353b80 pc=0x100125ece
testing.tRunner(0xc001003a00, 0xc000dcfcd8)
        /usr/local/Cellar/go/1.19.4/libexec/src/testing/testing.go:1446 +0x10b fp=0xc002353c20 sp=0xc002353bd0 pc=0x10012318b
testing.runTests(0xc0010428c0?, {0x1034ddfc0, 0x8, 0x8}, {0x103d03d28?, 0x40?, 0x1034ffb40?})
        /usr/local/Cellar/go/1.19.4/libexec/src/testing/testing.go:1844 +0x456 fp=0xc002353d08 sp=0xc002353c20 pc=0x100125d76
testing.(*M).Run(0xc0010428c0)
        /usr/local/Cellar/go/1.19.4/libexec/src/testing/testing.go:1726 +0x5d9 fp=0xc002353f00 sp=0xc002353d08 pc=0x100124859
main.main()
        _testmain.go:61 +0x1aa fp=0xc002353f80 sp=0xc002353f00 pc=0x10170f08a
runtime.main()
        /usr/local/Cellar/go/1.19.4/libexec/src/runtime/proc.go:250 +0x212 fp=0xc002353fe0 sp=0xc002353f80 pc=0x10003f212
runtime.goexit()
        /usr/local/Cellar/go/1.19.4/libexec/src/runtime/asm_amd64.s:1594 +0x1 fp=0xc002353fe8 sp=0xc002353fe0 pc=0x100071601

Solution

This is due to using the same directory DefaultNodeHome when initializing application. To fix the issue, we must use the temporary directory that is returned from SetupSimulation function.

@queencre queencre marked this pull request as ready for review January 13, 2023 05:12
@queencre queencre mentioned this pull request Jan 26, 2023
12 tasks
@github-actions
Copy link

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Feb 28, 2023
@github-actions github-actions bot closed this Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrate CosmWasm
1 participant