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

fix: fix comments to the base branch #418

Merged
merged 5 commits into from
Jan 17, 2025
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
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/cometbft/cometbft-db v0.15.0
github.com/cosmos/cosmos-sdk v0.50.11
github.com/cosmos/ibc-go/modules/light-clients/08-wasm v0.0.0-20240429153234-e1e6da7e4ead
github.com/cosmos/relayer/v2 v2.5.2
github.com/cosmos/relayer/v2 v2.5.3
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -274,8 +274,4 @@ replace (

// Downgraded to stable version see: https://github.com/cosmos/cosmos-sdk/pull/14952
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7

// avoid v1.66 that has a breaking change for protobuf. That change breaks the relayer.
// https://github.com/grpc/grpc-go/issues/7569
google.golang.org/grpc => google.golang.org/grpc v1.65.0
)
1,032 changes: 75 additions & 957 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion proto/babylon/btcstaking/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ message MsgCreateFinalityProvider {
ProofOfPossessionBTC pop = 5;
// consumer_id is the ID of the consumer
// If it's empty, it's assumed to be Babylon's chain id
string consumer_id = 7;
string consumer_id = 6;
}

// MsgCreateFinalityProviderResponse is the response for MsgCreateFinalityProvider
Expand Down
4 changes: 4 additions & 0 deletions x/btcstaking/keeper/btc_delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"fmt"
"slices"

"cosmossdk.io/store/prefix"
"github.com/btcsuite/btcd/chaincfg/chainhash"
Expand Down Expand Up @@ -284,6 +285,9 @@ func (k Keeper) restakedFPConsumerIDs(ctx context.Context, fpBTCPKs []bbn.BIP340
uniqueConsumerIDs = append(uniqueConsumerIDs, consumerID)
}

// Sort consumer IDs for deterministic ordering
slices.Sort(uniqueConsumerIDs)

return uniqueConsumerIDs, nil
}

Expand Down
21 changes: 11 additions & 10 deletions x/btcstaking/keeper/btc_delegators.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ func (k Keeper) getBTCDelegatorDelegations(ctx context.Context, fpBTCPK *bbn.BIP
return &types.BTCDelegatorDelegations{Dels: btcDels}
}

// GetFPBTCDelegations retrieves all BTC delegations for a given finality provider.
// HandleFPBTCDelegations processes all BTC delegations for a given finality provider using a provided handler function.
// This function works for both Babylon finality providers and consumer finality providers.
// It automatically determines and selects the appropriate KV store based on the finality provider type.
//
// Parameters:
// - ctx: The context for the operation
// - fpBTCPK: The Bitcoin public key of the finality provider
// - handler: A function that processes each BTCDelegation
//
// Returns:
// - A slice of BTCDelegation pointers representing all delegations for the given finality provider
// - An error if the finality provider is not found or if there's an issue retrieving the delegations
func (k Keeper) GetFPBTCDelegations(ctx context.Context, fpBTCPK *bbn.BIP340PubKey) ([]*types.BTCDelegation, error) {
// - An error if the finality provider is not found or if there's an issue processing the delegations
func (k Keeper) HandleFPBTCDelegations(ctx context.Context, fpBTCPK *bbn.BIP340PubKey, handler func(*types.BTCDelegation) error) error {
var store prefix.Store
// Determine which store to use based on the finality provider type
switch {
Expand All @@ -84,32 +84,33 @@ func (k Keeper) GetFPBTCDelegations(ctx context.Context, fpBTCPK *bbn.BIP340PubK
store = k.btcConsumerDelegatorStore(ctx, fpBTCPK)
default:
// if not found in either store, return error
return nil, types.ErrFpNotFound
return types.ErrFpNotFound
}

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
return err
}

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

return btcDels, nil
return nil
}

// btcDelegatorFpStore returns the KVStore of the BTC delegators
Expand Down
83 changes: 29 additions & 54 deletions x/btcstaking/keeper/finality_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,15 @@ func (k Keeper) SlashConsumerFinalityProvider(ctx context.Context, consumerID st
fp.SlashedBtcHeight = btcTip.Height
k.BscKeeper.SetConsumerFinalityProvider(ctx, fp)

// Get all delegations for this consumer finality provider
btcDels, err := k.GetFPBTCDelegations(ctx, fpBTCPK)
if err != nil {
return fmt.Errorf("failed to get BTC delegations: %w", err)
}

// Record slashed BTC delegation events for each affected delegation
// These events will be processed in the next `BeginBlock` to update
// the power distribution of the involved Babylon FPs
for _, btcDel := range btcDels {
// Process all delegations for this consumer finality provider and record slashed events
err = k.HandleFPBTCDelegations(ctx, fpBTCPK, func(btcDel *types.BTCDelegation) error {
stakingTxHash := btcDel.MustGetStakingTxHash().String()
eventSlashedBTCDelegation := types.NewEventPowerDistUpdateWithSlashedBTCDelegation(stakingTxHash)
k.addPowerDistUpdateEvent(ctx, btcTip.Height, eventSlashedBTCDelegation)
return nil
})
if err != nil {
return fmt.Errorf("failed to handle BTC delegations: %w", err)
}

return nil
Expand All @@ -187,51 +183,19 @@ func (k Keeper) SlashConsumerFinalityProvider(ctx context.Context, consumerID st
// Returns:
// - An error if any operation fails, nil otherwise.
func (k Keeper) PropagateFPSlashingToConsumers(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
}

// Collect slashed events for each consumer
consumerEvents, err := k.collectSlashedConsumerEvents(ctx, delegations)
if err != nil {
return err
}

// Send collected events to each involved consumer chain
for consumerID, events := range consumerEvents {
if err := k.AddBTCStakingConsumerEvents(ctx, consumerID, events); err != nil {
return err
}
}

return nil
}

// collectSlashedConsumerEvents processes delegations and collects slashing events for each consumer chain.
// It ensures that each consumer receives only one event per delegation, even if multiple finality providers
// in the delegation belong to the same consumer.
//
// Parameters:
// - ctx: The context for the operation.
// - delegations: A slice of BTCDelegation objects to process.
//
// Returns:
// - A map where the key is the consumer ID and the value is a slice of BTCStakingConsumerEvents.
// - An error if any operation fails, nil otherwise.
func (k Keeper) collectSlashedConsumerEvents(ctx context.Context, delegations []*types.BTCDelegation) (map[string][]*types.BTCStakingConsumerEvent, error) {
// 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)
// Create a map to store FP to consumer ID mappings
fpToConsumerMap := make(map[string]string)

for _, delegation := range delegations {
// Process all delegations for this finality provider and collect slashing events
// for each consumer chain. Ensures that each consumer receives only one event per
// delegation, even if multiple finality providers in the delegation belong to the same consumer.
err := k.HandleFPBTCDelegations(ctx, fpBTCPK, func(delegation *types.BTCDelegation) error {
consumerEvent := types.CreateSlashedBTCDelegationEvent(delegation)

// Track consumers seen for this delegation
seenConsumers := make(map[string]bool)
seenConsumers := make(map[string]struct{})

for _, delegationFPBTCPK := range delegation.FpBtcPkList {
fpBTCPKHex := delegationFPBTCPK.MarshalHex()
Expand All @@ -244,19 +208,30 @@ func (k Keeper) collectSlashedConsumerEvents(ctx context.Context, delegations []
// Found consumer, add to map
fpToConsumerMap[fpBTCPKHex] = consumerID
} else {
return nil, types.ErrFpNotFound.Wrapf("finality provider pk %s is not found", fpBTCPKHex)
return types.ErrFpNotFound.Wrapf("finality provider pk %s is not found", fpBTCPKHex)
}
}

// Add event to the consumer's event list only if not seen for this delegation
if !seenConsumers[consumerID] {
// Only add event once per consumer per delegation
if _, ok := seenConsumers[consumerID]; !ok {
consumerEvents[consumerID] = append(consumerEvents[consumerID], consumerEvent)
seenConsumers[consumerID] = true
seenConsumers[consumerID] = struct{}{}
}
}
return nil
})
if err != nil {
return err
}

// Send collected events to each involved consumer chain
for consumerID, events := range consumerEvents {
if err := k.AddBTCStakingConsumerEvents(ctx, consumerID, events); err != nil {
return err
}
}

return consumerEvents, nil
return nil
}

// JailFinalityProvider jails a finality provider with the given PK
Expand Down
19 changes: 1 addition & 18 deletions x/btcstaking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,6 @@ func (ms msgServer) CreateBTCDelegation(goCtx context.Context, req *types.MsgCre
return nil, types.ErrReusedStakingTx.Wrapf("duplicated tx hash: %s", stakingTxHash.String())
}

// verify proof of possession
if err := req.Pop.Verify(parsedMsg.StakerAddress, req.BtcPk, ms.btcNet); err != nil {
return nil, types.ErrInvalidProofOfPossession.Wrapf("error while validating proof of possession: %v", err)
}

// Ensure all finality providers
// - are known to Babylon,
// - at least 1 one of them is a Babylon finality provider,
Expand All @@ -233,7 +228,7 @@ func (ms msgServer) CreateBTCDelegation(goCtx context.Context, req *types.MsgCre
// and then check whether the BTC stake is restaked to FPs of consumers
// TODO: ensure the BTC delegation does not restake to too many finality providers
// (pending concrete design)
restakedToConsumers, err := ms.validateRestakedFPs(ctx, req.FpBtcPkList)
restakedToConsumers, err := ms.validateRestakedFPs(ctx, parsedMsg.FinalityProviderKeys.PublicKeysBbnFormat)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -454,12 +449,6 @@ func (ms msgServer) AddCovenantSigs(goCtx context.Context, req *types.MsgAddCove
return nil, err
}

// check whether the BTC stake is restaked to FPs of consumers
_, err = ms.validateRestakedFPs(ctx, btcDel.FpBtcPkList)
if err != nil {
panic(err) // btcDel has passed verification and this can only be programming error
}

// ensure that the given covenant PK is in the parameter
if !params.HasCovenantPK(req.Pk) {
return nil, types.ErrInvalidCovenantPK.Wrapf("covenant pk: %s", req.Pk.MarshalHex())
Expand Down Expand Up @@ -619,12 +608,6 @@ func (ms msgServer) BTCUndelegate(goCtx context.Context, req *types.MsgBTCUndele
return nil, err
}

// check whether the BTC stake is restaked to FPs of consumers
_, err = ms.validateRestakedFPs(ctx, btcDel.FpBtcPkList)
if err != nil {
panic(err) // btcDel has passed verification and this can only be programming error
}

// ensure the BTC delegation with the given staking tx hash is active
btcTip := ms.btclcKeeper.GetTipInfo(ctx)

Expand Down
Loading
Loading