Skip to content

Commit

Permalink
test: end point to get intents across all zones for a delegator address
Browse files Browse the repository at this point in the history
#466 (#468)

* front-port v1.2.13

* fix delegation flush test

* add tests for HandleDepositTransaction

* update tests for new error handling

* remove superflous field in proto

* final v1.2.13 front-port and test fixes

* s/s/suite/

* remove fmt.Printlns used for debugging

* update participation rewards and protocoldatas, to add validation, automatic key generation, basechain/basedenom in osmosisparams and updated tvl calculation

* front-port v1.2.13

* remove balancerPoolPretty json marshal/unmarshal, as we deal with raw KV store objects; switch to native bech32 decode/encode as we dont expect prefix of pools to match sdk.GetConfig().Bech32Prefix

* fix: allow support for multiple denoms from same zone in tvl calc

* lint

* fix: refactor utils/address.go to avoid import cycle when using in osmosis-types

* add test for randomutils

* fix: remove potential non-det by iterating over maps using sorted keys

* resolve outstanding nits

* lint

* fix nits

* bump go to 1.20.5

* Update x/interchainstaking/keeper/receipt.go

* Apply suggestions from code review

* lint

* bump hermes to v1.5.0

* update icq and relayer in test scripts

* copying

* Update x/interchainstaking/keeper/grpc_query.go

Co-authored-by: Alex Johnson <[email protected]>

* adding nil check

* proto format

---------

Co-authored-by: Joe Bowman <[email protected]>
Co-authored-by: Alex Johnson <[email protected]>
  • Loading branch information
3 people authored Jun 23, 2023
1 parent d060410 commit 27ac85c
Show file tree
Hide file tree
Showing 5 changed files with 1,035 additions and 137 deletions.
21 changes: 21 additions & 0 deletions proto/quicksilver/interchainstaking/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ service Query {
rpc DepositAccount(QueryDepositAccountForChainRequest) returns (QueryDepositAccountForChainResponse) {
option (google.api.http).get = "/quicksilver/interchainstaking/v1/zones/{chain_id}/deposit_address";
}

// DelegatorIntent provides data on the intent of the delegator for the given
// zone.
rpc DelegatorIntent(QueryDelegatorIntentRequest) returns (QueryDelegatorIntentResponse) {
Expand All @@ -37,6 +38,13 @@ service Query {
"{delegator_address}";
}

// DelegatorIntents provides data on the intent of the delegator for all zones
rpc DelegatorIntents(QueryDelegatorIntentsRequest) returns (QueryDelegatorIntentsResponse) {
option (google.api.http).get =
"/quicksilver/interchainstaking/v1/delegator_intents/"
"{delegator_address}";
}

// Delegations provides data on the delegations for the given zone.
rpc Delegations(QueryDelegationsRequest) returns (QueryDelegationsResponse) {
option (google.api.http).get = "/quicksilver/interchainstaking/v1/zones/{chain_id}/delegations";
Expand Down Expand Up @@ -143,6 +151,19 @@ message QueryDelegatorIntentResponse {
DelegatorIntent intent = 1;
}

message QueryDelegatorIntentsRequest {
string delegator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message DelegatorIntentsResponse {
string chain_id = 1;
DelegatorIntent intent = 2;
}

message QueryDelegatorIntentsResponse {
repeated DelegatorIntentsResponse intents = 1;
}

message QueryDelegationsRequest {
string chain_id = 1 [(gogoproto.moretags) = "yaml:\"chain_id\""];
cosmos.base.query.v1beta1.PageRequest pagination = 2;
Expand Down
21 changes: 21 additions & 0 deletions x/interchainstaking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ func (k *Keeper) DelegatorIntent(c context.Context, req *types.QueryDelegatorInt
return &types.QueryDelegatorIntentResponse{Intent: &intent}, nil
}

// DelegatorIntents returns information about the delegation intent of the given delegator for all zones.
func (k *Keeper) DelegatorIntents(c context.Context, req *types.QueryDelegatorIntentsRequest) (*types.QueryDelegatorIntentsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

intents := []*types.DelegatorIntentsResponse{}

k.IterateZones(ctx, func(_ int64, zone *types.Zone) bool {
intent, _ := k.GetDelegatorIntent(ctx, zone, req.DelegatorAddress, false)
if intent.Intents != nil {
intents = append(intents, &types.DelegatorIntentsResponse{ChainId: zone.ChainId, Intent: &intent})
}
return false
})

return &types.QueryDelegatorIntentsResponse{Intents: intents}, nil
}

func (k *Keeper) Delegations(c context.Context, req *types.QueryDelegationsRequest) (*types.QueryDelegationsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down
144 changes: 138 additions & 6 deletions x/interchainstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/ingenuity-build/quicksilver/x/interchainstaking/types"
)

var delegatorAddress = "quick16pxh2v4hr28h2gkntgfk8qgh47pfmjfhzgeure"

func (suite *KeeperTestSuite) TestKeeper_Zones() {
icsKeeper := suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper
ctx := suite.chainA.GetContext()
Expand Down Expand Up @@ -313,6 +315,136 @@ func (suite *KeeperTestSuite) TestKeeper_DelegatorIntent() {
}
}

func (suite *KeeperTestSuite) TestKeeper_DelegatorIntents() {
icsKeeper := suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper
ctx := suite.chainA.GetContext()

tests := []struct {
name string
malleate func()
req *types.QueryDelegatorIntentsRequest
wantErr bool
verify func(delegation []*types.DelegatorIntentsResponse)
}{
{
name: "DelegatorIntent_Nil_Request",
malleate: func() {},
req: nil,
wantErr: true,
verify: func([]*types.DelegatorIntentsResponse) {
},
},
{
"DelegatorIntent_No_Delegator_Intents",
func() {
},
&types.QueryDelegatorIntentsRequest{
DelegatorAddress: testAddress,
},
false,
func(intents []*types.DelegatorIntentsResponse) {
for _, intent := range intents {
suite.Require().Equal(intent.ChainId, suite.chainB.ChainID)
suite.Require().Equal(len(intent.Intent.Intents), 0)
}
},
},
{
"DelegatorIntent_Valid_Intents across multiple zones",
func() {
zone, found := icsKeeper.GetZone(ctx, suite.chainB.ChainID)
suite.Require().True(found)
// give funds
suite.giveFunds(ctx, zone.LocalDenom, 5000000, testAddress)
// set intents
intents := []types.DelegatorIntent{
{
Delegator: testAddress,
Intents: types.ValidatorIntents{
&types.ValidatorIntent{
ValoperAddress: icsKeeper.GetValidators(ctx, suite.chainB.ChainID)[0].ValoperAddress,
Weight: sdk.OneDec(),
},
},
},
}
for _, intent := range intents {
icsKeeper.SetDelegatorIntent(ctx, &zone, intent, false)
}

// cosmos zone
zone = types.Zone{
ConnectionId: "connection-77001",
ChainId: "cosmoshub-4",
AccountPrefix: "cosmos",
LocalDenom: "uqatom",
BaseDenom: "uatom",
MultiSend: false,
LiquidityModule: false,
Is_118: true,
}
(&icsKeeper).SetZone(ctx, &zone)
// give funds
suite.giveFunds(ctx, zone.LocalDenom, 5000000, testAddress)
// set intents
intents = []types.DelegatorIntent{
{
Delegator: testAddress,
Intents: types.ValidatorIntents{
&types.ValidatorIntent{
ValoperAddress: icsKeeper.GetValidators(ctx, suite.chainB.ChainID)[0].ValoperAddress,
Weight: sdk.OneDec(),
},
},
},
}
for _, intent := range intents {
icsKeeper.SetDelegatorIntent(ctx, &zone, intent, false)
}
},
&types.QueryDelegatorIntentsRequest{
DelegatorAddress: testAddress,
},
false,
func(intents []*types.DelegatorIntentsResponse) {
suite.Require().Equal(len(intents), 2)
suite.Require().Equal(intents[0].ChainId, "cosmoshub-4")
suite.Require().Equal(intents[1].ChainId, suite.chainB.ChainID)
for _, intent := range intents {
suite.Require().Equal(intent.Intent.Delegator, testAddress)
suite.Require().Equal(len(intent.Intent.Intents), 1)
}
},
},
}

// run tests:
suite.setupTestZones()

for _, tt := range tests {
suite.Run(tt.name, func() {
tt.malleate()
resp, err := icsKeeper.DelegatorIntents(
ctx,
tt.req,
)
if tt.wantErr {
suite.T().Logf("Error:\n%v\n", err)
suite.Require().Error(err)
return
}
suite.Require().NoError(err)
suite.Require().NotNil(resp)
tt.verify(resp.Intents)

vstr, err := json.MarshalIndent(resp, "", "\t")
suite.Require().NoError(err)

suite.T().Logf("Response:\n%s\n", vstr)
})
}
}

func (suite *KeeperTestSuite) TestKeeper_Delegations() {
icsKeeper := suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper
ctx := suite.chainA.GetContext()
Expand Down Expand Up @@ -525,7 +657,7 @@ func (suite *KeeperTestSuite) TestKeeper_ZoneWithdrawalRecords() {
func() {},
&types.QueryWithdrawalRecordsRequest{
ChainId: suite.chainB.ChainID,
DelegatorAddress: "quick16pxh2v4hr28h2gkntgfk8qgh47pfmjfhzgeure",
DelegatorAddress: delegatorAddress,
},
false,
0,
Expand All @@ -551,7 +683,7 @@ func (suite *KeeperTestSuite) TestKeeper_ZoneWithdrawalRecords() {
icsKeeper.AddWithdrawalRecord(
ctx,
zone.ChainId,
"quick16pxh2v4hr28h2gkntgfk8qgh47pfmjfhzgeure",
delegatorAddress,
distribution,
testAddress,
sdk.NewCoins(sdk.NewCoin(zone.BaseDenom, math.NewInt(15000000))),
Expand All @@ -563,7 +695,7 @@ func (suite *KeeperTestSuite) TestKeeper_ZoneWithdrawalRecords() {
},
&types.QueryWithdrawalRecordsRequest{
ChainId: suite.chainB.ChainID,
DelegatorAddress: "quick16pxh2v4hr28h2gkntgfk8qgh47pfmjfhzgeure",
DelegatorAddress: delegatorAddress,
},
false,
1,
Expand Down Expand Up @@ -655,7 +787,7 @@ func (suite *KeeperTestSuite) TestKeeper_UserWithdrawalRecords() {
icsKeeper.AddWithdrawalRecord(
ctx,
zone.ChainId,
"quick16pxh2v4hr28h2gkntgfk8qgh47pfmjfhzgeure",
delegatorAddress,
distribution,
testAddress,
sdk.NewCoins(sdk.NewCoin(zone.BaseDenom, math.NewInt(15000000))),
Expand All @@ -666,7 +798,7 @@ func (suite *KeeperTestSuite) TestKeeper_UserWithdrawalRecords() {
)
},
&types.QueryUserWithdrawalRecordsRequest{
UserAddress: "quick16pxh2v4hr28h2gkntgfk8qgh47pfmjfhzgeure",
UserAddress: delegatorAddress,
},
false,
1,
Expand Down Expand Up @@ -747,7 +879,7 @@ func (suite *KeeperTestSuite) TestKeeper_WithdrawalRecords() {
icsKeeper.AddWithdrawalRecord(
ctx,
zone.ChainId,
"quick16pxh2v4hr28h2gkntgfk8qgh47pfmjfhzgeure",
delegatorAddress,
distribution,
testAddress,
sdk.NewCoins(sdk.NewCoin(zone.BaseDenom, math.NewInt(15000000))),
Expand Down
Loading

0 comments on commit 27ac85c

Please sign in to comment.