diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index ff0eb7625f..895eb8f773 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -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") @@ -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) @@ -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 { @@ -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()) @@ -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()) @@ -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 @@ -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) { @@ -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) diff --git a/x/wasm/keeper/keeper_test.go b/x/wasm/keeper/keeper_test.go index b9addff343..9211e74cb7 100644 --- a/x/wasm/keeper/keeper_test.go +++ b/x/wasm/keeper/keeper_test.go @@ -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) } @@ -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) { diff --git a/x/wasm/keeper/querier.go b/x/wasm/keeper/querier.go index 6ceed97b64..0d31364ddd 100644 --- a/x/wasm/keeper/querier.go +++ b/x/wasm/keeper/querier.go @@ -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 } @@ -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) @@ -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 @@ -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 } @@ -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, @@ -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(), diff --git a/x/wasm/keeper/querier_test.go b/x/wasm/keeper/querier_test.go index 788f4b402d..52c4230fed 100644 --- a/x/wasm/keeper/querier_test.go +++ b/x/wasm/keeper/querier_test.go @@ -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{ @@ -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 { @@ -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 @@ -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 { @@ -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")}, @@ -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) diff --git a/x/wasm/module_test.go b/x/wasm/module_test.go index bd788d0cc3..dbff565292 100644 --- a/x/wasm/module_test.go +++ b/x/wasm/module_test.go @@ -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)