Skip to content

Commit

Permalink
update intent to use defaults as fallback when vp has not claimed (#510)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Johnson <[email protected]>
Co-authored-by: Ajaz Ahmed Ansari <[email protected]>
  • Loading branch information
3 people authored Jul 21, 2023
1 parent 8f30cdd commit 2a6fb85
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions x/interchainstaking/keeper/intent.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,15 @@ func (k *Keeper) AggregateDelegatorIntents(ctx sdk.Context, zone *types.Zone) er
snapshot := false
aggregate := make(types.ValidatorIntents, 0)
ordinalizedIntentSum := sdk.ZeroDec()
// reduce intents

k.IterateDelegatorIntents(ctx, zone, snapshot, func(_ int64, delIntent types.DelegatorIntent) (stop bool) {
// addr, localErr := sdk.AccAddressFromBech32(intent.Delegator)
// if localErr != nil {
// err = localErr
// return true
// }
// balance := k.BankKeeper.GetBalance(ctx, addr, zone.LocalDenom)
balance := sdk.NewCoin(zone.LocalDenom, sdkmath.ZeroInt())

// grab offchain asset value, and raise the users' base value by this amount.
// currently ignoring base value (locally held assets)
k.ClaimsManagerKeeper.IterateLastEpochUserClaims(ctx, zone.ChainId, delIntent.Delegator, func(index int64, data prtypes.Claim) (stop bool) {
balance.Amount = balance.Amount.Add(sdkmath.NewIntFromUint64(data.Amount))
// claim amounts are in zone.baseDenom - but given weights are all relative to one another this okay.
k.Logger(ctx).Error(
k.Logger(ctx).Debug(
"intents - found claim for user",
"user", delIntent.Delegator,
"claim amount", data.Amount,
Expand All @@ -120,7 +112,7 @@ func (k *Keeper) AggregateDelegatorIntents(ctx sdk.Context, zone *types.Zone) er
})

valIntents := delIntent.Ordinalize(sdk.NewDecFromInt(balance.Amount)).Intents
k.Logger(ctx).Error(
k.Logger(ctx).Debug(
"intents - ordinalized",
"user", delIntent.Delegator,
"new balance", balance.Amount,
Expand All @@ -142,14 +134,35 @@ func (k *Keeper) AggregateDelegatorIntents(ctx sdk.Context, zone *types.Zone) er
return false
})

// weight supply for which we do not have claim equally across active validators.
// this stops a small number of claimants exercising a disproportionate amount of
// power, in the event claims cannot be made properly.
supply := k.BankKeeper.GetSupply(ctx, zone.LocalDenom)
defaults := k.DefaultAggregateIntents(ctx, zone.ChainId)
nonVotingSupply := sdk.NewDecFromInt(supply.Amount).Sub(ordinalizedIntentSum)
di := types.DelegatorIntent{Delegator: "", Intents: defaults}
di = di.Ordinalize(nonVotingSupply)
defaults = di.Intents

for idx := range defaults.Sort() {
valIntent, found := aggregate.GetForValoper(defaults[idx].ValoperAddress)
ordinalizedIntentSum = ordinalizedIntentSum.Add(defaults[idx].Weight)
if !found {
aggregate = append(aggregate, defaults[idx])
} else {
valIntent.Weight = valIntent.Weight.Add(defaults[idx].Weight)
aggregate = aggregate.SetForValoper(defaults[idx].ValoperAddress, valIntent)
}
}

if len(aggregate) > 0 && ordinalizedIntentSum.IsZero() {
return errors.New("ordinalized intent sum is zero, this may happen if no claims are recorded")
}

// normalise aggregated intents again.
newAggregate := make(types.ValidatorIntents, 0)
for _, valIntent := range aggregate.Sort() {
if !valIntent.Weight.IsZero() && valIntent.Weight.IsPositive() {
if valIntent.Weight.IsPositive() {
valIntent.Weight = valIntent.Weight.Quo(ordinalizedIntentSum)
newAggregate = append(newAggregate, valIntent)
}
Expand Down

0 comments on commit 2a6fb85

Please sign in to comment.