Skip to content

Commit

Permalink
chore: removed BTC del distribution info structure
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Dec 19, 2024
1 parent 46a5156 commit c0417f0
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 546 deletions.
21 changes: 3 additions & 18 deletions proto/babylon/finality/v1/finality.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,16 @@ message FinalityProviderDistInfo {
];
// total_bonded_sat is the total amount of bonded BTC stake (in Satoshi) of the finality provider
uint64 total_bonded_sat = 4;
// btc_dels is a list of BTC delegations' voting power information under this finality provider
repeated BTCDelDistInfo btc_dels = 5;
// is_timestamped indicates whether the finality provider
// has timestamped public randomness committed
// if no, it should not be assigned voting power
bool is_timestamped = 6;
bool is_timestamped = 5;
// is_jailed indicates whether the finality provider
// is jailed, if so, it should not be assigned voting power
bool is_jailed = 7;
bool is_jailed = 6;
// is_slashed indicates whether the finality provider
// is slashed, if so, it should not be assigned voting power
bool is_slashed = 8;
}

// BTCDelDistInfo contains the information related to voting power distribution for a BTC delegation
message BTCDelDistInfo {
// btc_pk is the Bitcoin secp256k1 PK of this BTC delegation
// the PK follows encoding in BIP-340 spec
bytes btc_pk = 1 [ (gogoproto.customtype) = "github.com/babylonlabs-io/babylon/types.BIP340PubKey" ];
// staker_addr is the address to receive rewards from BTC delegation.
string staker_addr = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// staking_tx_hash is the staking tx hash of the BTC delegation
string staking_tx_hash = 3;
// total_sat is the amount of BTC stake (in Satoshi) of the BTC delegation
uint64 total_sat = 4;
bool is_slashed = 7;
}

// IndexedBlock is the necessary metadata and finalization status of a block
Expand Down
52 changes: 27 additions & 25 deletions testutil/datagen/incentive.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,53 +78,55 @@ func GenRandomGauge(r *rand.Rand) *itypes.Gauge {
return itypes.NewGauge(coins...)
}

func GenRandomBTCDelDistInfo(r *rand.Rand) (*ftypes.BTCDelDistInfo, error) {
btcPK, err := GenRandomBIP340PubKey(r)
if err != nil {
return nil, err
}
return &ftypes.BTCDelDistInfo{
BtcPk: btcPK,
StakerAddr: GenRandomAccount().Address,
TotalSat: RandomInt(r, 1000) + 1,
}, nil
func GenRandomAddrAndSat(r *rand.Rand) (string, uint64) {
return GenRandomAccount().Address, RandomInt(r, 1000) + 1
}

func GenRandomFinalityProviderDistInfo(r *rand.Rand) (*ftypes.FinalityProviderDistInfo, error) {
func GenRandomFinalityProviderDistInfo(r *rand.Rand) (
fpDistInfo *ftypes.FinalityProviderDistInfo,
btcTotalSatByAddress map[string]uint64,
err error,
) {
// create finality provider with random commission
fp, err := GenRandomFinalityProvider(r)
if err != nil {
return nil, err
return nil, nil, err
}
// create finality provider distribution info
fpDistInfo := ftypes.NewFinalityProviderDistInfo(fp)
fpDistInfo = ftypes.NewFinalityProviderDistInfo(fp)
// add a random number of BTC delegation distribution info
numBTCDels := RandomInt(r, 100) + 1
btcTotalSatByAddress = make(map[string]uint64, numBTCDels)
for i := uint64(0); i < numBTCDels; i++ {
btcDelDistInfo, err := GenRandomBTCDelDistInfo(r)
if err != nil {
return nil, err
}
fpDistInfo.BtcDels = append(fpDistInfo.BtcDels, btcDelDistInfo)
fpDistInfo.TotalBondedSat += btcDelDistInfo.TotalSat
btcAddr, totalSat := GenRandomAddrAndSat(r)
btcTotalSatByAddress[btcAddr] += totalSat
fpDistInfo.TotalBondedSat += totalSat
fpDistInfo.IsTimestamped = true
}
return fpDistInfo, nil
return fpDistInfo, btcTotalSatByAddress, nil
}

func GenRandomVotingPowerDistCache(r *rand.Rand, maxFPs uint32) (*ftypes.VotingPowerDistCache, error) {
dc := ftypes.NewVotingPowerDistCache()
func GenRandomVotingPowerDistCache(r *rand.Rand, maxFPs uint32) (
dc *ftypes.VotingPowerDistCache,
// fpAddr => delAddr => totalSat
btcTotalSatByDelAddressByFpAddress map[string]map[string]uint64,
err error,
) {
dc = ftypes.NewVotingPowerDistCache()
// a random number of finality providers
numFps := RandomInt(r, 10) + 1

btcTotalSatByDelAddressByFpAddress = make(map[string]map[string]uint64, numFps)
for i := uint64(0); i < numFps; i++ {
v, err := GenRandomFinalityProviderDistInfo(r)
v, btcTotalSatByAddress, err := GenRandomFinalityProviderDistInfo(r)
if err != nil {
return nil, err
return nil, nil, err
}
btcTotalSatByDelAddressByFpAddress[v.GetAddress().String()] = btcTotalSatByAddress
dc.AddFinalityProviderDistInfo(v)
}
dc.ApplyActiveFinalityProviders(maxFPs)
return dc, nil
return dc, btcTotalSatByDelAddressByFpAddress, nil
}

func GenRandomCheckpointAddressPair(r *rand.Rand) *btcctypes.CheckpointAddressPair {
Expand Down
50 changes: 34 additions & 16 deletions x/finality/keeper/power_dist_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
// a map where key is finality provider's BTC PK hex and value is a list
// of BTC delegations that newly become active under this provider
activeBTCDels := map[string][]*types.BTCDelegation{}
// a map where key is unbonded BTC delegation's staking tx hash
unbondedBTCDels := map[string]struct{}{}
// a map where key is finality provider's BTC PK hex and value is a list
// of BTC delegations that were unbonded or expired without previously
// being unbonded
unbondedBTCDels := map[string][]*types.BTCDelegation{}
// a map where key is slashed finality providers' BTC PK
slashedFPs := map[string]struct{}{}
// a map where key is jailed finality providers' BTC PK
Expand Down Expand Up @@ -216,7 +218,11 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
})
case types.BTCDelegationStatus_UNBONDED:
// add the unbonded BTC delegation to the map
unbondedBTCDels[delStkTxHash] = struct{}{}
// unbondedBTCDels[delStkTxHash] = struct{}{}
for _, fpBTCPK := range btcDel.FpBtcPkList {
fpBTCPKHex := fpBTCPK.MarshalHex()
unbondedBTCDels[fpBTCPKHex] = append(unbondedBTCDels[fpBTCPKHex], btcDel)
}
k.processRewardTracker(ctx, cacheFpByBtcPkHex, btcDel, func(fp, del sdk.AccAddress, sats uint64) {
k.MustProcessBtcDelegationUnbonded(ctx, fp, del, sats)
})
Expand All @@ -226,7 +232,11 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
if !btcDel.IsUnbondedEarly() {
// only adds to the new unbonded list if it hasn't
// previously unbonded with types.BTCDelegationStatus_UNBONDED
unbondedBTCDels[delStkTxHash] = struct{}{}
// unbondedBTCDels[delStkTxHash] = struct{}{}
for _, fpBTCPK := range btcDel.FpBtcPkList {
fpBTCPKHex := fpBTCPK.MarshalHex()
unbondedBTCDels[fpBTCPKHex] = append(unbondedBTCDels[fpBTCPKHex], btcDel)
}
k.processRewardTracker(ctx, cacheFpByBtcPkHex, btcDel, func(fp, del sdk.AccAddress, sats uint64) {
k.MustProcessBtcDelegationUnbonded(ctx, fp, del, sats)
})
Expand Down Expand Up @@ -261,9 +271,6 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
for i := range dc.FinalityProviders {
// create a copy of the finality provider
fp := *dc.FinalityProviders[i]
fp.TotalBondedSat = 0
fp.BtcDels = []*ftypes.BTCDelDistInfo{}

fpBTCPKHex := fp.BtcPk.MarshalHex()

// if this finality provider is slashed, continue to avoid
Expand All @@ -289,16 +296,17 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
fp.IsJailed = false
}

// add all BTC delegations that are not unbonded to the new finality provider
for j := range dc.FinalityProviders[i].BtcDels {
btcDel := *dc.FinalityProviders[i].BtcDels[j]

_, isUnbondedBtcDelegation := unbondedBTCDels[btcDel.StakingTxHash]
if isUnbondedBtcDelegation {
continue
// process all new BTC delegations under this finality provider
if fpUnbondedBTCDels, ok := unbondedBTCDels[fpBTCPKHex]; ok {
// handle unbonded delegations for this finality provider
for _, d := range fpUnbondedBTCDels {
fp.UnbondBTCDel(d)
}
// if it is not unbonded add to the del dist info
fp.AddBTCDelDistInfo(&btcDel)
// remove the finality provider entry in unbondedBTCDels map, so that
// after the for loop the rest entries in unbondedBTCDels belongs to new
// finality providers that might have btc delegations entries
// that activated and unbonded in the same slice of events
delete(unbondedBTCDels, fpBTCPKHex)
}

// process all new BTC delegations under this finality provider
Expand Down Expand Up @@ -343,6 +351,16 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
fpDistInfo.AddBTCDel(d)
}

// edge case where we might be processing an unbonded event
// from a newly active finality provider in the same slice
// of events received.
fpUnbondedBTCDels, ok := unbondedBTCDels[fpBTCPKHex]
if ok {
for _, d := range fpUnbondedBTCDels {
fpDistInfo.UnbondBTCDel(d)
}
}

// add this finality provider to the new cache if it has voting power
if fpDistInfo.TotalBondedSat > 0 {
newDc.AddFinalityProviderDistInfo(fpDistInfo)
Expand Down
4 changes: 0 additions & 4 deletions x/finality/keeper/power_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,6 @@ func FuzzRecordVotingPowerDistCache(f *testing.F) {
fp, ok := fpsWithVotingPowerMap[sdk.AccAddress(fpDistInfo.Addr).String()]
require.True(t, ok)
require.Equal(t, fpDistInfo.Commission, fp.Commission)
require.Len(t, fpDistInfo.BtcDels, int(numBTCDels))
for _, delDistInfo := range fpDistInfo.BtcDels {
require.Equal(t, delDistInfo.TotalSat, stakingValue)
}
}
})
}
Expand Down
Loading

0 comments on commit c0417f0

Please sign in to comment.