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

Add 08-Wasm Contract to Genesis #202

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion cmd/centaurid/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig app.EncodingConfig) {
genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
genutilcli.ValidateGenesisCmd(app.ModuleBasics),
AddGenesisAccountCmd(app.DefaultNodeHome),
AddWasmContractCmd(app.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
addDebugCommands(debug.Cmd()),
debug.Cmd(),
config.Cmd(),
CovertPrefixAddr(),
// this line is used by starport scaffolding # stargate/root/commands
Expand Down
117 changes: 117 additions & 0 deletions cmd/centaurid/cmd/wasm_contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package cmd

import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/CosmWasm/wasmd/x/wasm/ioutils"
wasmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
wasm08types "github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/types"
)

type Checksum = wasmtypes.Checksum

// AddGenesisAccountCmd returns add-genesis-account cobra Command.
func AddWasmContractCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "add-wasm-contract [wasm file]",
Short: "Add a wasm contract to genesis.json",
Long: `Add a wasm contract to genesis.json. Wasm contract don't need be added via gov module.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
depCdc := clientCtx.Codec
cdc := depCdc

serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config

config.SetRoot(clientCtx.HomeDir)

wasm, err := os.ReadFile(args[0])
if err != nil {
return err
}
if !ioutils.IsWasm(wasm) {
return fmt.Errorf("invalid input file. Use wasm binary or gzip")
}

genFile := config.GenesisFile()
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}
codeHash, err := CreateChecksum(wasm)
if err != nil {
return err
}
contract := wasm08types.GenesisContract{
CodeHash: codeHash,
ContractCode: wasm,
}

wasmGenState := GetGenesisStateFromAppState(cdc, appState)

wasmGenState.Contracts = append(wasmGenState.Contracts, contract)

wasmGenStateBz, err := cdc.MarshalJSON(wasmGenState)
if err != nil {
return fmt.Errorf("failed to marshal 08-wasm genesis state: %w", err)
}

appState["08-wasm"] = wasmGenStateBz

appStateJSON, err := json.Marshal(appState)
if err != nil {
return fmt.Errorf("failed to marshal application genesis state: %w", err)
}

genDoc.AppState = appStateJSON
return genutil.ExportGenesisFile(genDoc, genFile)
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// GetGenesisStateFromAppState return GenesisState given raw application
// genesis state.
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *wasm08types.GenesisState {
var genesisState wasm08types.GenesisState

if appState["08-wasm"] != nil {
cdc.MustUnmarshalJSON(appState["08-wasm"], &genesisState)
}

return &genesisState
}

func CreateChecksum(wasm []byte) (Checksum, error) {
if len(wasm) == 0 {
return Checksum{}, fmt.Errorf("wasm bytes nil or empty")
}
if len(wasm) < 4 {
return Checksum{}, fmt.Errorf("wasm bytes shorter than 4 bytes")
}
// magic number for Wasm is "\0asm"
// See https://webassembly.github.io/spec/core/binary/modules.html#binary-module
if !bytes.Equal(wasm[:4], []byte("\x00\x61\x73\x6D")) {
return Checksum{}, fmt.Errorf("wasm bytes do not not start with Wasm magic number")
}
hash := sha256.Sum256(wasm)
return Checksum(hash[:]), nil
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
cloud.google.com/go/iam v0.12.0 // indirect
cloud.google.com/go/storage v1.29.0 // indirect
cosmossdk.io/log v1.1.0 // indirect
github.com/CosmWasm/wasmvm v1.2.4 // indirect; indirect // safe because we're using permissioned cosmwasm
github.com/CosmWasm/wasmvm v1.2.4 // indirect // safe because we're using permissioned cosmwasm
github.com/aws/aws-sdk-go v1.44.203 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/chzyer/readline v1.5.1 // indirect
Expand Down Expand Up @@ -186,7 +186,7 @@ replace (
// lock wasmvm so we do not break the grandpa contract
github.com/CosmWasm/wasmvm => github.com/CosmWasm/wasmvm v1.2.2
// ibc-go with wasm client
github.com/cosmos/ibc-go/v7 => github.com/notional-labs/ibc-go/v7 v7.0.0-wasm-client
github.com/cosmos/ibc-go/v7 => github.com/notional-labs/ibc-go/v7 v7.0.0-wasm-client.0.20230718080912-f959b595d4d1

github.com/strangelove-ventures/packet-forward-middleware/v7 => github.com/notional-labs/packet-forward-middleware/v7 v7.0.0-20230615054050-5523db83e69a

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,8 @@ github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OS
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/notional-labs/alliance v1.0.1-0.20230523105704-66dba9499c01 h1:koCsoc1NGpU8TntXwM/s7Z/whsKNDVPiLim35HLBAzA=
github.com/notional-labs/alliance v1.0.1-0.20230523105704-66dba9499c01/go.mod h1:GFQ8TsXDMTpu7kif0Dwddz6rxazy0ZJQHfN38ZmAodI=
github.com/notional-labs/ibc-go/v7 v7.0.0-wasm-client h1:oQxReLXp48oCIW1zYL+YYg7Nr3L5ginxzJjWXSk1zHM=
github.com/notional-labs/ibc-go/v7 v7.0.0-wasm-client/go.mod h1:ISHo/Qitjtvj2svGmttaZv03zVXmS+uqvUyF9kFqlI0=
github.com/notional-labs/ibc-go/v7 v7.0.0-wasm-client.0.20230718080912-f959b595d4d1 h1:H2qC0F+SZtske/V+HgRUmin6QWGTZE6+dyfvW0j6hXQ=
github.com/notional-labs/ibc-go/v7 v7.0.0-wasm-client.0.20230718080912-f959b595d4d1/go.mod h1:ISHo/Qitjtvj2svGmttaZv03zVXmS+uqvUyF9kFqlI0=
github.com/notional-labs/packet-forward-middleware/v7 v7.0.0-20230615054050-5523db83e69a h1:MMb5U1UydbPHdQGmvLBy5/KgX9NUb8tY6H5aJvXXKjI=
github.com/notional-labs/packet-forward-middleware/v7 v7.0.0-20230615054050-5523db83e69a/go.mod h1:9C/cvYi6C0YqCc7sh0Eyv+pRYCXnt1DkJ1AN9q555Hw=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down
Empty file modified testnode.sh
100644 → 100755
Empty file.