Skip to content

Commit

Permalink
Add and document network messages in protocol.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonix committed Jul 5, 2022
1 parent 1676e45 commit 9b29112
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ bool CAlert::RelayTo(CNode* pnode) const
AppliesToMe() ||
GetAdjustedTime() < nRelayUntil)
{
pnode->PushMessage("alert", *this);
pnode->PushMessage(NetMsgType::ALERT, *this);
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/gridcoin/scraper/scraper_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ bool CSplitBlob::SendPartTo(CNode* pto, const uint256& hash) EXCLUSIVE_LOCKS_REQ
{
if (ipart->second.present())
{
pto->PushMessage("part",ipart->second.getReader());
pto->PushMessage(NetMsgType::PART,ipart->second.getReader());
return true;
}
}
Expand Down Expand Up @@ -275,7 +275,7 @@ EXCLUSIVE_LOCKS_REQUIRED(CSplitBlob::cs_mapParts)
{
LOCK(manifest->cs_manifest);

pto->PushMessage("scraperindex", *manifest);
pto->PushMessage(NetMsgType::SCRAPERINDEX, *manifest);

return true;
}
Expand Down
65 changes: 32 additions & 33 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3499,7 +3499,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
{
LogPrint(BCLog::LogFlags::NOISY, "received: %s from %s (%" PRIszu " bytes)", strCommand, pfrom->addrName, vRecv.size());

if (strCommand == "aries")
if (strCommand == NetMsgType::ARIES || strCommand == NetMsgType::VERSION)
{
// Each connection can only send one version message
if (pfrom->nVersion != 0)
Expand Down Expand Up @@ -3615,7 +3615,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
AddTimeData(pfrom->addr, nOffsetSample);

// Change version
pfrom->PushMessage("verack");
pfrom->PushMessage(NetMsgType::VERACK);
pfrom->ssSend.SetVersion(min(pfrom->nVersion, PROTOCOL_VERSION));


Expand All @@ -3628,7 +3628,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}

// Get recent addresses
pfrom->PushMessage("getaddr");
pfrom->PushMessage(NetMsgType::GETADDR);
pfrom->fGetAddr = true;
addrman.Good(pfrom->addr);
}
Expand Down Expand Up @@ -3672,13 +3672,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
pfrom->fDisconnect=true;
return false;
}
else if (strCommand == "verack")
else if (strCommand == NetMsgType::VERACK)
{
pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
}
else if (strCommand == "gridaddr")
else if (strCommand == NetMsgType::GRIDADDR || strCommand == NetMsgType::ADDR)
{
//addr->gridaddr
vector<CAddress> vAddr;
vRecv >> vAddr;

Expand Down Expand Up @@ -3742,7 +3741,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
pfrom->fDisconnect = true;
}

else if (strCommand == "inv")
else if (strCommand == NetMsgType::INV)
{
vector<CInv> vInv;
vRecv >> vInv;
Expand Down Expand Up @@ -3815,7 +3814,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}


else if (strCommand == "getdata")
else if (strCommand == NetMsgType::GETDATA)
{
vector<CInv> vInv;
vRecv >> vInv;
Expand Down Expand Up @@ -3849,7 +3848,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
CBlock block;
ReadBlockFromDisk(block, mi->second, Params().GetConsensus());

pfrom->PushMessage("encrypt", block);
pfrom->PushMessage(NetMsgType::ENCRYPT, block);

// Trigger them to send a getblocks request for the next batch of inventory
if (inv.hash == pfrom->hashContinue)
Expand All @@ -3859,7 +3858,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
// wait for other stuff first.
vector<CInv> vInv;
vInv.push_back(CInv(MSG_BLOCK, hashBestChain));
pfrom->PushMessage("inv", vInv);
pfrom->PushMessage(NetMsgType::INV, vInv);
pfrom->hashContinue.SetNull();
}
}
Expand All @@ -3882,7 +3881,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << tx;
pfrom->PushMessage("tx", ss);
pfrom->PushMessage(NetMsgType::TX, ss);
}
}
else if(!pushed && inv.type == MSG_PART) {
Expand Down Expand Up @@ -3947,7 +3946,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}
}

else if (strCommand == "getblocks")
else if (strCommand == NetMsgType::GETBLOCKS)
{
CBlockLocator locator;
uint256 hashStop;
Expand Down Expand Up @@ -3986,7 +3985,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}
}
}
else if (strCommand == "getheaders")
else if (strCommand == NetMsgType::GETHEADERS)
{
CBlockLocator locator;
uint256 hashStop;
Expand Down Expand Up @@ -4020,9 +4019,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
break;
}
pfrom->PushMessage("headers", vHeaders);
pfrom->PushMessage(NetMsgType::HEADERS, vHeaders);
}
else if (strCommand == "tx")
else if (strCommand == NetMsgType::TX)
{
vector<uint256> vWorkQueue;
vector<uint256> vEraseQueue;
Expand Down Expand Up @@ -4088,7 +4087,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}


else if (strCommand == "encrypt")
else if (strCommand == NetMsgType::ENCRYPT || strCommand == NetMsgType::BLOCK)
{
//Response from getblocks, message = block

Expand Down Expand Up @@ -4119,7 +4118,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}


else if (strCommand == "getaddr")
else if (strCommand == NetMsgType::GETADDR)
{
// Don't return addresses older than nCutOff timestamp
int64_t nCutOff = GetAdjustedTime() - (nNodeLifespan * 24 * 60 * 60);
Expand All @@ -4131,7 +4130,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}


else if (strCommand == "mempool")
else if (strCommand == NetMsgType::MEMPOOL)
{
LOCK(cs_main);

Expand All @@ -4145,9 +4144,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
break;
}
if (vInv.size() > 0)
pfrom->PushMessage("inv", vInv);
pfrom->PushMessage(NetMsgType::INV, vInv);
}
else if (strCommand == "ping")
else if (strCommand == NetMsgType::PING)
{
uint64_t nonce = 0;
vRecv >> nonce;
Expand All @@ -4163,9 +4162,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
// it, if the remote node sends a ping once per second and this node takes 5
// seconds to respond to each, the 5th ping the remote sends would appear to
// return very quickly.
pfrom->PushMessage("pong", nonce);
pfrom->PushMessage(NetMsgType::PONG, nonce);
}
else if (strCommand == "pong")
else if (strCommand == NetMsgType::PONG)
{
int64_t pingUsecEnd = GetTimeMicros();
uint64_t nonce = 0;
Expand Down Expand Up @@ -4220,7 +4219,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
pfrom->nPingNonceSent = 0;
}
}
else if (strCommand == "alert")
else if (strCommand == NetMsgType::ALERT)
{
CAlert alert;
vRecv >> alert;
Expand Down Expand Up @@ -4250,11 +4249,11 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}
}

else if (strCommand == "scraperindex")
else if (strCommand == NetMsgType::SCRAPERINDEX)
{
CScraperManifest::RecvManifest(pfrom, vRecv);
}
else if (strCommand == "part")
else if (strCommand == NetMsgType::PART)
{
CSplitBlob::RecvPart(pfrom, vRecv);
}
Expand All @@ -4276,7 +4275,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,

// Update the last seen time for this node's address
if (pfrom->fNetworkNode)
if (strCommand == "aries" || strCommand == "gridaddr" || strCommand == "inv" || strCommand == "getdata" || strCommand == "ping")
if (strCommand == NetMsgType::ARIES || strCommand == NetMsgType::GRIDADDR || strCommand == NetMsgType::INV || strCommand == NetMsgType::GETDATA || strCommand == NetMsgType::PING || strCommand == NetMsgType::VERSION || strCommand == NetMsgType::ADDR)
AddressCurrentlyConnected(pfrom->addr);

return true;
Expand Down Expand Up @@ -4444,7 +4443,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
pto->nPingUsecStart = GetTimeMicros();
pto->nPingNonceSent = nonce;

pto->PushMessage("ping", nonce);
pto->PushMessage(NetMsgType::PING, nonce);
}

// Resend wallet transactions that haven't gotten in a block yet
Expand Down Expand Up @@ -4482,14 +4481,14 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
// receiver rejects addr messages larger than 1000
if (vAddr.size() >= 1000)
{
pto->PushMessage("gridaddr", vAddr);
pto->PushMessage(NetMsgType::GRIDADDR, vAddr);
vAddr.clear();
}
}
}
pto->vAddrToSend.clear();
if (!vAddr.empty())
pto->PushMessage("gridaddr", vAddr);
pto->PushMessage(NetMsgType::GRIDADDR, vAddr);
}


Expand Down Expand Up @@ -4540,15 +4539,15 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
vInv.push_back(inv);
if (vInv.size() >= 1000)
{
pto->PushMessage("inv", vInv);
pto->PushMessage(NetMsgType::INV, vInv);
vInv.clear();
}
}
}
pto->vInventoryToSend = vInvWait;
}
if (!vInv.empty())
pto->PushMessage("inv", vInv);
pto->PushMessage(NetMsgType::INV, vInv);


//
Expand Down Expand Up @@ -4588,7 +4587,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
vGetData.push_back(inv);
if (vGetData.size() >= 1000)
{
pto->PushMessage("getdata", vGetData);
pto->PushMessage(NetMsgType::GETDATA, vGetData);
vGetData.clear();
}

Expand All @@ -4597,7 +4596,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
pto->mapAskFor.erase(pto->mapAskFor.begin());
}
if (!vGetData.empty())
pto->PushMessage("getdata", vGetData);
pto->PushMessage(NetMsgType::GETDATA, vGetData);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
g_getblocks_locator = CBlockLocator(pindexBegin);
}

PushMessage("getblocks", g_getblocks_locator, hashEnd);
PushMessage(NetMsgType::GETBLOCKS, g_getblocks_locator, hashEnd);
}

// find 'best' local address for a particular peer
Expand Down Expand Up @@ -492,7 +492,7 @@ void CNode::PushVersion()

//TODO: change `PushMessage()` to use ServiceFlags so we don't need to cast nLocalServices
PushMessage(
"aries",
NetMsgType::ARIES,
PROTOCOL_VERSION,
(uint64_t)nLocalServices,
nTime,
Expand Down
73 changes: 68 additions & 5 deletions src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,74 @@
# include <arpa/inet.h>
#endif

namespace NetMsgType {
const char *VERSION="version";
const char *VERACK="verack";
const char *ADDR="addr";
const char *INV="inv";
const char *GETDATA="getdata";
const char *GETBLOCKS="getblocks";
const char *GETHEADERS="getheaders";
const char *TX="tx";
const char *HEADERS="headers";
const char *BLOCK="block";
const char *GETADDR="getaddr";
const char *MEMPOOL="mempool";
const char *PING="ping";
const char *PONG="pong";
const char *ALERT="alert";

// Gridcoin aliases (to be removed)
const char *ENCRYPT="encrypt";
const char *GRIDADDR="gridaddr";
const char *ARIES="aries";

// Gridcoin specific
const char *SCRAPERINDEX="scraperindex";
const char *PART="part";
}

static const char* ppszTypeName[] =
{
"ERROR",
"tx",
"block",
"part",
"scraperindex",
"ERROR", // Should never occur
NetMsgType::TX,
NetMsgType::BLOCK,
NetMsgType::PART,
NetMsgType::SCRAPERINDEX,
};

/** All known message types. Keep this in the same order as the list of
* messages above and in protocol.h.
*/
const static std::string allNetMessageTypes[] = {
NetMsgType::VERSION,
NetMsgType::VERACK,
NetMsgType::ADDR,
NetMsgType::INV,
NetMsgType::GETDATA,
NetMsgType::GETBLOCKS,
NetMsgType::GETHEADERS,
NetMsgType::TX,
NetMsgType::HEADERS,
NetMsgType::BLOCK,
NetMsgType::GETADDR,
NetMsgType::MEMPOOL,
NetMsgType::PING,
NetMsgType::PONG,
NetMsgType::ALERT,

// Gridcoin aliases (to be removed)
NetMsgType::ENCRYPT,
NetMsgType::GRIDADDR,
NetMsgType::ARIES,

// Gridcoin specific
NetMsgType::SCRAPERINDEX,
NetMsgType::PART,
};

const static std::vector<std::string> allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes));

CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
{
memcpy(pchMessageStart, Params().MessageStart(), CMessageHeader::MESSAGE_START_SIZE);
Expand Down Expand Up @@ -122,3 +181,7 @@ void CInv::print() const
LogPrintf("CInv(%s)", ToString());
}

const std::vector<std::string> &getAllNetMessageTypes()
{
return allNetMessageTypesVec;
}
Loading

0 comments on commit 9b29112

Please sign in to comment.