diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index 16a248e568..50cd842893 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -232,7 +232,7 @@ func (k Keeper) instantiate( codeInfo := k.GetCodeInfo(ctx, codeID) if codeInfo == nil { - return nil, nil, sdkerrors.Wrap(types.ErrNotFound, "code") + return nil, nil, types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID) } if !authPolicy.CanInstantiateContract(codeInfo.InstantiateConfig, creator) { return nil, nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "can not instantiate") @@ -711,14 +711,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, sdkerrors.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, sdkerrors.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) @@ -840,7 +842,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 sdkerrors.Wrap(types.ErrNotFound, "code info") + return types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID) } if err := k.wasmVM.Pin(codeInfo.CodeHash); err != nil { @@ -861,7 +863,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 sdkerrors.Wrap(types.ErrNotFound, "code info") + return types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID) } if err := k.wasmVM.Unpin(codeInfo.CodeHash); err != nil { return sdkerrors.Wrap(types.ErrUnpinContractFailed, err.Error()) @@ -890,9 +892,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 sdkerrors.Wrap(types.ErrNotFound, "code info") + return types.ErrNoSuchCodeFn(codeID).Wrapf("code id %d", codeID) } if err := k.wasmVM.Pin(codeInfo.CodeHash); err != nil { return sdkerrors.Wrap(types.ErrPinContractFailed, err.Error()) @@ -905,7 +908,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 sdkerrors.Wrap(types.ErrNotFound, "contract info") + return types.ErrNoSuchContractFn(contractAddr.String()). + Wrapf("address %s", contractAddr.String()) } if err := info.SetExtension(ext); err != nil { return err @@ -918,7 +922,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 sdkerrors.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) { @@ -1025,7 +1029,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 sdkerrors.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 sdkerrors.Wrapf(types.ErrDuplicate, "contract: %s", contractAddr) diff --git a/x/wasm/keeper/keeper_test.go b/x/wasm/keeper/keeper_test.go index bee350b3c1..872f0925bc 100644 --- a/x/wasm/keeper/keeper_test.go +++ b/x/wasm/keeper/keeper_test.go @@ -699,7 +699,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) } @@ -979,7 +979,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/legacy_querier_test.go b/x/wasm/keeper/legacy_querier_test.go index 1b6006fa30..f4bce28dc2 100644 --- a/x/wasm/keeper/legacy_querier_test.go +++ b/x/wasm/keeper/legacy_querier_test.go @@ -120,16 +120,15 @@ func TestLegacyQueryContractState(t *testing.T) { }, "query smart with unknown address": { srcPath: []string{QueryGetContractState, anyAddr.String(), QueryMethodContractStateSmart}, - srcReq: abci.RequestQuery{Data: []byte(`{}`)}, + srcReq: abci.RequestQuery{Data: []byte(`{"verifier":{}}`)}, expModelLen: 0, - expErr: types.ErrNotFound, + expErr: types.ErrNoSuchContractFn(anyAddr.String()).Wrapf("address %s", anyAddr.String()), }, } for msg, spec := range specs { t.Run(msg, func(t *testing.T) { binResult, err := q(ctx, spec.srcPath, spec.srcReq) - // require.True(t, spec.expErr.Is(err), "unexpected error") require.True(t, errors.Is(err, spec.expErr), err) // if smart query, check custom response diff --git a/x/wasm/keeper/querier.go b/x/wasm/keeper/querier.go index cd573ab5a3..d829fb57d9 100644 --- a/x/wasm/keeper/querier.go +++ b/x/wasm/keeper/querier.go @@ -44,7 +44,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 } @@ -119,7 +120,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) @@ -154,7 +156,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 @@ -198,7 +201,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 } @@ -215,7 +219,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, @@ -254,7 +258,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 9568ad97d5..a50d6b3432 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 *sdkErrors.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 *sdkErrors.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)