Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup ErrNotFound cases #1334

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (k Keeper) instantiate(

codeInfo := k.GetCodeInfo(ctx, codeID)
if codeInfo == nil {
return nil, nil, errorsmod.Wrap(types.ErrNotFound, "code")
return nil, nil, types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID)
}
if !authPolicy.CanInstantiateContract(codeInfo.InstantiateConfig, creator) {
return nil, nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "can not instantiate")
Expand Down Expand Up @@ -734,14 +734,16 @@ func (k Keeper) contractInstance(ctx sdk.Context, contractAddress sdk.AccAddress

contractBz := store.Get(types.GetContractAddressKey(contractAddress))
if contractBz == nil {
return types.ContractInfo{}, types.CodeInfo{}, nil, errorsmod.Wrap(types.ErrNotFound, "contract")
return types.ContractInfo{}, types.CodeInfo{}, nil, types.ErrNoSuchContractFn(contractAddress.String()).
Wrapf("address %s", contractAddress.String())
}
var contractInfo types.ContractInfo
k.cdc.MustUnmarshal(contractBz, &contractInfo)

codeInfoBz := store.Get(types.GetCodeKey(contractInfo.CodeID))
if codeInfoBz == nil {
return contractInfo, types.CodeInfo{}, nil, errorsmod.Wrap(types.ErrNotFound, "code info")
return contractInfo, types.CodeInfo{}, nil, types.ErrNoSuchCodeFn(contractInfo.CodeID).
Wrapf("code id %d", contractInfo.CodeID)
}
var codeInfo types.CodeInfo
k.cdc.MustUnmarshal(codeInfoBz, &codeInfo)
Expand Down Expand Up @@ -863,7 +865,7 @@ func (k Keeper) GetByteCode(ctx sdk.Context, codeID uint64) ([]byte, error) {
func (k Keeper) pinCode(ctx sdk.Context, codeID uint64) error {
codeInfo := k.GetCodeInfo(ctx, codeID)
if codeInfo == nil {
return errorsmod.Wrap(types.ErrNotFound, "code info")
return types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID)
}

if err := k.wasmVM.Pin(codeInfo.CodeHash); err != nil {
Expand All @@ -884,7 +886,7 @@ func (k Keeper) pinCode(ctx sdk.Context, codeID uint64) error {
func (k Keeper) unpinCode(ctx sdk.Context, codeID uint64) error {
codeInfo := k.GetCodeInfo(ctx, codeID)
if codeInfo == nil {
return errorsmod.Wrap(types.ErrNotFound, "code info")
return types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID)
}
if err := k.wasmVM.Unpin(codeInfo.CodeHash); err != nil {
return errorsmod.Wrap(types.ErrUnpinContractFailed, err.Error())
Expand Down Expand Up @@ -913,9 +915,10 @@ func (k Keeper) InitializePinnedCodes(ctx sdk.Context) error {
defer iter.Close()

for ; iter.Valid(); iter.Next() {
codeInfo := k.GetCodeInfo(ctx, types.ParsePinnedCodeIndex(iter.Key()))
codeID := types.ParsePinnedCodeIndex(iter.Key())
codeInfo := k.GetCodeInfo(ctx, codeID)
if codeInfo == nil {
return errorsmod.Wrap(types.ErrNotFound, "code info")
return types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID)
}
if err := k.wasmVM.Pin(codeInfo.CodeHash); err != nil {
return errorsmod.Wrap(types.ErrPinContractFailed, err.Error())
Expand All @@ -928,7 +931,8 @@ func (k Keeper) InitializePinnedCodes(ctx sdk.Context) error {
func (k Keeper) setContractInfoExtension(ctx sdk.Context, contractAddr sdk.AccAddress, ext types.ContractInfoExtension) error {
info := k.GetContractInfo(ctx, contractAddr)
if info == nil {
return errorsmod.Wrap(types.ErrNotFound, "contract info")
return types.ErrNoSuchContractFn(contractAddr.String()).
Wrapf("address %s", contractAddr.String())
}
if err := info.SetExtension(ext); err != nil {
return err
Expand All @@ -941,7 +945,7 @@ func (k Keeper) setContractInfoExtension(ctx sdk.Context, contractAddr sdk.AccAd
func (k Keeper) setAccessConfig(ctx sdk.Context, codeID uint64, caller sdk.AccAddress, newConfig types.AccessConfig, authz AuthorizationPolicy) error {
info := k.GetCodeInfo(ctx, codeID)
if info == nil {
return errorsmod.Wrap(types.ErrNotFound, "code info")
return types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID)
}
isSubset := newConfig.Permission.IsSubset(k.getInstantiateAccessConfig(ctx))
if !authz.CanModifyCodeAccessConfig(sdk.MustAccAddressFromBech32(info.Creator), caller, isSubset) {
Expand Down Expand Up @@ -1048,7 +1052,7 @@ func (k Keeper) importAutoIncrementID(ctx sdk.Context, lastIDKey []byte, val uin

func (k Keeper) importContract(ctx sdk.Context, contractAddr sdk.AccAddress, c *types.ContractInfo, state []types.Model, entries []types.ContractCodeHistoryEntry) error {
if !k.containsCodeInfo(ctx, c.CodeID) {
return errorsmod.Wrapf(types.ErrNotFound, "code id: %d", c.CodeID)
return types.ErrNoSuchCodeFn(c.CodeID).Wrapf("code id %d", c.CodeID)
}
if k.HasContractInfo(ctx, contractAddr) {
return errorsmod.Wrapf(types.ErrDuplicate, "contract: %s", contractAddr)
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ func TestInstantiateWithNonExistingCodeID(t *testing.T) {

const nonExistingCodeID = 9999
addr, _, err := keepers.ContractKeeper.Instantiate(ctx, nonExistingCodeID, creator, nil, initMsgBz, "demo contract 2", nil)
require.True(t, types.ErrNotFound.Is(err), err)
require.Equal(t, types.ErrNoSuchCodeFn(nonExistingCodeID).Wrapf("code id %d", nonExistingCodeID).Error(), err.Error())
require.Nil(t, addr)
}

Expand Down Expand Up @@ -983,7 +983,7 @@ func TestExecuteWithNonExistingAddress(t *testing.T) {
// unauthorized - trialCtx so we don't change state
nonExistingAddress := RandomAccountAddress(t)
_, err := keeper.Execute(ctx, nonExistingAddress, creator, []byte(`{}`), nil)
require.True(t, types.ErrNotFound.Is(err), err)
require.Equal(t, types.ErrNoSuchContractFn(nonExistingAddress.String()).Wrapf("address %s", nonExistingAddress.String()).Error(), err.Error())
}

func TestExecuteWithPanic(t *testing.T) {
Expand Down
17 changes: 11 additions & 6 deletions x/wasm/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func (q GrpcQuerier) ContractInfo(c context.Context, req *types.QueryContractInf
case err != nil:
return nil, err
case rsp == nil:
return nil, types.ErrNotFound
return nil, types.ErrNoSuchContractFn(contractAddr.String()).
Wrapf("address %s", contractAddr.String())
}
return rsp, nil
}
Expand Down Expand Up @@ -120,7 +121,8 @@ func (q GrpcQuerier) AllContractState(c context.Context, req *types.QueryAllCont
}
ctx := sdk.UnwrapSDKContext(c)
if !q.keeper.HasContractInfo(ctx, contractAddr) {
return nil, types.ErrNotFound
return nil, types.ErrNoSuchContractFn(contractAddr.String()).
Wrapf("address %s", contractAddr.String())
}

r := make([]types.Model, 0)
Expand Down Expand Up @@ -155,7 +157,8 @@ func (q GrpcQuerier) RawContractState(c context.Context, req *types.QueryRawCont
}

if !q.keeper.HasContractInfo(ctx, contractAddr) {
return nil, types.ErrNotFound
return nil, types.ErrNoSuchContractFn(contractAddr.String()).
Wrapf("address %s", contractAddr.String())
}
rsp := q.keeper.QueryRaw(ctx, contractAddr, req.QueryData)
return &types.QueryRawContractStateResponse{Data: rsp}, nil
Expand Down Expand Up @@ -199,7 +202,8 @@ func (q GrpcQuerier) SmartContractState(c context.Context, req *types.QuerySmart
case err != nil:
return nil, err
case bz == nil:
return nil, types.ErrNotFound
return nil, types.ErrNoSuchContractFn(contractAddr.String()).
Wrapf("address %s", contractAddr.String())
}
return &types.QuerySmartContractStateResponse{Data: bz}, nil
}
Expand All @@ -216,7 +220,7 @@ func (q GrpcQuerier) Code(c context.Context, req *types.QueryCodeRequest) (*type
case err != nil:
return nil, err
case rsp == nil:
return nil, types.ErrNotFound
return nil, types.ErrNoSuchCodeFn(req.CodeId).Wrapf("code id %d", req.CodeId)
}
return &types.QueryCodeResponse{
CodeInfoResponse: rsp.CodeInfoResponse,
Expand Down Expand Up @@ -255,7 +259,8 @@ func (q GrpcQuerier) Codes(c context.Context, req *types.QueryCodesRequest) (*ty
func queryContractInfo(ctx sdk.Context, addr sdk.AccAddress, keeper types.ViewKeeper) (*types.QueryContractInfoResponse, error) {
info := keeper.GetContractInfo(ctx, addr)
if info == nil {
return nil, types.ErrNotFound
return nil, types.ErrNoSuchContractFn(addr.String()).
Wrapf("address %s", addr.String())
}
return &types.QueryContractInfoResponse{
Address: addr.String(),
Expand Down
27 changes: 17 additions & 10 deletions x/wasm/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ func TestQueryAllContractState(t *testing.T) {
}
require.NoError(t, keeper.importContractState(ctx, contractAddr, contractModel))

randomAddr := RandomBech32AccountAddress(t)

q := Querier(keeper)
specs := map[string]struct {
srcQuery *types.QueryAllContractStateRequest
expModelContains []types.Model
expModelContainsNot []types.Model
expErr *errorsmod.Error
expErr error
}{
"query all": {
srcQuery: &types.QueryAllContractStateRequest{Address: contractAddr.String()},
expModelContains: contractModel,
},
"query all with unknown address": {
srcQuery: &types.QueryAllContractStateRequest{Address: RandomBech32AccountAddress(t)},
expErr: types.ErrNotFound,
srcQuery: &types.QueryAllContractStateRequest{Address: randomAddr},
expErr: types.ErrNoSuchContractFn(randomAddr).Wrapf("address %s", randomAddr),
},
"with pagination offset": {
srcQuery: &types.QueryAllContractStateRequest{
Expand Down Expand Up @@ -99,8 +101,9 @@ func TestQueryAllContractState(t *testing.T) {
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
got, err := q.AllContractState(sdk.WrapSDKContext(ctx), spec.srcQuery)
require.True(t, spec.expErr.Is(err), err)

if spec.expErr != nil {
require.Equal(t, spec.expErr.Error(), err.Error())
return
}
for _, exp := range spec.expModelContains {
Expand All @@ -120,6 +123,8 @@ func TestQuerySmartContractState(t *testing.T) {
exampleContract := InstantiateHackatomExampleContract(t, ctx, keepers)
contractAddr := exampleContract.Contract.String()

randomAddr := RandomBech32AccountAddress(t)

q := Querier(keeper)
specs := map[string]struct {
srcAddr sdk.AccAddress
Expand All @@ -140,8 +145,8 @@ func TestQuerySmartContractState(t *testing.T) {
expErr: status.Error(codes.InvalidArgument, "invalid query data"),
},
"query smart with unknown address": {
srcQuery: &types.QuerySmartContractStateRequest{Address: RandomBech32AccountAddress(t), QueryData: []byte(`{"verifier":{}}`)},
expErr: types.ErrNotFound,
srcQuery: &types.QuerySmartContractStateRequest{Address: randomAddr, QueryData: []byte(`{"verifier":{}}`)},
expErr: types.ErrNoSuchContractFn(randomAddr),
},
}
for msg, spec := range specs {
Expand Down Expand Up @@ -213,11 +218,13 @@ func TestQueryRawContractState(t *testing.T) {
}
require.NoError(t, keeper.importContractState(ctx, exampleContract.Contract, contractModel))

randomAddr := RandomBech32AccountAddress(t)

q := Querier(keeper)
specs := map[string]struct {
srcQuery *types.QueryRawContractStateRequest
expData []byte
expErr *errorsmod.Error
expErr error
}{
"query raw key": {
srcQuery: &types.QueryRawContractStateRequest{Address: contractAddr, QueryData: []byte("foo")},
Expand All @@ -240,15 +247,15 @@ func TestQueryRawContractState(t *testing.T) {
expData: nil,
},
"query raw with unknown address": {
srcQuery: &types.QueryRawContractStateRequest{Address: RandomBech32AccountAddress(t), QueryData: []byte("foo")},
expErr: types.ErrNotFound,
srcQuery: &types.QueryRawContractStateRequest{Address: randomAddr, QueryData: []byte("foo")},
expErr: types.ErrNoSuchContractFn(randomAddr).Wrapf("address %s", randomAddr),
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
got, err := q.RawContractState(sdk.WrapSDKContext(ctx), spec.srcQuery)
require.True(t, spec.expErr.Is(err), err)
if spec.expErr != nil {
assert.Equal(t, spec.expErr.Error(), err.Error())
return
}
assert.Equal(t, spec.expData, got.Data)
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func assertCodeBytes(t *testing.T, q *baseapp.GRPCQueryRouter, ctx sdk.Context,
path := "/cosmwasm.wasm.v1.Query/Code"
resp, err := q.Route(path)(ctx, abci.RequestQuery{Path: path, Data: bz})
if len(expectedBytes) == 0 {
assert.ErrorIs(t, err, types.ErrNotFound)
require.Equal(t, types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID).Error(), err.Error())
return
}
require.NoError(t, err)
Expand Down