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 19, 2017
1 parent 9f33da5 commit a10ed4d
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bool CAlert::AppliesTo(int nVersion, const std::string& strSubVerIn) const

bool CAlert::AppliesToMe() const
{
return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<std::string>()));
return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<std::string>(), 0));
}

bool CAlert::RelayTo(CNode* pnode) const
Expand Down
14 changes: 13 additions & 1 deletion src/clientversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "tinyformat.h"

#include <string>
#include <iomanip>
#include <cmath>

/**
* Name of client reported in the 'version' message. Report the same name
Expand Down Expand Up @@ -94,8 +96,18 @@ std::string FormatFullVersion()
/**
* Format the subversion field according to BIP 14 spec (https://github.com/bitcoin/bips/blob/master/bip-0014.mediawiki)
*/
std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
std::string FormatSubVersion(const std::string& name, int nClientVersion, std::vector<std::string> comments, uint64_t nMaxBlockSize)
{
if (nMaxBlockSize) {
// Announce our excessive block acceptence.
comments.insert(comments.end(), std::string("BIP100"));

std::stringstream ss;
double dMaxBlockSize = static_cast<double>(nMaxBlockSize) / 1000000;
ss << "EB" << std::setprecision(static_cast<int>(std::log10(dMaxBlockSize))+7) << dMaxBlockSize;
comments.insert(comments.end(), ss.str());
}

std::ostringstream ss;
ss << "/";
ss << name << ":" << FormatVersion(nClientVersion);
Expand Down
3 changes: 2 additions & 1 deletion src/clientversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

#include <string>
#include <vector>
#include <stdint.h>

static const int CLIENT_VERSION =
1000000 * CLIENT_VERSION_MAJOR
Expand All @@ -63,7 +64,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);
std::string FormatSubVersion(const std::string& name, int nClientVersion, std::vector<std::string> comments, uint64_t nMaxBlockSize);

#endif // WINDRES_PREPROC

Expand Down
10 changes: 5 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,17 +1139,17 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
RegisterNodeSignals(GetNodeSignals());

// sanitize comments per BIP-0014, format user agent and check total size
std::vector<string> uacomments;
BOOST_FOREACH(string cmt, mapMultiArgs["-uacomment"])
{
if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT))
return InitError(strprintf(_("User Agent comment (%s) contains unsafe characters."), cmt));
uacomments.push_back(SanitizeString(cmt, SAFE_CHARS_UA_COMMENT));
vUAComments.push_back(SanitizeString(cmt, SAFE_CHARS_UA_COMMENT));
}
strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments);
if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) {
uint64_t hugeBlock = static_cast<uint64_t>((100000. / 3.) * MAX_BLOCK_SIZE);
size_t userAgentLen = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, vUAComments, hugeBlock).size();
if (userAgentLen > MAX_SUBVERSION_LENGTH) {
return InitError(strprintf(_("Total length of network version string (%i) exceeds maximum length (%i). Reduce the number or size of uacomments."),
strSubVersion.size(), MAX_SUBVERSION_LENGTH));
userAgentLen, MAX_SUBVERSION_LENGTH));
}

if (mapArgs.count("-onlynet")) {
Expand Down
10 changes: 9 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ int GetHeight()
return chainActive.Height();
}

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

void UpdatePreferredDownload(CNode* node, CNodeState* state)
{
nPreferredDownload -= state->fPreferredDownload;
Expand Down Expand Up @@ -572,6 +578,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 @@ -582,6 +589,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 Expand Up @@ -4000,7 +4008,7 @@ bool LoadBlockIndex(bool* fRebuildRequired)
return true;
}

bool InitBlockIndex(const CChainParams& chainparams)
bool InitBlockIndex(const CChainParams& chainparams)
{
LOCK(cs_main);

Expand Down
8 changes: 5 additions & 3 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static std::vector<ListenSocket> vhListenSocket;
CAddrMan addrman;
int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
bool fAddressesInitialized = false;
std::string strSubVersion;
std::vector<std::string> vUAComments;

vector<CNode*> vNodes;
CCriticalSection cs_vNodes;
Expand Down Expand Up @@ -442,6 +442,7 @@ void CNode::CloseSocketDisconnect()
void CNode::PushVersion()
{
int nBestHeight = g_signals.GetHeight().get_value_or(0);
int nMaxBlockSize = g_signals.GetMaxBlockSize().get_value_or(0);

int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0)));
Expand All @@ -451,6 +452,7 @@ void CNode::PushVersion()
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id);
else
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id);
std::string strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, vUAComments, nMaxBlockSize);
PushMessage(NetMsgType::VERSION, PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
nLocalHostNonce, strSubVersion, nBestHeight, !GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY));
}
Expand Down Expand Up @@ -2580,14 +2582,14 @@ bool CBanDB::Read(banmap_t& banSet)
// ... verify the network matches ours
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
return error("%s: Invalid network magic number", __func__);

// de-serialize address data into one CAddrMan object
ssBanlist >> banSet;
}
catch (const std::exception& e) {
return error("%s: Deserialize or I/O error - %s", __func__, e.what());
}

return true;
}

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


Expand Down Expand Up @@ -174,8 +175,8 @@ extern CCriticalSection cs_vAddedNodes;
extern NodeId nLastNodeId;
extern CCriticalSection cs_nLastNodeId;

/** Subversion as sent to the P2P network in `version` messages */
extern std::string strSubVersion;
/** Comments in subversion sent to the P2P network in `version` messages */
extern std::vector<std::string> vUAComments;

struct LocalServiceInfo {
int nScore;
Expand Down
2 changes: 1 addition & 1 deletion src/qt/clientmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ QString ClientModel::formatFullVersion() const

QString ClientModel::formatSubVersion() const
{
return QString::fromStdString(strSubVersion);
return QString::fromStdString(FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, vUAComments, 0));
}

QString ClientModel::formatBuildDate() const
Expand Down
3 changes: 2 additions & 1 deletion src/rpcnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp)

UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("version", CLIENT_VERSION));
obj.push_back(Pair("subversion", strSubVersion));
obj.push_back(Pair("subversion",
FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, vUAComments, 0)));
obj.push_back(Pair("protocolversion",PROTOCOL_VERSION));
obj.push_back(Pair("localservices", strprintf("%016x", nLocalServices)));
obj.push_back(Pair("timeoffset", GetTimeOffset()));
Expand Down
13 changes: 10 additions & 3 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "utilstrencodings.h"
#include "utilmoneystr.h"
#include "test/test_bitcoin.h"
#include "consensus/consensus.h"

#include <stdint.h>
#include <vector>
Expand Down Expand Up @@ -419,9 +420,15 @@ BOOST_AUTO_TEST_CASE(test_FormatSubVersion)
std::vector<std::string> comments2;
comments2.push_back(std::string("comment1"));
comments2.push_back(SanitizeString(std::string("Comment2; .,_?@-; !\"#$%&'()*+/<=>[]\\^`{|}~"), SAFE_CHARS_UA_COMMENT)); // Semicolon is discouraged but not forbidden by BIP-0014
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>()),std::string("/Test:0.9.99/"));
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:0.9.99(comment1)/"));
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/"));
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>(), 0),std::string("/Test:0.9.99/"));
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments, 0),std::string("/Test:0.9.99(comment1)/"));
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2, 0),std::string("/Test:0.9.99(comment1; Comment2; .,_?@-; )/"));

// BIP100
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, std::vector<std::string>(), MAX_BLOCK_SIZE),std::string("/Test:0.9.99(BIP100; EB1)/"));
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments, MAX_BLOCK_SIZE),std::string("/Test:0.9.99(comment1; BIP100; EB1)/"));
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments, MAX_BLOCK_SIZE + (MAX_BLOCK_SIZE / 3)),
std::string("/Test:0.9.99(comment1; BIP100; EB1.333333)/"));
}

BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
Expand Down

0 comments on commit a10ed4d

Please sign in to comment.