Skip to content

Commit

Permalink
notify consumers about slashed fp
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Sep 12, 2024
1 parent 0fc0205 commit e1aa8d8
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
27 changes: 27 additions & 0 deletions x/btcstaking/keeper/btc_delegators.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ func (k Keeper) getBTCDelegatorDelegations(ctx context.Context, fpBTCPK *bbn.BIP
return &types.BTCDelegatorDelegations{Dels: btcDels}
}

func (k Keeper) getFPBTCDelegations(ctx context.Context, fpBTCPK *bbn.BIP340PubKey) ([]*types.BTCDelegation, error) {
store := k.btcDelegatorFpStore(ctx, fpBTCPK)
iterator := store.Iterator(nil, nil)
defer iterator.Close()

btcDels := make([]*types.BTCDelegation, 0)
for ; iterator.Valid(); iterator.Next() {
var btcDelIndex types.BTCDelegatorDelegationIndex
if err := btcDelIndex.Unmarshal(iterator.Value()); err != nil {
return nil, err
}

for _, stakingTxHashBytes := range btcDelIndex.StakingTxHashList {
stakingTxHash, err := chainhash.NewHash(stakingTxHashBytes)
if err != nil {
return nil, err
}
btcDel := k.getBTCDelegation(ctx, *stakingTxHash)
if btcDel != nil {
btcDels = append(btcDels, btcDel)
}
}
}

return btcDels, nil
}

// btcDelegatorFpStore returns the KVStore of the BTC delegators
// prefix: BTCDelegatorKey || finality provider's Bitcoin secp256k1 PK
// key: delegator's Bitcoin secp256k1 PK
Expand Down
32 changes: 32 additions & 0 deletions x/btcstaking/keeper/finality_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"

bbn "github.com/babylonlabs-io/babylon/types"
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
)

Expand Down Expand Up @@ -67,6 +68,37 @@ func (k Keeper) SlashFinalityProvider(ctx context.Context, fpBTCPK []byte) error
return nil
}

func (k Keeper) NotifyConsumersOfSlashedFinalityProvider(ctx context.Context, fpBTCPK *bbn.BIP340PubKey) error {
// Get all delegations for this finality provider
delegations, err := k.getFPBTCDelegations(ctx, fpBTCPK)
if err != nil {
return err
}

for _, delegation := range delegations {
// Create SlashedBTCDelegation event
consumerEvent, err := types.CreateSlashedBTCDelegationEvent(delegation)
if err != nil {
return err
}

// Get consumer IDs of non-Babylon finality providers
restakedFPConsumerIDs, err := k.restakedFPConsumerIDs(ctx, delegation.FpBtcPkList)
if err != nil {
return err
}

// Send event to each involved consumer chain
for _, consumerID := range restakedFPConsumerIDs {
if err := k.AddBTCStakingConsumerEvent(ctx, consumerID, consumerEvent); err != nil {
return err
}
}
}

return nil
}

// RevertSluggishFinalityProvider sets the Sluggish flag of the given finality provider
// to false
func (k Keeper) RevertSluggishFinalityProvider(ctx context.Context, fpBTCPK []byte) error {
Expand Down
12 changes: 12 additions & 0 deletions x/btcstaking/types/btc_staking_consumer_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,15 @@ func CreateUnbondedBTCDelegationEvent(unbondedDel *BTCDelegation) (*BTCStakingCo

return event, nil
}

func CreateSlashedBTCDelegationEvent(slashedDel *BTCDelegation) (*BTCStakingConsumerEvent, error) {
event := &BTCStakingConsumerEvent{
Event: &BTCStakingConsumerEvent_SlashedDel{
SlashedDel: &SlashedBTCDelegation{
StakingTxHash: slashedDel.MustGetStakingTxHash().String(),
},
},
}

return event, nil
}
5 changes: 5 additions & 0 deletions x/finality/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ func (k Keeper) slashFinalityProvider(ctx context.Context, fpBtcPk *bbn.BIP340Pu
panic(fmt.Errorf("failed to slash finality provider: %v", err))
}

// Notify consumer chains about the slashed finality provider
if err := k.BTCStakingKeeper.NotifyConsumersOfSlashedFinalityProvider(ctx, fpBtcPk); err != nil {
panic(fmt.Errorf("failed to notify consumers of slashed finality provider: %w", err))
}

// emit slashing event
eventSlashing := types.NewEventSlashedFinalityProvider(evidence)
if err := sdk.UnwrapSDKContext(ctx).EventManager().EmitTypedEvent(eventSlashing); err != nil {
Expand Down
1 change: 1 addition & 0 deletions x/finality/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type BTCStakingKeeper interface {
GetFinalityProvider(ctx context.Context, fpBTCPK []byte) (*bstypes.FinalityProvider, error)
HasFinalityProvider(ctx context.Context, fpBTCPK []byte) bool
SlashFinalityProvider(ctx context.Context, fpBTCPK []byte) error
NotifyConsumersOfSlashedFinalityProvider(ctx context.Context, fpBTCPK *bbn.BIP340PubKey) error
GetVotingPower(ctx context.Context, fpBTCPK []byte, height uint64) uint64
GetVotingPowerTable(ctx context.Context, height uint64) map[string]uint64
GetBTCStakingActivatedHeight(ctx context.Context) (uint64, error)
Expand Down
14 changes: 14 additions & 0 deletions x/finality/types/mocked_keepers.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (k Keeper) BroadcastBTCStakingConsumerEvents(
continue
}

// Log the IBC packet
k.Logger(sdkCtx).Info("BroadcastBTCStakingConsumerEvents: Preparing ZoneConcierge packet",
"consumerID", consumerID,
"packetType", "BtcStaking",
"packetContent", fmt.Sprintf("%+v", ibcPacket))

// Prepare the packet for ZoneConcierge.
zcPacket := &types.ZoneconciergePacketData{
Packet: &types.ZoneconciergePacketData_BtcStaking{
Expand Down

0 comments on commit e1aa8d8

Please sign in to comment.