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

Encapsulate Nil Checks #9454

Open
rauljordan opened this issue Aug 24, 2021 · 9 comments
Open

Encapsulate Nil Checks #9454

rauljordan opened this issue Aug 24, 2021 · 9 comments
Assignees
Labels
Cleanup Code health! Help Wanted Extra attention is needed Security Security Related Issues

Comments

@rauljordan
Copy link
Contributor

💎 Issue

Background

One pattern we should opt for across our repo is to rely more on package-level errors rather than putting the burden of nil checks in certain situations on the caller. For example, a common pattern seen across the codebase is this:

st, err := beaconDB.StateByRoot(ctx, root)
if err != nil {
  return err
}
if st == nil || st.IsNil() {
  ...
}

One time, there was a PR with a bug because we forgot to add a nil check. Another time, there was another failure because we forgot state needs two nil checks. Pushing this check onto callers increases the probability. Go is already notorious for nil checks, which are risky if forgotten. Instead, we should aim to encapsulate them as much as possible and rely on error values as a sane alternative.

For example:

// beacon-chain/db/kv/state.go
var ErrNotFound = errors.New("not found")

func (s *Store) StateByRoot(root [32]byte) (*State, error) {
  state := s.db.view(root)
  if st == nil || st.IsNil() {
    return nil, ErrNotFound
  }
}

// elsewhere.
st, err := beaconDB.StateByRoot(ctx, root)
switch err {
  case kv.ErrNotFound:
    // Handle...
  default:
    return err
}

Notes

It is important to audit all uses of this nil encapsulation we are changing, however, as it is possible we introduce a bug in doing so. For example, some code paths might rely on error checking differently, and we may need to handle those error values in a sane manner. Additionally, some other code paths might actually rely on nil values returned from the db method to branch into certain execution. We should be careful with this change.

@rauljordan rauljordan added Help Wanted Extra attention is needed Cleanup Code health! Security Security Related Issues labels Aug 24, 2021
@rkapka
Copy link
Contributor

rkapka commented Aug 27, 2021

It would be neat to have a [dream]NotNil<T> generic type[/dream] signalling that the caller does not have to perform nil checks.

@pgagala
Copy link

pgagala commented Sep 9, 2021

I can try to do that (if that is not uber urgent and doesn't have to be delivered very fast)

@rkapka
Copy link
Contributor

rkapka commented Sep 10, 2021

@pgagala I think it would be nice if we could do this incrementally, package by package or so. One huge PR will be very hard to maintain, especially merging with develop might become a nightmare.

@pgagala
Copy link

pgagala commented Sep 11, 2021

I think that is a good idea

@pgagala
Copy link

pgagala commented Nov 4, 2021

so far I was able to introduce some changes https://github.com/pgagala/prysm/tree/encapsulate-nil-checks-9454
. I checked beacon-chain package without state and sync packages someone can overtake that if that is urgent because now I won't have much time unfortunately

@victordeleau
Copy link

I would gladly take over if that's a possibility. I can finish the beacon-chain package and make a PR for it. Then move on to the next package.

@terencechain
Copy link
Member

I would gladly take over if that's a possibility. I can finish the beacon-chain package and make a PR for it. Then move on to the next package.

Sounds good. Thanks for your help. I will also recommend to do it per beacon-chain sub package (ie blockchain, rpc.. etc). beacon-chain itself is an enormous package

@rauljordan
Copy link
Contributor Author

@rauljordan
Copy link
Contributor Author

#9976

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cleanup Code health! Help Wanted Extra attention is needed Security Security Related Issues
Projects
None yet
Development

No branches or pull requests

5 participants