From c509fb91d8b8232e7ff1981e24fc16e49cb61dd4 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 25 Feb 2020 17:24:57 +0100 Subject: [PATCH] Remove contract string list, add address info to by code id, filter out init_msg in list --- x/wasm/alias.go | 28 ++++++++++---------- x/wasm/client/cli/query.go | 22 ---------------- x/wasm/client/rest/query.go | 14 ---------- x/wasm/internal/keeper/querier.go | 44 ++++++++++++++++--------------- x/wasm/module_test.go | 11 +++++--- 5 files changed, 45 insertions(+), 74 deletions(-) diff --git a/x/wasm/alias.go b/x/wasm/alias.go index efba1c7..8d7fe6b 100644 --- a/x/wasm/alias.go +++ b/x/wasm/alias.go @@ -19,7 +19,6 @@ const ( MaxWasmSize = types.MaxWasmSize GasMultiplier = keeper.GasMultiplier MaxGas = keeper.MaxGas - QueryListContracts = keeper.QueryListContracts QueryGetContract = keeper.QueryGetContract QueryGetContractState = keeper.QueryGetContractState QueryGetCode = keeper.QueryGetCode @@ -68,17 +67,18 @@ var ( ) type ( - GenesisState = types.GenesisState - Code = types.Code - Contract = types.Contract - MsgStoreCode = types.MsgStoreCode - MsgInstantiateContract = types.MsgInstantiateContract - MsgExecuteContract = types.MsgExecuteContract - Model = types.Model - CodeInfo = types.CodeInfo - ContractInfo = types.ContractInfo - WasmConfig = types.WasmConfig - Keeper = keeper.Keeper - GetCodeResponse = keeper.GetCodeResponse - ListCodeResponse = keeper.ListCodeResponse + GenesisState = types.GenesisState + Code = types.Code + Contract = types.Contract + MsgStoreCode = types.MsgStoreCode + MsgInstantiateContract = types.MsgInstantiateContract + MsgExecuteContract = types.MsgExecuteContract + Model = types.Model + CodeInfo = types.CodeInfo + ContractInfo = types.ContractInfo + ContractInfoWithAddress = types.ContractInfoWithAddress + WasmConfig = types.WasmConfig + Keeper = keeper.Keeper + GetCodeResponse = keeper.GetCodeResponse + ListCodeResponse = keeper.ListCodeResponse ) diff --git a/x/wasm/client/cli/query.go b/x/wasm/client/cli/query.go index f29b41c..a819971 100644 --- a/x/wasm/client/cli/query.go +++ b/x/wasm/client/cli/query.go @@ -34,7 +34,6 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command { queryCmd.AddCommand(flags.GetCommands( GetCmdListCode(cdc), GetCmdQueryCode(cdc), - GetCmdListContracts(cdc), GetCmdGetContractInfo(cdc), GetCmdGetContractState(cdc), )...) @@ -102,27 +101,6 @@ func GetCmdQueryCode(cdc *codec.Codec) *cobra.Command { } } -// GetCmdListContracts lists all instantiated contracts -func GetCmdListContracts(cdc *codec.Codec) *cobra.Command { - return &cobra.Command{ - Use: "list-contracts", - Short: "List addresses of all instantiated contracts on the chain", - Long: "List addresses of all instantiated contracts on the chain", - Args: cobra.ExactArgs(0), - RunE: func(cmd *cobra.Command, args []string) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) - - route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryListContracts) - res, _, err := cliCtx.Query(route) - if err != nil { - return err - } - fmt.Println(string(res)) - return nil - }, - } -} - // GetCmdGetContractInfo gets details about a given contract func GetCmdGetContractInfo(cdc *codec.Codec) *cobra.Command { return &cobra.Command{ diff --git a/x/wasm/client/rest/query.go b/x/wasm/client/rest/query.go index a50895d..b5c3486 100644 --- a/x/wasm/client/rest/query.go +++ b/x/wasm/client/rest/query.go @@ -21,7 +21,6 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc("/wasm/code", listCodesHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/code/{codeID}", queryCodeHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/code/{codeID}/contracts", listContractsByCodeHandlerFn(cliCtx)).Methods("GET") - r.HandleFunc("/wasm/contract", listAllContractsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}", queryContractHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}/state", queryContractStateAllHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}/smart/{query}", queryContractStateSmartHandlerFn(cliCtx)).Queries("encoding", "{encoding}").Methods("GET") @@ -92,19 +91,6 @@ func listContractsByCodeHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } } -func listAllContractsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryListContracts) - res, _, err := cliCtx.Query(route) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) - - return - } - rest.PostProcessResponse(w, cliCtx, string(res)) - } -} - func queryContractHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["contractAddr"]) diff --git a/x/wasm/internal/keeper/querier.go b/x/wasm/internal/keeper/querier.go index 48e974d..22b3096 100644 --- a/x/wasm/internal/keeper/querier.go +++ b/x/wasm/internal/keeper/querier.go @@ -2,7 +2,7 @@ package keeper import ( "encoding/json" - "sort" + "fmt" "strconv" sdk "github.com/cosmos/cosmos-sdk/types" @@ -14,7 +14,6 @@ import ( ) const ( - QueryListContracts = "list-contracts" QueryListContractByCode = "list-contracts-by-code" QueryGetContract = "contract-info" QueryGetContractState = "contract-state" @@ -28,6 +27,13 @@ const ( QueryMethodContractStateRaw = "raw" ) +// ContractInfoWithAddress adds the address (key) to the ContractInfo representation +type ContractInfoWithAddress struct { + // embedded here, so all json items remain top level + *types.ContractInfo + Address sdk.AccAddress `json:"address"` +} + // controls error output on querier - set true when testing/debugging const debug = false @@ -37,8 +43,6 @@ func NewQuerier(keeper Keeper) sdk.Querier { switch path[0] { case QueryGetContract: return queryContractInfo(ctx, path[1], req, keeper) - case QueryListContracts: - return queryContractList(ctx, req, keeper) case QueryListContractByCode: return queryContractListByCode(ctx, path[1], req, keeper) case QueryGetContractState: @@ -62,25 +66,16 @@ func queryContractInfo(ctx sdk.Context, bech string, req abci.RequestQuery, keep return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, err.Error()) } info := keeper.GetContractInfo(ctx, addr) + infoWithAddress := ContractInfoWithAddress{ + Address: addr, + ContractInfo: info, + } - bz, err := json.MarshalIndent(info, "", " ") - if err != nil { - return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) - } - return bz, nil -} - -func queryContractList(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) { - var addrs []string - keeper.ListContractInfo(ctx, func(addr sdk.AccAddress, _ types.ContractInfo) bool { - addrs = append(addrs, addr.String()) - return false - }) - sort.Strings(addrs) - bz, err := json.MarshalIndent(addrs, "", " ") + bz, err := json.MarshalIndent(infoWithAddress, "", " ") if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) } + fmt.Println(string(bz)) return bz, nil } @@ -90,10 +85,17 @@ func queryContractListByCode(ctx sdk.Context, codeIDstr string, req abci.Request return nil, err } - var contracts []types.ContractInfo + var contracts []ContractInfoWithAddress keeper.ListContractInfo(ctx, func(addr sdk.AccAddress, info types.ContractInfo) bool { if info.CodeID == codeID { - contracts = append(contracts, info) + // remove init message on list + info.InitMsg = nil + // and add the address + infoWithAddress := ContractInfoWithAddress{ + Address: addr, + ContractInfo: &info, + } + contracts = append(contracts, infoWithAddress) } return false }) diff --git a/x/wasm/module_test.go b/x/wasm/module_test.go index cddc801..d2e98b8 100644 --- a/x/wasm/module_test.go +++ b/x/wasm/module_test.go @@ -382,8 +382,8 @@ func assertCodeBytes(t *testing.T, q sdk.Querier, ctx sdk.Context, codeID uint64 assert.Equal(t, expectedBytes, res.Code) } -func assertContractList(t *testing.T, q sdk.Querier, ctx sdk.Context, addrs []string) { - bz, sdkerr := q(ctx, []string{QueryListContracts}, abci.RequestQuery{}) +func assertContractList(t *testing.T, q sdk.Querier, ctx sdk.Context, codeID uint64, addrs []string) { + bz, sdkerr := q(ctx, []string{QueryListContractByCode, fmt.Sprintf("%d", codeID)}, abci.RequestQuery{}) require.NoError(t, sdkerr) if len(bz) == 0 { @@ -391,10 +391,15 @@ func assertContractList(t *testing.T, q sdk.Querier, ctx sdk.Context, addrs []st return } - var res []string + var res []ContractInfo err := json.Unmarshal(bz, &res) require.NoError(t, err) + var hasAddrs = make([]string, len(res)) + for i, r := range res { + hasAddrs[i] = r.Address + } + assert.Equal(t, addrs, res) }