Skip to content

Commit

Permalink
De-duplicate fetching intermediate accounts (#7411)
Browse files Browse the repository at this point in the history
* De-duplicate fetching intermediate accounts

* Fix build

* De-duplicate BondDenom fetch

(cherry picked from commit fda7971)
  • Loading branch information
ValarDragon authored and mergify[bot] committed Feb 5, 2024
1 parent 95f96f6 commit 06fc76c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion x/incentives/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ func (k Keeper) Distribute(ctx sdk.Context, gauges []types.Gauge) (sdk.Coins, er

locksByDenomCache := make(map[string][]lockuptypes.PeriodLock)
totalDistributedCoins := sdk.NewCoins()
scratchSlice := make([]*lockuptypes.PeriodLock, 0, 10000)
scratchSlice := make([]*lockuptypes.PeriodLock, 0, 50000)

for _, gauge := range gauges {
var gaugeDistributedCoins sdk.Coins
Expand Down
13 changes: 7 additions & 6 deletions x/superfluid/keeper/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ func (k Keeper) AfterEpochStartBeginBlock(ctx sdk.Context) {
// the supplied epoch number is wrong at time of commit. hence we get from the info.
curEpoch := k.ek.GetEpochInfo(ctx, k.GetEpochIdentifier(ctx)).CurrentEpoch

accs := k.GetAllIntermediaryAccounts(ctx)
// Move delegation rewards to perpetual gauge
ctx.Logger().Info("Move delegation rewards to gauges")
k.MoveSuperfluidDelegationRewardToGauges(ctx)
k.MoveSuperfluidDelegationRewardToGauges(ctx, accs)

ctx.Logger().Info("Distribute Superfluid gauges")
//nolint:errcheck
Expand All @@ -56,11 +57,11 @@ func (k Keeper) AfterEpochStartBeginBlock(ctx sdk.Context) {
// Refresh intermediary accounts' delegation amounts,
// making staking rewards follow the updated multiplier numbers.
ctx.Logger().Info("Refresh all superfluid delegation amounts")
k.RefreshIntermediaryDelegationAmounts(ctx)
k.RefreshIntermediaryDelegationAmounts(ctx, accs)
}

func (k Keeper) MoveSuperfluidDelegationRewardToGauges(ctx sdk.Context) {
accs := k.GetAllIntermediaryAccounts(ctx)
func (k Keeper) MoveSuperfluidDelegationRewardToGauges(ctx sdk.Context, accs []types.SuperfluidIntermediaryAccount) {
bondDenom := k.sk.BondDenom(ctx)
for _, acc := range accs {
addr := acc.GetAccAddress()
valAddr, err := sdk.ValAddressFromBech32(acc.ValAddr)
Expand All @@ -74,7 +75,8 @@ func (k Keeper) MoveSuperfluidDelegationRewardToGauges(ctx sdk.Context) {
_, err := k.ck.WithdrawDelegationRewards(cacheCtx, addr, valAddr)
if errors.Is(err, distributiontypes.ErrEmptyDelegationDistInfo) {
ctx.Logger().Debug("no swaps occurred in this pool between last epoch and this epoch")
return nil
} else if err != nil {
return err
}
return err
})
Expand All @@ -83,7 +85,6 @@ func (k Keeper) MoveSuperfluidDelegationRewardToGauges(ctx sdk.Context) {
_ = osmoutils.ApplyFuncIfNoError(ctx, func(cacheCtx sdk.Context) error {
// Note! We only send the bond denom (osmo), to avoid attack vectors where people
// send many different denoms to the intermediary account, and make a resource exhaustion attack on end block.
bondDenom := k.sk.BondDenom(cacheCtx)
balance := k.bk.GetBalance(cacheCtx, addr, bondDenom)
if balance.IsZero() {
return nil
Expand Down
6 changes: 4 additions & 2 deletions x/superfluid/keeper/epoch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ func (s *KeeperTestSuite) TestMoveSuperfluidDelegationRewardToGauges() {
}

// move intermediary account delegation rewards to gauges
s.App.SuperfluidKeeper.MoveSuperfluidDelegationRewardToGauges(s.Ctx)
accs := s.App.SuperfluidKeeper.GetAllIntermediaryAccounts(s.Ctx)
s.App.SuperfluidKeeper.MoveSuperfluidDelegationRewardToGauges(s.Ctx, accs)

// check invariant is fine
reason, broken := keeper.AllInvariants(*s.App.SuperfluidKeeper)(s.Ctx)
Expand Down Expand Up @@ -292,7 +293,8 @@ func (s *KeeperTestSuite) TestDistributeSuperfluidGauges() {
}

// move intermediary account delegation rewards to gauges
s.App.SuperfluidKeeper.MoveSuperfluidDelegationRewardToGauges(s.Ctx)
accs := s.App.SuperfluidKeeper.GetAllIntermediaryAccounts(s.Ctx)
s.App.SuperfluidKeeper.MoveSuperfluidDelegationRewardToGauges(s.Ctx, accs)

// move gauges to active gauge by declaring epoch end
s.Ctx = s.Ctx.WithBlockTime(s.Ctx.BlockTime().Add(time.Minute))
Expand Down
3 changes: 2 additions & 1 deletion x/superfluid/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@ func (h Hooks) AfterValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, in
if slashFactor.IsZero() {
return
}
h.k.RefreshIntermediaryDelegationAmounts(ctx)
accs := h.k.GetAllIntermediaryAccounts(ctx)
h.k.RefreshIntermediaryDelegationAmounts(ctx, accs)
}
3 changes: 1 addition & 2 deletions x/superfluid/keeper/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ func (k Keeper) GetExpectedDelegationAmount(ctx sdk.Context, acc types.Superflui
// RefreshIntermediaryDelegationAmounts refreshes the amount of delegation for all intermediary accounts.
// This method includes minting new osmo if the refreshed delegation amount has increased, and
// instantly undelegating and burning if the refreshed delegation has decreased.
func (k Keeper) RefreshIntermediaryDelegationAmounts(ctx sdk.Context) {
func (k Keeper) RefreshIntermediaryDelegationAmounts(ctx sdk.Context, accs []types.SuperfluidIntermediaryAccount) {
// iterate over all intermedairy accounts - every (denom, validator) pair
accs := k.GetAllIntermediaryAccounts(ctx)
for _, acc := range accs {
mAddr := acc.GetAccAddress()

Expand Down
6 changes: 4 additions & 2 deletions x/superfluid/keeper/stake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,8 @@ func (s *KeeperTestSuite) TestRefreshIntermediaryDelegationAmounts() {
s.App.SuperfluidKeeper.SetOsmoEquivalentMultiplier(s.Ctx, 2, denom, multiplier)
}

s.App.SuperfluidKeeper.RefreshIntermediaryDelegationAmounts(s.Ctx)
accs := s.App.SuperfluidKeeper.GetAllIntermediaryAccounts(s.Ctx)
s.App.SuperfluidKeeper.RefreshIntermediaryDelegationAmounts(s.Ctx, accs)

originalMultiplier := osmomath.NewDec(20)
for interAccIndex, intermediaryAcc := range intermediaryAccs {
Expand Down Expand Up @@ -1022,7 +1023,8 @@ func (s *KeeperTestSuite) TestRefreshIntermediaryDelegationAmounts() {
}

// refresh intermediary account delegations
s.App.SuperfluidKeeper.RefreshIntermediaryDelegationAmounts(s.Ctx)
accs = s.App.SuperfluidKeeper.GetAllIntermediaryAccounts(s.Ctx)
s.App.SuperfluidKeeper.RefreshIntermediaryDelegationAmounts(s.Ctx, accs)

for _, intermediaryAcc := range intermediaryAccs {
// check unbonded amount is removed after refresh operation
Expand Down

0 comments on commit 06fc76c

Please sign in to comment.