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

epoching: fuzz tests on epoched undelegations and redelegations #66

Merged
merged 4 commits into from
Jul 21, 2022

Conversation

SebastianElvis
Copy link
Member

Fixes BM-63

This PR provides fuzz tests on MsgWrappedUndelegate and MsgWrappedBeginRedelegate, where we perform a random number of undelegations/redelegations, enter a new epoch, and assert the messages have taken effect. The implementation follows the testing infra and the test on MsgWrappedDelegate introduced in #65.

This PR will mark the end of testing the epoching module solely. Some other tests such as MsgWrappedCreateValidator require working with other modules such as x/checkpointing/. We will implement these tests in the integration test phase
when SimApp is ready.

Copy link
Contributor

@aakoshh aakoshh left a comment

Choose a reason for hiding this comment

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

This looks dandy, but the limited size of the unbonding queue is a bit worrying.

How did you come across that setting? Was it failing tests, or just saw it in the code?

@@ -98,7 +98,8 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))

bondAmt := sdk.NewInt(1000000)
// sdk.DefaultPowerReduction is 1 unit of voting power, which by default needs 1000000 tokens
bondAmt := sdk.DefaultPowerReduction.MulRaw(1000)
Copy link
Contributor

Choose a reason for hiding this comment

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

So this is 1,000,000,000 tokens, enough to survive 1000 reductions, whereas the previous wiped out the validator in one go?

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly. Cosmos SDK specifies DefaultPowerReduction = 1000000. In the previous implementation, each genesis validator is given 1000000 tokens and thus 1 unit of voting power. Any undelegation with more than >=1 tokens will make the voting power to be zero and thus wipes out this validator.

genAddr := genAccs[0].GetAddress()

// unbond a random amount of tokens from the validator
numNewVals := rand.Int63n(7) + 1 // numNewVals \in [1, 7] since UBD queue contains at most DefaultMaxEntries=7 validators
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh really? What happens after 7 entries? Can the unbonding validator abuse it somehow?

Copy link
Member Author

Choose a reason for hiding this comment

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

I should have been more specific. MaxEntries means that for any pair of delegatorAddr and validatorAddr, there can be <=MaxEntries concurrent undelegations at any time slot. If this delegator submits more than MaxEntries delegation requests, then only MaxEntries of them will be processed and others will be rejected. This is processed in https://github.com/cosmos/cosmos-sdk/blob/v0.45.5/x/staking/keeper/delegation.go#L814-L816. Will update the comments to make it more clear.

Comment on lines 84 to 86
numNewVals := rand.Int63n(1000) + 1
for i := int64(0); i < numNewVals; i++ {
helper.WrappedDelegate(genAddr, val, coinWithOnePower.Amount)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if it's really worth doing this 1000 times at the extreme end. You could randomise the amount and the validator to delegate to instead. I mean, what do you learn if you do the exact same thing 10 times vs 900 times?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah we can also randomise the amount to achieve the same goal. The only advantage of randomising the number of delegation requests is to also fuzz the delegation/undelegation/redelegation queues. This is also how I found the MaxEntries thing.

Comment on lines 231 to 232
// Note that in Babylon, redelegation happens unconditionally upon `EndEpoch`, rather than upon checkpointed. Meanwhile in Cosmos SDK, redelegation happens upon `EndBlock`.
// This is because slashable security only requires `unbonding` -> `unbonded` to depend on checkpoints, and redelegation does not unbond any stake from the system.
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean that no tokens enter the unbonding queue during a BeginRedelegate, because they just get moved between validators? If so, why this this comment still say that the unbonding queue can take at most 7 items?

Copy link
Member Author

@SebastianElvis SebastianElvis Jul 21, 2022

Choose a reason for hiding this comment

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

Does this mean that no tokens enter the unbonding queue during a BeginRedelegate, because they just get moved between validators?

Exactly. Upon redelegations, there is no token leaving the system, unlike upon undelegations. Thus, redelegations do not affect PoS security, in both PoS and checkpointed PoS.

If so, why this this comment still say that the unbonding queue can take at most 7 items?

I guess the MaxEntries thing aims at some sort of rate limiting, specifying that a validator operator should not spam undelegation/redelegation requests.

I will add more documentations to make this clear.

@SebastianElvis SebastianElvis merged commit d0891a0 into main Jul 21, 2022
@SebastianElvis SebastianElvis deleted the epoching-undel-redel-tests branch July 21, 2022 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants