Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: port Initia Pool and DecPools fixes #164

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x/rewards/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (k *Keeper) calculateDelegationRewardsBetween(
}

for _, diff := range differences {
rewards = append(rewards, types.NewDecPool(
rewards = rewards.Add(types.NewDecPool(
diff.Denom,
diff.DecCoins.MulDecTruncate(stakes.AmountOf(diff.Denom)),
))
Expand Down
4 changes: 2 additions & 2 deletions x/rewards/keeper/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func (k *Keeper) IncrementDelegationTargetPeriod(ctx context.Context, target res
// can't calculate ratio for zero-token targets
// ergo we instead add to the community pool
communityFunding = communityFunding.Add(types.NewDecPool(token.Denom, rewardCoins))
current = append(current, types.NewDecPool(token.Denom, sdk.DecCoins{}))
current = current.Add(types.NewDecPool(token.Denom, sdk.DecCoins{}))
} else {
current = append(current,
current = current.Add(
types.NewDecPool(token.Denom, rewardCoins.QuoDecTruncate(math.LegacyNewDecFromInt(token.Amount))))
}
}
Expand Down
31 changes: 26 additions & 5 deletions x/rewards/types/dec_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewDecPoolsFromPools(pools Pools) DecPools {
decPools = append(decPools, NewDecPool(p.Denom, sdk.NewDecCoinsFromCoins(p.Coins...)))
}

return decPools
return decPools.Sort()
}

// Sum returns sum of pool tokens
Expand All @@ -38,6 +38,13 @@ func (pools DecPools) Add(poolsB ...DecPool) DecPools {

// Add will perform addition of two DecPools sets.
func (pools DecPools) safeAdd(poolsB DecPools) DecPools {
if !pools.isSorted() {
panic("Pools (self) must be sorted")
}
if !poolsB.isSorted() {
panic("Wrong argument: Pools must be sorted")
}

sum := ([]DecPool)(nil)
indexA, indexB := 0, 0
lenA, lenB := len(pools), len(poolsB)
Expand Down Expand Up @@ -256,16 +263,30 @@ var _ sort.Interface = DecPools{}

// Sort is a helper function to sort the set of p in-place
func (pools DecPools) Sort() DecPools {
sort.Sort(pools)
// sort.Sort does a costly runtime copy as part of `runtime.convTSlice`
// So we avoid this heap allocation if len(dec pools) <= 1. In the future, we should hopefully find
// a strategy to always avoid this.
if len(pools) > 1 {
sort.Sort(pools)
}
return pools
}

func (pools DecPools) isSorted() bool {
for i := 1; i < len(pools); i++ {
if pools[i-1].Denom > pools[i].Denom {
return false
}
}
return true
}

//-----------------------------------------------------------------------------
// DecPool functions

// NewDecPool return new pool instance
func NewDecPool(denom string, coins sdk.DecCoins) DecPool {
return DecPool{denom, coins}
return DecPool{denom, sdk.NewDecCoins(coins...)}
}

// IsEmpty returns wether the pool coins are empty or not
Expand Down Expand Up @@ -301,7 +322,7 @@ func (pool DecPool) TruncateDecimal() (Pool, DecPool) {
}

func removeZeroDecPools(pools DecPools) DecPools {
result := make([]DecPool, 0, len(pools))
result := make(DecPools, 0, len(pools))

for _, pool := range pools {
if !pool.IsEmpty() {
Expand All @@ -315,7 +336,7 @@ func removeZeroDecPools(pools DecPools) DecPools {
// IsEqual returns true if the two sets of DecPools have the same value.
func (pool DecPool) IsEqual(other DecPool) bool {
if pool.Denom != other.Denom {
panic(fmt.Sprintf("invalid pool denominations; %s, %s", pool.Denom, other.Denom))
return false
}

return pool.DecCoins.Equal(other.DecCoins)
Expand Down
Loading
Loading