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!: IS rebase #13122

Merged
merged 16 commits into from
Oct 9, 2022
Merged

feat!: IS rebase #13122

merged 16 commits into from
Oct 9, 2022

Conversation

tac0turtle
Copy link
Member

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

Did a first pass through. I'm gonna need more time to look at the staking changes. The index key stuff I find to be pretty confusing atm.

proto/cosmos/staking/v1beta1/staking.proto Show resolved Hide resolved
proto/cosmos/staking/v1beta1/staking.proto Show resolved Hide resolved
proto/cosmos/staking/v1beta1/staking.proto Show resolved Hide resolved
proto/cosmos/staking/v1beta1/tx.proto Outdated Show resolved Hide resolved
x/staking/keeper/delegation.go Show resolved Hide resolved
x/staking/keeper/keeper.go Show resolved Hide resolved
x/staking/keeper/slash.go Show resolved Hide resolved
x/staking/keeper/unbonding.go Outdated Show resolved Hide resolved
) (val types.Validator, found bool) {
store := ctx.KVStore(k.storeKey)

valKey := store.Get(types.GetUnbondingIndexKey(id))
Copy link
Contributor

Choose a reason for hiding this comment

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

So am I correct in reading that the GetUnbondingIndexKey key can store numerous different things? I.e. validator, redelegation keys, and undelegation keys?

Copy link

Choose a reason for hiding this comment

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

Exactly. The UnbondingIndexKey is a mapping from UnbondingIDs to strings, where each string is the key of an unbonding operation, i.e., validator (that is unbonding), undelegation entry, redelegation entry.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. This initially seems like a codesmell/poor design choice to me. Is it possible that a value overwrite an existing ID's value unintentionally?

Copy link

Choose a reason for hiding this comment

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

Is it possible that a value overwrite an existing ID's value unintentionally?

Not sure that I follow.

This initially seems like a codesmell/poor design choice to me.

How else can you avoid duplicating logic that is the same regardless of the type of unbonding operation. For example, the CCV module is dealing with the completion of undelegation and redelegation in the same way. By adding different hooks and different IDs (e.g., ubdeID and redeID), CCV will need to duplicate code.

Copy link
Contributor

Choose a reason for hiding this comment

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

You use a dedicated key for each stored value type. Having to type check/error check based on the value is a codesmell. I.e. "I'm storing value X at key K. What is X? Idk, let me try these 5 different types..."

Copy link
Member Author

Choose a reason for hiding this comment

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

Got a second opinion form @ethanfrey and he tends to agree this approach is a code smell. We should refactor this. Would consider this a blocker for the merge. @mpoke any chance you or someone on your team can pick this up. once completed we can merge this work.

Copy link
Contributor

Choose a reason for hiding this comment

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

@marbar3778 @alexanderbez I will take a look at this right now. I wrote the offending code

Copy link
Contributor

Choose a reason for hiding this comment

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

The pattern in question is most clearly demonstrated not here, but in the top level code that calls this function in UnbondingCanComplete. UnbondingCanComplete works sort of like an if/else statement.

if (is redelegation) {

} else if (is undelegation) {

} else if (is validator unbonding) {

} else {

}

This didn't seem so bad when I wrote it, but I guess the part that troubles you is just the fact that references to records of different types are being kept in one store? Or is it that we are checking which type it is with failed deserializations?

The simplest way I can think of to avoid the latter issue is to make another index which stores unbondingId->type. Then we would check this first to find out what to deserialize to without the potential several failed deserializations which are bugging you.

Copy link
Contributor

Choose a reason for hiding this comment

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

but I guess the part that troubles you is just the fact that references to records of different types are being kept in one store?

Yes, exactly! It's that a key, a dedicated prefix really, can store values of different types yielding the need to have custom business logic of these if/else blocks as you pointed out. To me this is a code smell and no other SDK module does this.

What if we just combined/flattened all the possible value types into one type and have a field/enum that describes what kind of action to take?

something like

type MyValue struct {
  Type string // or enum

 // all fields that can represent everything you need
}

Copy link
Member

Choose a reason for hiding this comment

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

Seems like maybe this is something a proto oneof could deal with efficiently

@julienrbrt julienrbrt self-assigned this Sep 27, 2022
if !red.Entries[i].OnHold() && red.Entries[i].IsMature(ctx.BlockHeader().Time) {
// If matured, complete it.
// Remove entry
red.RemoveEntry(int64(i))

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion
}

// Remove entry
ubd.RemoveEntry(int64(i))

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion

// Convert into bytes for storage
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, uint64(unbondingType))

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion
@julienrbrt julienrbrt mentioned this pull request Sep 29, 2022
21 tasks
@julienrbrt
Copy link
Member

I have rebased to main here. I'll fix the tests and the build in #13420.

@julienrbrt julienrbrt added the T: API Breaking Breaking changes that impact APIs and the SDK only (not state machine). label Oct 4, 2022
@julienrbrt julienrbrt changed the title feat: IS rebase feat!: IS rebase Oct 4, 2022
x/staking/keeper/val_state_change.go Fixed Show fixed Hide fixed
x/staking/keeper/delegation.go Fixed Show fixed Hide fixed
x/staking/keeper/delegation.go Fixed Show fixed Hide fixed
@codecov
Copy link

codecov bot commented Oct 4, 2022

Codecov Report

Merging #13122 (a2e80d5) into main (6efbedb) will increase coverage by 0.03%.
The diff coverage is 67.38%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #13122      +/-   ##
==========================================
+ Coverage   54.01%   54.04%   +0.03%     
==========================================
  Files         648      646       -2     
  Lines       55429    55711     +282     
==========================================
+ Hits        29938    30109     +171     
- Misses      23093    23188      +95     
- Partials     2398     2414      +16     
Impacted Files Coverage Δ
x/distribution/keeper/hooks.go 22.50% <0.00%> (-0.58%) ⬇️
x/evidence/keeper/infraction.go 0.00% <0.00%> (ø)
x/slashing/keeper/hooks.go 51.11% <0.00%> (-2.38%) ⬇️
x/slashing/keeper/infractions.go 0.00% <0.00%> (ø)
x/slashing/keeper/keeper.go 52.94% <0.00%> (ø)
x/staking/keeper/grpc_query.go 1.74% <0.00%> (-0.01%) ⬇️
x/staking/types/expected_keepers.go 0.00% <ø> (ø)
x/staking/types/hooks.go 0.00% <0.00%> (ø)
x/staking/types/keys.go 42.42% <0.00%> (-1.79%) ⬇️
x/staking/keeper/keeper.go 61.40% <33.33%> (-7.49%) ⬇️
... and 23 more

facundomedica
facundomedica previously approved these changes Oct 4, 2022
Copy link
Member

@facundomedica facundomedica left a comment

Choose a reason for hiding this comment

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

LGTM, added a couple of comments

x/staking/keeper/slash.go Show resolved Hide resolved
@facundomedica facundomedica dismissed their stale review October 5, 2022 15:41

I approved this without fully understanding the code, removing my approval to avoid any confusion

@danwt
Copy link
Contributor

danwt commented Oct 5, 2022

Just reminding that we please don't merge until we test this as a dependency in interchain-security/. Thanks.

tests/integration/staking/keeper/unbonding_test.go Outdated Show resolved Hide resolved
tests/integration/staking/keeper/unbonding_test.go Outdated Show resolved Hide resolved
tests/integration/staking/keeper/unbonding_test.go Outdated Show resolved Hide resolved
tests/integration/staking/keeper/unbonding_test.go Outdated Show resolved Hide resolved
x/staking/keeper/val_state_change.go Show resolved Hide resolved
x/staking/keeper/validator.go Show resolved Hide resolved
x/staking/keeper/validator.go Show resolved Hide resolved
x/staking/keeper/validator.go Show resolved Hide resolved
x/staking/keeper/validator.go Outdated Show resolved Hide resolved
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Simon <sainoe@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:Simulations C:x/distribution distribution module related C:x/evidence C:x/slashing C:x/staking T: API Breaking Breaking changes that impact APIs and the SDK only (not state machine).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants