Skip to content

Commit

Permalink
chore: adding shape to FP reward tracker periods
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Dec 1, 2024
1 parent 598608c commit 530cd88
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 191 deletions.
175 changes: 0 additions & 175 deletions x/btcstaking/keeper/btc_delegators.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import (

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

corestoretypes "cosmossdk.io/core/store"
sdkmath "cosmossdk.io/math"
"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/btcsuite/btcd/chaincfg/chainhash"

Expand Down Expand Up @@ -81,175 +78,3 @@ func (k Keeper) btcDelegatorStore(ctx context.Context) prefix.Store {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
return prefix.NewStore(storeAdapter, types.BTCDelegatorKey)
}

// storeDelStaked returns the KVStore of the delegator amount staked
// prefix: (DelegatorStakedBTCKey)
// key: Del addr
// value: sdk math Int
func (k Keeper) storeDelStaked(ctx context.Context) prefix.Store {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
return prefix.NewStore(storeAdapter, types.DelegatorStakedBTCKey)
}

// Total active satoshi staked that is entitled to earn rewards.
func (k Keeper) TotalSatoshiStaked(ctx context.Context) (sdkmath.Int, error) {
kv := k.storeService.OpenKVStore(ctx)
key := types.TotalStakedBTCKey
return StoreGetInt(kv, key)
}

func (k Keeper) addTotalSatoshiStaked(ctx context.Context, amtToAdd sdkmath.Int) (sdkmath.Int, error) {
kv := k.storeService.OpenKVStore(ctx)
key := types.TotalStakedBTCKey

current, err := StoreGetInt(kv, key)
if err != nil {
return sdkmath.Int{}, err
}

total := current.Add(amtToAdd)
if err := StoreSetInt(kv, key, total); err != nil {
return sdkmath.Int{}, err
}

return total, nil
}

func (k Keeper) subTotalSatoshiStaked(ctx context.Context, amtToAdd sdkmath.Int) (sdkmath.Int, error) {
kv := k.storeService.OpenKVStore(ctx)
key := types.TotalStakedBTCKey

current, err := StoreGetInt(kv, key)
if err != nil {
return sdkmath.Int{}, err
}

total := current.Sub(amtToAdd)
if err := StoreSetInt(kv, key, total); err != nil {
return sdkmath.Int{}, err
}

return total, nil
}

func (k Keeper) AddDelStaking(ctx context.Context, del sdk.AccAddress, amt sdkmath.Int) error {
st := k.storeDelStaked(ctx)

currentStk, err := PrefixStoreGetInt(st, del)
if err != nil {
return err
}

totalDelStaked := currentStk.Add(amt)
bz, err := totalDelStaked.Marshal()
if err != nil {
return err
}

st.Set(del, bz)
_, err = k.addTotalSatoshiStaked(ctx, amt)
return err
}

func (k Keeper) SubDelStaking(ctx context.Context, del sdk.AccAddress, amt sdkmath.Int) error {
st := k.storeDelStaked(ctx)

currentStk, err := PrefixStoreGetInt(st, del)
if err != nil {
return err
}

totalDelStaked := currentStk.Sub(amt)
bz, err := totalDelStaked.Marshal()
if err != nil {
return err
}

st.Set(del, bz)
_, err = k.subTotalSatoshiStaked(ctx, amt)
return err
}

func PrefixStoreGetInt(st prefix.Store, key []byte) (vInt sdkmath.Int, err error) {
if !st.Has(key) {
return sdkmath.NewInt(0), nil
}

bz := st.Get(key)
vInt, err = ParseInt(bz)
if err != nil {
return sdkmath.Int{}, err
}

return vInt, nil
}

// StoreSetInt stores an sdkmath.Int from the KVStore.
func StoreSetInt(kv corestoretypes.KVStore, key []byte, vInt sdkmath.Int) (err error) {
bz, err := vInt.Marshal()
if err != nil {
return err
}
return kv.Set(key, bz)
}

// StoreGetInt retrieves an sdkmath.Int from the KVStore. It returns zero int if not found.
func StoreGetInt(kv corestoretypes.KVStore, key []byte) (vInt sdkmath.Int, err error) {
exists, err := kv.Has(key)
if err != nil {
return sdkmath.Int{}, err
}

if !exists {
return sdkmath.NewInt(0), nil
}

bz, err := kv.Get(key)
if err != nil {
return sdkmath.Int{}, err
}

vInt, err = ParseInt(bz)
if err != nil {
return sdkmath.Int{}, err
}
return vInt, nil
}

// ParseInt parses an sdkmath.Int from bytes.
func ParseInt(bz []byte) (sdkmath.Int, error) {
var val sdkmath.Int
if err := val.Unmarshal(bz); err != nil {
return val, err
}
return val, nil
}

// IterateBTCDelegators iterates over all the delegators that have some active BTC delegator
// staked and the total satoshi staked for that delegator address until an error is returned
// or the iterator finishes. Stops if error is returned.
// Should keep track of the total satoshi staked per delegator to avoid iterating over the
// delegator delegations
// func (k Keeper) IterateBTCDelegators(ctx context.Context, i func(delegator sdk.AccAddress, totalSatoshiStaked sdkmath.Int) error) error {
// st := k.storeDelStaked(ctx)

// iter := st.Iterator(nil, nil)
// defer iter.Close()

// for ; iter.Valid(); iter.Next() {
// sdkAddrBz := iter.Key()
// delAddr := sdk.AccAddress(sdkAddrBz)

// delBtcStaked, err := ParseInt(iter.Value())
// if err != nil {
// return err
// }

// err = i(delAddr, delBtcStaked)
// if err != nil {
// return err
// }
// }

// return nil
// }
2 changes: 0 additions & 2 deletions x/btcstaking/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ var (
// 0x07 was used for something else in the past
PowerDistUpdateKey = []byte{0x08} // key prefix for power distribution update events
AllowedStakingTxHashesKey = collections.NewPrefix(9) // key prefix for allowed staking tx hashes
DelegatorStakedBTCKey = []byte{0xA} // key prefix for delegator active staked amount int math.Int
TotalStakedBTCKey = []byte{0xB} // key prefix for protocol active staked amount int math.Int
)
10 changes: 10 additions & 0 deletions x/finality/keeper/power_dist_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
btcDel := *dc.FinalityProviders[i].BtcDels[j]
if _, ok := unbondedBTCDels[btcDel.StakingTxHash]; !ok {
fp.AddBTCDelDistInfo(&btcDel)

err := k.IncentiveKeeper.BtcDelegationUnbonded(ctx, fp.GetAddress(), sdk.MustAccAddressFromBech32(btcDel.StakerAddr), btcDel.TotalSat)
if err != nil {
panic(err) // check if it should panic
}
}
}

Expand Down Expand Up @@ -325,6 +330,11 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
fpActiveBTCDels := activeBTCDels[fpBTCPKHex]
for _, d := range fpActiveBTCDels {
fpDistInfo.AddBTCDel(d)

err := k.IncentiveKeeper.BtcDelegationActivated(ctx, sdk.MustAccAddressFromBech32(newFP.Addr), sdk.MustAccAddressFromBech32(d.StakerAddr), d.TotalSat)
if err != nil {
panic(err) // check if it should panic
}
}

// add this finality provider to the new cache if it has voting power
Expand Down
Loading

0 comments on commit 530cd88

Please sign in to comment.