Merge pull request #367 from CosmWasm/genesis-fix

Genesis fix
This commit is contained in:
Ethan Frey 2021-01-11 08:08:57 +01:00 committed by GitHub
commit e184f9e7f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 130 additions and 18 deletions

View File

@ -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
@ -86,9 +86,9 @@ 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:
@ -96,7 +96,7 @@ func GenesisInstantiateContractCmd(defaultNodeHome string) *cobra.Command {
}
// does code id exists?
codeInfos, err := getAllCodes(s)
codeInfos, err := getAllCodes(state)
if err != nil {
return err
}
@ -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
@ -153,9 +153,9 @@ 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:
@ -163,10 +163,10 @@ func GenesisExecuteContractCmd(defaultNodeHome string) *cobra.Command {
}
// - 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
@ -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
}
@ -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

View File

@ -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{
@ -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,
},
}
@ -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(),
@ -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,
},