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

test: lock in types.Header RLP encoding #87

Merged
merged 2 commits into from
Dec 12, 2024

Conversation

ARR4N
Copy link
Collaborator

@ARR4N ARR4N commented Dec 12, 2024

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.

@ARR4N ARR4N requested review from a team, darioush, qdm12 and ceyonur and removed request for a team December 12, 2024 12:00
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)
Copy link
Collaborator

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?

Suggested change
t.Logf("%T:\n%+v", hdr, hdr)

Copy link
Collaborator Author

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.

Comment on lines +69 to +73
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)
Copy link
Collaborator

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?

Suggested change
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)

Copy link
Collaborator Author

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.

Comment on lines 60 to 62
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))
Copy link
Collaborator

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 😄

Copy link
Collaborator Author

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))
Copy link
Collaborator

@qdm12 qdm12 Dec 12, 2024

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.

Copy link
Collaborator Author

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.

@ARR4N ARR4N enabled auto-merge (squash) December 12, 2024 12:53
@ARR4N ARR4N merged commit bd44839 into main Dec 12, 2024
4 checks passed
@ARR4N ARR4N deleted the arr4n/header-rlp-change-detector branch December 12, 2024 13:00
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.

2 participants