Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Net] Add and document network messages in protocol.h #1542

Merged
merged 3 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4204,7 +4204,7 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, CBlock* pblock, CDis
//if we get this far, check if the prev block is our prev block, if not then request sync and return false
BlockMap::iterator mi = mapBlockIndex.find(pblock->hashPrevBlock);
if (mi == mapBlockIndex.end()) {
pfrom->PushMessage("getblocks", chainActive.GetLocator(), UINT256_ZERO);
pfrom->PushMessage(NetMsgType::GETBLOCKS, chainActive.GetLocator(), UINT256_ZERO);
return false;
}
}
Expand Down Expand Up @@ -5091,7 +5091,7 @@ void static ProcessGetData(CNode* pfrom)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << mapTxLockVote[inv.hash];
pfrom->PushMessage("txlvote", ss);
pfrom->PushMessage(NetMsgType::IXLOCKVOTE, ss);
pushed = true;
}
}
Expand All @@ -5100,7 +5100,7 @@ void static ProcessGetData(CNode* pfrom)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << mapTxLockReq[inv.hash];
pfrom->PushMessage("ix", ss);
pfrom->PushMessage(NetMsgType::IX, ss);
pushed = true;
}
}
Expand All @@ -5109,7 +5109,7 @@ void static ProcessGetData(CNode* pfrom)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << mapSporks[inv.hash];
pfrom->PushMessage("spork", ss);
pfrom->PushMessage(NetMsgType::SPORK, ss);
pushed = true;
}
}
Expand All @@ -5118,7 +5118,7 @@ void static ProcessGetData(CNode* pfrom)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << masternodePayments.mapMasternodePayeeVotes[inv.hash];
pfrom->PushMessage("mnw", ss);
pfrom->PushMessage(NetMsgType::MNWINNER, ss);
pushed = true;
}
}
Expand All @@ -5127,7 +5127,7 @@ void static ProcessGetData(CNode* pfrom)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << budget.mapSeenMasternodeBudgetVotes[inv.hash];
pfrom->PushMessage("mvote", ss);
pfrom->PushMessage(NetMsgType::BUDGETVOTE, ss);
pushed = true;
}
}
Expand All @@ -5137,7 +5137,7 @@ void static ProcessGetData(CNode* pfrom)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << budget.mapSeenMasternodeBudgetProposals[inv.hash];
pfrom->PushMessage("mprop", ss);
pfrom->PushMessage(NetMsgType::BUDGETPROPOSAL, ss);
pushed = true;
}
}
Expand All @@ -5147,7 +5147,7 @@ void static ProcessGetData(CNode* pfrom)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << budget.mapSeenFinalizedBudgetVotes[inv.hash];
pfrom->PushMessage("fbvote", ss);
pfrom->PushMessage(NetMsgType::FINALBUDGETVOTE, ss);
pushed = true;
}
}
Expand All @@ -5157,7 +5157,7 @@ void static ProcessGetData(CNode* pfrom)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << budget.mapSeenFinalizedBudgets[inv.hash];
pfrom->PushMessage("fbs", ss);
pfrom->PushMessage(NetMsgType::FINALBUDGET, ss);
pushed = true;
}
}
Expand All @@ -5167,7 +5167,7 @@ void static ProcessGetData(CNode* pfrom)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss.reserve(1000);
ss << mnodeman.mapSeenMasternodeBroadcast[inv.hash];
pfrom->PushMessage("mnb", ss);
pfrom->PushMessage(NetMsgType::MNBROADCAST, ss);
pushed = true;
}
}
Expand Down Expand Up @@ -5268,7 +5268,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR

if (fMissingSporks || !fRequestedSporksIDB){
LogPrintf("asking peer for sporks\n");
pfrom->PushMessage("getsporks");
pfrom->PushMessage(NetMsgType::GETSPORKS);
fRequestedSporksIDB = true;
}

Expand Down Expand Up @@ -5905,9 +5905,9 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
}

else if (!(nLocalServices & NODE_BLOOM) &&
(strCommand == "filterload" ||
strCommand == "filteradd" ||
strCommand == "filterclear")) {
(strCommand == NetMsgType::FILTERLOAD ||
strCommand == NetMsgType::FILTERADD ||
strCommand == NetMsgType::FILTERCLEAR)) {
LogPrintf("bloom message=%s\n", strCommand);
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100);
Expand Down Expand Up @@ -5981,13 +5981,27 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
LogPrint(BCLog::NET, "Unparseable reject message received\n");
}
} else {
//probably one the extensions
mnodeman.ProcessMessage(pfrom, strCommand, vRecv);
budget.ProcessMessage(pfrom, strCommand, vRecv);
masternodePayments.ProcessMessageMasternodePayments(pfrom, strCommand, vRecv);
ProcessMessageSwiftTX(pfrom, strCommand, vRecv);
sporkManager.ProcessSpork(pfrom, strCommand, vRecv);
masternodeSync.ProcessMessage(pfrom, strCommand, vRecv);
bool found = false;
const std::vector<std::string> &allMessages = getAllNetMessageTypes();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used for the tier two messages check, would be good to call a getAllTierTwoMessageTypes instead. Decreasing the amount of cycles of the loop that is below.

for (const std::string msg : allMessages) {
if(msg == strCommand) {
found = true;
break;
}
}

if (found) {
//probably one the extensions
mnodeman.ProcessMessage(pfrom, strCommand, vRecv);
budget.ProcessMessage(pfrom, strCommand, vRecv);
masternodePayments.ProcessMessageMasternodePayments(pfrom, strCommand, vRecv);
ProcessMessageSwiftTX(pfrom, strCommand, vRecv);
sporkManager.ProcessSpork(pfrom, strCommand, vRecv);
masternodeSync.ProcessMessage(pfrom, strCommand, vRecv);
} else {
// Ignore unknown commands for extensibility
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->id);
}
}


Expand Down
22 changes: 11 additions & 11 deletions src/masternode-budget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,26 +1071,26 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData

LOCK(cs_budget);

if (strCommand == "mnvs") { //Masternode vote sync
if (strCommand == NetMsgType::BUDGETVOTESYNC) { //Masternode vote sync
uint256 nProp;
vRecv >> nProp;

if (Params().NetworkID() == CBaseChainParams::MAIN) {
if (nProp.IsNull()) {
if (pfrom->HasFulfilledRequest("mnvs")) {
if (pfrom->HasFulfilledRequest("budgetvotesync")) {
LogPrint(BCLog::MNBUDGET,"mnvs - peer already asked me for the list\n");
Misbehaving(pfrom->GetId(), 20);
return;
}
pfrom->FulfilledRequest("mnvs");
pfrom->FulfilledRequest("budgetvotesync");
}
}

Sync(pfrom, nProp);
LogPrint(BCLog::MNBUDGET, "mnvs - Sent Masternode votes to peer %i\n", pfrom->GetId());
}

if (strCommand == "mprop") { //Masternode Proposal
if (strCommand == NetMsgType::BUDGETPROPOSAL) { //Masternode Proposal
CBudgetProposalBroadcast budgetProposalBroadcast;
vRecv >> budgetProposalBroadcast;

Expand Down Expand Up @@ -1126,7 +1126,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
CheckOrphanVotes();
}

if (strCommand == "mvote") { //Masternode Vote
if (strCommand == NetMsgType::BUDGETVOTE) { // Budget Vote
CBudgetVote vote;
vRecv >> vote;
vote.fValid = true;
Expand Down Expand Up @@ -1164,7 +1164,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
LogPrint(BCLog::MNBUDGET,"mvote - new budget vote for budget %s - %s\n", vote.nProposalHash.ToString(), vote.GetHash().ToString());
}

if (strCommand == "fbs") { //Finalized Budget Suggestion
if (strCommand == NetMsgType::FINALBUDGET) { //Finalized Budget Suggestion
CFinalizedBudgetBroadcast finalizedBudgetBroadcast;
vRecv >> finalizedBudgetBroadcast;

Expand Down Expand Up @@ -1201,7 +1201,7 @@ void CBudgetManager::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
CheckOrphanVotes();
}

if (strCommand == "fbvote") { //Finalized Budget Vote
if (strCommand == NetMsgType::FINALBUDGETVOTE) { //Finalized Budget Vote
CFinalizedBudgetVote vote;
vRecv >> vote;
vote.fValid = true;
Expand Down Expand Up @@ -1360,7 +1360,7 @@ void CBudgetManager::Sync(CNode* pfrom, uint256 nProp, bool fPartial)
++it1;
}

pfrom->PushMessage("ssc", MASTERNODE_SYNC_BUDGET_PROP, nInvCount);
pfrom->PushMessage(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_BUDGET_PROP, nInvCount);

LogPrint(BCLog::MNBUDGET, "CBudgetManager::Sync - sent %d items\n", nInvCount);

Expand Down Expand Up @@ -1388,7 +1388,7 @@ void CBudgetManager::Sync(CNode* pfrom, uint256 nProp, bool fPartial)
++it3;
}

pfrom->PushMessage("ssc", MASTERNODE_SYNC_BUDGET_FIN, nInvCount);
pfrom->PushMessage(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_BUDGET_FIN, nInvCount);
LogPrint(BCLog::MNBUDGET, "CBudgetManager::Sync - sent %d items\n", nInvCount);
}

Expand All @@ -1406,7 +1406,7 @@ bool CBudgetManager::UpdateProposal(CBudgetVote& vote, CNode* pfrom, std::string
mapOrphanMasternodeBudgetVotes[vote.nProposalHash] = vote;

if (!askedForSourceProposalOrBudget.count(vote.nProposalHash)) {
pfrom->PushMessage("mnvs", vote.nProposalHash);
pfrom->PushMessage(NetMsgType::BUDGETVOTESYNC, vote.nProposalHash);
askedForSourceProposalOrBudget[vote.nProposalHash] = GetTime();
}
}
Expand All @@ -1433,7 +1433,7 @@ bool CBudgetManager::UpdateFinalizedBudget(CFinalizedBudgetVote& vote, CNode* pf
mapOrphanFinalizedBudgetVotes[vote.nBudgetHash] = vote;

if (!askedForSourceProposalOrBudget.count(vote.nBudgetHash)) {
pfrom->PushMessage("mnvs", vote.nBudgetHash);
pfrom->PushMessage(NetMsgType::BUDGETVOTESYNC, vote.nBudgetHash);
askedForSourceProposalOrBudget[vote.nBudgetHash] = GetTime();
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,24 +420,24 @@ void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st
if (fLiteMode) return; //disable all Masternode related functionality


if (strCommand == "mnget") { //Masternode Payments Request Sync
if (strCommand == NetMsgType::GETMNWINNERS) { //Masternode Payments Request Sync
if (fLiteMode) return; //disable all Masternode related functionality

int nCountNeeded;
vRecv >> nCountNeeded;

if (Params().NetworkID() == CBaseChainParams::MAIN) {
if (pfrom->HasFulfilledRequest("mnget")) {
if (pfrom->HasFulfilledRequest(NetMsgType::GETMNWINNERS)) {
LogPrintf("CMasternodePayments::ProcessMessageMasternodePayments() : mnget - peer already asked me for the list\n");
Misbehaving(pfrom->GetId(), 20);
return;
}
}

pfrom->FulfilledRequest("mnget");
pfrom->FulfilledRequest(NetMsgType::GETMNWINNERS);
masternodePayments.Sync(pfrom, nCountNeeded);
LogPrint(BCLog::MASTERNODE, "mnget - Sent Masternode winners to peer %i\n", pfrom->GetId());
} else if (strCommand == "mnw") { //Masternode Payments Declare Winner
} else if (strCommand == NetMsgType::MNWINNER) { //Masternode Payments Declare Winner
//this is required in litemodef
CMasternodePaymentWinner winner;
vRecv >> winner;
Expand Down Expand Up @@ -787,7 +787,7 @@ void CMasternodePayments::Sync(CNode* node, int nCountNeeded)
}
++it;
}
node->PushMessage("ssc", MASTERNODE_SYNC_MNW, nInvCount);
node->PushMessage(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_MNW, nInvCount);
}

std::string CMasternodePayments::ToString() const
Expand Down
22 changes: 11 additions & 11 deletions src/masternode-sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ std::string CMasternodeSync::GetSyncStatus()

void CMasternodeSync::ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv)
{
if (strCommand == "ssc") { //Sync status count
if (strCommand == NetMsgType::SYNCSTATUSCOUNT) { //Sync status count
int nItemID;
int nCount;
vRecv >> nItemID >> nCount;
Expand Down Expand Up @@ -260,7 +260,7 @@ void CMasternodeSync::Process()
if (tick++ % MASTERNODE_SYNC_TIMEOUT != 0) return;

if (IsSynced()) {
/*
/*
Resync if we lose all masternodes from sleep/wake or failure to sync originally
*/
if (mnodeman.CountEnabled() == 0) {
Expand Down Expand Up @@ -290,14 +290,14 @@ void CMasternodeSync::Process()
for (CNode* pnode : vNodes) {
if (isRegTestNet) {
if (RequestedMasternodeAttempt <= 2) {
pnode->PushMessage("getsporks"); //get current network sporks
pnode->PushMessage(NetMsgType::GETSPORKS); //get current network sporks
} else if (RequestedMasternodeAttempt < 4) {
mnodeman.DsegUpdate(pnode);
} else if (RequestedMasternodeAttempt < 6) {
int nMnCount = mnodeman.CountEnabled();
pnode->PushMessage("mnget", nMnCount); //sync payees
pnode->PushMessage(NetMsgType::GETMNWINNERS, nMnCount); //sync payees
uint256 n;
pnode->PushMessage("mnvs", n); //sync masternode votes
pnode->PushMessage(NetMsgType::BUDGETVOTESYNC, n); //sync masternode votes
} else {
RequestedMasternodeAssets = MASTERNODE_SYNC_FINISHED;
}
Expand All @@ -310,7 +310,7 @@ void CMasternodeSync::Process()
if (pnode->HasFulfilledRequest("getspork")) continue;
pnode->FulfilledRequest("getspork");

pnode->PushMessage("getsporks"); //get current network sporks
pnode->PushMessage(NetMsgType::GETSPORKS); //get current network sporks
if (RequestedMasternodeAttempt >= 2) GetNextAsset();
RequestedMasternodeAttempt++;

Expand Down Expand Up @@ -377,7 +377,7 @@ void CMasternodeSync::Process()
if (RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD * 3) return;

int nMnCount = mnodeman.CountEnabled();
pnode->PushMessage("mnget", nMnCount); //sync payees
pnode->PushMessage(NetMsgType::GETMNWINNERS, nMnCount); //sync payees
RequestedMasternodeAttempt++;

return;
Expand All @@ -386,10 +386,10 @@ void CMasternodeSync::Process()

if (pnode->nVersion >= ActiveProtocol()) {
if (RequestedMasternodeAssets == MASTERNODE_SYNC_BUDGET) {

// We'll start rejecting votes if we accidentally get set as synced too soon
if (lastBudgetItem > 0 && lastBudgetItem < GetTime() - MASTERNODE_SYNC_TIMEOUT * 2 && RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD) {
if (lastBudgetItem > 0 && lastBudgetItem < GetTime() - MASTERNODE_SYNC_TIMEOUT * 2 && RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD) {

// Hasn't received a new item in the last five seconds, so we'll move to the
GetNextAsset();

Expand All @@ -414,7 +414,7 @@ void CMasternodeSync::Process()
if (RequestedMasternodeAttempt >= MASTERNODE_SYNC_THRESHOLD * 3) return;

uint256 n;
pnode->PushMessage("mnvs", n); //sync masternode votes
pnode->PushMessage(NetMsgType::BUDGETVOTESYNC, n); //sync masternode votes
RequestedMasternodeAttempt++;

return;
Expand Down
6 changes: 3 additions & 3 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData

LOCK(cs_process_message);

if (strCommand == "mnb") { //Masternode Broadcast
if (strCommand == NetMsgType::MNBROADCAST) { //Masternode Broadcast
CMasternodeBroadcast mnb;
vRecv >> mnb;

Expand Down Expand Up @@ -759,7 +759,7 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
}
}

else if (strCommand == "mnp") { //Masternode Ping
else if (strCommand == NetMsgType::MNPING) { //Masternode Ping
CMasternodePing mnp;
vRecv >> mnp;

Expand Down Expand Up @@ -834,7 +834,7 @@ void CMasternodeMan::ProcessMessage(CNode* pfrom, std::string& strCommand, CData
}

if (vin == CTxIn()) {
pfrom->PushMessage("ssc", MASTERNODE_SYNC_LIST, nInvCount);
pfrom->PushMessage(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_LIST, nInvCount);
LogPrint(BCLog::MASTERNODE, "dseg - Sent %d Masternode entries to peer %i\n", nInvCount, pfrom->GetId());
}
}
Expand Down
Loading