Remove contract string list, add address info to by code id, filter out init_msg in list

This commit is contained in:
Ethan Frey 2020-02-25 17:24:57 +01:00
parent 105b2cd894
commit c509fb91d8
5 changed files with 45 additions and 74 deletions

View File

@ -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
)

View File

@ -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{

View File

@ -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"])

View File

@ -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
})

View File

@ -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)
}