Skip to content

Commit

Permalink
resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoke committed Aug 29, 2024
2 parents 2cf18a8 + aec6c93 commit 04d5eb3
Show file tree
Hide file tree
Showing 9 changed files with 1,066 additions and 199 deletions.
21 changes: 21 additions & 0 deletions proto/interchain_security/ccv/provider/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ service Query {
option (google.api.http).get =
"/interchain_security/ccv/provider/consumer_id/{client_id}";
}

// QueryConsumerChain returns the consumer chain
// associated with the provided consumer id
rpc QueryConsumerChain(QueryConsumerChainRequest)
returns (QueryConsumerChainResponse) {
option (google.api.http).get =
"/interchain_security/ccv/provider/consumer_chain/{consumer_id}";
}
}

message QueryConsumerGenesisRequest {
Expand Down Expand Up @@ -382,3 +390,16 @@ message QueryConsumerIdFromClientIdResponse {
// the consumer id of the chain associated with this client id
string consumer_id = 1;
}

message QueryConsumerChainRequest {
string consumer_id = 1;
}

message QueryConsumerChainResponse {
string chain_id = 1;
string owner_address = 2;
string phase = 3;
ConsumerMetadata metadata = 4 [ (gogoproto.nullable) = false ];
ConsumerInitializationParameters init_params = 5;
PowerShapingParameters power_shaping_params = 6;
}
2 changes: 1 addition & 1 deletion tests/integration/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *CCVTestSuite) getFirstBundle() icstestingutils.ConsumerBundle {
}

func (s *CCVTestSuite) getBundleByIdx(index int) icstestingutils.ConsumerBundle {
return *s.consumerBundles[fmt.Sprintf("%d", 2+index)]
return *s.consumerBundles[fmt.Sprintf("%d", index)]
}

func (s *CCVTestSuite) providerCtx() sdk.Context {
Expand Down
7 changes: 3 additions & 4 deletions tests/integration/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ func (suite *CCVTestSuite) SetupTest() {
// 1. the consumer chain is added to the coordinator
// 2. MakeGenesis is called on the provider chain
// 3. ibc/testing sets the tendermint header for the consumer chain app

providerKeeper.SetConsumerPhase(suite.providerCtx(), icstestingutils.FirstConsumerId, providertypes.ConsumerPhase_CONSUMER_PHASE_INITIALIZED)
preProposalKeyAssignment(suite, icstestingutils.FirstConsumerId)
providerKeeper.SetConsumerPhase(suite.providerCtx(), icstestingutils.FirstConsumerID, providertypes.ConsumerPhase_CONSUMER_PHASE_INITIALIZED)
preProposalKeyAssignment(suite, icstestingutils.FirstConsumerID)

// start consumer chains
suite.consumerBundles = make(map[string]*icstestingutils.ConsumerBundle)
Expand Down Expand Up @@ -284,7 +283,7 @@ func initConsumerChain(
err = bundle.Path.EndpointA.UpdateClient()
s.Require().NoError(err)

if consumerId == "2" {
if consumerId == "0" {
// Support tests that were written before multiple consumers were supported.
firstBundle := s.getFirstBundle()
s.consumerApp = firstBundle.App
Expand Down
18 changes: 11 additions & 7 deletions testutil/ibc_testing/generic_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const (
)

var (
FirstConsumerId string
firstConsumerChainID string
FirstConsumerID string
provChainID string
democConsumerChainID string
consumerTopNParams [NumConsumers]uint32
Expand All @@ -47,7 +48,8 @@ var (
func init() {
// Disable revision format
ibctesting.ChainIDSuffix = ""
FirstConsumerId = "2"
firstConsumerChainID = ibctesting.GetChainID(2)
FirstConsumerID = ""
provChainID = ibctesting.GetChainID(1)
democConsumerChainID = ibctesting.GetChainID(5000)
// TopN parameter values per consumer chain initiated
Expand Down Expand Up @@ -153,7 +155,10 @@ func AddConsumer[Tp testutil.ProviderApp, Tc testutil.ConsumerApp](
powerShapingParameters := testkeeper.GetTestPowerShapingParameters()
powerShapingParameters.Top_N = consumerTopNParams[index] // isn't used in CreateConsumerClient

consumerId := fmt.Sprintf("%d", index+2)
consumerId := providerKeeper.FetchAndIncrementConsumerId(providerChain.GetContext())
if chainID == firstConsumerChainID {
FirstConsumerID = consumerId
}
providerKeeper.SetConsumerChainId(providerChain.GetContext(), consumerId, chainID)
providerKeeper.SetConsumerMetadata(providerChain.GetContext(), consumerId, consumerMetadata)
providerKeeper.SetConsumerInitializationParameters(providerChain.GetContext(), consumerId, initializationParameters)
Expand All @@ -180,14 +185,13 @@ func AddConsumer[Tp testutil.ProviderApp, Tc testutil.ConsumerApp](
providerChain.GetContext(),
consumerId,
)
s.Require().True(found, "consumer genesis not found in AddConsumer")

foo, found := providerKeeper.GetConsumerClientId(
_, found = providerKeeper.GetConsumerClientId(
providerChain.GetContext(),
consumerId,
)
_ = foo

s.Require().True(found, "consumer genesis not found in AddConsumer")
s.Require().True(found, "clientID not found in AddConsumer")

// use InitialValSet as the valset on the consumer
var valz []*tmtypes.Validator
Expand Down
45 changes: 7 additions & 38 deletions testutil/keeper/unit_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,18 @@ func GetTestConsumerMetadata() providertypes.ConsumerMetadata {
}

func GetTestInitializationParameters() providertypes.ConsumerInitializationParameters {
initialHeight := clienttypes.NewHeight(4, 5)
spawnTime := time.Now().UTC()
ccvTimeoutPeriod := types.DefaultCCVTimeoutPeriod
transferTimeoutPeriod := types.DefaultTransferTimeoutPeriod
unbondingPeriod := types.DefaultConsumerUnbondingPeriod
return providertypes.ConsumerInitializationParameters{
InitialHeight: initialHeight,
InitialHeight: clienttypes.NewHeight(4, 5),
GenesisHash: []byte("gen_hash"),
BinaryHash: []byte("bin_hash"),
SpawnTime: spawnTime,
SpawnTime: time.Now().UTC(),
ConsumerRedistributionFraction: types.DefaultConsumerRedistributeFrac,
BlocksPerDistributionTransmission: types.DefaultBlocksPerDistributionTransmission,
DistributionTransmissionChannel: "",
HistoricalEntries: types.DefaultHistoricalEntries,
CcvTimeoutPeriod: ccvTimeoutPeriod,
TransferTimeoutPeriod: transferTimeoutPeriod,
UnbondingPeriod: unbondingPeriod,
CcvTimeoutPeriod: types.DefaultCCVTimeoutPeriod,
TransferTimeoutPeriod: types.DefaultTransferTimeoutPeriod,
UnbondingPeriod: types.DefaultConsumerUnbondingPeriod,
}
}

Expand All @@ -309,37 +304,11 @@ func GetTestPowerShapingParameters() providertypes.PowerShapingParameters {
ValidatorSetCap: 0,
Allowlist: nil,
Denylist: nil,
MinStake: 0,
AllowInactiveVals: false,
}
}

func GetTestConsumerAdditionProp() *providertypes.ConsumerAdditionProposal {
prop := providertypes.NewConsumerAdditionProposal(
"chainID",
"description",
"chainID",
clienttypes.NewHeight(4, 5),
[]byte("gen_hash"),
[]byte("bin_hash"),
time.Now(),
types.DefaultConsumerRedistributeFrac,
types.DefaultBlocksPerDistributionTransmission,
"",
types.DefaultHistoricalEntries,
types.DefaultCCVTimeoutPeriod,
types.DefaultTransferTimeoutPeriod,
types.DefaultConsumerUnbondingPeriod,
0,
0,
0,
nil,
nil,
0,
false,
).(*providertypes.ConsumerAdditionProposal)

return prop
}

func GetTestMsgUpdateConsumer() providertypes.MsgUpdateConsumer {
return providertypes.MsgUpdateConsumer{
Signer: "signer",
Expand Down
46 changes: 46 additions & 0 deletions x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,49 @@ func (k Keeper) QueryConsumerIdFromClientId(goCtx context.Context, req *types.Qu

return &types.QueryConsumerIdFromClientIdResponse{ConsumerId: consumerId}, nil
}

// QueryConsumerChain returns the consumer chain associated with the consumer id
func (k Keeper) QueryConsumerChain(goCtx context.Context, req *types.QueryConsumerChainRequest) (*types.QueryConsumerChainResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

consumerId := req.ConsumerId
if err := types.ValidateConsumerId(consumerId); err != nil {
return nil, status.Error(codes.InvalidArgument, errorsmod.Wrap(types.ErrInvalidConsumerId, consumerId).Error())
}
ctx := sdk.UnwrapSDKContext(goCtx)

chainId, err := k.GetConsumerChainId(ctx, consumerId)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "cannot retrieve chain id for consumer id: %s", consumerId)
}

ownerAddress, err := k.GetConsumerOwnerAddress(ctx, consumerId)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "cannot retrieve owner address for consumer id: %s", consumerId)
}

phase := k.GetConsumerPhase(ctx, consumerId)
if phase == types.ConsumerPhase_CONSUMER_PHASE_UNSPECIFIED {
return nil, status.Errorf(codes.InvalidArgument, "cannot retrieve phase for consumer id: %s", consumerId)
}

metadata, err := k.GetConsumerMetadata(ctx, consumerId)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "cannot retrieve metadata for consumer id: %s", consumerId)
}

// neither the init nor the power shaping params are mandatory for consumers
initParams, _ := k.GetConsumerInitializationParameters(ctx, consumerId)
powerParams, _ := k.GetConsumerPowerShapingParameters(ctx, consumerId)

return &types.QueryConsumerChainResponse{
ChainId: chainId,
OwnerAddress: ownerAddress,
Phase: phase.String(),
Metadata: metadata,
InitParams: &initParams,
PowerShapingParams: &powerParams,
}, nil
}
79 changes: 70 additions & 9 deletions x/ccv/provider/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,6 @@ func TestQueryConsumerValidators(t *testing.T) {
res, err = pk.QueryConsumerValidators(ctx, &req)
require.NoError(t, err)
require.Equal(t, val1.Commission.Rate, res.Validators[0].ConsumerCommissionRate)

// set consumer to stopped phase
pk.SetConsumerPhase(ctx, consumerId, types.ConsumerPhase_CONSUMER_PHASE_STOPPED)
// expect empty valset
res, err = pk.QueryConsumerValidators(ctx, &req)
require.NoError(t, err)
require.Empty(t, res)
}

func TestQueryConsumerChainsValidatorHasToValidate(t *testing.T) {
Expand Down Expand Up @@ -444,8 +437,6 @@ func TestGetConsumerChain(t *testing.T) {
Top_N: topN,
ValidatorSetCap: validatorSetCaps[i],
ValidatorsPowerCap: validatorPowerCaps[i],
AllowInactiveVals: allowInactiveVals[i],
MinStake: minStakes[i].Uint64(),
})
pk.SetMinimumPowerInTopN(ctx, consumerID, expectedMinPowerInTopNs[i])
for _, addr := range allowlists[i] {
Expand Down Expand Up @@ -486,6 +477,76 @@ func TestGetConsumerChain(t *testing.T) {
}
}

func TestQueryConsumerChain(t *testing.T) {
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

consumerId := "0"
chainId := "consumer-1"

req := types.QueryConsumerChainRequest{
ConsumerId: consumerId,
}

// expect error when consumer isn't associated with a chain id
_, err := providerKeeper.QueryConsumerChain(ctx, &req)
require.Error(t, err)

providerKeeper.SetConsumerChainId(ctx, consumerId, chainId)

// expect error when consumer doesn't have an owner address set
_, err = providerKeeper.QueryConsumerChain(ctx, &req)
require.Error(t, err)

providerKeeper.SetConsumerOwnerAddress(ctx, consumerId, providerKeeper.GetAuthority())

// expect error when consumer doesn't have a valid phase
_, err = providerKeeper.QueryConsumerChain(ctx, &req)
require.Error(t, err)

providerKeeper.SetConsumerPhase(ctx, consumerId, types.ConsumerPhase_CONSUMER_PHASE_REGISTERED)

// expect error when consumer doesn't have metadata
_, err = providerKeeper.QueryConsumerChain(ctx, &req)
require.Error(t, err)

providerKeeper.SetConsumerMetadata(ctx, consumerId, types.ConsumerMetadata{Name: chainId})

expRes := types.QueryConsumerChainResponse{
ChainId: chainId,
OwnerAddress: providerKeeper.GetAuthority(),
Metadata: types.ConsumerMetadata{Name: chainId},
Phase: types.ConsumerPhase_CONSUMER_PHASE_REGISTERED.String(),
InitParams: &types.ConsumerInitializationParameters{},
PowerShapingParams: &types.PowerShapingParameters{},
}

// expect no error when neither the consumer init and power shaping params are set
res, err := providerKeeper.QueryConsumerChain(ctx, &req)
require.NoError(t, err)
require.Equal(t, &expRes, res)

providerKeeper.SetConsumerInitializationParameters(
ctx,
consumerId,
types.ConsumerInitializationParameters{SpawnTime: ctx.BlockTime()},
)

providerKeeper.SetConsumerPowerShapingParameters(
ctx,
consumerId,
types.PowerShapingParameters{Top_N: uint32(50)},
)

expRes.InitParams = &types.ConsumerInitializationParameters{SpawnTime: ctx.BlockTime()}
expRes.PowerShapingParams = &types.PowerShapingParameters{Top_N: uint32(50)}

// expect no error
res, err = providerKeeper.QueryConsumerChain(ctx, &req)
require.NoError(t, err)
require.Equal(t, &expRes, res)
}

func TestQueryConsumerIdFromClientId(t *testing.T) {
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
Expand Down
Loading

0 comments on commit 04d5eb3

Please sign in to comment.