Skip to content

Commit

Permalink
updates based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Aug 13, 2024
1 parent e5e4867 commit b195a19
Show file tree
Hide file tree
Showing 21 changed files with 974 additions and 817 deletions.
6 changes: 0 additions & 6 deletions proto/interchain_security/ccv/provider/v1/provider.proto
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,6 @@ message ConsumerRemovalProposals {
// AddressList contains a list of consensus addresses
message AddressList { repeated bytes addresses = 1; }

// ChannelToChain is used to map a CCV channel ID to the consumer chainID
message ChannelToChain {
string channel_id = 1;
string chain_id = 2;
}

// ValidatorSetChangePackets is a pb list of ccv.ValidatorSetChangePacketData.
message ValidatorSetChangePackets {
repeated interchain_security.ccv.v1.ValidatorSetChangePacketData list = 1
Expand Down
8 changes: 5 additions & 3 deletions proto/interchain_security/ccv/provider/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,14 @@ message MsgRegisterConsumer {

// ConsumerRegistrationRecord is the record that contains information for the registered chain
message ConsumerRegistrationRecord {
// the title of the chain to-be-registered
// the title of the chain that is to be registered
string title = 1;
// the description of the chain to-be-registered
// the description of the chain that is to be registered
string description = 2;
// the metadata (e.g., GitHub repository URL) of the chain that is to be registered
string metadata = 3;
// the chain id of the new consumer chain
string chain_id = 3;
string chain_id = 4;
}

// MsgRegisterConsumerResponse defines response type for MsgRegisterConsumer
Expand Down
8 changes: 4 additions & 4 deletions testutil/keeper/unit_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ func SetupForStoppingConsumerChain(t *testing.T, ctx sdk.Context,

gomock.InOrder(expectations...)

providerKeeper.SetConsumerIdToRegistrationRecord(ctx, consumerId, GetTestRegistrationRecord())
providerKeeper.SetConsumerIdToInitializationRecord(ctx, consumerId, GetTestInitializationRecord())
providerKeeper.SetConsumerIdToUpdateRecord(ctx, consumerId, GetTestUpdateRecord())
providerKeeper.SetConsumerIdToPhase(ctx, consumerId, providerkeeper.Initialized)
providerKeeper.SetConsumerRegistrationRecord(ctx, consumerId, GetTestRegistrationRecord())
providerKeeper.SetConsumerInitializationRecord(ctx, consumerId, GetTestInitializationRecord())
providerKeeper.SetConsumerUpdateRecord(ctx, consumerId, GetTestUpdateRecord())
providerKeeper.SetConsumerPhase(ctx, consumerId, providerkeeper.Initialized)

err := providerKeeper.CreateConsumerClient(ctx, consumerId)
require.NoError(t, err)
Expand Down
22 changes: 9 additions & 13 deletions x/ccv/provider/keeper/consumer_equivocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,13 @@ func (k Keeper) HandleConsumerDoubleVoting(
}

// get the chainId of this consumer chain to verify the double-voting evidence
chainId, found := k.GetConsumerIdToRegistrationRecord(ctx, consumerId)
if !found {
return errorsmod.Wrapf(
ccvtypes.ErrInvalidDoubleVotingEvidence,
"could not find the chain id of the consumer chain with consuemr id: %s",
consumerId,
)
chainId, err := k.GetConsumerRegistrationRecord(ctx, consumerId)
if err != nil {
return err
}

// verifies the double voting evidence using the consumer chain public key
if err := k.VerifyDoubleVotingEvidence(*evidence, chainId.ChainId, pubkey); err != nil {
if err = k.VerifyDoubleVotingEvidence(*evidence, chainId.ChainId, pubkey); err != nil {
return err
}

Expand All @@ -79,10 +75,10 @@ func (k Keeper) HandleConsumerDoubleVoting(
types.NewConsumerConsAddress(sdk.ConsAddress(evidence.VoteA.ValidatorAddress.Bytes())),
)

if err := k.SlashValidator(ctx, providerAddr); err != nil {
if err = k.SlashValidator(ctx, providerAddr); err != nil {
return err
}
if err := k.JailAndTombstoneValidator(ctx, providerAddr); err != nil {
if err = k.JailAndTombstoneValidator(ctx, providerAddr); err != nil {
return err
}

Expand Down Expand Up @@ -305,9 +301,9 @@ func headerToLightBlock(h ibctmtypes.Header) (*tmtypes.LightBlock, error) {
func (k Keeper) CheckMisbehaviour(ctx sdk.Context, consumerId string, misbehaviour ibctmtypes.Misbehaviour) error {
chainId := misbehaviour.Header1.Header.ChainID

registrationRecord, found := k.GetConsumerIdToRegistrationRecord(ctx, consumerId)
if !found {
return fmt.Errorf("cannot find registration record of consumer chain (consumerId): %s", consumerId)
registrationRecord, err := k.GetConsumerRegistrationRecord(ctx, consumerId)
if err != nil {
return err
} else if registrationRecord.ChainId != chainId {
return fmt.Errorf("incorrect misbheaviour for a different chain id (%s) than that of the consumer chain %s (consumerId): %s",
chainId,
Expand Down
35 changes: 21 additions & 14 deletions x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ func (k Keeper) QueryConsumerGenesis(c context.Context, req *types.QueryConsumer
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

if req.ConsumerId == "" {
return nil, status.Errorf(codes.InvalidArgument, "invalid request: consumer id cannot be empty")
consumerId := req.ConsumerId
if err := types.ValidateConsumerId(consumerId); err != nil {
return nil, status.Error(codes.InvalidArgument, errorsmod.Wrap(types.ErrInvalidConsumerId, consumerId).Error())
}

gen, ok := k.GetConsumerGenesis(ctx, req.ConsumerId)
gen, ok := k.GetConsumerGenesis(ctx, consumerId)
if !ok {
return nil, status.Error(
codes.NotFound,
errorsmod.Wrap(types.ErrUnknownConsumerId, req.ConsumerId).Error(),
errorsmod.Wrap(types.ErrUnknownConsumerId, consumerId).Error(),
)
}

Expand Down Expand Up @@ -146,13 +147,18 @@ func (k Keeper) QueryValidatorConsumerAddr(goCtx context.Context, req *types.Que

ctx := sdk.UnwrapSDKContext(goCtx)

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

providerAddrTmp, err := sdk.ConsAddressFromBech32(req.ProviderAddress)
if err != nil {
return nil, err
}
providerAddr := types.NewProviderConsAddress(providerAddrTmp)

consumerKey, found := k.GetValidatorConsumerPubKey(ctx, req.ConsumerId, providerAddr)
consumerKey, found := k.GetValidatorConsumerPubKey(ctx, consumerId, providerAddr)
if !found {
return &types.QueryValidatorConsumerAddrResponse{}, nil
}
Expand Down Expand Up @@ -241,15 +247,16 @@ func (k Keeper) QueryAllPairsValConAddrByConsumerChainID(goCtx context.Context,
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if req.ConsumerId == "" {
return nil, status.Errorf(codes.InvalidArgument, "invalid request: consumer id cannot be empty")
consumerId := req.ConsumerId
if err := types.ValidateConsumerId(consumerId); err != nil {
return nil, status.Error(codes.InvalidArgument, errorsmod.Wrap(types.ErrInvalidConsumerId, consumerId).Error())
}

// list of pairs valconsensus addr <providerValConAddrs : consumerValConAddrs>
pairValConAddrs := []*types.PairValConAddrProviderAndConsumer{}

ctx := sdk.UnwrapSDKContext(goCtx)
validatorConsumerPubKeys := k.GetAllValidatorConsumerPubKeys(ctx, &req.ConsumerId)
validatorConsumerPubKeys := k.GetAllValidatorConsumerPubKeys(ctx, &consumerId)
for _, data := range validatorConsumerPubKeys {
consumerAddr, err := ccvtypes.TMCryptoPublicKeyToConsAddr(*data.ConsumerKey)
if err != nil {
Expand Down Expand Up @@ -286,8 +293,8 @@ func (k Keeper) QueryConsumerChainOptedInValidators(goCtx context.Context, req *
}

consumerId := req.ConsumerId
if consumerId == "" {
return nil, status.Error(codes.InvalidArgument, "empty consumer id")
if err := types.ValidateConsumerId(consumerId); err != nil {
return nil, status.Error(codes.InvalidArgument, errorsmod.Wrap(types.ErrInvalidConsumerId, consumerId).Error())
}

optedInVals := []string{}
Expand All @@ -313,8 +320,8 @@ func (k Keeper) QueryConsumerValidators(goCtx context.Context, req *types.QueryC
}

consumerId := req.ConsumerId
if consumerId == "" {
return nil, status.Error(codes.InvalidArgument, "empty consumer id")
if err := types.ValidateConsumerId(consumerId); err != nil {
return nil, status.Error(codes.InvalidArgument, errorsmod.Wrap(types.ErrInvalidConsumerId, consumerId).Error())
}

ctx := sdk.UnwrapSDKContext(goCtx)
Expand Down Expand Up @@ -430,8 +437,8 @@ func (k Keeper) QueryValidatorConsumerCommissionRate(goCtx context.Context, req
}

consumerId := req.ConsumerId
if consumerId == "" {
return nil, status.Error(codes.InvalidArgument, "empty chainId")
if err := types.ValidateConsumerId(consumerId); err != nil {
return nil, status.Error(codes.InvalidArgument, errorsmod.Wrap(types.ErrInvalidConsumerId, consumerId).Error())
}

consAddr, err := sdk.ConsAddressFromBech32(req.ProviderAddress)
Expand Down
45 changes: 22 additions & 23 deletions x/ccv/provider/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func TestQueryAllPairsValConAddrByConsumerChainID(t *testing.T) {
chainID := consumer
consumerId := "0"

providerConsAddress, err := sdk.ConsAddressFromBech32("cosmosvalcons1wpex7anfv3jhystyv3eq20r35a")
require.NoError(t, err)
Expand All @@ -34,9 +34,9 @@ func TestQueryAllPairsValConAddrByConsumerChainID(t *testing.T) {
pk, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

pk.SetValidatorConsumerPubKey(ctx, chainID, providerAddr, consumerKey)
pk.SetValidatorConsumerPubKey(ctx, consumerId, providerAddr, consumerKey)

consumerPubKey, found := pk.GetValidatorConsumerPubKey(ctx, chainID, providerAddr)
consumerPubKey, found := pk.GetValidatorConsumerPubKey(ctx, consumerId, providerAddr)
require.True(t, found, "consumer pubkey not found")
require.NotEmpty(t, consumerPubKey, "consumer pubkey is empty")
require.Equal(t, consumerPubKey, consumerKey)
Expand All @@ -50,12 +50,11 @@ func TestQueryAllPairsValConAddrByConsumerChainID(t *testing.T) {
require.Error(t, err)

// Request with chainId is invalid
response, err := pk.QueryAllPairsValConAddrByConsumerChainID(ctx, &types.QueryAllPairsValConAddrByConsumerChainIDRequest{ConsumerId: "invalidChainId"})
require.NoError(t, err)
require.Equal(t, []*types.PairValConAddrProviderAndConsumer{}, response.PairValConAddr)
response, err := pk.QueryAllPairsValConAddrByConsumerChainID(ctx, &types.QueryAllPairsValConAddrByConsumerChainIDRequest{ConsumerId: "invalidConsumerId"})
require.Error(t, err)

// Request is valid
response, err = pk.QueryAllPairsValConAddrByConsumerChainID(ctx, &types.QueryAllPairsValConAddrByConsumerChainIDRequest{ConsumerId: chainID})
response, err = pk.QueryAllPairsValConAddrByConsumerChainID(ctx, &types.QueryAllPairsValConAddrByConsumerChainIDRequest{ConsumerId: consumerId})
require.NoError(t, err)

expectedResult := types.PairValConAddrProviderAndConsumer{
Expand All @@ -68,42 +67,42 @@ func TestQueryAllPairsValConAddrByConsumerChainID(t *testing.T) {
}

func TestQueryConsumerChainOptedInValidators(t *testing.T) {
chainID := "chainID"

pk, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

consumerId := "0"

req := types.QueryConsumerChainOptedInValidatorsRequest{
ConsumerId: chainID,
ConsumerId: consumerId,
}

// error returned from not yet proposed or not yet registered chain
_, err := pk.QueryConsumerChainOptedInValidators(ctx, &req)
require.Error(t, err)

pk.SetProposedConsumerChain(ctx, chainID, 1)
pk.SetProposedConsumerChain(ctx, consumerId, 1)

providerAddr1 := types.NewProviderConsAddress([]byte("providerAddr1"))
providerAddr2 := types.NewProviderConsAddress([]byte("providerAddr2"))
expectedResponse := types.QueryConsumerChainOptedInValidatorsResponse{
ValidatorsProviderAddresses: []string{providerAddr1.String(), providerAddr2.String()},
}

pk.SetOptedIn(ctx, chainID, providerAddr1)
pk.SetOptedIn(ctx, chainID, providerAddr2)
pk.SetOptedIn(ctx, consumerId, providerAddr1)
pk.SetOptedIn(ctx, consumerId, providerAddr2)
res, err := pk.QueryConsumerChainOptedInValidators(ctx, &req)
require.NoError(t, err)
require.Equal(t, &expectedResponse, res)
}

func TestQueryConsumerValidators(t *testing.T) {
chainID := "chainID"

pk, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

consumerId := "0"

req := types.QueryConsumerValidatorsRequest{
ConsumerId: chainID,
ConsumerId: consumerId,
}

// error returned from not-started chain
Expand All @@ -126,8 +125,8 @@ func TestQueryConsumerValidators(t *testing.T) {
}

// set up the client id so the chain looks like it "started"
pk.SetConsumerClientId(ctx, chainID, "clientID")
pk.SetConsumerValSet(ctx, chainID, []types.ConsensusValidator{consumerValidator1, consumerValidator2})
pk.SetConsumerClientId(ctx, consumerId, "clientID")
pk.SetConsumerValSet(ctx, consumerId, []types.ConsensusValidator{consumerValidator1, consumerValidator2})

res, err := pk.QueryConsumerValidators(ctx, &req)
require.NoError(t, err)
Expand Down Expand Up @@ -183,30 +182,30 @@ func TestQueryConsumerChainsValidatorHasToValidate(t *testing.T) {
}

func TestQueryValidatorConsumerCommissionRate(t *testing.T) {
chainID := "chainID"
consumerId := "0"

pk, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

providerAddr := types.NewProviderConsAddress([]byte("providerAddr"))
req := types.QueryValidatorConsumerCommissionRateRequest{
ConsumerId: chainID,
ConsumerId: consumerId,
ProviderAddress: providerAddr.String(),
}

// error returned from not yet proposed or not yet registered chain
_, err := pk.QueryValidatorConsumerCommissionRate(ctx, &req)
require.Error(t, err)

pk.SetProposedConsumerChain(ctx, chainID, 1)
pk.SetProposedConsumerChain(ctx, consumerId, 1)
// validator with set consumer commission rate
expectedCommissionRate := math.LegacyMustNewDecFromStr("0.123")
pk.SetConsumerCommissionRate(ctx, chainID, providerAddr, expectedCommissionRate)
pk.SetConsumerCommissionRate(ctx, consumerId, providerAddr, expectedCommissionRate)
res, _ := pk.QueryValidatorConsumerCommissionRate(ctx, &req)
require.Equal(t, expectedCommissionRate, res.Rate)

// validator with no set consumer commission rate
pk.DeleteConsumerCommissionRate(ctx, chainID, providerAddr)
pk.DeleteConsumerCommissionRate(ctx, consumerId, providerAddr)
expectedCommissionRate = math.LegacyMustNewDecFromStr("0.456")

// because no consumer commission rate is set, the validator's set commission rate on the provider is used
Expand Down
24 changes: 15 additions & 9 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,30 +314,36 @@ func (k Keeper) DeleteChannelIdToConsumerId(ctx sdk.Context, channelId string) {
store.Delete(types.ChannelToConsumerIdKey(channelId))
}

// GetAllChannelToChains gets all channel to chain mappings. If a mapping exists,
// GetAllChannelToConsumers gets all channel to chain mappings. If a mapping exists,
// then the CCV channel to that consumer chain is established.
//
// Note that mapping from CCV channel IDs to consumer chainIDs
// Note that mapping from CCV channel IDs to consumer IDs
// is stored under keys with the following format:
// ChannelIdToConsumerIdKeyPrefix | channelID
// Thus, the returned array is in ascending order of channelIDs.
func (k Keeper) GetAllChannelToChains(ctx sdk.Context) (channels []types.ChannelToChain) {
func (k Keeper) GetAllChannelToConsumers(ctx sdk.Context) (channelsToConsumers []struct {
ChannelId string
ConsumerId string
}) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChannelIdToConsumerIdKeyPrefix())
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
// remove prefix from key to retrieve channelID
channelID := string(iterator.Key()[1:])
chainID := string(iterator.Value())

channels = append(channels, types.ChannelToChain{
ChannelId: channelID,
ChainId: chainID,
consumerId := string(iterator.Value())

channelsToConsumers = append(channelsToConsumers, struct {
ChannelId string
ConsumerId string
}{
ChannelId: channelID,
ConsumerId: consumerId,
})
}

return channels
return channelsToConsumers
}

func (k Keeper) SetConsumerGenesis(ctx sdk.Context, consumerId string, gen ccv.ConsumerGenesisState) error {
Expand Down
Loading

0 comments on commit b195a19

Please sign in to comment.