github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/client/cli/genesis_msg_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"os"
     8  	"path"
     9  	"strings"
    10  	"testing"
    11  
    12  	apptypes "github.com/fibonacci-chain/fbc/app/types"
    13  	clictx "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/server"
    15  	authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth"
    16  	auth "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    17  	"github.com/fibonacci-chain/fbc/libs/tendermint/config"
    18  	"github.com/fibonacci-chain/fbc/x/wasm/client/utils"
    19  
    20  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
    21  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/crypto/keys"
    22  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
    23  
    24  	//"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/testutil"
    25  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/tests"
    26  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    27  	banktypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/bank"
    28  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/genutil"
    29  	genutiltypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/genutil/types"
    30  	stakingtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/staking/types"
    31  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
    32  	"github.com/spf13/cobra"
    33  	"github.com/stretchr/testify/assert"
    34  	"github.com/stretchr/testify/require"
    35  
    36  	authexported "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/exported"
    37  	"github.com/fibonacci-chain/fbc/x/wasm/keeper"
    38  	"github.com/fibonacci-chain/fbc/x/wasm/types"
    39  )
    40  
    41  var wasmIdent = []byte("\x00\x61\x73\x6D")
    42  
    43  var myWellFundedAccount = keeper.RandomBech32AccountAddress(nil)
    44  
    45  const defaultTestKeyName = "my-key-name"
    46  
    47  func TestGenesisStoreCodeCmd(t *testing.T) {
    48  	minimalWasmGenesis := types.GenesisState{
    49  		Params: types.DefaultParams(),
    50  	}
    51  	anyValidWasmFile, err := ioutil.TempFile(t.TempDir(), "wasm")
    52  	require.NoError(t, err)
    53  	anyValidWasmFile.Write(wasmIdent)
    54  	require.NoError(t, anyValidWasmFile.Close())
    55  
    56  	specs := map[string]struct {
    57  		srcGenesis types.GenesisState
    58  		mutator    func(cmd *cobra.Command)
    59  		expError   bool
    60  	}{
    61  		"all good with actor address": {
    62  			srcGenesis: minimalWasmGenesis,
    63  			mutator: func(cmd *cobra.Command) {
    64  				cmd.SetArgs([]string{anyValidWasmFile.Name()})
    65  				flagSet := cmd.Flags()
    66  				flagSet.Set("run-as", keeper.RandomBech32AccountAddress(t))
    67  			},
    68  		},
    69  		"all good with key name": {
    70  			srcGenesis: minimalWasmGenesis,
    71  			mutator: func(cmd *cobra.Command) {
    72  				cmd.SetArgs([]string{anyValidWasmFile.Name()})
    73  				flagSet := cmd.Flags()
    74  				flagSet.Set("run-as", defaultTestKeyName)
    75  			},
    76  		},
    77  		"with unknown actor key name should fail": {
    78  			srcGenesis: minimalWasmGenesis,
    79  			mutator: func(cmd *cobra.Command) {
    80  				cmd.SetArgs([]string{anyValidWasmFile.Name()})
    81  				flagSet := cmd.Flags()
    82  				flagSet.Set("run-as", "unknown key")
    83  			},
    84  			expError: true,
    85  		},
    86  		"without actor should fail": {
    87  			srcGenesis: minimalWasmGenesis,
    88  			mutator: func(cmd *cobra.Command) {
    89  				cmd.SetArgs([]string{anyValidWasmFile.Name()})
    90  			},
    91  			expError: true,
    92  		},
    93  	}
    94  	for msg, spec := range specs {
    95  		t.Run(msg, func(t *testing.T) {
    96  			homeDir := setupGenesis(t, spec.srcGenesis)
    97  
    98  			// when
    99  			cmd := GenesisStoreCodeCmd(homeDir, NewDefaultGenesisIO())
   100  			spec.mutator(cmd)
   101  			err := executeCmdWithContext(t, homeDir, cmd)
   102  			if spec.expError {
   103  				require.Error(t, err)
   104  				return
   105  			}
   106  			require.NoError(t, err)
   107  			// then
   108  			moduleState := loadModuleState(t, homeDir)
   109  			assert.Len(t, moduleState.GenMsgs, 1)
   110  		})
   111  	}
   112  }
   113  
   114  func TestInstantiateContractCmd(t *testing.T) {
   115  	minimalWasmGenesis := types.GenesisState{
   116  		Params: types.DefaultParams(),
   117  	}
   118  	anyValidWasmFile, err := ioutil.TempFile(t.TempDir(), "wasm")
   119  	require.NoError(t, err)
   120  	anyValidWasmFile.Write(wasmIdent)
   121  	require.NoError(t, anyValidWasmFile.Close())
   122  
   123  	specs := map[string]struct {
   124  		srcGenesis  types.GenesisState
   125  		mutator     func(cmd *cobra.Command)
   126  		expMsgCount int
   127  		expError    bool
   128  	}{
   129  		"all good with code id in genesis codes": {
   130  			srcGenesis: types.GenesisState{
   131  				Params: types.DefaultParams(),
   132  				Codes: []types.Code{
   133  					{
   134  						CodeID: 1,
   135  						CodeInfo: types.CodeInfo{
   136  							CodeHash: []byte("a-valid-code-hash"),
   137  							Creator:  keeper.RandomBech32AccountAddress(t),
   138  							InstantiateConfig: types.AccessConfig{
   139  								Permission: types.AccessTypeEverybody,
   140  							},
   141  						},
   142  						CodeBytes: wasmIdent,
   143  					},
   144  				},
   145  			},
   146  			mutator: func(cmd *cobra.Command) {
   147  				cmd.SetArgs([]string{"1", `{}`})
   148  				flagSet := cmd.Flags()
   149  				flagSet.Set("label", "testing")
   150  				flagSet.Set("run-as", myWellFundedAccount)
   151  				flagSet.Set("no-admin", "true")
   152  			},
   153  			expMsgCount: 1,
   154  		},
   155  		"all good with code id from genesis store messages without initial sequence": {
   156  			srcGenesis: types.GenesisState{
   157  				Params: types.DefaultParams(),
   158  				GenMsgs: []types.GenesisState_GenMsgs{
   159  					{Sum: &types.GenesisState_GenMsgs_StoreCode{StoreCode: types.MsgStoreCodeFixture()}},
   160  				},
   161  			},
   162  			mutator: func(cmd *cobra.Command) {
   163  				cmd.SetArgs([]string{"1", `{}`})
   164  				flagSet := cmd.Flags()
   165  				flagSet.Set("label", "testing")
   166  				flagSet.Set("run-as", myWellFundedAccount)
   167  				flagSet.Set("admin", myWellFundedAccount)
   168  			},
   169  			expMsgCount: 2,
   170  		},
   171  		"all good with code id from genesis store messages and sequence set": {
   172  			srcGenesis: types.GenesisState{
   173  				Params: types.DefaultParams(),
   174  				GenMsgs: []types.GenesisState_GenMsgs{
   175  					{Sum: &types.GenesisState_GenMsgs_StoreCode{StoreCode: types.MsgStoreCodeFixture()}},
   176  				},
   177  				Sequences: []types.Sequence{
   178  					{IDKey: types.KeyLastCodeID, Value: 100},
   179  				},
   180  			},
   181  			mutator: func(cmd *cobra.Command) {
   182  				cmd.SetArgs([]string{"100", `{}`})
   183  				flagSet := cmd.Flags()
   184  				flagSet.Set("label", "testing")
   185  				flagSet.Set("run-as", myWellFundedAccount)
   186  				flagSet.Set("no-admin", "true")
   187  			},
   188  			expMsgCount: 2,
   189  		},
   190  		"fails with codeID not existing in codes": {
   191  			srcGenesis: minimalWasmGenesis,
   192  			mutator: func(cmd *cobra.Command) {
   193  				cmd.SetArgs([]string{"2", `{}`})
   194  				flagSet := cmd.Flags()
   195  				flagSet.Set("label", "testing")
   196  				flagSet.Set("run-as", myWellFundedAccount)
   197  				flagSet.Set("no-admin", "true")
   198  			},
   199  			expError: true,
   200  		},
   201  		"fails when instantiation permissions not granted": {
   202  			srcGenesis: types.GenesisState{
   203  				Params: types.DefaultParams(),
   204  				GenMsgs: []types.GenesisState_GenMsgs{
   205  					{Sum: &types.GenesisState_GenMsgs_StoreCode{StoreCode: types.MsgStoreCodeFixture(func(code *types.MsgStoreCode) {
   206  						code.InstantiatePermission = &types.AllowNobody
   207  					})}},
   208  				},
   209  			},
   210  			mutator: func(cmd *cobra.Command) {
   211  				cmd.SetArgs([]string{"1", `{}`})
   212  				flagSet := cmd.Flags()
   213  				flagSet.Set("label", "testing")
   214  				flagSet.Set("run-as", myWellFundedAccount)
   215  				flagSet.Set("no-admin", "true")
   216  			},
   217  			expError: true,
   218  		},
   219  		"fails if no explicit --no-admin passed": {
   220  			srcGenesis: types.GenesisState{
   221  				Params: types.DefaultParams(),
   222  				Codes: []types.Code{
   223  					{
   224  						CodeID: 1,
   225  						CodeInfo: types.CodeInfo{
   226  							CodeHash: []byte("a-valid-code-hash"),
   227  							Creator:  keeper.RandomBech32AccountAddress(t),
   228  							InstantiateConfig: types.AccessConfig{
   229  								Permission: types.AccessTypeEverybody,
   230  							},
   231  						},
   232  						CodeBytes: wasmIdent,
   233  					},
   234  				},
   235  			},
   236  			mutator: func(cmd *cobra.Command) {
   237  				cmd.SetArgs([]string{"1", `{}`})
   238  				flagSet := cmd.Flags()
   239  				flagSet.Set("label", "testing")
   240  				flagSet.Set("run-as", myWellFundedAccount)
   241  			},
   242  			expError: true,
   243  		},
   244  		"fails if both --admin and --no-admin passed": {
   245  			srcGenesis: types.GenesisState{
   246  				Params: types.DefaultParams(),
   247  				Codes: []types.Code{
   248  					{
   249  						CodeID: 1,
   250  						CodeInfo: types.CodeInfo{
   251  							CodeHash: []byte("a-valid-code-hash"),
   252  							Creator:  keeper.RandomBech32AccountAddress(t),
   253  							InstantiateConfig: types.AccessConfig{
   254  								Permission: types.AccessTypeEverybody,
   255  							},
   256  						},
   257  						CodeBytes: wasmIdent,
   258  					},
   259  				},
   260  			},
   261  			mutator: func(cmd *cobra.Command) {
   262  				cmd.SetArgs([]string{"1", `{}`})
   263  				flagSet := cmd.Flags()
   264  				flagSet.Set("label", "testing")
   265  				flagSet.Set("run-as", myWellFundedAccount)
   266  				flagSet.Set("no-admin", "true")
   267  				flagSet.Set("admin", myWellFundedAccount)
   268  			},
   269  			expError: true,
   270  		},
   271  		"succeeds with unknown account when no funds": {
   272  			srcGenesis: types.GenesisState{
   273  				Params: types.DefaultParams(),
   274  				Codes: []types.Code{
   275  					{
   276  						CodeID: 1,
   277  						CodeInfo: types.CodeInfo{
   278  							CodeHash: []byte("a-valid-code-hash"),
   279  							Creator:  keeper.RandomBech32AccountAddress(t),
   280  							InstantiateConfig: types.AccessConfig{
   281  								Permission: types.AccessTypeEverybody,
   282  							},
   283  						},
   284  						CodeBytes: wasmIdent,
   285  					},
   286  				},
   287  			},
   288  			mutator: func(cmd *cobra.Command) {
   289  				cmd.SetArgs([]string{"1", `{}`})
   290  				flagSet := cmd.Flags()
   291  				flagSet.Set("label", "testing")
   292  				flagSet.Set("run-as", keeper.RandomBech32AccountAddress(t))
   293  				flagSet.Set("no-admin", "true")
   294  			},
   295  			expMsgCount: 1,
   296  		},
   297  		"succeeds with funds from well funded account": {
   298  			srcGenesis: types.GenesisState{
   299  				Params: types.DefaultParams(),
   300  				Codes: []types.Code{
   301  					{
   302  						CodeID: 1,
   303  						CodeInfo: types.CodeInfo{
   304  							CodeHash: []byte("a-valid-code-hash"),
   305  							Creator:  keeper.RandomBech32AccountAddress(t),
   306  							InstantiateConfig: types.AccessConfig{
   307  								Permission: types.AccessTypeEverybody,
   308  							},
   309  						},
   310  						CodeBytes: wasmIdent,
   311  					},
   312  				},
   313  			},
   314  			mutator: func(cmd *cobra.Command) {
   315  				cmd.SetArgs([]string{"1", `{}`})
   316  				flagSet := cmd.Flags()
   317  				flagSet.Set("label", "testing")
   318  				flagSet.Set("run-as", myWellFundedAccount)
   319  				flagSet.Set("amount", "100stake")
   320  				flagSet.Set("no-admin", "true")
   321  			},
   322  			expMsgCount: 1,
   323  		},
   324  		"fails without enough sender balance": {
   325  			srcGenesis: types.GenesisState{
   326  				Params: types.DefaultParams(),
   327  				Codes: []types.Code{
   328  					{
   329  						CodeID: 1,
   330  						CodeInfo: types.CodeInfo{
   331  							CodeHash: []byte("a-valid-code-hash"),
   332  							Creator:  keeper.RandomBech32AccountAddress(t),
   333  							InstantiateConfig: types.AccessConfig{
   334  								Permission: types.AccessTypeEverybody,
   335  							},
   336  						},
   337  						CodeBytes: wasmIdent,
   338  					},
   339  				},
   340  			},
   341  			mutator: func(cmd *cobra.Command) {
   342  				cmd.SetArgs([]string{"1", `{}`})
   343  				flagSet := cmd.Flags()
   344  				flagSet.Set("label", "testing")
   345  				flagSet.Set("run-as", keeper.RandomBech32AccountAddress(t))
   346  				flagSet.Set("amount", "10stake")
   347  				flagSet.Set("no-admin", "true")
   348  			},
   349  			expError: true,
   350  		},
   351  	}
   352  	for msg, spec := range specs {
   353  		t.Run(msg, func(t *testing.T) {
   354  			homeDir := setupGenesis(t, spec.srcGenesis)
   355  
   356  			// when
   357  			cmd := GenesisInstantiateContractCmd(homeDir, NewDefaultGenesisIO())
   358  			spec.mutator(cmd)
   359  			err := executeCmdWithContext(t, homeDir, cmd)
   360  			if spec.expError {
   361  				require.Error(t, err)
   362  				return
   363  			}
   364  			require.NoError(t, err)
   365  			// then
   366  			moduleState := loadModuleState(t, homeDir)
   367  			assert.Len(t, moduleState.GenMsgs, spec.expMsgCount)
   368  		})
   369  	}
   370  }
   371  
   372  func TestExecuteContractCmd(t *testing.T) {
   373  	const firstContractAddress = "cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr"
   374  	minimalWasmGenesis := types.GenesisState{
   375  		Params: types.DefaultParams(),
   376  	}
   377  	anyValidWasmFile, err := ioutil.TempFile(t.TempDir(), "wasm")
   378  	require.NoError(t, err)
   379  	anyValidWasmFile.Write(wasmIdent)
   380  	require.NoError(t, anyValidWasmFile.Close())
   381  
   382  	specs := map[string]struct {
   383  		srcGenesis  types.GenesisState
   384  		mutator     func(cmd *cobra.Command)
   385  		expMsgCount int
   386  		expError    bool
   387  	}{
   388  		"all good with contract in genesis contracts": {
   389  			srcGenesis: types.GenesisState{
   390  				Params: types.DefaultParams(),
   391  				Codes: []types.Code{
   392  					{
   393  						CodeID:    1,
   394  						CodeInfo:  types.CodeInfoFixture(),
   395  						CodeBytes: wasmIdent,
   396  					},
   397  				},
   398  				Contracts: []types.Contract{
   399  					{
   400  						ContractAddress: firstContractAddress,
   401  						ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
   402  							info.Created = nil
   403  						}),
   404  						ContractState: []types.Model{},
   405  					},
   406  				},
   407  			},
   408  			mutator: func(cmd *cobra.Command) {
   409  				cmd.SetArgs([]string{firstContractAddress, `{}`})
   410  				flagSet := cmd.Flags()
   411  				flagSet.Set("run-as", myWellFundedAccount)
   412  			},
   413  			expMsgCount: 1,
   414  		},
   415  		"all good with contract from genesis store messages without initial sequence": {
   416  			srcGenesis: types.GenesisState{
   417  				Params: types.DefaultParams(),
   418  				Codes: []types.Code{
   419  					{
   420  						CodeID:    1,
   421  						CodeInfo:  types.CodeInfoFixture(),
   422  						CodeBytes: wasmIdent,
   423  					},
   424  				},
   425  				GenMsgs: []types.GenesisState_GenMsgs{
   426  					{Sum: &types.GenesisState_GenMsgs_InstantiateContract{InstantiateContract: types.MsgInstantiateContractFixture()}},
   427  				},
   428  			},
   429  			mutator: func(cmd *cobra.Command) {
   430  				cmd.SetArgs([]string{firstContractAddress, `{}`})
   431  				flagSet := cmd.Flags()
   432  				flagSet.Set("run-as", myWellFundedAccount)
   433  			},
   434  			expMsgCount: 2,
   435  		},
   436  		"all good with contract from genesis store messages and contract sequence set": {
   437  			srcGenesis: types.GenesisState{
   438  				Params: types.DefaultParams(),
   439  				Codes: []types.Code{
   440  					{
   441  						CodeID:    1,
   442  						CodeInfo:  types.CodeInfoFixture(),
   443  						CodeBytes: wasmIdent,
   444  					},
   445  				},
   446  				GenMsgs: []types.GenesisState_GenMsgs{
   447  					{Sum: &types.GenesisState_GenMsgs_InstantiateContract{InstantiateContract: types.MsgInstantiateContractFixture()}},
   448  				},
   449  				Sequences: []types.Sequence{
   450  					{IDKey: types.KeyLastInstanceID, Value: 100},
   451  				},
   452  			},
   453  			mutator: func(cmd *cobra.Command) {
   454  				// See TestBuildContractAddress in keeper_test.go
   455  				cmd.SetArgs([]string{"cosmos1mujpjkwhut9yjw4xueyugc02evfv46y0dtmnz4lh8xxkkdapym9stu5qm8", `{}`})
   456  				flagSet := cmd.Flags()
   457  				flagSet.Set("run-as", myWellFundedAccount)
   458  			},
   459  			expMsgCount: 2,
   460  		},
   461  		"fails with unknown contract address": {
   462  			srcGenesis: minimalWasmGenesis,
   463  			mutator: func(cmd *cobra.Command) {
   464  				cmd.SetArgs([]string{keeper.RandomBech32AccountAddress(t), `{}`})
   465  				flagSet := cmd.Flags()
   466  				flagSet.Set("run-as", myWellFundedAccount)
   467  			},
   468  			expError: true,
   469  		},
   470  		"succeeds with unknown account when no funds": {
   471  			srcGenesis: types.GenesisState{
   472  				Params: types.DefaultParams(),
   473  				Codes: []types.Code{
   474  					{
   475  						CodeID:    1,
   476  						CodeInfo:  types.CodeInfoFixture(),
   477  						CodeBytes: wasmIdent,
   478  					},
   479  				},
   480  				Contracts: []types.Contract{
   481  					{
   482  						ContractAddress: firstContractAddress,
   483  						ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
   484  							info.Created = nil
   485  						}),
   486  						ContractState: []types.Model{},
   487  					},
   488  				},
   489  			},
   490  			mutator: func(cmd *cobra.Command) {
   491  				cmd.SetArgs([]string{firstContractAddress, `{}`})
   492  				flagSet := cmd.Flags()
   493  				flagSet.Set("run-as", keeper.RandomBech32AccountAddress(t))
   494  			},
   495  			expMsgCount: 1,
   496  		},
   497  		"succeeds with funds from well funded account": {
   498  			srcGenesis: types.GenesisState{
   499  				Params: types.DefaultParams(),
   500  				Codes: []types.Code{
   501  					{
   502  						CodeID:    1,
   503  						CodeInfo:  types.CodeInfoFixture(),
   504  						CodeBytes: wasmIdent,
   505  					},
   506  				},
   507  				Contracts: []types.Contract{
   508  					{
   509  						ContractAddress: firstContractAddress,
   510  						ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
   511  							info.Created = nil
   512  						}),
   513  						ContractState: []types.Model{},
   514  					},
   515  				},
   516  			},
   517  			mutator: func(cmd *cobra.Command) {
   518  				cmd.SetArgs([]string{firstContractAddress, `{}`})
   519  				flagSet := cmd.Flags()
   520  				flagSet.Set("run-as", myWellFundedAccount)
   521  				flagSet.Set("amount", "100stake")
   522  			},
   523  			expMsgCount: 1,
   524  		},
   525  		"fails without enough sender balance": {
   526  			srcGenesis: types.GenesisState{
   527  				Params: types.DefaultParams(),
   528  				Codes: []types.Code{
   529  					{
   530  						CodeID:    1,
   531  						CodeInfo:  types.CodeInfoFixture(),
   532  						CodeBytes: wasmIdent,
   533  					},
   534  				},
   535  				Contracts: []types.Contract{
   536  					{
   537  						ContractAddress: firstContractAddress,
   538  						ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) {
   539  							info.Created = nil
   540  						}),
   541  						ContractState: []types.Model{},
   542  					},
   543  				},
   544  			},
   545  			mutator: func(cmd *cobra.Command) {
   546  				cmd.SetArgs([]string{firstContractAddress, `{}`})
   547  				flagSet := cmd.Flags()
   548  				flagSet.Set("run-as", keeper.RandomBech32AccountAddress(t))
   549  				flagSet.Set("amount", "10stake")
   550  			},
   551  			expError: true,
   552  		},
   553  	}
   554  	for msg, spec := range specs {
   555  		t.Run(msg, func(t *testing.T) {
   556  			homeDir := setupGenesis(t, spec.srcGenesis)
   557  			cmd := GenesisExecuteContractCmd(homeDir, NewDefaultGenesisIO())
   558  			spec.mutator(cmd)
   559  
   560  			// when
   561  			err := executeCmdWithContext(t, homeDir, cmd)
   562  			if spec.expError {
   563  				require.Error(t, err)
   564  				return
   565  			}
   566  			require.NoError(t, err)
   567  			// then
   568  			moduleState := loadModuleState(t, homeDir)
   569  			assert.Len(t, moduleState.GenMsgs, spec.expMsgCount)
   570  		})
   571  	}
   572  }
   573  
   574  func TestGetAllContracts(t *testing.T) {
   575  	specs := map[string]struct {
   576  		src types.GenesisState
   577  		exp []ContractMeta
   578  	}{
   579  		"read from contracts state": {
   580  			src: types.GenesisState{
   581  				Contracts: []types.Contract{
   582  					{
   583  						ContractAddress: "first-contract",
   584  						ContractInfo:    types.ContractInfo{Label: "first"},
   585  					},
   586  					{
   587  						ContractAddress: "second-contract",
   588  						ContractInfo:    types.ContractInfo{Label: "second"},
   589  					},
   590  				},
   591  			},
   592  			exp: []ContractMeta{
   593  				{
   594  					ContractAddress: "first-contract",
   595  					Info:            types.ContractInfo{Label: "first"},
   596  				},
   597  				{
   598  					ContractAddress: "second-contract",
   599  					Info:            types.ContractInfo{Label: "second"},
   600  				},
   601  			},
   602  		},
   603  		"read from message state": {
   604  			src: types.GenesisState{
   605  				GenMsgs: []types.GenesisState_GenMsgs{
   606  					{Sum: &types.GenesisState_GenMsgs_InstantiateContract{InstantiateContract: &types.MsgInstantiateContract{Label: "first"}}},
   607  					{Sum: &types.GenesisState_GenMsgs_InstantiateContract{InstantiateContract: &types.MsgInstantiateContract{Label: "second"}}},
   608  				},
   609  			},
   610  			exp: []ContractMeta{
   611  				{
   612  					ContractAddress: keeper.BuildContractAddress(0, 1).String(),
   613  					Info:            types.ContractInfo{Label: "first"},
   614  				},
   615  				{
   616  					ContractAddress: keeper.BuildContractAddress(0, 2).String(),
   617  					Info:            types.ContractInfo{Label: "second"},
   618  				},
   619  			},
   620  		},
   621  		"read from message state with contract sequence": {
   622  			src: types.GenesisState{
   623  				Sequences: []types.Sequence{
   624  					{IDKey: types.KeyLastInstanceID, Value: 100},
   625  				},
   626  				GenMsgs: []types.GenesisState_GenMsgs{
   627  					{Sum: &types.GenesisState_GenMsgs_InstantiateContract{InstantiateContract: &types.MsgInstantiateContract{Label: "hundred"}}},
   628  				},
   629  			},
   630  			exp: []ContractMeta{
   631  				{
   632  					ContractAddress: keeper.BuildContractAddress(0, 100).String(),
   633  					Info:            types.ContractInfo{Label: "hundred"},
   634  				},
   635  			},
   636  		},
   637  		"read from contract and message state with contract sequence": {
   638  			src: types.GenesisState{
   639  				Contracts: []types.Contract{
   640  					{
   641  						ContractAddress: "first-contract",
   642  						ContractInfo:    types.ContractInfo{Label: "first"},
   643  					},
   644  				},
   645  				Sequences: []types.Sequence{
   646  					{IDKey: types.KeyLastInstanceID, Value: 100},
   647  				},
   648  				GenMsgs: []types.GenesisState_GenMsgs{
   649  					{Sum: &types.GenesisState_GenMsgs_InstantiateContract{InstantiateContract: &types.MsgInstantiateContract{Label: "hundred"}}},
   650  				},
   651  			},
   652  			exp: []ContractMeta{
   653  				{
   654  					ContractAddress: "first-contract",
   655  					Info:            types.ContractInfo{Label: "first"},
   656  				},
   657  				{
   658  					ContractAddress: keeper.BuildContractAddress(0, 100).String(),
   659  					Info:            types.ContractInfo{Label: "hundred"},
   660  				},
   661  			},
   662  		},
   663  	}
   664  	for msg, spec := range specs {
   665  		t.Run(msg, func(t *testing.T) {
   666  			got := GetAllContracts(&spec.src)
   667  			assert.Equal(t, spec.exp, got)
   668  		})
   669  	}
   670  }
   671  
   672  func setupGenesis(t *testing.T, wasmGenesis types.GenesisState) string {
   673  	appCodec := keeper.MakeEncodingConfig(t).Marshaler
   674  	homeDir := t.TempDir()
   675  
   676  	require.NoError(t, os.Mkdir(path.Join(homeDir, "config"), 0o700))
   677  	genFilename := path.Join(homeDir, "config", "genesis.json")
   678  	appState := make(map[string]json.RawMessage)
   679  	appState[types.ModuleName] = appCodec.GetProtocMarshal().MustMarshalJSON(&wasmGenesis)
   680  
   681  	bankGenesis := banktypes.DefaultGenesisState()
   682  	//bankGenesis.Balances = append(bankGenesis.Balances, banktypes.Balance{
   683  	//	// add a balance for the default sender account
   684  	//	Address: myWellFundedAccount,
   685  	//	Coins:   sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(10000000000))),
   686  	//})
   687  	appState[banktypes.ModuleName] = appCodec.GetCdc().MustMarshalJSON(bankGenesis)
   688  	appState[stakingtypes.ModuleName] = appCodec.GetCdc().MustMarshalJSON(stakingtypes.DefaultGenesisState())
   689  	i, ok := sdk.NewIntFromString("10000000000")
   690  	require.True(t, ok)
   691  	balance := sdk.NewCoins(apptypes.NewPhotonCoin(i))
   692  	my, err := sdk.AccAddressFromBech32(myWellFundedAccount)
   693  	require.NoError(t, err)
   694  	genesisAcc := auth.NewBaseAccount(my.Bytes(), balance, keeper.PubKeyCache[myWellFundedAccount], 0, 0)
   695  	authState := authtypes.NewGenesisState(authtypes.DefaultParams(), []authexported.GenesisAccount{genesisAcc})
   696  	appState[authtypes.ModuleName] = appCodec.GetCdc().MustMarshalJSON(authState)
   697  
   698  	appStateBz, err := json.Marshal(appState)
   699  	require.NoError(t, err)
   700  	genDoc := tmtypes.GenesisDoc{
   701  		ChainID:  "testing",
   702  		AppState: appStateBz,
   703  	}
   704  	err = genutil.ExportGenesisFile(&genDoc, genFilename)
   705  	require.NoError(t, err)
   706  
   707  	return homeDir
   708  }
   709  
   710  func executeCmdWithContext(t *testing.T, homeDir string, cmd *cobra.Command) error {
   711  	logger := log.NewNopLogger()
   712  	cfg := config.TestConfig()
   713  	cfg.SetRoot(homeDir)
   714  	//cfg, err := genutiltest.CreateDefaultTendermintConfig(homeDir)
   715  	//require.NoError(t, err)
   716  	ctx := context.Background()
   717  	appCodec := keeper.MakeEncodingConfig(t).Marshaler
   718  	serverCtx := server.NewContext(cfg, logger)
   719  	clientCtx := clictx.CLIContext{HomeDir: homeDir}.WithCodec(appCodec.GetCdc()).WithProxy(&appCodec)
   720  
   721  	ctx = context.WithValue(ctx, utils.ClientContextKey, &clientCtx)
   722  	ctx = context.WithValue(ctx, utils.ServerContextKey, serverCtx)
   723  	flagSet := cmd.Flags()
   724  	flagSet.Set("home", homeDir)
   725  	flagSet.Set(flags.FlagKeyringBackend, keys.BackendTest)
   726  
   727  	mockIn := strings.NewReader("")
   728  
   729  	kb, err := keys.NewKeyring(sdk.KeyringServiceName(), keys.BackendTest, homeDir, mockIn)
   730  	require.NoError(t, err)
   731  	_, err = kb.CreateAccount(defaultTestKeyName, tests.TestMnemonic, "", "", sdk.FullFundraiserPath, keys.Secp256k1)
   732  	require.NoError(t, err)
   733  	return cmd.ExecuteContext(ctx)
   734  }
   735  
   736  func loadModuleState(t *testing.T, homeDir string) types.GenesisState {
   737  	appCodec := keeper.MakeEncodingConfig(t).Marshaler
   738  	genFilename := path.Join(homeDir, "config", "genesis.json")
   739  	appState, _, err := genutiltypes.GenesisStateFromGenFile(appCodec.GetCdc(), genFilename)
   740  	require.NoError(t, err)
   741  	require.Contains(t, appState, types.ModuleName)
   742  
   743  	var moduleState types.GenesisState
   744  	require.NoError(t, appCodec.GetProtocMarshal().UnmarshalJSON(appState[types.ModuleName], &moduleState))
   745  	return moduleState
   746  }