Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Aug 22, 2024
1 parent b47d837 commit 3beaa8e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
3 changes: 1 addition & 2 deletions x/btcstaking/keeper/incentive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ func FuzzRecordVotingPowerDistCache(f *testing.F) {
require.NoError(t, err)
require.NotNil(t, dc)
require.Equal(t, dc.TotalVotingPower, numFpsWithVotingPower*numBTCDels*stakingValue)
maxNumFps := h.BTCStakingKeeper.GetParams(h.Ctx).MaxActiveFinalityProviders
activeFPs := dc.GetActiveFinalityProviderSet(maxNumFps)
activeFPs := dc.GetActiveFinalityProviderSet()
for _, fpDistInfo := range activeFPs {
require.Equal(t, fpDistInfo.TotalVotingPower, numBTCDels*stakingValue)
fp, ok := fpsWithVotingPowerMap[fpDistInfo.Addr]
Expand Down
16 changes: 8 additions & 8 deletions x/btcstaking/keeper/power_dist_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (k Keeper) UpdatePowerDist(ctx context.Context) {
if len(events) == 0 {
if dc != nil {
// map everything in prev height to this height
k.recordVotingPowerAndCache(ctx, dc, maxActiveFps)
k.recordVotingPowerAndCache(ctx, dc)
}
return
}
Expand All @@ -57,24 +57,24 @@ func (k Keeper) UpdatePowerDist(ctx context.Context) {
newDc := k.ProcessAllPowerDistUpdateEvents(ctx, dc, events, maxActiveFps)

// find newly bonded finality providers and execute the hooks
newBondedFinalityProviders := newDc.FindNewActiveFinalityProviders(dc, maxActiveFps)
newBondedFinalityProviders := newDc.FindNewActiveFinalityProviders(dc)
for _, fp := range newBondedFinalityProviders {
if err := k.hooks.AfterFinalityProviderActivated(ctx, fp.BtcPk); err != nil {
panic(fmt.Errorf("failed to execute after finality provider %s bonded", fp.BtcPk.MarshalHex()))
}
}

// record voting power and cache for this height
k.recordVotingPowerAndCache(ctx, newDc, maxActiveFps)
k.recordVotingPowerAndCache(ctx, newDc)
// record metrics
k.recordMetrics(newDc, maxActiveFps)
k.recordMetrics(newDc)
}

func (k Keeper) recordVotingPowerAndCache(ctx context.Context, dc *types.VotingPowerDistCache, maxActiveFps uint32) {
func (k Keeper) recordVotingPowerAndCache(ctx context.Context, dc *types.VotingPowerDistCache) {
babylonTipHeight := uint64(sdk.UnwrapSDKContext(ctx).HeaderInfo().Height)

// set voting power table for this height
for i := uint32(0); i < dc.GetNumActiveFPs(maxActiveFps); i++ {
for i := uint32(0); i < dc.NumActiveFps; i++ {
fp := dc.FinalityProviders[i]
k.SetVotingPower(ctx, fp.BtcPk.MustMarshal(), babylonTipHeight, fp.TotalVotingPower)
}
Expand All @@ -83,9 +83,9 @@ func (k Keeper) recordVotingPowerAndCache(ctx context.Context, dc *types.VotingP
k.setVotingPowerDistCache(ctx, babylonTipHeight, dc)
}

func (k Keeper) recordMetrics(dc *types.VotingPowerDistCache, maxActiveFps uint32) {
func (k Keeper) recordMetrics(dc *types.VotingPowerDistCache) {
// number of active FPs
numActiveFPs := int(dc.GetNumActiveFPs(maxActiveFps))
numActiveFPs := int(dc.NumActiveFps)
types.RecordActiveFinalityProviders(numActiveFPs)
// number of inactive FPs
numInactiveFPs := len(dc.FinalityProviders) - numActiveFPs
Expand Down
22 changes: 10 additions & 12 deletions x/btcstaking/types/incentive.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func (dc *VotingPowerDistCache) AddFinalityProviderDistInfo(v *FinalityProviderD
dc.FinalityProviders = append(dc.FinalityProviders, v)
}

func (dc *VotingPowerDistCache) FindNewActiveFinalityProviders(prevDc *VotingPowerDistCache, maxActiveFPs uint32) []*FinalityProviderDistInfo {
activeFps := dc.GetActiveFinalityProviderSet(maxActiveFPs)
prevActiveFps := prevDc.GetActiveFinalityProviderSet(maxActiveFPs)
func (dc *VotingPowerDistCache) FindNewActiveFinalityProviders(prevDc *VotingPowerDistCache) []*FinalityProviderDistInfo {
activeFps := dc.GetActiveFinalityProviderSet()
prevActiveFps := prevDc.GetActiveFinalityProviderSet()
newActiveFps := make([]*FinalityProviderDistInfo, 0)

for pk, fp := range activeFps {
Expand All @@ -44,6 +44,8 @@ func (dc *VotingPowerDistCache) ApplyActiveFinalityProviders(maxActiveFPs uint32

numActiveFPs := uint32(0)

// finality providers are in the descending order of voting power
// and timestamped ones come in the last
for _, fp := range dc.FinalityProviders {
if numActiveFPs == maxActiveFPs {
break
Expand All @@ -52,7 +54,7 @@ func (dc *VotingPowerDistCache) ApplyActiveFinalityProviders(maxActiveFPs uint32
break
}
if !fp.IsTimestamped {
continue
break
}
numActiveFPs++
}
Expand All @@ -67,15 +69,11 @@ func (dc *VotingPowerDistCache) ApplyActiveFinalityProviders(maxActiveFPs uint32
dc.NumActiveFps = numActiveFPs
}

func (dc *VotingPowerDistCache) GetNumActiveFPs(maxActiveFPs uint32) uint32 {
return min(maxActiveFPs, dc.NumActiveFps)
}

// GetActiveFinalityProviderSet returns a set of active finality providers
// keyed by the hex string of the finality provider's BTC public key
// i.e., top N of them in terms of voting power
func (dc *VotingPowerDistCache) GetActiveFinalityProviderSet(maxActiveFPs uint32) map[string]*FinalityProviderDistInfo {
numActiveFPs := dc.GetNumActiveFPs(maxActiveFPs)
func (dc *VotingPowerDistCache) GetActiveFinalityProviderSet() map[string]*FinalityProviderDistInfo {
numActiveFPs := dc.NumActiveFps

activeFps := make(map[string]*FinalityProviderDistInfo)

Expand All @@ -89,8 +87,8 @@ func (dc *VotingPowerDistCache) GetActiveFinalityProviderSet(maxActiveFPs uint32
// FilterVotedDistCache filters out a voting power distribution cache
// with finality providers that have voted according to a map of given
// voters, and their total voted power.
func (dc *VotingPowerDistCache) FilterVotedDistCache(maxActiveFPs uint32, voterBTCPKs map[string]struct{}) *VotingPowerDistCache {
activeFPs := dc.GetActiveFinalityProviderSet(maxActiveFPs)
func (dc *VotingPowerDistCache) FilterVotedDistCache(voterBTCPKs map[string]struct{}) *VotingPowerDistCache {
activeFPs := dc.GetActiveFinalityProviderSet()
var filteredFps []*FinalityProviderDistInfo
totalVotingPower := uint64(0)
for k, v := range activeFPs {
Expand Down
6 changes: 3 additions & 3 deletions x/finality/keeper/tallying.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"fmt"

"github.com/babylonlabs-io/babylon/x/finality/types"
sdk "github.com/cosmos/cosmos-sdk/types"

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

// TallyBlocks tries to finalise all blocks that are non-finalised AND have a non-nil
Expand Down Expand Up @@ -89,8 +90,7 @@ func (k Keeper) finalizeBlock(ctx context.Context, block *types.IndexedBlock, vo
panic(err)
}
// filter out voted finality providers
maxActiveFPs := k.BTCStakingKeeper.GetParams(ctx).MaxActiveFinalityProviders
filteredDc := dc.FilterVotedDistCache(maxActiveFPs, voterBTCPKs)
filteredDc := dc.FilterVotedDistCache(voterBTCPKs)
// reward voted finality providers
k.IncentiveKeeper.RewardBTCStaking(ctx, block.Height, filteredDc)
// remove reward distribution cache afterwards
Expand Down

0 comments on commit 3beaa8e

Please sign in to comment.