Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: zero value nonce is invalid (#881)
## Description Because `0` is the zero (default) value for `uint64`, if it is valid to be used as a nonce, it becomes difficult to distinguish the scenario where sender did not set a nonce from one where they explicitly set it to `0`. I'm not confident whether the ability to make this distinction matters now or has the potential to later but was following a feeling. <!-- reviewpad:summarize:start --> ### Summary generated by Reviewpad on 11 Jul 23 11:56 UTC This pull request includes the following changes: - In the `p2p/module.go` file, the `handlePocketEnvelope` function now checks if `poktEnvelope.Nonce` is zero and returns an error if it is. The error message includes the hex-encoded nonce value. - In the `p2p/module_test.go` file, new test cases have been added to test the handling of an invalid nonce. - In the `p2p/types/errors.go` file, a new error variable `ErrInvalidNonce` has been added to represent an invalid nonce value. - In the `shared/crypto/rand.go` file, a check has been added to ensure that the generated nonce value is not zero. If it is zero, the function recursively calls itself to generate a new nonce. <!-- reviewpad:summarize:end --> ## Issue N/A; observation made while working on #732. ## Type of change Please mark the relevant option(s): - [ ] New feature, functionality or library - [x] Bug fix - [ ] Code health or cleanup - [ ] Major breaking change - [ ] Documentation - [ ] Other <!-- add details here if it a different type of change --> ## List of changes - Adds `ErrInvalidNonce` P2P error type - Ensures the P2P message handler rejects the zero value `Nonce` (`uint64(0)`) is invalid - Ensures the `GetNonce` function never returns the zero value ## Testing - [ ] `make develop_test`; if any code changes were made - [ ] `make test_e2e` on [k8s LocalNet](https://github.com/pokt-network/pocket/blob/main/build/localnet/README.md); if any code changes were made - [ ] `e2e-devnet-test` passes tests on [DevNet](https://pocketnetwork.notion.site/How-to-DevNet-ff1598f27efe44c09f34e2aa0051f0dd); if any code was changed - [ ] [Docker Compose LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md); if any major functionality was changed or introduced - [ ] [k8s LocalNet](https://github.com/pokt-network/pocket/blob/main/build/localnet/README.md); if any infrastructure or configuration changes were made ## Required Checklist - [ ] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have added, or updated, [`godoc` format comments](https://go.dev/blog/godoc) on touched members (see: [tip.golang.org/doc/comment](https://tip.golang.org/doc/comment)) - [ ] I have tested my changes using the available tooling - [ ] I have updated the corresponding CHANGELOG ### If Applicable Checklist - [ ] I have updated the corresponding README(s); local and/or global - [x] I have added tests that prove my fix is effective or that my feature works - [ ] I have added, or updated, [mermaid.js](https://mermaid-js.github.io) diagrams in the corresponding README(s) - [ ] I have added, or updated, documentation and [mermaid.js](https://mermaid-js.github.io) diagrams in `shared/docs/*` if I updated `shared/*`README(s)
- Loading branch information