Skip to content

Commit

Permalink
BUIP005 Settings info user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
YarkoL committed Jan 9, 2016
1 parent 82fd775 commit f3ed098
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -43,6 +43,8 @@ src/qt/forms/ui_*.h

src/qt/test/moc*.cpp

Makefile.am.user

.deps
.dirstamp
.libs
Expand Down
2 changes: 1 addition & 1 deletion Makefile.am.user
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.6.0, 2016-01-04T19:44:09. -->
<!-- Written by QtCreator 3.6.0, 2016-01-09T19:24:07. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.test.include
Expand Up @@ -47,6 +47,7 @@ BITCOIN_TESTS =\
test/compress_tests.cpp \
test/crypto_tests.cpp \
test/DoS_tests.cpp \
test/excessiveblock_test.cpp \
test/getarg_tests.cpp \
test/hash_tests.cpp \
test/key_tests.cpp \
Expand Down
7 changes: 3 additions & 4 deletions src/net.cpp
Expand Up @@ -16,6 +16,8 @@
#include "scheduler.h"
#include "ui_interface.h"
#include "crypto/common.h"
#include "unlimited.h"
#include <boost/lexical_cast.hpp>

#ifdef WIN32
#include <string.h>
Expand Down Expand Up @@ -428,13 +430,10 @@ void CNode::PushVersion()
else
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id);
PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>()), nBestHeight, true);
nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, BUComments), nBestHeight, true);
}





std::map<CNetAddr, int64_t> CNode::setBanned;
CCriticalSection CNode::cs_setBanned;

Expand Down
2 changes: 2 additions & 0 deletions src/qt/unlimitedmodel.cpp
Expand Up @@ -146,10 +146,12 @@ bool UnlimitedModel::setData(const QModelIndex& index, const QVariant& value, in
break;
case ExcessiveBlockSize:
excessiveBlockSize = value.toUInt();
settingsToUserAgentString();
settings.setValue("excessiveBlockSize", excessiveBlockSize);
break;
case ExcessiveAcceptDepth:
excessiveAcceptDepth = value.toUInt();
settingsToUserAgentString();
settings.setValue("excessiveAcceptDepth",excessiveAcceptDepth);
break;
case UseReceiveShaping:
Expand Down
4 changes: 2 additions & 2 deletions src/rpcnet.cpp
Expand Up @@ -376,7 +376,7 @@ Value getnetworkinfo(const Array& params, bool fHelp)
"\nResult:\n"
"{\n"
" \"version\": xxxxx, (numeric) the server version\n"
" \"subversion\": \"/Satoshi:x.x.x/\", (string) the server subversion string\n"
" \"subversion\": \"/BitcoinUnlimited:x.x.x/\", (string) the server subversion string\n"
" \"protocolversion\": xxxxx, (numeric) the protocol version\n"
" \"localservices\": \"xxxxxxxxxxxxxxxx\", (string) the services we offer to the network\n"
" \"timeoffset\": xxxxx, (numeric) the time offset\n"
Expand Down Expand Up @@ -410,7 +410,7 @@ Value getnetworkinfo(const Array& params, bool fHelp)
Object obj;
obj.push_back(Pair("version", CLIENT_VERSION));
obj.push_back(Pair("subversion",
FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>())));
FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, BUComments)));
obj.push_back(Pair("protocolversion",PROTOCOL_VERSION));
obj.push_back(Pair("localservices", strprintf("%016x", nLocalServices)));
obj.push_back(Pair("timeoffset", GetTimeOffset()));
Expand Down
81 changes: 81 additions & 0 deletions src/test/excessiveblock_test.cpp
@@ -0,0 +1,81 @@
#include "unlimited.h"

#include "test/test_bitcoin.h"

#include <boost/algorithm/string.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/lexical_cast.hpp>

using namespace std;
using namespace json_spirit;

extern Value CallRPC(string args);

BOOST_FIXTURE_TEST_SUITE(excessiveblock_test, TestingSetup)

BOOST_AUTO_TEST_CASE(rpc_excessive)
{
BOOST_CHECK_NO_THROW(CallRPC("getexcessiveblock"));

BOOST_CHECK_NO_THROW(CallRPC("getminingmaxblock"));

BOOST_CHECK_THROW(CallRPC("setexcessiveblock not_uint"), runtime_error);
BOOST_CHECK_THROW(CallRPC("setexcessiveblock 0 not_uint"), boost::bad_lexical_cast);
BOOST_CHECK_THROW(CallRPC("setexcessiveblock 0 -1"), boost::bad_lexical_cast);
BOOST_CHECK_THROW(CallRPC("setexcessiveblock -1 0"), boost::bad_lexical_cast);
BOOST_CHECK_NO_THROW(CallRPC("setexcessiveblock 0 0"));
BOOST_CHECK_THROW(CallRPC("setexcessiveblock 0 0 0"), runtime_error);

BOOST_CHECK_THROW(CallRPC("setminingmaxblock"), runtime_error);
BOOST_CHECK_NO_THROW(CallRPC("setminingmaxblock 100000"));
BOOST_CHECK_THROW(CallRPC("setminingmaxblock not_uint"), boost::bad_lexical_cast);
BOOST_CHECK_THROW(CallRPC("setminingmaxblock -1"), boost::bad_lexical_cast);
BOOST_CHECK_THROW(CallRPC("setminingmaxblock 0"), runtime_error);
BOOST_CHECK_THROW(CallRPC("setminingmaxblock 0 0"), runtime_error);
}

BOOST_AUTO_TEST_CASE(buip005)
{
string exceptedEB;
string exceptedAD;
excessiveBlockSize = 1000000;
excessiveAcceptDepth = 9999999;
exceptedEB = "EB1";
exceptedAD = "AD9999999";
settingsToUserAgentString();
BOOST_CHECK_MESSAGE(BUComments.front() == exceptedEB,
"EB ought to have been " << exceptedEB << " when excessiveBlockSize = "
<< excessiveBlockSize << " but was " << BUComments.front());
BOOST_CHECK_MESSAGE(BUComments.back() == exceptedAD,
"AD ought to have been " << exceptedAD << " when excessiveBlockSize = " << excessiveAcceptDepth);
excessiveBlockSize = 100000;
excessiveAcceptDepth = 9999999 + 1;
exceptedEB = "EB0.1";
exceptedAD = "AD9999999";
settingsToUserAgentString();
BOOST_CHECK_MESSAGE(BUComments.front() == exceptedEB,
"EB ought to have been " << exceptedEB << " when excessiveBlockSize = "
<< excessiveBlockSize << " but was " << BUComments.front());
BOOST_CHECK_MESSAGE(BUComments.back() == exceptedAD,
"AD ought to have been " << exceptedAD << " when excessiveBlockSize = " << excessiveAcceptDepth);
excessiveBlockSize = 10000;
exceptedEB = "EB0";
settingsToUserAgentString();
BOOST_CHECK_MESSAGE(BUComments.front() == exceptedEB,
"EB ought to have been " << exceptedEB << " when excessiveBlockSize = "
<< excessiveBlockSize << " but was " << BUComments.front());
excessiveBlockSize = 150000;
exceptedEB = "EB0.1";
settingsToUserAgentString();
BOOST_CHECK_MESSAGE(BUComments.front() == exceptedEB,
"EB ought to have been rounded to " << exceptedEB << " when excessiveBlockSize = "
<< excessiveBlockSize << " but was " << BUComments.front());
excessiveBlockSize = 150000;
exceptedEB = "EB0.1";
settingsToUserAgentString();
BOOST_CHECK_MESSAGE(BUComments.front() == exceptedEB,
"EB ought to have been rounded to " << exceptedEB << " when excessiveBlockSize = "
<< excessiveBlockSize << " but was " << BUComments.front());
}

BOOST_AUTO_TEST_SUITE_END()
29 changes: 26 additions & 3 deletions src/unlimited.cpp
Expand Up @@ -18,6 +18,7 @@
#include "txmempool.h"
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <iomanip>

using namespace std;
using namespace json_spirit;
Expand All @@ -29,6 +30,8 @@ unsigned int excessiveBlockSize = DEFAULT_EXCESSIVE_BLOCK_SIZE;
unsigned int excessiveAcceptDepth = DEFAULT_EXCESSIVE_ACCEPT_DEPTH;
unsigned int maxMessageSizeMultiplier = DEFAULT_MAX_MESSAGE_SIZE_MULTIPLIER;

std::vector<std::string> BUComments = std::vector<std::string>();

// Variables for traffic shaping
CLeakyBucket receiveShaper(DEFAULT_MAX_RECV_BURST, DEFAULT_AVE_RECV);
CLeakyBucket sendShaper(DEFAULT_MAX_SEND_BURST, DEFAULT_AVE_SEND);
Expand Down Expand Up @@ -103,12 +106,29 @@ void UnlimitedPushTxns(CNode* dest)
dest->PushMessage("inv", vInv);
}

void settingsToUserAgentString()
{
BUComments.clear();

double ebInMegaBytes = (double)excessiveBlockSize/1000000;
std::stringstream ebss;
ebss <<std::fixed << std::setprecision(1) << ebInMegaBytes;
std::string eb = ebss.str();
std::string eb_formatted;
eb_formatted = (eb.at(eb.size() - 1) == '0' ? eb.substr(0, eb.size() - 2) : eb); //strip zero decimal
BUComments.push_back("EB" + eb_formatted);

int ad_formatted;
ad_formatted = (excessiveAcceptDepth >= 9999999 ? 9999999 : excessiveAcceptDepth);
BUComments.push_back("AD" + boost::lexical_cast<std::string>(ad_formatted));
}

void UnlimitedSetup(void)
{
maxGeneratedBlock = GetArg("-blockmaxsize", DEFAULT_MAX_GENERATED_BLOCK_SIZE);
excessiveBlockSize = GetArg("-excessiveblocksize", DEFAULT_EXCESSIVE_BLOCK_SIZE);
excessiveAcceptDepth = GetArg("-excessiveacceptdepth", DEFAULT_EXCESSIVE_ACCEPT_DEPTH);

settingsToUserAgentString();
// Init network shapers
int64_t rb = GetArg("-receiveburst", DEFAULT_MAX_RECV_BURST);
// parameter is in KBytes/sec, leaky bucket is in bytes/sec. But if it is "off" then don't multiply
Expand Down Expand Up @@ -204,7 +224,7 @@ Value getexcessiveblock(const Array& params, bool fHelp)

Value setexcessiveblock(const Array& params, bool fHelp)
{
if (fHelp || params.size() >= 3)
if (fHelp || params.size() < 2 || params.size() >= 3)
throw runtime_error(
"setexcessiveblock blockSize acceptDepth\n"
"\nSet the excessive block size and accept depth. Excessive blocks will not be used in the active chain or relayed until they are several blocks deep in the blockchain. This discourages the propagation of blocks that you consider excessively large. However, if the mining majority of the network builds upon the block then you will eventually accept it, maintaining consensus."
Expand All @@ -218,16 +238,18 @@ Value setexcessiveblock(const Array& params, bool fHelp)
excessiveBlockSize = params[0].get_uint64();
else {
string temp = params[0].get_str();
if (temp[0] == '-') boost::throw_exception( boost::bad_lexical_cast() );
excessiveBlockSize = boost::lexical_cast<unsigned int>(temp);
}

if (params[1].is_uint64())
excessiveAcceptDepth = params[1].get_uint64();
else {
string temp = params[1].get_str();
if (temp[0] == '-') boost::throw_exception( boost::bad_lexical_cast() );
excessiveAcceptDepth = boost::lexical_cast<unsigned int>(temp);
}

settingsToUserAgentString();
return Value::null;
}

Expand Down Expand Up @@ -265,6 +287,7 @@ Value setminingmaxblock(const Array& params, bool fHelp)
arg = params[0].get_uint64();
else {
string temp = params[0].get_str();
if (temp[0] == '-') boost::throw_exception( boost::bad_lexical_cast() );
arg = boost::lexical_cast<uint64_t>(temp);
}

Expand Down
3 changes: 3 additions & 0 deletions src/unlimited.h
Expand Up @@ -28,6 +28,9 @@ extern unsigned int excessiveBlockSize;
extern unsigned int excessiveAcceptDepth;
extern unsigned int maxMessageSizeMultiplier;

extern std::vector<std::string> BUComments;
extern void settingsToUserAgentString();

extern void UnlimitedSetup(void);
extern std::string UnlimitedCmdLineHelp();

Expand Down

0 comments on commit f3ed098

Please sign in to comment.