Skip to content

Commit

Permalink
Merge pull request #16 from terra-money/bugfix/reload-validator-after…
Browse files Browse the repository at this point in the history
…-delegate

[Bugfix] Reload validator after delegation
  • Loading branch information
yun-yeo authored May 25, 2022
2 parents 7c29ae9 + 1940937 commit 6779091
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ func (app *TerraApp) enforceStakingForVestingTokens(ctx sdk.Context, genesisStat

i := 0
stakeSplitCondition := sdk.NewInt(1_000_000_000_000)
powerReduction := app.StakingKeeper.PowerReduction(ctx)
for _, acc := range authState.GetAccounts() {
var account authtypes.AccountI
if err := app.InterfaceRegistry().UnpackAny(acc, &account); err != nil {
Expand All @@ -869,24 +870,36 @@ func (app *TerraApp) enforceStakingForVestingTokens(ctx sdk.Context, genesisStat
if vestingAcc, ok := account.(vestingexported.VestingAccount); ok {
amt := vestingAcc.GetOriginalVesting().AmountOf(app.StakingKeeper.BondDenom(ctx))

// to prevent staking multiple times over the same validator
// adjust split amount for the whale account
splitAmt := stakeSplitCondition
if amt.GT(stakeSplitCondition.MulRaw(int64(validatorLen))) {
splitAmt = amt.QuoRaw(int64(validatorLen))
}

// if a vesting account has more staking token than `stakeSplitCondition`,
// split staking balance to distribute staking power evenly
// Ex) 2_200_000_000_000
// stake 1_000_000_000_000 to val1
// stake 1_000_000_000_000 to val2
// stake 200_000_000_000 to val3
for ; amt.GTE(sdk.OneInt()); amt = amt.Sub(stakeSplitCondition) {
for ; amt.GTE(powerReduction); amt = amt.Sub(splitAmt) {
validator := validators[i%validatorLen]
if _, err := app.StakingKeeper.Delegate(
ctx,
vestingAcc.GetAddress(),
sdk.MinInt(amt, stakeSplitCondition),
sdk.MinInt(amt, splitAmt),
stakingtypes.Unbonded,
validators[i%validatorLen],
validator,
true,
); err != nil {
panic(err)
}

// reload validator to avoid power index problem
validator, _ = app.StakingKeeper.GetValidator(ctx, validator.GetOperator())
validators[i%validatorLen] = validator

// increase index only when staking happened
i++
}
Expand Down

0 comments on commit 6779091

Please sign in to comment.