Skip to content

Commit

Permalink
fix: ignore small share roundings smaller than 1e6
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Feb 1, 2023
1 parent f79cb93 commit 4874492
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
13 changes: 7 additions & 6 deletions x/alliance/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,15 @@ func (k Keeper) SetValidatorInfo(ctx sdk.Context, valAddr sdk.ValAddress, val ty
// Returns the number of shares that represents the amount of staked tokens that was requested
func (k Keeper) ValidateDelegatedAmount(delegation types.Delegation, coin sdk.Coin, val types.AllianceValidator, asset types.AllianceAsset) (shares sdk.Dec, err error) {
delegationSharesToUpdate := types.GetDelegationSharesFromTokens(val, asset, coin.Amount)
if delegation.Shares.LT(delegationSharesToUpdate.TruncateDec()) {
return sdk.Dec{}, stakingtypes.ErrInsufficientShares
}

// Account for rounding in which shares for a full withdraw is slightly more or less than the number of shares recorded
// Withdraw all in that case
if delegation.Shares.Sub(delegationSharesToUpdate).Abs().LT(sdk.OneDec()) {
delegationSharesToUpdate = delegation.Shares
// 1e6 of margin should be enough to handle realistic rounding issues caused by using the fix-point math.
if delegation.Shares.Sub(delegationSharesToUpdate).Abs().LT(sdk.NewDecWithPrec(1, 6)) {
return delegation.Shares, nil
}

if delegation.Shares.LT(delegationSharesToUpdate.TruncateDec()) {
return sdk.Dec{}, stakingtypes.ErrInsufficientShares
}

return delegationSharesToUpdate, nil
Expand Down
9 changes: 6 additions & 3 deletions x/alliance/tests/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

var (
SEED = 1
SEED = int64(1)
NumOfBlocks = 1000
BlocktimeInSeconds = 5
VoteRate = 0.8
Expand All @@ -38,7 +38,7 @@ var (
var createdDelegations = []types.Delegation{}

func TestRunBenchmarks(t *testing.T) {
r := rand.New(rand.NewSource(1))
r := rand.New(rand.NewSource(SEED))
app, ctx, assets, vals, dels := benchmark.SetupApp(t, r, NumOfAssets, NumOfValidators, NumOfDelegators)
powerReduction := sdk.OneInt()
operations := make(map[string]int)
Expand Down Expand Up @@ -212,7 +212,10 @@ func undelegateOperation(ctx sdk.Context, app *test_helpers.App, r *rand.Rand) {
if amountToUndelegate.IsZero() {
return
}
app.AllianceKeeper.Undelegate(ctx, delAddr, validator, sdk.NewCoin(asset.Denom, amountToUndelegate)) //nolint:errcheck
_, err := app.AllianceKeeper.Undelegate(ctx, delAddr, validator, sdk.NewCoin(asset.Denom, amountToUndelegate)) //nolint:errcheck
if err != nil {
panic(err)
}
}

func claimRewardsOperation(ctx sdk.Context, app *test_helpers.App, r *rand.Rand) {
Expand Down

0 comments on commit 4874492

Please sign in to comment.