Skip to content

Commit

Permalink
Merge pull request #1228 from lavanet/CNS-871-iprpc-queries
Browse files Browse the repository at this point in the history
CNS-871: IPRPC part 4 - Queries
  • Loading branch information
Yaroms authored Mar 6, 2024
2 parents 227d6b8 + 4154781 commit 452620c
Show file tree
Hide file tree
Showing 30 changed files with 2,265 additions and 215 deletions.
32 changes: 32 additions & 0 deletions proto/lavanet/lava/rewards/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "lavanet/lava/rewards/params.proto";
import "lavanet/lava/rewards/iprpc.proto";
import "cosmos/base/v1beta1/coin.proto";
// this line is used by starport scaffolding # 1

Expand All @@ -31,11 +32,21 @@ service Query {
rpc ShowIprpcData(QueryShowIprpcDataRequest) returns (QueryShowIprpcDataResponse) {
option (google.api.http).get = "/lavanet/lava/rewards/show_iprpc_data";
}

// ProviderReward queries for the providers reward for their services
rpc ProviderReward(QueryProviderRewardRequest) returns (QueryProviderRewardResponse) {
option (google.api.http).get = "/lavanet/lava/rewards/provider_reward";
}

// IprpcProviderRewardEstimation queries for a provider's current IPRPC reward (relative to its serviced CU)
rpc IprpcProviderRewardEstimation(QueryIprpcProviderRewardEstimationRequest) returns (QueryIprpcProviderRewardEstimationResponse) {
option (google.api.http).get = "/lavanet/lava/rewards/iprpc_provider_reward/{provider}";
}

// IprpcSpecReward queries for a spec's IPRPC reward
rpc IprpcSpecReward(QueryIprpcSpecRewardRequest) returns (QueryIprpcSpecRewardResponse) {
option (google.api.http).get = "/lavanet/lava/rewards/iprpc_spec_reward/{spec}";
}
// this line is used by starport scaffolding # 2
}

Expand Down Expand Up @@ -102,4 +113,25 @@ message QueryProviderRewardResponse {
repeated RewardInfo rewards = 1 [(gogoproto.nullable) = false];
}

// QueryIprpcProviderRewardEstimationRequest is request type for the Query/IprpcProviderRewardEstimation RPC method.
message QueryIprpcProviderRewardEstimationRequest {
string provider = 1;
}

// QueryIprpcProviderRewardEstimationResponse is response type for the Query/IprpcProviderRewardEstimation RPC method.
message QueryIprpcProviderRewardEstimationResponse {
repeated Specfund spec_funds = 1 [(gogoproto.nullable) = false];
}

// QueryIprpcSpecRewardRequest is request type for the Query/IprpcSpecReward RPC method.
message QueryIprpcSpecRewardRequest {
string spec = 1;
}

// QueryIprpcSpecRewardResponse is response type for the Query/IprpcSpecReward RPC method.
message QueryIprpcSpecRewardResponse {
repeated IprpcReward iprpc_rewards = 1 [(gogoproto.nullable) = false];
uint64 current_month_id = 2;
}

// this line is used by starport scaffolding # 3
2 changes: 2 additions & 0 deletions scripts/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ echo "Testing rewards q commands"
trace lavad q rewards pools >/dev/null
trace lavad q rewards block-reward >/dev/null
trace lavad q rewards show-iprpc-data > /dev/null
trace lavad q rewards iprpc-provider-reward > /dev/null
trace lavad q rewards iprpc-spec-reward > /dev/null
trace lavad q rewards provider-reward >/dev/null

echo "Testing events command"
Expand Down
30 changes: 27 additions & 3 deletions testutil/common/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ func (ts *Tester) GetBalance(accAddr sdk.AccAddress) int64 {
return ts.Keepers.BankKeeper.GetBalance(ts.Ctx, accAddr, denom).Amount.Int64()
}

func (ts *Tester) GetBalances(accAddr sdk.AccAddress) sdk.Coins {
return ts.Keepers.BankKeeper.GetAllBalances(ts.Ctx, accAddr)
}

func (ts *Tester) FindPlan(index string, block uint64) (planstypes.Plan, bool) {
return ts.Keepers.Plans.FindPlan(ts.Ctx, index, block)
}
Expand Down Expand Up @@ -610,9 +614,14 @@ func (ts *Tester) TxPairingUnfreezeProvider(addr, chainID string) (*pairingtypes
return ts.Servers.PairingServer.UnfreezeProvider(ts.GoCtx, msg)
}

func (ts *Tester) TxRewardsSetIprpcDataProposal(ctx sdk.Context, authority string, cost sdk.Coin, subs []string) (*rewardstypes.MsgSetIprpcDataResponse, error) {
func (ts *Tester) TxRewardsSetIprpcDataProposal(authority string, cost sdk.Coin, subs []string) (*rewardstypes.MsgSetIprpcDataResponse, error) {
msg := rewardstypes.NewMsgSetIprpcData(authority, cost, subs)
return ts.Servers.RewardsServer.SetIprpcData(sdk.WrapSDKContext(ctx), msg)
return ts.Servers.RewardsServer.SetIprpcData(ts.GoCtx, msg)
}

func (ts *Tester) TxRewardsFundIprpc(creator string, spec string, duration uint64, fund sdk.Coins) (*rewardstypes.MsgFundIprpcResponse, error) {
msg := rewardstypes.NewMsgFundIprpc(creator, spec, duration, fund)
return ts.Servers.RewardsServer.FundIprpc(ts.GoCtx, msg)
}

// TxCreateValidator: implement 'tx staking createvalidator' and bond its tokens
Expand Down Expand Up @@ -859,11 +868,26 @@ func (ts *Tester) QueryRewardsBlockReward() (*rewardstypes.QueryBlockRewardRespo
return ts.Keepers.Rewards.BlockReward(ts.GoCtx, msg)
}

func (ts *Tester) QueryShowIprpcData() (*rewardstypes.QueryShowIprpcDataResponse, error) {
// QueryRewardsShowIprpcData implements 'q rewards show-iprpc-data'
func (ts *Tester) QueryRewardsShowIprpcData() (*rewardstypes.QueryShowIprpcDataResponse, error) {
msg := &rewardstypes.QueryShowIprpcDataRequest{}
return ts.Keepers.Rewards.ShowIprpcData(ts.GoCtx, msg)
}

func (ts *Tester) QueryRewardsIprpcProviderRewardEstimation(provider string) (*rewardstypes.QueryIprpcProviderRewardEstimationResponse, error) {
msg := &rewardstypes.QueryIprpcProviderRewardEstimationRequest{
Provider: provider,
}
return ts.Keepers.Rewards.IprpcProviderRewardEstimation(ts.GoCtx, msg)
}

func (ts *Tester) QueryRewardsIprpcSpecReward(spec string) (*rewardstypes.QueryIprpcSpecRewardResponse, error) {
msg := &rewardstypes.QueryIprpcSpecRewardRequest{
Spec: spec,
}
return ts.Keepers.Rewards.IprpcSpecReward(ts.GoCtx, msg)
}

// block/epoch helpers
// QueryRewardsProviderReward implements 'q rewards provider-reward'
func (ts *Tester) QueryRewardsProviderReward(chainID string, provider string) (*rewardstypes.QueryProviderRewardResponse, error) {
Expand Down
43 changes: 5 additions & 38 deletions testutil/keeper/mock_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,9 @@ func (k mockBankKeeper) GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk
}

func (k mockBankKeeper) SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error {
// TODO support multiple coins
moduleAcc := GetModuleAddress(recipientModule)
if amt.Len() > 1 {
return fmt.Errorf("mockbankkeeper dont support more than 1 coin")
}
coin := amt[0]

accountCoin := k.GetBalance(ctx, senderAddr, coin.Denom)
if coin.Amount.GT(accountCoin.Amount) {
accountCoins := k.GetAllBalances(ctx, senderAddr)
if !accountCoins.IsAllGTE(amt) {
return fmt.Errorf("not enough coins")
}

Expand All @@ -99,47 +93,20 @@ func (k mockBankKeeper) UndelegateCoinsFromModuleToAccount(ctx sdk.Context, send
}

func (k mockBankKeeper) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
// TODO support multiple coins
moduleAcc := GetModuleAddress(senderModule)

if amt.Len() > 1 {
return fmt.Errorf("mockbankkeeper doesn't support more than 1 coin")
}
coin := amt[0]

accountCoin := k.GetBalance(ctx, moduleAcc, coin.Denom)
if coin.Amount.GT(accountCoin.Amount) {
accountCoins := k.GetAllBalances(ctx, moduleAcc)
if !accountCoins.IsAllGTE(amt) {
return fmt.Errorf("not enough coins")
}

k.SubFromBalance(moduleAcc, amt)

k.AddToBalance(recipientAddr, amt)

return nil
}

func (k mockBankKeeper) SendCoinsFromModuleToModule(ctx sdk.Context, senderModule string, recipientModule string, amt sdk.Coins) error {
// TODO support multiple coins

senderModuleAcc := GetModuleAddress(senderModule)
recipientModuleAcc := GetModuleAddress(recipientModule)

if amt.Len() > 1 {
return fmt.Errorf("mockbankkeeper doesn't support more than 1 coin")
}
coin := amt[0]

senderAccountCoin := k.GetBalance(ctx, senderModuleAcc, coin.Denom)
if coin.Amount.GT(senderAccountCoin.Amount) {
return fmt.Errorf("not enough coins")
}

k.SubFromBalance(senderModuleAcc, amt)

k.AddToBalance(recipientModuleAcc, amt)

return nil
return k.SendCoinsFromAccountToModule(ctx, senderModuleAcc, recipientModule, amt)
}

func (k mockBankKeeper) DelegateCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error {
Expand Down
2 changes: 1 addition & 1 deletion x/pairing/client/cli/query_effective_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func CmdEffectivePolicy() *cobra.Command {
if len(args) > 1 {
address = args[1]
} else {
clientCtxForTx, err := client.GetClientTxContext(cmd)
clientCtxForTx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
Expand Down
9 changes: 2 additions & 7 deletions x/protocol/types/message_set_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@ func TestSetVersion_ValidateBasic(t *testing.T) {
name: "invalid authority address",
msg: MsgSetVersion{
Authority: "invalid_address",
},
valid: false,
},
{
name: "invalid subscription address",
msg: MsgSetVersion{
Authority: sample.AccAddress(),
Version: &DefaultGenesis().Params.Version,
},
valid: false,
},
{
name: "valid message",
msg: MsgSetVersion{
Authority: sample.AccAddress(),
Version: &DefaultGenesis().Params.Version,
},
valid: true,
},
Expand Down
2 changes: 2 additions & 0 deletions x/rewards/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdQueryPools())
cmd.AddCommand(CmdQueryBlockReward())
cmd.AddCommand(CmdQueryShowIprpcData())
cmd.AddCommand(CmdQueryIprpcProviderRewardEstimation())
cmd.AddCommand(CmdQueryIprpcSpecReward())
cmd.AddCommand(CmdQueryProviderReward())
// this line is used by starport scaffolding # 1

Expand Down
2 changes: 1 addition & 1 deletion x/rewards/client/cli/query_block_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func CmdQueryBlockReward() *cobra.Command {
Short: "Query for amount of validator rewards for proposing a block",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
Expand Down
43 changes: 43 additions & 0 deletions x/rewards/client/cli/query_iprpc_provider_reward_estimation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/lavanet/lava/x/rewards/types"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdQueryIprpcProviderRewardEstimation() *cobra.Command {
cmd := &cobra.Command{
Use: "iprpc-provider-reward-estimation [provider]",
Short: "Query for current estimation of IPRPC reward for a specific provider",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryIprpcProviderRewardEstimationRequest{
Provider: args[0],
}

res, err := queryClient.IprpcProviderRewardEstimation(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
48 changes: 48 additions & 0 deletions x/rewards/client/cli/query_iprpc_spec_reward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/lavanet/lava/x/rewards/types"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdQueryIprpcSpecReward() *cobra.Command {
cmd := &cobra.Command{
Use: "iprpc-spec-reward {spec}",
Short: "Query for IPRPC rewards for a specific spec. If no spec is given, all IPRPC rewards will be shown",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
var spec string
if len(args) > 0 {
spec = args[0]
}

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryIprpcSpecRewardRequest{
Spec: spec,
}

res, err := queryClient.IprpcSpecReward(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
6 changes: 4 additions & 2 deletions x/rewards/client/cli/query_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ func CmdQueryParams() *cobra.Command {
Short: "shows the parameters of the module",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
Expand Down
2 changes: 1 addition & 1 deletion x/rewards/client/cli/query_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func CmdQueryPools() *cobra.Command {
Short: "Query for validators and providers rewards pools information",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/rewards/client/cli/query_show_iprpc_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func CmdQueryShowIprpcData() *cobra.Command {
Short: "Query for IPRPC data: min cost and IPRPC eligible subscriptions",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/rewards/keeper/base_pay.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (k Keeper) SetAllBasePay(ctx sdk.Context, list []types.BasePayGenesis) {
}
}

func (k Keeper) GetAllBasePayForChain(ctx sdk.Context, chainID string, provider string) (list []types.BasePayWithIndex) {
func (k Keeper) getAllBasePayForChain(ctx sdk.Context, chainID string, provider string) (list []types.BasePayWithIndex) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BasePayPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte(types.BasePayIndex{ChainID: chainID, Provider: provider}.String()))

Expand Down
Loading

0 comments on commit 452620c

Please sign in to comment.