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

Change database *Index() to use slice #4466

Merged
merged 4 commits into from Jan 9, 2020
Merged

Change database *Index() to use slice #4466

merged 4 commits into from Jan 9, 2020

Conversation

mcdee
Copy link
Contributor

@mcdee mcdee commented Jan 9, 2020

This patch change the signature of the validator-related methods in beacon-chain/db/iface/interface.go from taking arrays to taking slices as their argument for the public key.

There are a few reasons for doing this. First, every instance of the code that called these functions outside of tests held slices, so wrapped all of the keys in bytesutil.ToBytes48() to pass the argument which were then all accessed with [:] as the downstream functions also use slices. So this change reduces unnecessary allocations.

Second, wrapping the keys hid potential issues with keys of incorrect lengths, as ToBytes48() will always provide a 48-byte array regardless of the length of the slice. Storing and retrieving keys will now error if they are of an incorrect size.

Third, arrays are hard-coded to 48 bytes when we store this value in our configuration parameters and would prefer to use that. The code now checks length against params.BeaconConfig().BLSPubkeyLength

The patch also fixes up a number of tests where invalid public keys (e.g. []byte{'A'}) were used.

terencechain
terencechain previously approved these changes Jan 9, 2020
@@ -27,6 +28,13 @@ func init() {
params.OverrideBeaconConfig(params.MinimalSpecConfig())
}

// _pubKey is a helper to generate a well-formed public key.
func _pubKey(i uint64) []byte {
Copy link
Member

Choose a reason for hiding this comment

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

Please remove the underscore. Go naming convention is mixedCase and lower case functions are considered private.

https://golang.org/doc/effective_go.html#mixed-caps

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reason I did that is because the test is in the aggregator package and I didn't want a function used only for testing and in a _test file to potentially be used by functions in the non-test package. Will change it if you're okay with this (there's another one out there in another package I'll change as well).

Copy link
Member

Choose a reason for hiding this comment

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

The compiler enforces that you can't use code from _test files in non test files so it is safe to do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool; never knew that. Updated.

@prestonvanloon prestonvanloon merged commit 3839f57 into prysmaticlabs:master Jan 9, 2020
@mcdee mcdee deleted the arraytoslice branch January 9, 2020 22:52
cryptomental pushed a commit to cryptomental/prysm that referenced this pull request Feb 24, 2020
* Change database *Index() to use slice

* Remove underscore from helper name
cryptomental pushed a commit to cryptomental/prysm that referenced this pull request Feb 28, 2020
* Change database *Index() to use slice

* Remove underscore from helper name
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.

None yet

3 participants