Skip to content

Conversation

jonasschnelli
Copy link
Contributor

@jonasschnelli jonasschnelli commented Jun 13, 2019

This refactors the network message deserialization.

  • It transforms the CNetMessage into a transport protocol agnostic message container.
  • A new class TransportDeserializer (unique pointer of CNode) is introduced, handling the network buffer reading and the decomposing to a CNetMessage
  • No behavioral changes (in terms of disconnecting, punishing)
  • Moves the checksum finalizing into the SocketHandler thread (finalizing was in ProcessMessages before)

The optional last commit makes the TransportDeserializer following an adapter pattern (polymorphic interface) to make it easier to later add a V2 transport protocol deserializer.

Intentionally not touching the sending part.

Pre-Requirement for BIP324 (v2 message transport protocol).
Replacement for #14046 and inspired by a comment from sipa

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 13, 2019

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #16839 (Replace Connman and BanMan globals with Node local by ryanofsky)
  • #16562 (Refactor message transport packaging by jonasschnelli)
  • #15197 (Refactor and slightly stricter p2p message processing by jonasschnelli)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@jonasschnelli jonasschnelli force-pushed the 2019/06/net_refactor_1 branch 2 times, most recently from 3fa744d to 7bd4dd2 Compare June 14, 2019 15:50
Copy link

@ariard ariard left a comment

Choose a reason for hiding this comment

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

+1 for the adapter pattern, there may still room for few interfaces improvements.

Current draft of BIP 324 : https://gist.github.com/jonasschnelli/c530ea8421b8d0e80c51486325587c52 (a help for review and not yet in bips repo I think)

@jonasschnelli jonasschnelli force-pushed the 2019/06/net_refactor_1 branch 4 times, most recently from 4cbe0af to e85d88d Compare July 25, 2019 11:07
@jonasschnelli
Copy link
Contributor Author

Thanks @ariard for the review.
Fixed those points/nits. Mainly the private declaration of members and methods of the V1TransportDeserializer class.

Copy link

@ariard ariard left a comment

Choose a reason for hiding this comment

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

utACK e85d88d

@sipa
Copy link
Member

sipa commented Jul 29, 2019

Concept and approach ACK.

I'll go through the code in more detail soon, but one thing I noticed is that this introduces an unnecessary copy of the message payload in the GetMessage() function. It can be avoided by moving the CDataStream: https://github.com/sipa/bitcoin/tree/pr16202

@jonasschnelli
Copy link
Contributor Author

Thanks @sipa.
Added sipa's 6e4d183 to this PR

@jonasschnelli jonasschnelli force-pushed the 2019/06/net_refactor_1 branch 2 times, most recently from 5f34746 to 1396bd4 Compare August 5, 2019 11:54
@promag
Copy link
Contributor

promag commented Aug 6, 2019

Concept ACK, following #14046 (comment) does makes sense vs #14046.

Copy link
Contributor

@dongcarl dongcarl left a comment

Choose a reason for hiding this comment

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

ACK 7370bed
Reviewed code, ran unit tests and functional tests successfully.

@@ -3280,7 +3280,7 @@ bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& inter
return false;
// Just take one message
msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
pfrom->nProcessQueueSize -= msgs.front().m_recv.size() + CMessageHeader::HEADER_SIZE;
pfrom->nProcessQueueSize -= msgs.front().m_raw_message_size;
Copy link
Contributor

@dongcarl dongcarl Sep 5, 2019

Choose a reason for hiding this comment

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

Had to do a double-take here... But this shouldn't change behaviour as msgs.front().m_recv.size() is always equal to msgs.front().m_message_size because the msgs.front().m_recv hasn't advanced its nReadPos yet. Verified by placing an assert and running the unit and functional tests.

@etscrivner
Copy link
Contributor

ACK 7370bed

Approach is very clean. All unit and functional tests pass. Manually tested some P2P functionality to provide additional confidence.

src/net.h Outdated
std::string m_command;

CNetMessage(const CDataStream& recv_in) : m_recv(std::move(recv_in)) {
m_time = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Use default member initializers for these rather than repeating the members in the constructor.

Copy link
Contributor

Choose a reason for hiding this comment

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

d8c3e95

Agree with @jkczyz.

@jkczyz
Copy link
Contributor

jkczyz commented Sep 25, 2019

Not sure if more refactoring would be first required, but it would be nice if TransportDeserializer's interface could be simplified to a single method. Roughly speaking:

StatusOr<CNetMessage> Deserialize(CDataStream& stream);

Where StatusOr is essentially a variant type of error or result, though I suppose exceptions could be used instead. An output parameter for the result is another possibility. I'm not sure what the project conventions are for the error or result pattern.

Then TransportDeserializer could be stateless. Instead all state would be local to the Deserialize method. Additionally, the return value (or exception) could be examined for error status. This could reduce the complexity of the code quite a bit.

Otherwise, the client is responsible for knowing in which order the methods of TransportDeserializer should be called in, whether calling a method is valid at a given point, and in general the inner-workings of the TransportDeserializer implementation.

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

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

ACK 7370bed, reviewed code, ran unit and functional tests both at the mentioned commit and also rebased on current master fdfaeb6, with additional asserts and custom logging as a sanity check. I am currently observing a running node with these changes rebased on master with custom logging and asserts.

I do agree that continued refactoring to simplify handling of state might potentially be worthwhile. Given this PR is the result of at least one previous refactoring attempt and has been in review since June (and depending on the author's motivation to keep reworking it), it might be more flexible to try it in a follow-up PR if merging this one as-is can further v2 p2p progress.

@fjahr
Copy link
Contributor

fjahr commented Sep 30, 2019

ACK 7370bed

Successfully ran unit and functional tests, reviewed code. I agree with @jkczyz as in I would prefer a stateless deserializer as well but also with @jonatack that this would be better as a follow-up PR.

SanitizeString(strCommand), nMessageSize,
HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
LogPrint(BCLog::NET, "%s(%s, %u bytes): CHECKSUM ERROR\n", __func__,
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be useful to print pfrom->GetId() in here also.

@marcinja
Copy link
Contributor

marcinja commented Oct 4, 2019

ACK 7370bed

Reviewed code and ran tests at each commit. I also agree with other reviewers that a stateless deserializer would be nice for a potential follow-up.

@fanquake fanquake merged commit ed2dc5e into bitcoin:master Oct 28, 2019
maflcko pushed a commit that referenced this pull request Feb 28, 2020
16d6113 Refactor message transport packaging (Jonas Schnelli)

Pull request description:

  This PR factors out transport packaging logic from `CConnman::PushMessage()`.
  It's similar to #16202 (where we refactor deserialization).

  This allows implementing a new message transport protocol like BIP324.

ACKs for top commit:
  dongcarl:
    ACK 16d6113 FWIW
  ariard:
    Code review ACK 16d6113
  elichai:
    semiACK 16d6113 ran functional+unit tests.
  MarcoFalke:
    ACK 16d6113 🙎

Tree-SHA512: 8c2f8ab9f52e9b94327973ae15019a08109d5d9f9247492703a842827c5b5d634fc0411759e0bb316d824c586614b0220c2006410851933613bc143e58f7e6c1
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 29, 2020
16d6113 Refactor message transport packaging (Jonas Schnelli)

Pull request description:

  This PR factors out transport packaging logic from `CConnman::PushMessage()`.
  It's similar to bitcoin#16202 (where we refactor deserialization).

  This allows implementing a new message transport protocol like BIP324.

ACKs for top commit:
  dongcarl:
    ACK 16d6113 FWIW
  ariard:
    Code review ACK 16d6113
  elichai:
    semiACK 16d6113 ran functional+unit tests.
  MarcoFalke:
    ACK 16d6113 🙎

Tree-SHA512: 8c2f8ab9f52e9b94327973ae15019a08109d5d9f9247492703a842827c5b5d634fc0411759e0bb316d824c586614b0220c2006410851933613bc143e58f7e6c1
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 3, 2020
Summary:
Partial backport 1/7 of core [[bitcoin/bitcoin#16202 | PR16202]]:
bitcoin/bitcoin@6294ecd

Adapted to match our codebase.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK, jasonbcox

Reviewed By: #bitcoin_abc, PiRK, jasonbcox

Subscribers: jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D8222
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 3, 2020
Summary:
Partial backport 2/7 of core [[bitcoin/bitcoin#16202 | PR16202]]:
bitcoin/bitcoin@1a5c656

Depends on D8222.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK, jasonbcox

Reviewed By: #bitcoin_abc, PiRK, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D8223
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 3, 2020
Summary:
Partial backport 3/7 of core [[bitcoin/bitcoin#16202 | PR16202]]:
bitcoin/bitcoin@efecb74

Depends on D8223.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK, jasonbcox

Reviewed By: #bitcoin_abc, PiRK, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D8224
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 3, 2020
Summary:
Partial backport 4/7 of core [[bitcoin/bitcoin#16202 | PR16202]]:
bitcoin/bitcoin@b0e10ff

Depends on D8224.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK, jasonbcox

Reviewed By: #bitcoin_abc, PiRK, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D8225
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 3, 2020
Summary:
Partial backport 5/7 of core [[bitcoin/bitcoin#16202 | PR16202]]:
bitcoin/bitcoin@6a91499

Depends on D8225.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK, jasonbcox

Reviewed By: #bitcoin_abc, PiRK, jasonbcox

Subscribers: jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D8226
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 3, 2020
Summary:
Partial backport 6/7 of core [[bitcoin/bitcoin#16202 | PR16202]]:
bitcoin/bitcoin@f342a5e

Depends on D8226.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK, jasonbcox

Reviewed By: #bitcoin_abc, PiRK, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D8227
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 3, 2020
Summary:
Completes backport (7/7) of core [[bitcoin/bitcoin#16202 | PR16202]]:
bitcoin/bitcoin@ed2dc5e

Depends on D8227.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK, jasonbcox

Reviewed By: #bitcoin_abc, PiRK, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D8228
sidhujag pushed a commit to syscoin-core/syscoin that referenced this pull request Nov 10, 2020
16d6113 Refactor message transport packaging (Jonas Schnelli)

Pull request description:

  This PR factors out transport packaging logic from `CConnman::PushMessage()`.
  It's similar to bitcoin#16202 (where we refactor deserialization).

  This allows implementing a new message transport protocol like BIP324.

ACKs for top commit:
  dongcarl:
    ACK 16d6113 FWIW
  ariard:
    Code review ACK 16d6113
  elichai:
    semiACK 16d6113 ran functional+unit tests.
  MarcoFalke:
    ACK 16d6113 🙎

Tree-SHA512: 8c2f8ab9f52e9b94327973ae15019a08109d5d9f9247492703a842827c5b5d634fc0411759e0bb316d824c586614b0220c2006410851933613bc143e58f7e6c1
Reset();
}

bool Complete() const override
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this function in the .h instead of the .cpp?


int readHeader(const char *pch, unsigned int nBytes);
int readData(const char *pch, unsigned int nBytes);
int Read(const char *pch, unsigned int nBytes) override {
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this function in the .h instead of in the .cpp?

kwvg added a commit to kwvg/dash that referenced this pull request Aug 5, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Aug 5, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Aug 9, 2021
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
kwvg added a commit to kwvg/dash that referenced this pull request Aug 12, 2021
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
kwvg added a commit to kwvg/dash that referenced this pull request Aug 22, 2021
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
kwvg added a commit to kwvg/dash that referenced this pull request Aug 30, 2021
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
kwvg added a commit to kwvg/dash that referenced this pull request Nov 1, 2021
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
kwvg added a commit to kwvg/dash that referenced this pull request Nov 3, 2021
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
pravblockc pushed a commit to pravblockc/dash that referenced this pull request Nov 18, 2021
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
gades pushed a commit to cosanta/cosanta-core that referenced this pull request May 16, 2022
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Status: Merged
Development

Successfully merging this pull request may close these issues.