Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cleanup ccv types #1350

Merged
merged 7 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.17.0
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb
bermuell marked this conversation as resolved.
Show resolved Hide resolved
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.11.0 // indirect
google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98 // indirect
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/consumer/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (am AppModule) OnChanOpenAck(

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeFeeTransferChannelOpened,
consumertypes.EventTypeFeeTransferChannelOpened,
sdk.NewAttribute(sdk.AttributeKeyModule, consumertypes.ModuleName),
sdk.NewAttribute(channeltypes.AttributeKeyChannelID, channelID),
sdk.NewAttribute(channeltypes.AttributeKeyPortID, transfertypes.PortID),
Expand Down
12 changes: 6 additions & 6 deletions x/ccv/consumer/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ func (k Keeper) SendRewardsToProvider(ctx sdk.Context) error {
currentHeight := ctx.BlockHeight()
ctx.EventManager().EmitEvent(
sdk.NewEvent(
ccv.EventTypeFeeDistribution,
types.EventTypeFeeDistribution,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(ccv.AttributeDistributionCurrentHeight, strconv.Itoa(int(currentHeight))),
sdk.NewAttribute(ccv.AttributeDistributionNextHeight, strconv.Itoa(int(currentHeight+k.GetBlocksPerDistributionTransmission(ctx)))),
sdk.NewAttribute(ccv.AttributeDistributionFraction, (k.GetConsumerRedistributionFrac(ctx))),
sdk.NewAttribute(ccv.AttributeDistributionTotal, allBalances.String()),
sdk.NewAttribute(ccv.AttributeDistributionToProvider, sentCoins.String()),
sdk.NewAttribute(types.AttributeDistributionCurrentHeight, strconv.Itoa(int(currentHeight))),
sdk.NewAttribute(types.AttributeDistributionNextHeight, strconv.Itoa(int(currentHeight+k.GetBlocksPerDistributionTransmission(ctx)))),
sdk.NewAttribute(types.AttributeDistributionFraction, (k.GetConsumerRedistributionFrac(ctx))),
sdk.NewAttribute(types.AttributeDistributionTotal, allBalances.String()),
sdk.NewAttribute(types.AttributeDistributionToProvider, sentCoins.String()),
),
)

Expand Down
8 changes: 4 additions & 4 deletions x/ccv/consumer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ func (k Keeper) QueueVSCMaturedPackets(ctx sdk.Context) {

ctx.EventManager().EmitEvent(
sdk.NewEvent(
ccv.EventTypeVSCMatured,
types.EventTypeVSCMatured,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(ccv.AttributeChainID, ctx.ChainID()),
sdk.NewAttribute(ccv.AttributeConsumerHeight, strconv.Itoa(int(ctx.BlockHeight()))),
sdk.NewAttribute(types.AttributeConsumerHeight, strconv.Itoa(int(ctx.BlockHeight()))),
sdk.NewAttribute(ccv.AttributeValSetUpdateID, strconv.Itoa(int(maturityTime.VscId))),
sdk.NewAttribute(ccv.AttributeTimestamp, ctx.BlockTime().String()),
sdk.NewAttribute(types.AttributeTimestamp, ctx.BlockTime().String()),
),
)
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (k Keeper) QueueSlashPacket(ctx sdk.Context, validator abci.Validator, vals

ctx.EventManager().EmitEvent(
sdk.NewEvent(
ccv.EventTypeConsumerSlashRequest,
types.EventTypeConsumerSlashRequest,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(ccv.AttributeValidatorAddress, sdk.ConsAddress(validator.Address).String()),
sdk.NewAttribute(ccv.AttributeValSetUpdateID, strconv.Itoa(int(valsetUpdateID))),
Expand Down
18 changes: 18 additions & 0 deletions x/ccv/consumer/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package types

const (
AttributeConsumerHeight = "consumer_height"
AttributeTimestamp = "timestamp"

EventTypeFeeDistribution = "fee_distribution"
EventTypeVSCMatured = "vsc_matured"
EventTypeConsumerSlashRequest = "consumer_slash_request"
EventTypeFeeTransferChannelOpened = "fee_transfer_channel_opened"

AttributeDistributionCurrentHeight = "current_distribution_height"
//#nosec G101 -- (false positive) this is not a hardcoded credential
AttributeDistributionNextHeight = "next_distribution_height"
AttributeDistributionFraction = "distribution_fraction"
AttributeDistributionTotal = "total"
AttributeDistributionToProvider = "provider_amount"
)
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (k Keeper) VerifyConsumerChain(ctx sdk.Context, channelID string, connectio
return errorsmod.Wrapf(ccv.ErrClientNotFound, "cannot find client for consumer chain %s", tmClient.ChainId)
}
if ccvClientId != clientID {
return errorsmod.Wrapf(ccv.ErrInvalidConsumerClient, "CCV channel must be built on top of CCV client. expected %s, got %s", ccvClientId, clientID)
return errorsmod.Wrapf(types.ErrInvalidConsumerClient, "CCV channel must be built on top of CCV client. expected %s, got %s", ccvClientId, clientID)
}

// Verify that there isn't already a CCV channel for the consumer chain
Expand Down
7 changes: 3 additions & 4 deletions x/ccv/provider/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"

"github.com/cosmos/interchain-security/v3/x/ccv/provider/types"
ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types"
)

type msgServer struct {
Expand Down Expand Up @@ -100,9 +99,9 @@ func (k msgServer) AssignConsumerKey(goCtx context.Context, msg *types.MsgAssign

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
ccvtypes.EventTypeAssignConsumerKey,
sdk.NewAttribute(ccvtypes.AttributeProviderValidatorAddress, msg.ProviderAddr),
sdk.NewAttribute(ccvtypes.AttributeConsumerConsensusPubKey, consumerTMPublicKey.String()),
types.EventTypeAssignConsumerKey,
sdk.NewAttribute(types.AttributeProviderValidatorAddress, msg.ProviderAddr),
sdk.NewAttribute(types.AttributeConsumerConsensusPubKey, consumerTMPublicKey.String()),
),
})

Expand Down
22 changes: 11 additions & 11 deletions x/ccv/provider/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (k Keeper) CreateConsumerClient(ctx sdk.Context, prop *types.ConsumerAdditi
chainID := prop.ChainId
// check that a client for this chain does not exist
if _, found := k.GetConsumerClientId(ctx, chainID); found {
return errorsmod.Wrap(ccv.ErrDuplicateConsumerChain,
return errorsmod.Wrap(types.ErrDuplicateConsumerChain,
fmt.Sprintf("cannot create client for existent consumer chain: %s", chainID))
}

Expand Down Expand Up @@ -108,14 +108,14 @@ func (k Keeper) CreateConsumerClient(ctx sdk.Context, prop *types.ConsumerAdditi

ctx.EventManager().EmitEvent(
sdk.NewEvent(
ccv.EventTypeConsumerClientCreated,
types.EventTypeConsumerClientCreated,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(ccv.AttributeChainID, chainID),
sdk.NewAttribute(clienttypes.AttributeKeyClientID, clientID),
sdk.NewAttribute(ccv.AttributeInitialHeight, prop.InitialHeight.String()),
sdk.NewAttribute(ccv.AttributeInitializationTimeout, strconv.Itoa(int(ts.UnixNano()))),
sdk.NewAttribute(ccv.AttributeTrustingPeriod, clientState.TrustingPeriod.String()),
sdk.NewAttribute(ccv.AttributeUnbondingPeriod, clientState.UnbondingPeriod.String()),
sdk.NewAttribute(types.AttributeInitialHeight, prop.InitialHeight.String()),
sdk.NewAttribute(types.AttributeInitializationTimeout, strconv.Itoa(int(ts.UnixNano()))),
sdk.NewAttribute(types.AttributeTrustingPeriod, clientState.TrustingPeriod.String()),
sdk.NewAttribute(types.AttributeUnbondingPeriod, clientState.UnbondingPeriod.String()),
),
)

Expand Down Expand Up @@ -155,7 +155,7 @@ func (k Keeper) HandleConsumerRemovalProposal(ctx sdk.Context, p *types.Consumer
func (k Keeper) StopConsumerChain(ctx sdk.Context, chainID string, closeChan bool) (err error) {
// check that a client for chainID exists
if _, found := k.GetConsumerClientId(ctx, chainID); !found {
return errorsmod.Wrap(ccv.ErrConsumerChainNotFound,
return errorsmod.Wrap(types.ErrConsumerChainNotFound,
fmt.Sprintf("cannot stop non-existent consumer chain: %s", chainID))
}

Expand Down Expand Up @@ -612,8 +612,8 @@ func (k Keeper) HandleConsumerRewardDenomProposal(ctx sdk.Context, p *types.Chan
}
k.SetConsumerRewardDenom(ctx, denomToAdd)
ctx.EventManager().EmitEvent(sdk.NewEvent(
ccv.EventTypeAddConsumerRewardDenom,
sdk.NewAttribute(ccv.AttributeConsumerRewardDenom, denomToAdd),
types.EventTypeAddConsumerRewardDenom,
sdk.NewAttribute(types.AttributeConsumerRewardDenom, denomToAdd),
))
}
for _, denomToRemove := range p.DenomsToRemove {
Expand All @@ -624,8 +624,8 @@ func (k Keeper) HandleConsumerRewardDenomProposal(ctx sdk.Context, p *types.Chan
}
k.DeleteConsumerRewardDenom(ctx, denomToRemove)
ctx.EventManager().EmitEvent(sdk.NewEvent(
ccv.EventTypeRemoveConsumerRewardDenom,
sdk.NewAttribute(ccv.AttributeConsumerRewardDenom, denomToRemove),
types.EventTypeRemoveConsumerRewardDenom,
sdk.NewAttribute(types.AttributeConsumerRewardDenom, denomToRemove),
))
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions x/ccv/provider/keeper/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func TestPendingConsumerAdditionPropDeletion(t *testing.T) {
defer ctrl.Finish()

for _, tc := range testCases {
tc := tc
bermuell marked this conversation as resolved.
Show resolved Hide resolved
providerKeeper.SetPendingConsumerAdditionProp(ctx, &tc.ConsumerAdditionProposal)
}

Expand Down Expand Up @@ -580,6 +581,7 @@ func TestPendingConsumerRemovalPropDeletion(t *testing.T) {
defer ctrl.Finish()

for _, tc := range testCases {
tc := tc
providerKeeper.SetPendingConsumerRemovalProp(ctx, &tc.ConsumerRemovalProposal)
}

Expand Down
8 changes: 4 additions & 4 deletions x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,11 @@ func (k Keeper) HandleSlashPacket(ctx sdk.Context, chainID string, data ccv.Slas

ctx.EventManager().EmitEvent(
sdk.NewEvent(
ccv.EventTypeExecuteConsumerChainSlash,
providertypes.EventTypeExecuteConsumerChainSlash,
sdk.NewAttribute(sdk.AttributeKeyModule, providertypes.ModuleName),
sdk.NewAttribute(ccv.AttributeValidatorAddress, providerConsAddr.String()),
sdk.NewAttribute(ccv.AttributeInfractionType, data.Infraction.String()),
sdk.NewAttribute(ccv.AttributeInfractionHeight, strconv.Itoa(int(infractionHeight))),
sdk.NewAttribute(providertypes.AttributeInfractionHeight, strconv.Itoa(int(infractionHeight))),
sdk.NewAttribute(ccv.AttributeValSetUpdateID, strconv.Itoa(int(data.ValsetUpdateId))),
),
)
Expand All @@ -452,7 +452,7 @@ func (k Keeper) EndBlockCCR(ctx sdk.Context) {
"chainID", initTimeoutTimestamp.ChainId)
err := k.StopConsumerChain(ctx, initTimeoutTimestamp.ChainId, false)
if err != nil {
if ccv.ErrConsumerChainNotFound.Is(err) {
if providertypes.ErrConsumerChainNotFound.Is(err) {
// consumer chain not found
continue
}
Expand All @@ -479,7 +479,7 @@ func (k Keeper) EndBlockCCR(ctx sdk.Context) {
)
err := k.StopConsumerChain(ctx, channelToChain.ChainId, true)
if err != nil {
if ccv.ErrConsumerChainNotFound.Is(err) {
if providertypes.ErrConsumerChainNotFound.Is(err) {
// consumer chain not found
continue
}
Expand Down
3 changes: 3 additions & 0 deletions x/ccv/provider/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ var (
ErrInvalidProviderAddress = errorsmod.Register(ModuleName, 13, "invalid provider address")
ErrInvalidConsumerRewardDenom = errorsmod.Register(ModuleName, 14, "invalid consumer reward denom")
ErrInvalidDepositorAddress = errorsmod.Register(ModuleName, 15, "invalid depositor address")
ErrInvalidConsumerClient = errorsmod.Register(ModuleName, 16, "ccv channel is not built on correct client")
ErrDuplicateConsumerChain = errorsmod.Register(ModuleName, 17, "consumer chain already exists")
ErrConsumerChainNotFound = errorsmod.Register(ModuleName, 18, "consumer chain not found")
)
18 changes: 18 additions & 0 deletions x/ccv/provider/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package types

// Provider events
const (
EventTypeConsumerClientCreated = "consumer_client_created"
EventTypeAssignConsumerKey = "assign_consumer_key"
EventTypeAddConsumerRewardDenom = "add_consumer_reward_denom"
EventTypeRemoveConsumerRewardDenom = "remove_consumer_reward_denom"
EventTypeExecuteConsumerChainSlash = "execute_consumer_chain_slash"
AttributeInfractionHeight = "infraction_height"
AttributeInitialHeight = "initial_height"
AttributeInitializationTimeout = "initialization_timeout"
AttributeTrustingPeriod = "trusting_period"
AttributeUnbondingPeriod = "unbonding_period"
AttributeProviderValidatorAddress = "provider_validator_address"
AttributeConsumerConsensusPubKey = "consumer_consensus_pub_key"
AttributeConsumerRewardDenom = "consumer_reward_denom"
)
31 changes: 6 additions & 25 deletions x/ccv/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,12 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
)

// RegisterLegacyAminoCodec registers the necessary x/ibc transfer interfaces and concrete types
// on the provided LegacyAmino codec. These types are used for Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
}

// RegisterInterfaces register the ibc transfer module interfaces to protobuf
// Any.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
}

var (
amino = codec.NewLegacyAmino()

// ModuleCdc references the global x/ibc-transfer module codec. Note, the codec
// should ONLY be used in certain instances of tests and for JSON encoding.
//
// The actual codec used for serialization should be provided to x/ibc transfer and
// defined at the application level.
ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())

// AminoCdc is a amino codec created to support amino json compatible msgs.
AminoCdc = codec.NewAminoCodec(amino)
)
// ModuleCdc references the global x/ibc-transfer module codec. Note, the codec
// should ONLY be used in certain instances of tests and for JSON encoding.
//
// The actual codec used for serialization should be provided to x/ibc transfer and
// defined at the application level.
var ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())

func init() {
RegisterLegacyAminoCodec(amino)
amino.Seal()
}
29 changes: 10 additions & 19 deletions x/ccv/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,14 @@ import (

// CCV sentinel errors
var (
ErrInvalidPacketData = errorsmod.Register(ModuleName, 2, "invalid CCV packet data")
ErrInvalidPacketTimeout = errorsmod.Register(ModuleName, 3, "invalid packet timeout")
ErrInvalidVersion = errorsmod.Register(ModuleName, 4, "invalid CCV version")
ErrInvalidChannelFlow = errorsmod.Register(ModuleName, 5, "invalid message sent to channel end")
ErrInvalidConsumerChain = errorsmod.Register(ModuleName, 6, "invalid consumer chain")
ErrInvalidProviderChain = errorsmod.Register(ModuleName, 7, "invalid provider chain")
ErrInvalidStatus = errorsmod.Register(ModuleName, 8, "invalid channel status")
ErrInvalidGenesis = errorsmod.Register(ModuleName, 9, "invalid genesis state")
ErrDuplicateChannel = errorsmod.Register(ModuleName, 10, "CCV channel already exists")
ErrInvalidVSCMaturedId = errorsmod.Register(ModuleName, 11, "invalid vscId for VSC packet")
ErrInvalidVSCMaturedTime = errorsmod.Register(ModuleName, 12, "invalid maturity time for VSC packet")
ErrInvalidConsumerState = errorsmod.Register(ModuleName, 13, "provider chain has invalid state for consumer chain")
ErrInvalidConsumerClient = errorsmod.Register(ModuleName, 14, "ccv channel is not built on correct client")
ErrInvalidProposal = errorsmod.Register(ModuleName, 15, "invalid proposal")
ErrInvalidHandshakeMetadata = errorsmod.Register(ModuleName, 16, "invalid provider handshake metadata")
ErrChannelNotFound = errorsmod.Register(ModuleName, 17, "channel not found")
ErrClientNotFound = errorsmod.Register(ModuleName, 18, "client not found")
ErrDuplicateConsumerChain = errorsmod.Register(ModuleName, 19, "consumer chain already exists")
ErrConsumerChainNotFound = errorsmod.Register(ModuleName, 20, "consumer chain not found")
ErrInvalidPacketData = errorsmod.Register(ModuleName, 1, "invalid CCV packet data")
ErrInvalidVersion = errorsmod.Register(ModuleName, 2, "invalid CCV version")
ErrInvalidChannelFlow = errorsmod.Register(ModuleName, 3, "invalid message sent to channel end")
ErrInvalidGenesis = errorsmod.Register(ModuleName, 4, "invalid genesis state")
ErrDuplicateChannel = errorsmod.Register(ModuleName, 5, "CCV channel already exists")
ErrInvalidVSCMaturedId = errorsmod.Register(ModuleName, 6, "invalid vscId for VSC packet")
ErrInvalidVSCMaturedTime = errorsmod.Register(ModuleName, 7, "invalid maturity time for VSC packet")
ErrInvalidHandshakeMetadata = errorsmod.Register(ModuleName, 8, "invalid provider handshake metadata")
ErrChannelNotFound = errorsmod.Register(ModuleName, 9, "channel not found")
ErrClientNotFound = errorsmod.Register(ModuleName, 10, "client not found")
)
42 changes: 7 additions & 35 deletions x/ccv/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,17 @@ package types

// CCV events
const (
EventTypeTimeout = "timeout"
EventTypePacket = "ccv_packet"
EventTypeChannelEstablished = "channel_established"
EventTypeFeeTransferChannelOpened = "fee_transfer_channel_opened"
EventTypeConsumerClientCreated = "consumer_client_created"
EventTypeAssignConsumerKey = "assign_consumer_key"
EventTypeAddConsumerRewardDenom = "add_consumer_reward_denom"
EventTypeRemoveConsumerRewardDenom = "remove_consumer_reward_denom"

EventTypeExecuteConsumerChainSlash = "execute_consumer_chain_slash"
EventTypeFeeDistribution = "fee_distribution"
EventTypeConsumerSlashRequest = "consumer_slash_request"
EventTypeVSCMatured = "vsc_matured"
EventTypeTimeout = "timeout"
EventTypePacket = "ccv_packet"
EventTypeChannelEstablished = "channel_established"

AttributeKeyAckSuccess = "success"
AttributeKeyAck = "acknowledgement"
AttributeKeyAckError = "error"

AttributeChainID = "chain_id"
AttributeValidatorAddress = "validator_address"
AttributeValidatorConsumerAddress = "validator_consumer_address"
AttributeInfractionType = "infraction_type"
AttributeInfractionHeight = "infraction_height"
AttributeConsumerHeight = "consumer_height"
AttributeValSetUpdateID = "valset_update_id"
AttributeTimestamp = "timestamp"
AttributeInitialHeight = "initial_height"
AttributeInitializationTimeout = "initialization_timeout"
AttributeTrustingPeriod = "trusting_period"
AttributeUnbondingPeriod = "unbonding_period"
AttributeProviderValidatorAddress = "provider_validator_address"
AttributeConsumerConsensusPubKey = "consumer_consensus_pub_key"

AttributeDistributionCurrentHeight = "current_distribution_height"
//#nosec G101 -- (false positive) this is not a hardcoded credential
AttributeDistributionNextHeight = "next_distribution_height"
AttributeDistributionFraction = "distribution_fraction"
AttributeDistributionTotal = "total"
AttributeDistributionToProvider = "provider_amount"
AttributeChainID = "chain_id"
AttributeValidatorAddress = "validator_address"
AttributeInfractionType = "infraction_type"

AttributeConsumerRewardDenom = "consumer_reward_denom"
AttributeValSetUpdateID = "valset_update_id"
)
Loading