Skip to content

Commit

Permalink
pr comment: optimize PropagateFPSlashingToConsumers further
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Sep 16, 2024
1 parent 319afc1 commit 03bbfaf
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions x/btcstaking/keeper/finality_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,33 @@ func (k Keeper) PropagateFPSlashingToConsumers(ctx context.Context, fpBTCPK *bbn
return err
}

// Create a map to store FP to consumer ID mappings
fpToConsumerMap := make(map[string]string)

// Map to collect events for each consumer
consumerEvents := make(map[string][]*types.BTCStakingConsumerEvent)

for _, delegation := range delegations {
// Create SlashedBTCDelegation event
consumerEvent := types.CreateSlashedBTCDelegationEvent(delegation)

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

// Collect events for each consumer
for _, consumerID := range restakedFPConsumerIDs {
for _, delegationFPBTCPK := range delegation.FpBtcPkList {
fpBTCPKHex := delegationFPBTCPK.MarshalHex()
consumerID, exists := fpToConsumerMap[fpBTCPKHex]
if !exists {
// If not in map, check if it's a Babylon FP or get its consumer
if _, err := k.GetFinalityProvider(ctx, delegationFPBTCPK); err == nil {
// It's a Babylon FP, skip
continue
} else if consumerID, err = k.bscKeeper.GetConsumerOfFinalityProvider(ctx, &delegationFPBTCPK); err == nil {
// Found consumer, add to map
fpToConsumerMap[fpBTCPKHex] = consumerID
} else {
return types.ErrFpNotFound.Wrapf("finality provider pk %s is not found", fpBTCPKHex)
}
}

// Add event to the consumer's event list
consumerEvents[consumerID] = append(consumerEvents[consumerID], consumerEvent)
}
}
Expand Down

0 comments on commit 03bbfaf

Please sign in to comment.