Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unbond with rebalanced val set weights #6685

Merged
merged 9 commits into from
Oct 13, 2023

Conversation

czarcas7ic
Copy link
Member

@czarcas7ic czarcas7ic commented Oct 12, 2023

Closes: #XXX

What is the purpose of the change

The current implementation of unbonding from validator set is broken. It is really close to being fixed, but we decided to go with a method that simply determines the new weights of the val set based on the user's current delegations to that val set.

For ex: if my valset is 30/60 to valA and valB, but I have 10 OSMO delgated to valA and 90 to valB, if I call the new undelegate rebalanced method here for 50 OSMO, it undelegates 5 OSMO from valA and 45 OSMO from valB.

I know we have opinions on dead code existing in prod, but I really think it makes sense to just disable the original undelegate message, and post v20 we can make the small tweak needed to fix this and test it, rather than completely removing it and having to rewire it.

Testing and Verifying

Added a gotest to show the above behavior is what actually happens.

@czarcas7ic czarcas7ic added V:state/breaking State machine breaking PR A:backport/v20.x backport patches to v20.x branch labels Oct 12, 2023
@github-actions github-actions bot added the C:CLI label Oct 12, 2023
Comment on lines +52 to +60
// ctx := sdk.UnwrapSDKContext(goCtx)

// err := server.keeper.UndelegateFromValidatorSet(ctx, msg.Delegator, msg.Coin)
// if err != nil {
// return nil, err
// }

return &types.MsgUndelegateFromValidatorSetResponse{}, fmt.Errorf("not implemented, utilize UndelegateFromRebalancedValidatorSet instead")
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disables UndelegateFromValidatorSet in the msg server

// }
// }

func (s *KeeperTestSuite) TestUnDelegateFromRebalancedValidatorSet() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestUnDelegateFromRebalancedValidatorSet is identical to the commented out TestUnDelegateFromValidatorSet, because the behavior of UnDelegateFromValidatorSet is technically UnDelegateFromRebalancedValidatorSet.

Comment on lines +48 to +55
// func NewUnDelValSetCmd() (*osmocli.TxCliDesc, *types.MsgUndelegateFromValidatorSet) {
// return &osmocli.TxCliDesc{
// Use: "undelegate-valset",
// Short: "UnDelegate tokens from existing valset using delegatorAddress and tokenAmount.",
// Example: "osmosisd tx valset-pref undelegate-valset osmo1... 100stake",
// NumArgs: 2,
// }, &types.MsgUndelegateFromValidatorSet{}
// }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disable the CLI for undelegate-valset cmd.

@czarcas7ic czarcas7ic marked this pull request as ready for review October 12, 2023 20:46
Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed having a separate function offline and I agree this gets us to a preferable place in the tradeoff space (where minimizing last-minute implementation overhead is a high priority).

I left two comments covering what I believe are blocking issues that should be quick to address. The rest looks good to me.

proto/osmosis/valset-pref/v1beta1/tx.proto Outdated Show resolved Hide resolved
x/valset-pref/client/cli/tx.go Show resolved Hide resolved
x/valset-pref/msg_server.go Outdated Show resolved Hide resolved
x/valset-pref/validator_set_test.go Show resolved Hide resolved
}

// System Under Test
err := s.App.ValidatorSetPreferenceKeeper.UndelegateFromRebalancedValidatorSet(s.Ctx, defaultDelegator.String(), sdk.NewCoin(bondDenom, test.undelegateAmt))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't want to block this PR, but I think we're actually not testing the main important thing here which is that directly delegating/undelegating from a validator changes the weights when the new message is called.

E.g. if it starts at 0.1/0.9 with 10 tokens in validator A and 90 in validator B, then when you delegate 100 tokens to validator A, then withdrawing rebalanced should yield a distribution of 0.55/0.45 (since now validator A has 110 tokens out of 200 and validator B has 90 out of 200)

Would it be possible for us to add tests covering these? I think this could potentially be done with an additional test field and putting system under test into a loop so it runs multiple times with different conditions.

Comment on lines 286 to 290
// in the last valset iteration we don't calculate it from shares using decimals and trucation,
// we use whats remaining to get more accurate value
if len(existingSet.Preferences)-1 == index {
amountToUnDelegate = undelegation.Amount.Sub(totalUnDelAmt).ToLegacyDec().TruncateInt()
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets make amountToUndelegate = Min(amountToUndelegate, amount_staked_to_this_val)

just in case some rounding issue increases it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the logic you described cc584e7

Although I am not quite sure how to force this in a gotest, seems like just a guard rail.

Copy link
Member

@ValarDragon ValarDragon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic Lgtm; have not reviewed tests

@czarcas7ic czarcas7ic merged commit ce280e8 into dev/valset-pref-unbond-issues Oct 13, 2023
1 check passed
@czarcas7ic czarcas7ic deleted the adam/valset-pref-unbond-v20 branch October 13, 2023 18:46
AlpinYukseloglu pushed a commit that referenced this pull request Oct 13, 2023
* Add helper to get existing delegations along with valset pref

* Lay out more of the logic nuance

* Add pseudo-code needed

* algorithm v1

* added algorithm docs

* fixed all test

* removed unwanted files

* remove more code

* added more tests

* update changelog

* added test and addressed feedback

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/README.md

* Minor cleanup

* re-use validator gets

* Refactor

* Highlight bug

* fixed the issue

* added comments

* update changelog

* fixed the issue

* fixed go test

* fixed test

* lint

* Finish ValSet Pref Unbonding (#6630)

* Add Test cases and fix

* Update x/valset-pref/keeper.go

* Adams comment

* add error types and tidy test

---------

Co-authored-by: Adam Tucker <adam@osmosis.team>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>

* feat: unbond with rebalanced val set weights (#6685)

* initial push

* add todo

* msg server test

* add godoc

* custom error

* update godoc

* update proto and remove duplication

* add more TODOs

* use min when calculating amt to withdraw from last val

* Update x/valset-pref/validator_set.go

---------

Co-authored-by: stackman27 <sis1001@berkeley.edu>
Co-authored-by: devbot-wizard <141283918+devbot-wizard@users.noreply.github.com>
Co-authored-by: Adam Tucker <adam@osmosis.team>
Co-authored-by: roman <roman@osmosis.team>
Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>
mergify bot pushed a commit that referenced this pull request Oct 13, 2023
* Add helper to get existing delegations along with valset pref

* Lay out more of the logic nuance

* Add pseudo-code needed

* algorithm v1

* added algorithm docs

* fixed all test

* removed unwanted files

* remove more code

* added more tests

* update changelog

* added test and addressed feedback

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/README.md

* Minor cleanup

* re-use validator gets

* Refactor

* Highlight bug

* fixed the issue

* added comments

* update changelog

* fixed the issue

* fixed go test

* fixed test

* lint

* Finish ValSet Pref Unbonding (#6630)

* Add Test cases and fix

* Update x/valset-pref/keeper.go

* Adams comment

* add error types and tidy test

---------

Co-authored-by: Adam Tucker <adam@osmosis.team>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>

* feat: unbond with rebalanced val set weights (#6685)

* initial push

* add todo

* msg server test

* add godoc

* custom error

* update godoc

* update proto and remove duplication

* add more TODOs

* use min when calculating amt to withdraw from last val

* Update x/valset-pref/validator_set.go

---------

Co-authored-by: stackman27 <sis1001@berkeley.edu>
Co-authored-by: devbot-wizard <141283918+devbot-wizard@users.noreply.github.com>
Co-authored-by: Adam Tucker <adam@osmosis.team>
Co-authored-by: roman <roman@osmosis.team>
Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>
(cherry picked from commit e12011b)
czarcas7ic pushed a commit that referenced this pull request Oct 13, 2023
* Add helper to get existing delegations along with valset pref

* Lay out more of the logic nuance

* Add pseudo-code needed

* algorithm v1

* added algorithm docs

* fixed all test

* removed unwanted files

* remove more code

* added more tests

* update changelog

* added test and addressed feedback

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/README.md

* Minor cleanup

* re-use validator gets

* Refactor

* Highlight bug

* fixed the issue

* added comments

* update changelog

* fixed the issue

* fixed go test

* fixed test

* lint

* Finish ValSet Pref Unbonding (#6630)

* Add Test cases and fix

* Update x/valset-pref/keeper.go

* Adams comment

* add error types and tidy test

---------

Co-authored-by: Adam Tucker <adam@osmosis.team>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>

* feat: unbond with rebalanced val set weights (#6685)

* initial push

* add todo

* msg server test

* add godoc

* custom error

* update godoc

* update proto and remove duplication

* add more TODOs

* use min when calculating amt to withdraw from last val

* Update x/valset-pref/validator_set.go

---------

Co-authored-by: stackman27 <sis1001@berkeley.edu>
Co-authored-by: devbot-wizard <141283918+devbot-wizard@users.noreply.github.com>
Co-authored-by: Adam Tucker <adam@osmosis.team>
Co-authored-by: roman <roman@osmosis.team>
Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>
(cherry picked from commit e12011b)

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
pysel pushed a commit that referenced this pull request Oct 17, 2023
* Add helper to get existing delegations along with valset pref

* Lay out more of the logic nuance

* Add pseudo-code needed

* algorithm v1

* added algorithm docs

* fixed all test

* removed unwanted files

* remove more code

* added more tests

* update changelog

* added test and addressed feedback

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/validator_set.go

* Update x/valset-pref/README.md

* Minor cleanup

* re-use validator gets

* Refactor

* Highlight bug

* fixed the issue

* added comments

* update changelog

* fixed the issue

* fixed go test

* fixed test

* lint

* Finish ValSet Pref Unbonding (#6630)

* Add Test cases and fix

* Update x/valset-pref/keeper.go

* Adams comment

* add error types and tidy test

---------

Co-authored-by: Adam Tucker <adam@osmosis.team>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>

* feat: unbond with rebalanced val set weights (#6685)

* initial push

* add todo

* msg server test

* add godoc

* custom error

* update godoc

* update proto and remove duplication

* add more TODOs

* use min when calculating amt to withdraw from last val

* Update x/valset-pref/validator_set.go

---------

Co-authored-by: stackman27 <sis1001@berkeley.edu>
Co-authored-by: devbot-wizard <141283918+devbot-wizard@users.noreply.github.com>
Co-authored-by: Adam Tucker <adam@osmosis.team>
Co-authored-by: roman <roman@osmosis.team>
Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v20.x backport patches to v20.x branch A:no-changelog C:CLI V:state/breaking State machine breaking PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants