Skip to content

Commit

Permalink
Add distribution query
Browse files Browse the repository at this point in the history
(cherry picked from commit a17f5f2)

# Conflicts:
#	x/wasm/keeper/query_plugins.go
#	x/wasm/keeper/query_plugins_test.go
#	x/wasm/types/expected_keepers.go
  • Loading branch information
alpe authored and mergify[bot] committed Jul 19, 2023
1 parent 309146a commit 17ed673
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 30 deletions.
87 changes: 57 additions & 30 deletions x/wasm/keeper/query_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"errors"
"fmt"

<<<<<<< HEAD
=======
errorsmod "cosmossdk.io/errors"
>>>>>>> a17f5f2f (Add distribution query)
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -14,8 +18,12 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
<<<<<<< HEAD
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
abci "github.com/tendermint/tendermint/abci/types"
=======
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
>>>>>>> a17f5f2f (Add distribution query)

"github.com/CosmWasm/wasmd/x/wasm/types"
)
Expand Down Expand Up @@ -78,12 +86,13 @@ func (q QueryHandler) GasConsumed() uint64 {
type CustomQuerier func(ctx sdk.Context, request json.RawMessage) ([]byte, error)

type QueryPlugins struct {
Bank func(ctx sdk.Context, request *wasmvmtypes.BankQuery) ([]byte, error)
Custom CustomQuerier
IBC func(ctx sdk.Context, caller sdk.AccAddress, request *wasmvmtypes.IBCQuery) ([]byte, error)
Staking func(ctx sdk.Context, request *wasmvmtypes.StakingQuery) ([]byte, error)
Stargate func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error)
Wasm func(ctx sdk.Context, request *wasmvmtypes.WasmQuery) ([]byte, error)
Bank func(ctx sdk.Context, request *wasmvmtypes.BankQuery) ([]byte, error)
Custom CustomQuerier
IBC func(ctx sdk.Context, caller sdk.AccAddress, request *wasmvmtypes.IBCQuery) ([]byte, error)
Staking func(ctx sdk.Context, request *wasmvmtypes.StakingQuery) ([]byte, error)
Stargate func(ctx sdk.Context, request *wasmvmtypes.StargateQuery) ([]byte, error)
Wasm func(ctx sdk.Context, request *wasmvmtypes.WasmQuery) ([]byte, error)
Distribution func(ctx sdk.Context, request *wasmvmtypes.DistributionQuery) ([]byte, error)
}

type contractMetaDataSource interface {
Expand All @@ -106,12 +115,13 @@ func DefaultQueryPlugins(
wasm wasmQueryKeeper,
) QueryPlugins {
return QueryPlugins{
Bank: BankQuerier(bank),
Custom: NoCustomQuerier,
IBC: IBCQuerier(wasm, channelKeeper),
Staking: StakingQuerier(staking, distKeeper),
Stargate: RejectStargateQuerier(),
Wasm: WasmQuerier(wasm),
Bank: BankQuerier(bank),
Custom: NoCustomQuerier,
IBC: IBCQuerier(wasm, channelKeeper),
Staking: StakingQuerier(staking, distKeeper),
Stargate: RejectStargateQuerier(),
Wasm: WasmQuerier(wasm),
Distribution: DistributionQuerier(distKeeper),
}
}

Expand All @@ -138,29 +148,30 @@ func (e QueryPlugins) Merge(o *QueryPlugins) QueryPlugins {
if o.Wasm != nil {
e.Wasm = o.Wasm
}
if o.Distribution != nil {
e.Distribution = o.Distribution
}
return e
}

// HandleQuery executes the requested query
func (e QueryPlugins) HandleQuery(ctx sdk.Context, caller sdk.AccAddress, request wasmvmtypes.QueryRequest) ([]byte, error) {
func (e QueryPlugins) HandleQuery(ctx sdk.Context, caller sdk.AccAddress, req wasmvmtypes.QueryRequest) ([]byte, error) {
// do the query
if request.Bank != nil {
return e.Bank(ctx, request.Bank)
}
if request.Custom != nil {
return e.Custom(ctx, request.Custom)
}
if request.IBC != nil {
return e.IBC(ctx, caller, request.IBC)
}
if request.Staking != nil {
return e.Staking(ctx, request.Staking)
}
if request.Stargate != nil {
return e.Stargate(ctx, request.Stargate)
}
if request.Wasm != nil {
return e.Wasm(ctx, request.Wasm)
switch {
case req.Bank != nil:
return e.Bank(ctx, req.Bank)
case req.Custom != nil:
return e.Custom(ctx, req.Custom)
case req.IBC != nil:
return e.IBC(ctx, caller, req.IBC)
case req.Staking != nil:
return e.Staking(ctx, req.Staking)
case req.Stargate != nil:
return e.Stargate(ctx, req.Stargate)
case req.Wasm != nil:
return e.Wasm(ctx, req.Wasm)
case req.Distribution != nil:
return e.Distribution(ctx, req.Distribution)
}
return nil, wasmvmtypes.Unknown{}
}
Expand Down Expand Up @@ -589,6 +600,22 @@ func WasmQuerier(k wasmQueryKeeper) func(ctx sdk.Context, request *wasmvmtypes.W
}
}

func DistributionQuerier(k types.DistributionKeeper) func(ctx sdk.Context, request *wasmvmtypes.DistributionQuery) ([]byte, error) {
return func(ctx sdk.Context, req *wasmvmtypes.DistributionQuery) ([]byte, error) {
if req.DelegatorWithdrawAddress == nil {
return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown distribution query"}
}
addr, err := sdk.AccAddressFromBech32(req.DelegatorWithdrawAddress.DelegatorAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrap("delegator address")
}
res := wasmvmtypes.DelegatorWithdrawAddressResponse{
WithdrawAddress: k.GetDelegatorWithdrawAddr(ctx, addr).String(),
}
return json.Marshal(res)
}
}

// ConvertSdkCoinsToWasmCoins covert sdk type to wasmvm coins type
func ConvertSdkCoinsToWasmCoins(coins []sdk.Coin) wasmvmtypes.Coins {
converted := make(wasmvmtypes.Coins, len(coins))
Expand Down
84 changes: 84 additions & 0 deletions x/wasm/keeper/query_plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import (
"testing"
"time"

<<<<<<< HEAD
=======
"github.com/cometbft/cometbft/libs/rand"
"github.com/cosmos/cosmos-sdk/types/address"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

errorsmod "cosmossdk.io/errors"
>>>>>>> a17f5f2f (Add distribution query)
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -756,6 +764,82 @@ func TestAcceptListStargateQuerier(t *testing.T) {
}
}

func TestDistributionQuerier(t *testing.T) {
ctx := sdk.Context{}
var myAddr sdk.AccAddress = rand.Bytes(address.Len)
var myOtherAddr sdk.AccAddress = rand.Bytes(address.Len)
specs := map[string]struct {
q wasmvmtypes.DistributionQuery
mockFn func(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress
expAddr string
expErr bool
}{
"withdrawal override": {
q: wasmvmtypes.DistributionQuery{
DelegatorWithdrawAddress: &wasmvmtypes.DelegatorWithdrawAddressQuery{DelegatorAddress: myAddr.String()},
},
mockFn: func(_ sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress {
return myOtherAddr
},
expAddr: myOtherAddr.String(),
},
"no withdrawal override": {
q: wasmvmtypes.DistributionQuery{
DelegatorWithdrawAddress: &wasmvmtypes.DelegatorWithdrawAddressQuery{DelegatorAddress: myAddr.String()},
},
mockFn: func(_ sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress {
return delAddr
},
expAddr: myAddr.String(),
},
"empty address": {
q: wasmvmtypes.DistributionQuery{
DelegatorWithdrawAddress: &wasmvmtypes.DelegatorWithdrawAddressQuery{},
},
expErr: true,
},
"unknown query": {
q: wasmvmtypes.DistributionQuery{},
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
mock := distrKeeperMock{GetDelegatorWithdrawAddrFn: spec.mockFn}
q := keeper.DistributionQuerier(mock)

gotBz, gotErr := q(ctx, &spec.q)
if spec.expErr {
require.Error(t, gotErr)
return
}
require.NoError(t, gotErr)
var rsp wasmvmtypes.DelegatorWithdrawAddressResponse
require.NoError(t, json.Unmarshal(gotBz, &rsp))
assert.Equal(t, spec.expAddr, rsp.WithdrawAddress)
})
}
}

type distrKeeperMock struct {
DelegationRewardsFn func(c context.Context, req *distributiontypes.QueryDelegationRewardsRequest) (*distributiontypes.QueryDelegationRewardsResponse, error)
GetDelegatorWithdrawAddrFn func(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress
}

func (m distrKeeperMock) DelegationRewards(ctx context.Context, req *distributiontypes.QueryDelegationRewardsRequest) (*distributiontypes.QueryDelegationRewardsResponse, error) {
if m.DelegationRewardsFn == nil {
panic("not expected to be called")
}
return m.DelegationRewardsFn(ctx, req)
}

func (m distrKeeperMock) GetDelegatorWithdrawAddr(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress {
if m.GetDelegatorWithdrawAddrFn == nil {
panic("not expected to be called")
}
return m.GetDelegatorWithdrawAddrFn(ctx, delAddr)
}

type mockWasmQueryKeeper struct {
GetContractInfoFn func(ctx sdk.Context, contractAddress sdk.AccAddress) *types.ContractInfo
QueryRawFn func(ctx sdk.Context, contractAddress sdk.AccAddress, key []byte) []byte
Expand Down
5 changes: 5 additions & 0 deletions x/wasm/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ type AccountKeeper interface {

// DistributionKeeper defines a subset of methods implemented by the cosmos-sdk distribution keeper
type DistributionKeeper interface {
<<<<<<< HEAD
DelegationRewards(c context.Context, req *types.QueryDelegationRewardsRequest) (*types.QueryDelegationRewardsResponse, error)
=======
DelegationRewards(ctx context.Context, req *distrtypes.QueryDelegationRewardsRequest) (*distrtypes.QueryDelegationRewardsResponse, error)
GetDelegatorWithdrawAddr(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress
>>>>>>> a17f5f2f (Add distribution query)
}

// StakingKeeper defines a subset of methods implemented by the cosmos-sdk staking keeper
Expand Down

0 comments on commit 17ed673

Please sign in to comment.