-
Notifications
You must be signed in to change notification settings - Fork 2
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
test: lock in types.Header
RLP encoding
#87
Conversation
require.Equal(t, BloomByteLength, rng.Read(hdr.Bloom[:])) | ||
require.Equal(t, len(BlockNonce{}), rng.Read(hdr.Nonce[:])) | ||
require.Equal(t, numExtraBytes, rng.Read(hdr.Extra)) | ||
t.Logf("%T:\n%+v", hdr, hdr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this?
t.Logf("%T:\n%+v", hdr, hdr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to help make the input more readable in the case of a failure of RLP assertion later. Without it, there are just two huge hex blobs. See identifying the input as a specific instance of useful test failures.
require.NoError(t, err, "hex.DecodeString()") | ||
|
||
got, err := rlp.EncodeToBytes(hdr) | ||
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", hdr) | ||
assert.Equalf(t, want, got, "rlp.EncodeToBytes(%T)", hdr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit I don't think there is a need for assert/require additional error messages, it will log out the line number in the test and the diffs between the two values if there is any by default, so adding a custom message feels redundant. Thoughts?
require.NoError(t, err, "hex.DecodeString()") | |
got, err := rlp.EncodeToBytes(hdr) | |
require.NoErrorf(t, err, "rlp.EncodeToBytes(%T)", hdr) | |
assert.Equalf(t, want, got, "rlp.EncodeToBytes(%T)", hdr) | |
require.NoError(t, err) | |
got, err := rlp.EncodeToBytes(hdr) | |
require.NoErrorf(t, err) | |
assert.Equalf(t, want, got) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As with identifying input, see identifying the function. Yes, the line number is there, but this makes error messages self-contained.
It may be overkill but it's a habit that dies hard and I think the even small pro outweighs the negligible con of including it.
require.Equal(t, BloomByteLength, rng.Read(hdr.Bloom[:])) | ||
require.Equal(t, len(BlockNonce{}), rng.Read(hdr.Nonce[:])) | ||
require.Equal(t, numExtraBytes, rng.Read(hdr.Extra)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit it would be nice to populate bloom, nonce and extra before and separate from the assertions, it took me a solid minute to find where it was populated 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I've inlined them instead by creating the necessary methods on ethtest.PseudoRand
.
} | ||
require.Equal(t, BloomByteLength, rng.Read(hdr.Bloom[:])) | ||
require.Equal(t, len(BlockNonce{}), rng.Read(hdr.Nonce[:])) | ||
require.Equal(t, numExtraBytes, rng.Read(hdr.Extra)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit That check is rather unneeded, since we test the test really, especially given numExtraBytes is defined in the test and we create the slice inline in this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now obsolete since inlining of the random generation.
Why this should be merged
We're making changes to very sensitive code that, if broken, can result in block hashes being different.
How this works
Change-detector test.
How this was tested
A randomly filled
types.Header
with expected RLP locked as a constant in the test.