From 14015c9dd499d6c86d89d0e86fb5fe5d21ac1aed Mon Sep 17 00:00:00 2001 From: Joe Bowman Date: Fri, 5 Jan 2024 08:31:41 +0000 Subject: [PATCH] perf: avoid tiny delegations and redelegations (#1001) Co-authored-by: Jacob Gadikian --- x/interchainstaking/keeper/delegation.go | 6 +++++- x/interchainstaking/keeper/keeper.go | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/x/interchainstaking/keeper/delegation.go b/x/interchainstaking/keeper/delegation.go index a12f3c0ba..1f3bb45d5 100644 --- a/x/interchainstaking/keeper/delegation.go +++ b/x/interchainstaking/keeper/delegation.go @@ -195,7 +195,10 @@ func (*Keeper) PrepareDelegationMessagesForCoins(zone *types.Zone, allocations m var msgs []sdk.Msg for _, valoper := range utils.Keys(allocations) { if allocations[valoper].IsPositive() { - msgs = append(msgs, &stakingtypes.MsgDelegate{DelegatorAddress: zone.DelegationAddress.Address, ValidatorAddress: valoper, Amount: sdk.NewCoin(zone.BaseDenom, allocations[valoper])}) + if allocations[valoper].GTE(sdk.NewInt(1_000_000)) { + // don't delegate tiny amounts. TODO: make configurable per zone. + msgs = append(msgs, &stakingtypes.MsgDelegate{DelegatorAddress: zone.DelegationAddress.Address, ValidatorAddress: valoper, Amount: sdk.NewCoin(zone.BaseDenom, allocations[valoper])}) + } } } return msgs @@ -205,6 +208,7 @@ func (*Keeper) PrepareDelegationMessagesForShares(zone *types.Zone, coins sdk.Co var msgs []sdk.Msg for _, coin := range coins.Sort() { if coin.IsPositive() { + // no min amount here. msgs = append(msgs, &lsmstakingtypes.MsgRedeemTokensForShares{DelegatorAddress: zone.DelegationAddress.Address, Amount: coin}) } } diff --git a/x/interchainstaking/keeper/keeper.go b/x/interchainstaking/keeper/keeper.go index 0addccd1c..196e9a22d 100644 --- a/x/interchainstaking/keeper/keeper.go +++ b/x/interchainstaking/keeper/keeper.go @@ -725,14 +725,17 @@ func (k *Keeper) Rebalance(ctx sdk.Context, zone *types.Zone, epochNumber int64) rebalances := types.DetermineAllocationsForRebalancing(currentAllocations, currentLocked, currentSum, lockedSum, targetAllocations, maxCanAllocate, k.Logger(ctx)).RemoveDuplicates() msgs := make([]sdk.Msg, 0) for _, rebalance := range rebalances { - msgs = append(msgs, &stakingtypes.MsgBeginRedelegate{DelegatorAddress: zone.DelegationAddress.Address, ValidatorSrcAddress: rebalance.Source, ValidatorDstAddress: rebalance.Target, Amount: sdk.NewCoin(zone.BaseDenom, rebalance.Amount)}) - k.SetRedelegationRecord(ctx, types.RedelegationRecord{ - ChainId: zone.ChainId, - EpochNumber: epochNumber, - Source: rebalance.Source, - Destination: rebalance.Target, - Amount: rebalance.Amount.Int64(), - }) + if rebalance.Amount.GTE(sdk.NewInt(1_000_000)) { + // don't redelegate dust; TODO: config per zone + msgs = append(msgs, &stakingtypes.MsgBeginRedelegate{DelegatorAddress: zone.DelegationAddress.Address, ValidatorSrcAddress: rebalance.Source, ValidatorDstAddress: rebalance.Target, Amount: sdk.NewCoin(zone.BaseDenom, rebalance.Amount)}) + k.SetRedelegationRecord(ctx, types.RedelegationRecord{ + ChainId: zone.ChainId, + EpochNumber: epochNumber, + Source: rebalance.Source, + Destination: rebalance.Target, + Amount: rebalance.Amount.Int64(), + }) + } } if len(msgs) == 0 { k.Logger(ctx).Debug("No rebalancing required")