Skip to content

Commit

Permalink
cache num active fps
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Aug 21, 2024
1 parent 5a4d7dd commit c93c408
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 55 deletions.
4 changes: 4 additions & 0 deletions proto/babylon/btcstaking/v1/incentive.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ option go_package = "github.com/babylonlabs-io/babylon/x/btcstaking/types";
// VotingPowerDistCache is the cache for voting power distribution of finality providers
// and their BTC delegations at a height
message VotingPowerDistCache {
option (gogoproto.goproto_getters) = false;
// total_voting_power is the total voting power of all the finality providers in the cache
uint64 total_voting_power = 1;
// finality_providers is a list of finality providers' voting power information
repeated FinalityProviderDistInfo finality_providers = 2;
// num_active_fps is the number of finality providers that have positive voting power
uint32 num_active_fps = 3;
}

// FinalityProviderDistInfo is the reward distribution of a finality provider and its BTC delegations
Expand Down
1 change: 0 additions & 1 deletion x/btcstaking/keeper/power_dist_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func (k Keeper) recordVotingPowerAndCache(ctx context.Context, dc *types.VotingP
for i := uint32(0); i < dc.GetNumActiveFPs(maxActiveFps); i++ {
fp := dc.FinalityProviders[i]
k.SetVotingPower(ctx, fp.BtcPk.MustMarshal(), babylonTipHeight, fp.TotalVotingPower)

}

// set the voting power distribution cache of the current height
Expand Down
2 changes: 1 addition & 1 deletion x/btcstaking/keeper/voting_power_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func FuzzVotingPowerTable_ActiveFinalityProviders(f *testing.F) {
maxActiveFpsParam := h.BTCStakingKeeper.GetParams(h.Ctx).MaxActiveFinalityProviders
// get a map of expected active finality providers
types.SortFinalityProviders(fpsWithMeta)
expectedActiveFps := fpsWithMeta[:min(uint32(len(fpsWithMeta)), maxActiveFpsParam)]
expectedActiveFps := fpsWithMeta[:min(uint32(len(fpsWithMeta)-len(noTimestampedFps)), maxActiveFpsParam)]
expectedActiveFpsMap := map[string]uint64{}
for _, fp := range expectedActiveFps {
expectedActiveFpsMap[fp.BtcPk.MarshalHex()] = fp.TotalVotingPower
Expand Down
24 changes: 20 additions & 4 deletions x/btcstaking/types/incentive.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,33 @@ func (dc *VotingPowerDistCache) FindNewActiveFinalityProviders(prevDc *VotingPow
func (dc *VotingPowerDistCache) ApplyActiveFinalityProviders(maxActiveFPs uint32) {
// reset total voting power
dc.TotalVotingPower = 0

// sort finality providers
SortFinalityProviders(dc.FinalityProviders)
// calculate voting power of top N finality providers
numActiveFPs := dc.GetNumActiveFPs(maxActiveFPs)
for i := uint32(0); i < numActiveFPs; i++ {

// calculate voting power and get top N active finality providers
dc.calculateNumActiveFps()
for i := uint32(0); i < dc.GetNumActiveFPs(maxActiveFPs); i++ {
dc.TotalVotingPower += dc.FinalityProviders[i].TotalVotingPower
}
}

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

// calculateNumActiveFps iterates all the finality providers in the cache, calculates
// the number of finality providers with positive voting power, and stores it in the
// cache
func (dc *VotingPowerDistCache) calculateNumActiveFps() {
activeCount := uint32(0)
for _, fp := range dc.FinalityProviders {
if fp.TotalVotingPower > 0 {
activeCount++
}
}

dc.NumActiveFps = activeCount
}

// GetActiveFinalityProviderSet returns a set of active finality providers
Expand Down
112 changes: 65 additions & 47 deletions x/btcstaking/types/incentive.pb.go

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

2 changes: 0 additions & 2 deletions x/finality/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,6 @@ Upon `MsgAddFinalitySig`, a Babylon node will execute as follows:

1. Ensure the finality provider has been registered in Babylon and is not
slashed.
2. Ensure the epoch that the finality provider is registered has been finalized
by BTC timestamping.
3. Ensure the finality provider has voting power at this height.
4. Ensure the finality provider has not previously casted the same vote.
5. Derive the EOTS public randomness using the committed EOTS master public
Expand Down

0 comments on commit c93c408

Please sign in to comment.