Skip to content

Commit

Permalink
ensure IBC light client existence upon IBC handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Nov 26, 2024
1 parent 2db01d0 commit 5a3862d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
10 changes: 10 additions & 0 deletions x/btcstkconsumer/keeper/consumer_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/babylonlabs-io/babylon/x/btcstkconsumer/types"
)

// RegisterConsumer registers a new consumer
func (k Keeper) RegisterConsumer(ctx context.Context, consumerRegister *types.ConsumerRegister) error {
if k.IsConsumerRegistered(ctx, consumerRegister.ConsumerId) {
return types.ErrConsumerAlreadyRegistered
Expand All @@ -17,6 +18,15 @@ func (k Keeper) RegisterConsumer(ctx context.Context, consumerRegister *types.Co
return nil
}

// UpdateConsumer updates the consumer register for a given consumer ID
func (k Keeper) UpdateConsumer(ctx context.Context, consumerRegister *types.ConsumerRegister) error {
if !k.IsConsumerRegistered(ctx, consumerRegister.ConsumerId) {
return types.ErrConsumerNotRegistered
}
k.setConsumerRegister(ctx, consumerRegister)
return nil
}

func (k Keeper) setConsumerRegister(ctx context.Context, consumerRegister *types.ConsumerRegister) {
store := k.consumerRegistryStore(ctx)
store.Set([]byte(consumerRegister.ConsumerId), k.cdc.MustMarshal(consumerRegister))
Expand Down
39 changes: 36 additions & 3 deletions x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,46 @@ func (k Keeper) BroadcastBTCStakingConsumerEvents(
}
}

// HandleIBCChannelCreation processes the IBC handshake request. The handshake is successful
// only if the client ID is registered as a consumer in the ZoneConcierge
func (k Keeper) HandleIBCChannelCreation(
ctx sdk.Context,
portID string,
channelID string,
) error {
clientID, _, err := k.channelKeeper.GetChannelClientState(ctx, portID, channelID)
if err != nil {
return fmt.Errorf("failed to get client state: %w", err)
}

// Check if the client ID is registered as a consumer
consumerRegister, err := k.btcStkKeeper.GetConsumerRegister(ctx, clientID)
if err != nil {
return fmt.Errorf("client ID %s is not registered as a consumer: %w", clientID, err)
}

// Update the consumer metadata with the new channel ID
cosmosMetadata := consumerRegister.GetCosmosConsumerMetadata()
if cosmosMetadata == nil {
return fmt.Errorf("consumer %s is not a Cosmos consumer", clientID)
}

// all good, update the channel ID
cosmosMetadata.ChannelId = channelID
if err := k.btcStkKeeper.UpdateConsumer(ctx, consumerRegister); err != nil {
return fmt.Errorf("failed to update consumer register: %w", err)
}

return nil
}

func (k Keeper) HandleConsumerSlashing(
ctx sdk.Context,
destinationPort string,
destinationChannel string,
portID string,
channelID string,
consumerSlashing *types.ConsumerSlashingIBCPacket,
) error {
clientID, _, err := k.channelKeeper.GetChannelClientState(ctx, destinationPort, destinationChannel)
clientID, _, err := k.channelKeeper.GetChannelClientState(ctx, portID, channelID)
if err != nil {
return fmt.Errorf("failed to get client state: %w", err)
}
Expand Down
12 changes: 12 additions & 0 deletions x/zoneconcierge/module_ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func (im IBCModule) OnChanOpenInit(
return "", errorsmod.Wrapf(types.ErrInvalidVersion, "got %s, expected %s", version, types.Version)
}

// Handle the IBC handshake request, i.e., ensuring the client ID is registered as
// a Cosmos consumer
if err := im.keeper.HandleIBCChannelCreation(ctx, portID, channelID); err != nil {
return "", err
}

// Claim channel capability passed back by IBC module
if err := im.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return "", err
Expand Down Expand Up @@ -87,6 +93,12 @@ func (im IBCModule) OnChanOpenTry(
return "", errorsmod.Wrapf(types.ErrInvalidVersion, "invalid counterparty version: got: %s, expected %s", counterpartyVersion, types.Version)
}

// Handle the IBC handshake request, i.e., ensuring the client ID is registered as
// a Cosmos consumer
if err := im.keeper.HandleIBCChannelCreation(ctx, portID, channelID); err != nil {
return "", err
}

// Module may have already claimed capability in OnChanOpenInit in the case of crossing hellos
// (ie chainA and chainB both call ChanOpenInit before one of them calls ChanOpenTry)
// If module can already authenticate the capability then module already owns it so we don't need to claim
Expand Down
2 changes: 2 additions & 0 deletions x/zoneconcierge/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ type BTCStakingKeeper interface {

type BTCStkConsumerKeeper interface {
RegisterConsumer(ctx context.Context, consumerRegister *btcstkconsumertypes.ConsumerRegister) error
UpdateConsumer(ctx context.Context, consumerRegister *btcstkconsumertypes.ConsumerRegister) error
GetConsumerRegister(ctx context.Context, consumerID string) (*btcstkconsumertypes.ConsumerRegister, error)
GetConsumerOfFinalityProvider(ctx context.Context, fpBTCPK *bbn.BIP340PubKey) (string, error)
GetConsumerFinalityProvider(ctx context.Context, consumerID string, fpBTCPK *bbn.BIP340PubKey) (*bstypes.FinalityProvider, error)
SetConsumerFinalityProvider(ctx context.Context, fp *bstypes.FinalityProvider)
Expand Down
27 changes: 27 additions & 0 deletions x/zoneconcierge/types/mocked_keepers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5a3862d

Please sign in to comment.