Skip to content

Commit

Permalink
Merge pull request cosmos#367 from CosmWasm/genesis-fix
Browse files Browse the repository at this point in the history
Genesis fix
  • Loading branch information
ethanfrey authored Jan 11, 2021
2 parents 456ddae + 9650941 commit e184f9e
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 18 deletions.
38 changes: 21 additions & 17 deletions x/wasm/client/cli/genesis_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func GenesisStoreCodeCmd(defaultNodeHome string) *cobra.Command {
return err
}

return alterModuleState(cmd, func(s *types.GenesisState, _ map[string]json.RawMessage) error {
s.GenMsgs = append(s.GenMsgs, types.GenesisState_GenMsgs{
return alterModuleState(cmd, func(state *types.GenesisState, _ map[string]json.RawMessage) error {
state.GenMsgs = append(state.GenMsgs, types.GenesisState_GenMsgs{
Sum: &types.GenesisState_GenMsgs_StoreCode{StoreCode: &msg},
})
return nil
Expand Down Expand Up @@ -86,17 +86,17 @@ func GenesisInstantiateContractCmd(defaultNodeHome string) *cobra.Command {
return err
}

return alterModuleState(cmd, func(s *types.GenesisState, a map[string]json.RawMessage) error {
// simple sanity check that sender has some balance although it may be consumed by a previous message already
switch ok, err := hasAccountBalance(cmd, a, senderAddr, msg.InitFunds); {
return alterModuleState(cmd, func(state *types.GenesisState, appState map[string]json.RawMessage) error {
// simple sanity check that sender has some balance although it may be consumed by appState previous message already
switch ok, err := hasAccountBalance(cmd, appState, senderAddr, msg.InitFunds); {
case err != nil:
return err
case !ok:
return errors.New("sender has not enough account balance")
}

// does code id exists?
codeInfos, err := getAllCodes(s)
codeInfos, err := getAllCodes(state)
if err != nil {
return err
}
Expand All @@ -112,9 +112,9 @@ func GenesisInstantiateContractCmd(defaultNodeHome string) *cobra.Command {
}
// permissions correct?
if !codeInfo.Info.InstantiateConfig.Allowed(senderAddr) {
return fmt.Errorf("permissions were not granted for %s", senderAddr)
return fmt.Errorf("permissions were not granted for %state", senderAddr)
}
s.GenMsgs = append(s.GenMsgs, types.GenesisState_GenMsgs{
state.GenMsgs = append(state.GenMsgs, types.GenesisState_GenMsgs{
Sum: &types.GenesisState_GenMsgs_InstantiateContract{InstantiateContract: &msg},
})
return nil
Expand Down Expand Up @@ -153,20 +153,20 @@ func GenesisExecuteContractCmd(defaultNodeHome string) *cobra.Command {
return err
}

return alterModuleState(cmd, func(s *types.GenesisState, a map[string]json.RawMessage) error {
// simple sanity check that sender has some balance although it may be consumed by a previous message already
switch ok, err := hasAccountBalance(cmd, a, senderAddr, msg.SentFunds); {
return alterModuleState(cmd, func(state *types.GenesisState, appState map[string]json.RawMessage) error {
// simple sanity check that sender has some balance although it may be consumed by appState previous message already
switch ok, err := hasAccountBalance(cmd, appState, senderAddr, msg.SentFunds); {
case err != nil:
return err
case !ok:
return errors.New("sender has not enough account balance")
}

// - does contract address exists?
if !hasContract(s, msg.Contract) {
return fmt.Errorf("unknown contract: %s", msg.Contract)
if !hasContract(state, msg.Contract) {
return fmt.Errorf("unknown contract: %state", msg.Contract)
}
s.GenMsgs = append(s.GenMsgs, types.GenesisState_GenMsgs{
state.GenMsgs = append(state.GenMsgs, types.GenesisState_GenMsgs{
Sum: &types.GenesisState_GenMsgs_ExecuteContract{ExecuteContract: &msg},
})
return nil
Expand Down Expand Up @@ -316,14 +316,18 @@ func getAllContracts(state *types.GenesisState) []contractMeta {
return all
}

func hasAccountBalance(cmd *cobra.Command, a map[string]json.RawMessage, sender sdk.AccAddress, coins sdk.Coins) (bool, error) {
func hasAccountBalance(cmd *cobra.Command, appState map[string]json.RawMessage, sender sdk.AccAddress, coins sdk.Coins) (bool, error) {
// no coins needed, no account needed
if coins.IsZero() {
return true, nil
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return false, err
}
cdc := clientCtx.JSONMarshaler
var genBalIterator banktypes.GenesisBalancesIterator
err = genutil.ValidateAccountInGenesis(a, genBalIterator, sender, coins, cdc)
err = genutil.ValidateAccountInGenesis(appState, genBalIterator, sender, coins, cdc)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -385,7 +389,7 @@ func readGenesis(cmd *cobra.Command) (*genesisData, error) {
// unmarshalls the wasm module section into the object representation
// calls the callback function to modify it
// and marshals the modified state back into the genesis file
func alterModuleState(cmd *cobra.Command, callback func(s *types.GenesisState, a map[string]json.RawMessage) error) error {
func alterModuleState(cmd *cobra.Command, callback func(state *types.GenesisState, appState map[string]json.RawMessage) error) error {
g, err := readGenesis(cmd)
if err != nil {
return err
Expand Down
110 changes: 109 additions & 1 deletion x/wasm/client/cli/genesis_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func TestInstantiateContractCmd(t *testing.T) {
},
expError: true,
},
"fails without a sender balance": {
"succeeds with unknown account when no init_funds": {
srcGenesis: types.GenesisState{
Params: types.DefaultParams(),
Codes: []types.Code{
Expand All @@ -239,6 +239,58 @@ func TestInstantiateContractCmd(t *testing.T) {
flagSet.Set("label", "testing")
flagSet.Set("run-as", keeper.RandomBech32AccountAddress(t))
},
expMsgCount: 1,
},
"succeeds with funds from well funded account": {
srcGenesis: types.GenesisState{
Params: types.DefaultParams(),
Codes: []types.Code{
{
CodeID: 1,
CodeInfo: types.CodeInfo{
CodeHash: []byte("a-valid-code-hash"),
Creator: keeper.RandomBech32AccountAddress(t),
InstantiateConfig: types.AccessConfig{
Permission: types.AccessTypeEverybody,
},
},
CodeBytes: wasmIdent,
},
},
},
mutator: func(cmd *cobra.Command) {
cmd.SetArgs([]string{"1", `{}`})
flagSet := cmd.Flags()
flagSet.Set("label", "testing")
flagSet.Set("run-as", myWellFundedAccount)
flagSet.Set("amount", "100stake")
},
expMsgCount: 1,
},
"fails without enough sender balance": {
srcGenesis: types.GenesisState{
Params: types.DefaultParams(),
Codes: []types.Code{
{
CodeID: 1,
CodeInfo: types.CodeInfo{
CodeHash: []byte("a-valid-code-hash"),
Creator: keeper.RandomBech32AccountAddress(t),
InstantiateConfig: types.AccessConfig{
Permission: types.AccessTypeEverybody,
},
},
CodeBytes: wasmIdent,
},
},
},
mutator: func(cmd *cobra.Command) {
cmd.SetArgs([]string{"1", `{}`})
flagSet := cmd.Flags()
flagSet.Set("label", "testing")
flagSet.Set("run-as", keeper.RandomBech32AccountAddress(t))
flagSet.Set("amount", "10stake")
},
expError: true,
},
}
Expand Down Expand Up @@ -359,6 +411,61 @@ func TestExecuteContractCmd(t *testing.T) {
},
expError: true,
},
"succeeds with unknown account when no sent_funds": {
srcGenesis: types.GenesisState{
Params: types.DefaultParams(),
Codes: []types.Code{
{
CodeID: 1,
CodeInfo: types.CodeInfoFixture(),
CodeBytes: wasmIdent,
},
},
Contracts: []types.Contract{
{
ContractAddress: firstContractAddress,
ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
info.Created = nil
}),
ContractState: []types.Model{},
},
},
},
mutator: func(cmd *cobra.Command) {
cmd.SetArgs([]string{firstContractAddress, `{}`})
flagSet := cmd.Flags()
flagSet.Set("run-as", keeper.RandomBech32AccountAddress(t))
},
expMsgCount: 1,
},
"succeeds with funds from well funded account": {
srcGenesis: types.GenesisState{
Params: types.DefaultParams(),
Codes: []types.Code{
{
CodeID: 1,
CodeInfo: types.CodeInfoFixture(),
CodeBytes: wasmIdent,
},
},
Contracts: []types.Contract{
{
ContractAddress: firstContractAddress,
ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
info.Created = nil
}),
ContractState: []types.Model{},
},
},
},
mutator: func(cmd *cobra.Command) {
cmd.SetArgs([]string{firstContractAddress, `{}`})
flagSet := cmd.Flags()
flagSet.Set("run-as", myWellFundedAccount)
flagSet.Set("amount", "100stake")
},
expMsgCount: 1,
},
"fails without enough sender balance": {
srcGenesis: types.GenesisState{
Params: types.DefaultParams(),
Expand All @@ -383,6 +490,7 @@ func TestExecuteContractCmd(t *testing.T) {
cmd.SetArgs([]string{firstContractAddress, `{}`})
flagSet := cmd.Flags()
flagSet.Set("run-as", keeper.RandomBech32AccountAddress(t))
flagSet.Set("amount", "10stake")
},
expError: true,
},
Expand Down

0 comments on commit e184f9e

Please sign in to comment.