Skip to content

Commit

Permalink
Announce current EB setting in user agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
dagurval authored and dgenr8 committed Mar 17, 2017
1 parent c4586ad commit 7c3ccf1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/clientversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <string>
#include <boost/version.hpp>
#include <iomanip>
#include <cmath>

#if BOOST_VERSION >= 105500 // Boost 1.55 or newer
#include <boost/predef.h>
Expand Down Expand Up @@ -150,7 +152,7 @@ std::string FormatSubVersion(const std::string& name, int nClientVersion, const
/**
* The default Bitcoin XT subversion field according to BIP 14 spec
*/
std::string XTSubVersion()
std::string XTSubVersion(uint64_t nMaxBlockSize)
{
std::vector<std::string> comments = Opt().UAComment();

Expand All @@ -162,5 +164,11 @@ std::string XTSubVersion()
comments.insert(comments.end(), p.begin(), p.end());
}

// Announce our excessive block acceptence.
std::stringstream ss;
double dMaxBlockSize = double(nMaxBlockSize)/1000000;
ss << "EB" << std::setprecision(int(log10(dMaxBlockSize))+7) << dMaxBlockSize;
comments.insert(end(comments), ss.str());

return FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, comments, CLIENT_VERSION_XT_SUBVER);
}
2 changes: 1 addition & 1 deletion src/clientversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extern const std::string CLIENT_DATE;
std::string FormatFullVersion();
std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments, const char *releaseCharacter = "");

std::string XTSubVersion();
std::string XTSubVersion(uint64_t nMaxBlockSize);

#endif // WINDRES_PREPROC

Expand Down
5 changes: 3 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,11 +1001,12 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
return InitError(e.what());
}

if (XTSubVersion().size() > MAX_SUBVERSION_LENGTH)
uint64_t hugeBlock = 1000 * MAX_BLOCK_SIZE;
if (XTSubVersion(hugeBlock).size() > MAX_SUBVERSION_LENGTH)
return InitError(strprintf(
"Total length of network version string %i exceeds maximum of %i characters. "
"Reduce size of uacomment or hide platform details.",
XTSubVersion().size(), MAX_SUBVERSION_LENGTH));
XTSubVersion(hugeBlock).size(), MAX_SUBVERSION_LENGTH));

if (mapArgs.count("-onlynet")) {
std::set<enum Network> nets;
Expand Down
8 changes: 8 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ int GetHeight()
return chainActive.Height();
}

int GetMaxBlockSize()
{
LOCK(cs_main);
return chainActive.Tip()->nMaxBlockSize;
}

void UpdatePreferredDownload(CNode* node, NodeStatePtr& state)
{
nPreferredDownload -= state->fPreferredDownload;
Expand Down Expand Up @@ -590,6 +596,7 @@ void RegisterNodeSignals(CNodeSignals& nodeSignals)
nodeSignals.SendMessages.connect(&SendMessages);
nodeSignals.InitializeNode.connect(&InitializeNode);
nodeSignals.FinalizeNode.connect(&FinalizeNode);
nodeSignals.GetMaxBlockSize.connect(&GetMaxBlockSize);
}

void UnregisterNodeSignals(CNodeSignals& nodeSignals)
Expand All @@ -600,6 +607,7 @@ void UnregisterNodeSignals(CNodeSignals& nodeSignals)
nodeSignals.SendMessages.disconnect(&SendMessages);
nodeSignals.InitializeNode.disconnect(&InitializeNode);
nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
nodeSignals.GetMaxBlockSize.disconnect(&GetMaxBlockSize);
}

CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
Expand Down
5 changes: 3 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,11 @@ void CNode::PushVersion()
services = NODE_NETWORK;

PushMessage("version", 70002, services, nTime, addrYou, addrMe,
nLocalHostNonce, XTSubVersion(), nBestHeight, true);
nLocalHostNonce, XTSubVersion(0), nBestHeight, true);
} else {
int nMaxBlockSize = g_signals.GetMaxBlockSize().get_value_or(0);
PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
nLocalHostNonce, XTSubVersion(), nBestHeight, true);
nLocalHostNonce, XTSubVersion(nMaxBlockSize), nBestHeight, true);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ struct CNodeSignals
boost::signals2::signal<bool (CNode*, bool), CombinerAll> SendMessages;
boost::signals2::signal<void (NodeId, const CNode*)> InitializeNode;
boost::signals2::signal<void (NodeId)> FinalizeNode;
boost::signals2::signal<int ()> GetMaxBlockSize;
};


Expand Down
41 changes: 28 additions & 13 deletions src/test/clientversion_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ struct DummyArgGetter : public ArgGetter {
};

bool OsInStr(const std::string& version) {
// Assume OS is in string if string contains comments
return version.find("(") != std::string::npos;
std::vector<std::string> systems = {
"BSD", "Linux", "Mac OS", "Windows", "Unknown OS"
};
for (std::string os : systems)
if (version.find(os) != std::string::npos)
return true;
return false;
}

BOOST_AUTO_TEST_CASE(platform_in_xtsubversion)
Expand All @@ -57,16 +62,16 @@ BOOST_AUTO_TEST_CASE(platform_in_xtsubversion)

argPtr->hideplatform = true;
argPtr->stealthmode = false;
BOOST_CHECK(!OsInStr(XTSubVersion()));
BOOST_CHECK(!OsInStr(XTSubVersion(0)));

argPtr->hideplatform = false;
argPtr->stealthmode = true;
BOOST_CHECK(!OsInStr(XTSubVersion()));
BOOST_CHECK(!OsInStr(XTSubVersion(0)));

#if BOOST_VERSION >= 105500
argPtr->hideplatform = false;
argPtr->stealthmode = false;
BOOST_CHECK(OsInStr(XTSubVersion()));
BOOST_CHECK(OsInStr(XTSubVersion(0)));
#endif
}

Expand All @@ -78,10 +83,10 @@ BOOST_AUTO_TEST_CASE(xtsubversion_stealthmode)
= SetDummyArgGetter(std::unique_ptr<ArgGetter>(arg.release()));

argPtr->stealthmode = true;
BOOST_CHECK(XTSubVersion().find("XT") == std::string::npos);
BOOST_CHECK(XTSubVersion(0).find("XT") == std::string::npos);

argPtr->stealthmode = false;
BOOST_CHECK(XTSubVersion().find("XT") != std::string::npos);
BOOST_CHECK(XTSubVersion(0).find("XT") != std::string::npos);
}

BOOST_AUTO_TEST_CASE(xtsubversion_uacomment)
Expand All @@ -93,22 +98,32 @@ BOOST_AUTO_TEST_CASE(xtsubversion_uacomment)

argPtr->hideplatform = true;

// no comments
BOOST_CHECK(XTSubVersion().find("(") == std::string::npos);
// only EB comment
BOOST_CHECK(XTSubVersion(0).find("(EB0)") != std::string::npos);

// only uacomments
// uacomments + EB
argPtr->uacomment = {"hello", "world" };
BOOST_CHECK(XTSubVersion().find("(hello; world)") != std::string::npos);
BOOST_CHECK(XTSubVersion(0).find("(hello; world; EB0)") != std::string::npos);

#if BOOST_VERSION >= 105500
// combines with platform
argPtr->hideplatform = false;
BOOST_CHECK(XTSubVersion().find("(hello; world; ") != std::string::npos);
std::string withplatform = XTSubVersion(0);
BOOST_CHECK(withplatform.find("(hello; world; ") != std::string::npos);
BOOST_CHECK(OsInStr(withplatform.substr(withplatform.find("(hello; world; "))));
#endif

// allowed in stealth-mode
argPtr->stealthmode = true;
BOOST_CHECK(XTSubVersion().find("(hello; world)") != std::string::npos);
BOOST_CHECK(XTSubVersion(0).find("(hello; world)") != std::string::npos);
}

BOOST_AUTO_TEST_CASE(xtsubversion_eb)
{
// 1MB blocks
BOOST_CHECK(XTSubVersion(1000000).find("EB1)") != std::string::npos);
// 1GB blocks
BOOST_CHECK(XTSubVersion(1000 * 1000000).find("EB1000)") != std::string::npos);
}

BOOST_AUTO_TEST_SUITE_END()
Expand Down

0 comments on commit 7c3ccf1

Please sign in to comment.