Skip to content

Commit

Permalink
Add BIP100 string to getblocktemplate
Browse files Browse the repository at this point in the history
This adds BIP100 votes to blocks generated by miner software that
uses the rpc call getblocktemplate.
  • Loading branch information
dagurval authored and dgenr8 committed May 2, 2017
1 parent fbe17d6 commit 1a571fb
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1591,8 +1591,12 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
return nSubsidy;
}

bool fForceInitialBlockDownload = false;
bool IsInitialBlockDownload()
{
if (fForceInitialBlockDownload)
return false;

const CChainParams& chainParams = Params();
LOCK(cs_main);
if (fImporting || fReindex)
Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ void ThreadScriptCheck();
/** Try to detect Partition (network isolation) attacks against us */
void PartitionCheck(bool (*initialDownloadCheck)(), CCriticalSection& cs, const CBlockIndex *const &bestHeader, int64_t nPowTargetSpacing);
/** Check whether we are doing an initial block download (synchronizing from disk or network) */
extern bool fForceInitialBlockDownload;
bool IsInitialBlockDownload();
/** Format a string that describes several potential problems detected by the core.
* strFor can have three values:
Expand Down
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam
// BIP100 string:
// - Adds our block size vote (B) if configured.
// - Adds Excessive Block (EB) string. This announces how big blocks we currently accept.
static std::vector<unsigned char> BIP100Str(uint64_t hardLimit) {
std::vector<unsigned char> BIP100Str(uint64_t hardLimit) {
uint64_t blockVote = GetArg("-maxblocksizevote", 0);

std::stringstream ss;
Expand Down
1 change: 1 addition & 0 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& s
/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce, uint64_t nMaxBlockSize);
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
std::vector<unsigned char> BIP100Str(uint64_t hardlimit);

#endif // BITCOIN_MINER_H
7 changes: 4 additions & 3 deletions src/rpcmining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,14 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
int index_in_template = i - 1;
entry.push_back(Pair("fee", pblocktemplate->vTxFees[index_in_template]));
entry.push_back(Pair("sigops", pblocktemplate->vTxSigOps[index_in_template]));

transactions.push_back(entry);
}

uint64_t nMaxBlockSize = GetNextMaxBlockSize(chainActive.Tip(), Params().GetConsensus());
CScript flags = CScript() << BIP100Str(nMaxBlockSize);
flags += COINBASE_FLAGS;
UniValue aux(UniValue::VOBJ);
aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));
aux.push_back(Pair("flags", HexStr(flags.begin(), flags.end())));

arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);

Expand All @@ -605,7 +607,6 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
aMutable.push_back("transactions");
aMutable.push_back("prevblock");

uint64_t nMaxBlockSize = GetNextMaxBlockSize(chainActive.Tip(), Params().GetConsensus());
UniValue result(UniValue::VOBJ);
result.push_back(Pair("capabilities", aCaps));

Expand Down
1 change: 1 addition & 0 deletions src/test/miner_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_bip100str)
LOCK(cs_main);

// No vote defined. Should only contain EB.
mapArgs.erase("-maxblocksizevote");
std::string c = DefaultCoinbaseStr();
BOOST_CHECK(c.find("/BIP100/EB1/") != std::string::npos);
BOOST_CHECK(c.find("/B1/") == std::string::npos);
Expand Down
54 changes: 52 additions & 2 deletions src/test/rpc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
#include "rpcclient.h"

#include "base58.h"
#include "main.h"
#include "net.h"
#include "netbase.h"
#include "utilstrencodings.h"

#include "test/test_bitcoin.h"

Expand Down Expand Up @@ -228,7 +231,7 @@ BOOST_AUTO_TEST_CASE(json_parse_errors)
BOOST_AUTO_TEST_CASE(rpc_ban)
{
BOOST_CHECK_NO_THROW(CallRPC(string("clearbanned")));

UniValue r;
BOOST_CHECK_NO_THROW(r = CallRPC(string("setban 127.0.0.0 add")));
BOOST_CHECK_THROW(r = CallRPC(string("setban 127.0.0.0:8334")), runtime_error); //portnumber for setban not allowed
Expand Down Expand Up @@ -260,7 +263,7 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
adr = find_value(o1, "address");
banned_until = find_value(o1, "banned_until");
BOOST_CHECK_EQUAL(adr.get_str(), "127.0.0.0/24");
int64_t now = GetTime();
int64_t now = GetTime();
BOOST_CHECK(banned_until.get_int64() > now);
BOOST_CHECK(banned_until.get_int64()-now <= 200);

Expand Down Expand Up @@ -308,4 +311,51 @@ BOOST_AUTO_TEST_CASE(rpc_ban)
BOOST_CHECK_EQUAL(adr.get_str(), "2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/128");
}

static std::string get_coinbaseaux_flags(UniValue blocktpl) {
UniValue aux = find_value(blocktpl.get_obj(), "coinbaseaux").get_obj();
std::string hexstr = find_value(aux.get_obj(), "flags").get_str();
std::vector<unsigned char> parsed = ParseHex(hexstr);
return std::string(parsed.begin(), parsed.end());
}

// Put us in a state where we accept rpc calls.
class RpcMineState {
public:
RpcMineState()
{
// Don't throw "Bitcoin is downloading blocks"
fForceInitialBlockDownload = true;

// Don't throw "Bitcoin is not connected"
LOCK(cs_vNodes);
assert(vNodes.empty());
vNodes.push_back(new CNode(INVALID_SOCKET, CAddress()));

}
~RpcMineState() {
fForceInitialBlockDownload = false;

LOCK(cs_vNodes);
delete vNodes[0];
vNodes.clear();
}
};

BOOST_AUTO_TEST_CASE(rpc_getblocktemplate_vote)
{
RpcMineState raii;

mapArgs.erase("-maxblocksizevote");
UniValue noVote = CallRPC("getblocktemplate");
std::string f = get_coinbaseaux_flags(noVote);
BOOST_CHECK(f.find("/BIP100/EB1/") != std::string::npos);
BOOST_CHECK(f.find("/B1/") == std::string::npos);

SoftSetArg("-maxblocksizevote", "16");
UniValue hasVote = CallRPC("getblocktemplate");
f = get_coinbaseaux_flags(hasVote);
BOOST_CHECK(f.find("/BIP100/B16/EB1/") != std::string::npos);

}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 1a571fb

Please sign in to comment.