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

p2p: fix DiscReason encoding/decoding #30855

Merged
merged 9 commits into from
Dec 12, 2024

Conversation

lorenzo-dev1
Copy link
Contributor

This PR reverts back the change to the DiscReason rlp decoding done in #24507

It also adds a test to check for correctness. The current code in master will fail the test since the rlp decoding of DiscReason will fail. However the error returned from rlp.Decode is not checked, so the client proceeds using the default value for DiscReason, which is DiscRequested.

This PR also prevents a possible nil pointer dereference when closing an rlpx connection.

@lorenzo-dev1
Copy link
Contributor Author

I noticed that the test TestPeerDisconnect will fail with this implementation, however I think the test might be wrong. Because it calls SendItems to send the disconnect message, which in turns calls EncodeToReader.

However when closing a connection the client simply uses rlp.Encode to send the message.

If my understanding is correct, I will fix that test too.

p2p/peer_test.go Outdated
Comment on lines 24 to 25
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/require"
Copy link
Contributor

Choose a reason for hiding this comment

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

these should be added to the import-paragraph further down, not up here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes are not present anymore

@fjl
Copy link
Contributor

fjl commented Dec 6, 2024

Could you please provide some background why you are reverting #24507

@fjl
Copy link
Contributor

fjl commented Dec 6, 2024

Ahh I see why. So the server sends the reason as a byte array... In the spec it is defined to be an RLP list.

The fix needs to be applied on the writing side, i.e. in p2p/transport.go. The decoding part is actually correct. Though we could handle both cases as well.

@fjl fjl self-assigned this Dec 9, 2024
@fjl fjl changed the title Fix DiscReason decoding and prevent possible nil pointer dereference p2p: fix DiscReason decoding and prevent possible nil pointer dereference Dec 9, 2024
@lorenzo-dev1
Copy link
Contributor Author

Ahh I see why. So the server sends the reason as a byte array... In the spec it is defined to be an RLP list.

The fix needs to be applied on the writing side, i.e. in p2p/transport.go. The decoding part is actually correct. Though we could handle both cases as well.

@fjl ah I see! I will implement the fix in the writing side today then!

@lorenzo-dev1
Copy link
Contributor Author

@fjl should be good now, if you like the approach I will squash all commits

@lorenzo-dev1 lorenzo-dev1 changed the title p2p: fix DiscReason decoding and prevent possible nil pointer dereference p2p: fix DiscReason encoding/decoding and prevent possible nil pointer dereference Dec 10, 2024
And update some unit tests, since they were still using the wrong format.
fjl
fjl previously approved these changes Dec 10, 2024
Copy link
Contributor

@fjl fjl left a comment

Choose a reason for hiding this comment

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

I have updated the change a bit. No need to squash since we use squash merge anyway.

@@ -86,6 +88,7 @@ var discReasonToString = [...]string{
DiscSelf: "connected to self",
DiscReadTimeout: "read timeout",
DiscSubprotocolError: "subprotocol error",
DiscInvalid: "invalid disconnect reason",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fjl I think this bumps the length of the discReasonToString to 256... So it means that the String() func will never return the uknown reason error. it will return empty string instaed

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, thanks!

@fjl fjl changed the title p2p: fix DiscReason encoding/decoding and prevent possible nil pointer dereference p2p: fix DiscReason encoding/decoding Dec 10, 2024
@fjl
Copy link
Contributor

fjl commented Dec 10, 2024

About nil pointer isssue: I have removed the check for t.conn == nil since the conn field will never be nil.

@lorenzo-dev1 lorenzo-dev1 requested review from fjl and holiman December 11, 2024 08:45
@lorenzo-dev1
Copy link
Contributor Author

Seems like I cannot merge the PR even if approved, should I wait for other approvals?

@fjl
Copy link
Contributor

fjl commented Dec 11, 2024

Only team can merge. I was waiting for tests to finish.

@lorenzo-dev1
Copy link
Contributor Author

Only team can merge. I was waiting for tests to finish.

Ok I see, sorry it is my first PR on go-ethereum so I am not familiar with the process.

Could you also point me to where the CI is run?

@fjl
Copy link
Contributor

fjl commented Dec 11, 2024

It runs on AppVeyor. I will merge your PR later, still want to verify something about it.

@fjl fjl merged commit c1c2507 into ethereum:master Dec 12, 2024
2 checks passed
@fjl fjl added this to the 1.14.13 milestone Dec 12, 2024
@fjl fjl modified the milestones: 1.14.13, 1.15.0 Jan 23, 2025
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.

3 participants