Skip to content

Commit 08bb6f4

Browse files
committed
net: log an error rather than asserting if send version is misused
Also cleaned up the comments and moved from the header to the .cpp so that logging headers aren't needed from net.h
1 parent 7a8c251 commit 08bb6f4

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/net.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,33 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
689689
return true;
690690
}
691691

692+
void CNode::SetSendVersion(int nVersionIn)
693+
{
694+
// Send version may only be changed in the version message, and
695+
// only one version message is allowed per session. We can therefore
696+
// treat this value as const and even atomic as long as it's only used
697+
// once a version message has been successfully processed. Any attempt to
698+
// set this twice is an error.
699+
if (nSendVersion != 0) {
700+
error("Send version already set for node: %i. Refusing to change from %i to %i", id, nSendVersion, nVersionIn);
701+
} else {
702+
nSendVersion = nVersionIn;
703+
}
704+
}
705+
706+
int CNode::GetSendVersion() const
707+
{
708+
// The send version should always be explicitly set to
709+
// INIT_PROTO_VERSION rather than using this value until SetSendVersion
710+
// has been called.
711+
if (nSendVersion == 0) {
712+
error("Requesting unset send version for node: %i. Using %i", id, INIT_PROTO_VERSION);
713+
return INIT_PROTO_VERSION;
714+
}
715+
return nSendVersion;
716+
}
717+
718+
692719
int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
693720
{
694721
// copy data to temporary parsing buffer

src/net.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -723,25 +723,8 @@ class CNode
723723
{
724724
return nRecvVersion;
725725
}
726-
void SetSendVersion(int nVersionIn)
727-
{
728-
// Send version may only be changed in the version message, and
729-
// only one version message is allowed per session. We can therefore
730-
// treat this value as const and even atomic as long as it's only used
731-
// once the handshake is complete. Any attempt to set this twice is an
732-
// error.
733-
assert(nSendVersion == 0);
734-
nSendVersion = nVersionIn;
735-
}
736-
737-
int GetSendVersion() const
738-
{
739-
// The send version should always be explicitly set to
740-
// INIT_PROTO_VERSION rather than using this value until the handshake
741-
// is complete.
742-
assert(nSendVersion != 0);
743-
return nSendVersion;
744-
}
726+
void SetSendVersion(int nVersionIn);
727+
int GetSendVersion() const;
745728

746729
CNode* AddRef()
747730
{

0 commit comments

Comments
 (0)