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

Add error handling in newHashKey #499

Closed
wants to merge 4 commits into from

Conversation

Mdaiki0730
Copy link
Member

@Mdaiki0730 Mdaiki0730 commented Nov 14, 2022

Description

When calling the function newHashKey() if an error occurs while filling the new key with random data from the defined random source, the created hash key will be zeroed, which means, the content of the slice will be all 0's.

I think it's better to handle errors properly.

@Mdaiki0730 Mdaiki0730 added the C: enhancement Classification: New feature or its request, or improvement in maintainability of code label Nov 14, 2022
@Mdaiki0730 Mdaiki0730 self-assigned this Nov 14, 2022
@codecov
Copy link

codecov bot commented Nov 14, 2022

Codecov Report

Merging #499 (8748158) into main (0681636) will increase coverage by 0.16%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #499      +/-   ##
==========================================
+ Coverage   65.44%   65.60%   +0.16%     
==========================================
  Files         278      278              
  Lines       37936    37995      +59     
==========================================
+ Hits        24828    24928     +100     
+ Misses      11305    11261      -44     
- Partials     1803     1806       +3     
Impacted Files Coverage Δ
p2p/pex/addrbook.go 71.66% <100.00%> (ø)
privval/socket_listeners.go 79.72% <0.00%> (-4.06%) ⬇️
types/tx.go 90.24% <0.00%> (-3.66%) ⬇️
privval/signer_listener_endpoint.go 88.88% <0.00%> (-2.39%) ⬇️
crypto/ed25519/ed25519.go 45.12% <0.00%> (-1.22%) ⬇️
mempool/reactor.go 78.57% <0.00%> (-1.10%) ⬇️
p2p/transport.go 82.77% <0.00%> (-0.71%) ⬇️
p2p/node_info.go 95.12% <0.00%> (ø)
proxy/multi_app_conn.go 47.66% <0.00%> (ø)
crypto/vrf/vrf_r2ishiguro.go
... and 10 more

@Mdaiki0730 Mdaiki0730 marked this pull request as ready for review November 15, 2022 10:54
@torao
Copy link
Contributor

torao commented Nov 21, 2022

This title and description don't appear to represent the modifications contained in this PR correctly. Is newHashKey() not fixed to return an error?

@torao
Copy link
Contributor

torao commented Nov 22, 2022

Separating certain functions into a package for the purpose of passing coverage seems to me to be out of line with module design and doesn't seem to be a good idea. And I'm also concerned about the project-local implicit rules that arise.

  • What was the key point of the problems
  • What options were available
  • What were the considerations
  • Then, why did you decide this was the best approach?

@@ -20,6 +19,7 @@ import (
tmmath "github.com/line/ostracon/libs/math"
tmrand "github.com/line/ostracon/libs/rand"
"github.com/line/ostracon/libs/service"
stdCryptoRand "github.com/line/ostracon/libs/std/crypto/rand"
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems too assertive compared to other sources and golang's customary package alias. I think crand, scrand, or stdcrand is enough.

@@ -0,0 +1,11 @@
package rand
Copy link
Contributor

Choose a reason for hiding this comment

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

No one looking at the source would know why this function is separated in this package. Any purpose or intent that can only be recognized outside of the source code should be noted in the comment.

@Mdaiki0730
Copy link
Member Author

Mdaiki0730 commented Nov 22, 2022

@torao
After thinking about the following, I am implementing it now.

  • Problem: Error handling of crand.Read in newHashKey provides insufficient coverage, but it is difficult to write tests for it.
  • Options:
    • Ignore coverage → The convention of merging after all green is broken
    • Wrap crand.Read as a private function → A wrapper just for testing the standard package is born
    • Wrap crand.Read as public function in libs → Allows aggregation of packages that wrap functions for which test is essentially unnecessary

The current option is selected because the layer where the error occurs is not at the application level but is connected to the test ignore.
I would like to hear your opinion if there are better options for this.

@Mdaiki0730 Mdaiki0730 requested a review from torao November 24, 2022 05:40
@torao
Copy link
Contributor

torao commented Nov 24, 2022

@Mdaiki0730
Looking for other code that uses crand.Read(), there is a function CRandBytes() in crypto/random.go that could be used for this purpose. It may work the same as the current newHashKey(), so how about using this instead of creating a specific package?

https://github.com/line/ostracon/blob/main/crypto/random.go#L9-L22

@Mdaiki0730
Copy link
Member Author

I think very good@@
Thank you for finding this function!

@Mdaiki0730 Mdaiki0730 added the D: duplicate Decision: This issue or pull request already exists label Nov 24, 2022
@Mdaiki0730
Copy link
Member Author

I created another PR because we found a completely different solution than the this PR.
New One: #509

@Mdaiki0730 Mdaiki0730 closed this Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: enhancement Classification: New feature or its request, or improvement in maintainability of code D: duplicate Decision: This issue or pull request already exists
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants