Skip to content

Commit

Permalink
Introduce and preferentially peer with NODE_WITNESS service bit
Browse files Browse the repository at this point in the history
Service bit logic by Nicolas Dorier.
  • Loading branch information
sipa committed Apr 19, 2016
1 parent adb1c09 commit ac6886d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/main.cpp
Expand Up @@ -265,6 +265,8 @@ struct CNodeState {
bool fPreferredDownload;
//! Whether this peer wants invs or headers (when possible) for block announcements.
bool fPreferHeaders;
//! Whether this peer can give us witnesses
bool fHaveWitness;

CNodeState() {
fCurrentlyConnected = false;
Expand All @@ -281,6 +283,7 @@ struct CNodeState {
nBlocksInFlightValidHeaders = 0;
fPreferredDownload = false;
fPreferHeaders = false;
fHaveWitness = false;
}
};

Expand Down Expand Up @@ -4600,6 +4603,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,

pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);

if((pfrom->nServices & NODE_WITNESS))
{
LOCK(cs_main);
State(pfrom->GetId())->fHaveWitness = true;
}

// Potentially mark this peer as a preferred download peer.
UpdatePreferredDownload(pfrom, State(pfrom->GetId()));

Expand Down Expand Up @@ -4782,14 +4791,18 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,

for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
{
const CInv &inv = vInv[nInv];
CInv &inv = vInv[nInv];

boost::this_thread::interruption_point();
pfrom->AddInventoryKnown(inv);

bool fAlreadyHave = AlreadyHave(inv);
LogPrint("net", "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->id);

if (inv.type == MSG_TX && State(pfrom->GetId())->fHaveWitness) {
inv.type = MSG_WITNESS_TX;
}

if (inv.type == MSG_BLOCK) {
UpdateBlockAvailability(pfrom->GetId(), inv.hash);
if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
Expand All @@ -4805,6 +4818,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
CNodeState *nodestate = State(pfrom->GetId());
if (CanDirectFetch(chainparams.GetConsensus()) &&
nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
if (State(pfrom->GetId())->fHaveWitness) {
inv.type = MSG_WITNESS_BLOCK;
}
vToFetch.push_back(inv);
// Mark block as in flight already, even though the actual "getdata" message only goes out
// later (within the same cs_main lock, though).
Expand Down Expand Up @@ -5883,7 +5899,7 @@ bool SendMessages(CNode* pto)
NodeId staller = -1;
FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);
BOOST_FOREACH(CBlockIndex *pindex, vToDownload) {
vGetData.push_back(CInv(MSG_BLOCK, pindex->GetBlockHash()));
vGetData.push_back(CInv(State(staller)->fHaveWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK, pindex->GetBlockHash()));
MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);
LogPrint("net", "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
pindex->nHeight, pto->id);
Expand Down
8 changes: 6 additions & 2 deletions src/net.cpp
Expand Up @@ -72,14 +72,14 @@ namespace {
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";

/** Services this node implementation cares about */
static const uint64_t nRelevantServices = NODE_NETWORK;
static const uint64_t nRelevantServices = NODE_NETWORK | NODE_WITNESS;

//
// Global state variables
//
bool fDiscover = true;
bool fListen = true;
uint64_t nLocalServices = NODE_NETWORK;
uint64_t nLocalServices = NODE_NETWORK | NODE_WITNESS;
CCriticalSection cs_mapLocalHost;
map<CNetAddr, LocalServiceInfo> mapLocalHost;
static bool vfLimited[NET_MAX] = {};
Expand Down Expand Up @@ -1601,6 +1601,10 @@ void ThreadOpenConnections()
if (nANow - addr.nLastTry < 600 && nTries < 30)
continue;

// only consider non-witness nodes after 40 failed attemps
if (!(addr.nServices & NODE_WITNESS) && nTries < 40)
continue;

// do not allow non-default ports, unless after 50 invalid addresses selected already
if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/protocol.h
Expand Up @@ -236,6 +236,9 @@ enum {
// Bitcoin Core nodes used to support this by default, without advertising this bit,
// but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
NODE_BLOOM = (1 << 2),
// Indicates that a node can be asked for blocks and transactions including
// witness data.
NODE_WITNESS = (1 << 3),

// Bits 24-31 are reserved for temporary experiments. Just pick a bit that
// isn't getting used, or one not being used much, and notify the
Expand Down
3 changes: 3 additions & 0 deletions src/version.h
Expand Up @@ -39,4 +39,7 @@ static const int SENDHEADERS_VERSION = 70012;
//! "feefilter" tells peers to filter invs to you by fee starts with this version
static const int FEEFILTER_VERSION = 70013;

//! Version after which witness support potentially exists
static const int WITNESS_VERSION = 70013;

#endif // BITCOIN_VERSION_H

0 comments on commit ac6886d

Please sign in to comment.