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

Make SDK staking hook changes robust to order of staking.EndBlock and CanComplete #233

Closed
danwt opened this issue Jul 14, 2022 · 5 comments
Assignees
Labels
scope: cosmos-sdk Integration with Cosmos SDK type: bug Issues that need priority attention -- something isn't working type: feature-request New feature or request improvement

Comments

@danwt
Copy link
Contributor

danwt commented Jul 14, 2022

In the current latest commit

https://github.com/cosmos/cosmos-sdk/blob/c783aea68fbd856c2b188b2d467a7fa5cb4df1e6/x/staking/keeper/unbonding.go#L332-L343

func (k Keeper) validatorUnbondingCanComplete(ctx sdk.Context, id uint64) (found bool, err error) {
	val, found := k.GetValidatorByUnbondingId(ctx, id)
	if !found {
		return false, nil
	}


	if !val.IsMature(ctx.BlockTime(), ctx.BlockHeight()) {
		val.UnbondingOnHold = false
		k.SetValidator(ctx, val)
	} else {
		// If unbonding is mature complete it
		val = k.UnbondingToUnbonded(ctx, val)
		if val.GetDelegatorShares().IsZero() {
			k.RemoveValidator(ctx, val.GetOperator())
		}


		k.DeleteUnbondingIndex(ctx, id)
	}


	return true, nil
}

liveness property for unbonding in all cases is dependent on the order of staking.EndBlock and (at least) validatorUnbondingCanComplete.

Since the CanComplete functions are SDK side and can be called by anyone we must either enforce a contract on ordering, and be OK with that, or change the design slightly to allow liveness in all cases.

The last commit is faulty anyway due to

so we have to make some changes anyway.

I have a fix for #232 here https://github.com/danwt/cosmos-sdk/tree/7c48c6c11f6d57ae96ad94466078fdf68df0bf73 but it is explicity requiring a ordering contract (see lines)

// WARNING: precondition:
// Safety only guaranteed if this method is called AFTER staking.EndBlock
func (k Keeper) validatorUnbondingCanComplete(ctx sdk.Context, id uint64) (found bool, err error) {

so the job is not complete.

@danwt danwt added type: bug Issues that need priority attention -- something isn't working scope: cosmos-sdk Integration with Cosmos SDK ccv-core type: feature-request New feature or request improvement labels Jul 14, 2022
@danwt danwt moved this to Todo in Replicated Security Jul 14, 2022
@mpoke
Copy link
Contributor

mpoke commented Jul 28, 2022

Safety only guaranteed if this method is called AFTER staking.EndBlock

If we put such a comment, we should be more precise with the terminology. Safety is guaranteed regardless. If the function is called before and the validator unbonding is matured, then the validator moves to UNBONDED and the code panics when UnbondingToUnbonded is called in staking.EndBlock (see here).

@mpoke
Copy link
Contributor

mpoke commented Jul 28, 2022

A solution could be to not do anything on the else branch here https://github.com/cosmos/cosmos-sdk/blob/c783aea68fbd856c2b188b2d467a7fa5cb4df1e6/x/staking/keeper/unbonding.go#L332-L343 and to remove from the ValidatorQueue only the validator addresses that can be unbonded instead of all the addresses that are supposed to unbond at a given time, i.e., https://github.com/cosmos/cosmos-sdk/blob/c783aea68fbd856c2b188b2d467a7fa5cb4df1e6/x/staking/keeper/validator.go#L445

@danwt
Copy link
Contributor Author

danwt commented Aug 15, 2022

Do we get this for free due to the new implemention that uses reference counting? I will check.

@mpoke
Copy link
Contributor

mpoke commented Aug 15, 2022

Probably

@danwt
Copy link
Contributor Author

danwt commented Sep 1, 2022

I think this is fine.

@danwt danwt closed this as completed Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: cosmos-sdk Integration with Cosmos SDK type: bug Issues that need priority attention -- something isn't working type: feature-request New feature or request improvement
Projects
No open projects
Status: Done
Development

No branches or pull requests

2 participants