Skip to content

Commit

Permalink
use ordered voting power table
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Dec 13, 2024
1 parent c77ecac commit 68d5397
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
22 changes: 5 additions & 17 deletions x/finality/keeper/liveness.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"
"fmt"
"sort"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -15,31 +14,20 @@ import (
// including jailing sluggish finality providers and applying punishment (TBD)
func (k Keeper) HandleLiveness(ctx context.Context, height int64) {
// get all the active finality providers for the height
fpSet := k.GetVotingPowerTable(ctx, uint64(height))
vpTableOrdered := k.GetVotingPowerTableOrdered(ctx, uint64(height))
// get all the voters for the height
voterBTCPKs := k.GetVoters(ctx, uint64(height))

// Iterate over all the finality providers which *should* have signed this block
// store whether or not they have actually signed it, identify sluggish
// ones, and apply punishment (TBD)
// Create a sorted slice of keys for deterministic iteration
fpPkHexes := make([]string, 0, len(fpSet))
for fpPkHex := range fpSet {
fpPkHexes = append(fpPkHexes, fpPkHex)
}
sort.Strings(fpPkHexes)

// Iterate over all the finality providers in sorted order
for _, fpPkHex := range fpPkHexes {
fpPk, err := types.NewBIP340PubKeyFromHex(fpPkHex)
if err != nil {
panic(fmt.Errorf("invalid finality provider public key %s: %w", fpPkHex, err))
}

// Iterate over all the finality providers in sorted order by the voting power
for _, fpWithVp := range vpTableOrdered {
fpPkHex := fpWithVp.FpPk.MarshalHex()
_, ok := voterBTCPKs[fpPkHex]
missed := !ok

err = k.HandleFinalityProviderLiveness(ctx, fpPk, missed, height)
err := k.HandleFinalityProviderLiveness(ctx, fpWithVp.FpPk, missed, height)
if err != nil {
panic(fmt.Errorf("failed to handle liveness of finality provider %s: %w", fpPkHex, err))
}
Expand Down
34 changes: 34 additions & 0 deletions x/finality/keeper/power_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
"github.com/babylonlabs-io/babylon/x/finality/types"
)

type FpWithVotingPower struct {
FpPk *bbn.BIP340PubKey
VotingPower uint64
}

func (k Keeper) SetVotingPower(ctx context.Context, fpBTCPK []byte, height uint64, power uint64) {
store := k.votingPowerBbnBlockHeightStore(ctx, height)
store.Set(fpBTCPK, sdk.Uint64ToBigEndian(power))
Expand Down Expand Up @@ -93,6 +98,35 @@ func (k Keeper) GetVotingPowerTable(ctx context.Context, height uint64) map[stri
return fpSet
}

// GetVotingPowerTableOrdered gets the voting power table ordered by the voting power
func (k Keeper) GetVotingPowerTableOrdered(ctx context.Context, height uint64) []*FpWithVotingPower {
store := k.votingPowerBbnBlockHeightStore(ctx, height)
iter := store.Iterator(nil, nil)
defer iter.Close()

// if no finality provider at this height, return nil
if !iter.Valid() {
return nil
}

// get all finality providers at this height assuming the fps
// are already ordered by the voting power
fps := make([]*FpWithVotingPower, 0, k.GetParams(ctx).MaxActiveFinalityProviders)
for ; iter.Valid(); iter.Next() {
fpBTCPK, err := bbn.NewBIP340PubKey(iter.Key())
if err != nil {
// failing to unmarshal finality provider BTC PK in KVStore is a programming error
panic(fmt.Errorf("%w: %w", bbn.ErrUnmarshal, err))
}
fps = append(fps, &FpWithVotingPower{
FpPk: fpBTCPK,
VotingPower: sdk.BigEndianToUint64(iter.Value()),
})
}

return fps
}

// GetBTCStakingActivatedHeight returns the height when the BTC staking protocol is activated
// i.e., the first height where a finality provider has voting power
// Before the BTC staking protocol is activated, we don't index or tally any block
Expand Down

0 comments on commit 68d5397

Please sign in to comment.