Skip to content

Commit c398797

Browse files
committed
Refactor: Name GetDataMsg enum and replace ppszTypeName array for direct GetCommand call.
The array is duplicated with the NetMsgType constants
1 parent f99c5ed commit c398797

File tree

2 files changed

+49
-57
lines changed

2 files changed

+49
-57
lines changed

src/protocol.cpp

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,6 @@ const char* SYNCSTATUSCOUNT = "ssc";
5252
const char* GETMNLIST = "dseg";
5353
}; // namespace NetMsgType
5454

55-
static const char* ppszTypeName[] = {
56-
"ERROR", // Should never occur
57-
NetMsgType::TX,
58-
NetMsgType::BLOCK,
59-
"filtered block", // Should never occur
60-
"ix", // deprecated
61-
"txlvote", // deprecated
62-
NetMsgType::SPORK,
63-
NetMsgType::MNWINNER,
64-
"mnodescanerr",
65-
NetMsgType::BUDGETVOTE,
66-
NetMsgType::BUDGETPROPOSAL,
67-
NetMsgType::FINALBUDGET,
68-
NetMsgType::FINALBUDGETVOTE,
69-
"mnq",
70-
NetMsgType::MNBROADCAST,
71-
NetMsgType::MNPING,
72-
"dstx" // deprecated
73-
};
74-
7555
/** All known message types. Keep this in the same order as the list of
7656
* messages above and in protocol.h.
7757
*/
@@ -207,25 +187,36 @@ bool operator<(const CInv& a, const CInv& b)
207187
return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
208188
}
209189

210-
bool CInv::IsKnownType() const
211-
{
212-
return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
213-
}
214-
215190
bool CInv::IsMasterNodeType() const{
216191
return type > 2;
217192
}
218193

219-
const char* CInv::GetCommand() const
194+
std::string CInv::GetCommand() const
220195
{
221-
if (!IsKnownType()) {
222-
LogPrint(BCLog::NET, "CInv::GetCommand() : type=%d unknown type", type);
223-
return "UNKNOWN";
196+
std::string cmd;
197+
switch (type) {
198+
case MSG_TX: return cmd.append(NetMsgType::TX);
199+
case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
200+
case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
201+
case MSG_TXLOCK_REQUEST: return cmd.append("ix"); // Deprecated
202+
case MSG_TXLOCK_VOTE: return cmd.append("txlvote"); // Deprecated
203+
case MSG_SPORK: return cmd.append(NetMsgType::SPORK);
204+
case MSG_MASTERNODE_WINNER: return cmd.append(NetMsgType::MNWINNER);
205+
case MSG_MASTERNODE_SCANNING_ERROR: return cmd.append("mnodescanerr"); // Deprecated
206+
case MSG_BUDGET_VOTE: return cmd.append(NetMsgType::BUDGETVOTE);
207+
case MSG_BUDGET_PROPOSAL: return cmd.append(NetMsgType::BUDGETPROPOSAL);
208+
case MSG_BUDGET_FINALIZED: return cmd.append(NetMsgType::FINALBUDGET);
209+
case MSG_BUDGET_FINALIZED_VOTE: return cmd.append(NetMsgType::FINALBUDGETVOTE);
210+
case MSG_MASTERNODE_QUORUM: return cmd.append("mnq"); // Unused
211+
case MSG_MASTERNODE_ANNOUNCE: return cmd.append(NetMsgType::MNBROADCAST);
212+
case MSG_MASTERNODE_PING: return cmd.append(NetMsgType::MNPING);
213+
case MSG_DSTX: return cmd.append("dstx"); // Deprecated
214+
default:
215+
throw std::out_of_range(strprintf("%s: type=%d unknown type", __func__, type));
224216
}
225-
226-
return ppszTypeName[type];
227217
}
228218

219+
229220
std::string CInv::ToString() const
230221
{
231222
return strprintf("%s %s", GetCommand(), hash.ToString());

src/protocol.h

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -326,32 +326,11 @@ class CAddress : public CService
326326
unsigned int nTime;
327327
};
328328

329-
/** inv message data */
330-
class CInv
329+
/** getdata message types */
330+
enum GetDataMsg
331331
{
332-
public:
333-
CInv();
334-
CInv(int typeIn, const uint256& hashIn);
335-
336-
SERIALIZE_METHODS(CInv, obj) { READWRITE(obj.type, obj.hash); }
337-
338-
friend bool operator<(const CInv& a, const CInv& b);
339-
340-
bool IsKnownType() const;
341-
bool IsMasterNodeType() const;
342-
std::string ToString() const;
343-
344-
// TODO: make private (improves encapsulation)
345-
public:
346-
int type;
347-
uint256 hash;
348-
349-
private:
350-
const char* GetCommand() const;
351-
};
352-
353-
enum {
354-
MSG_TX = 1,
332+
UNDEFINED = 0,
333+
MSG_TX,
355334
MSG_BLOCK,
356335
// Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
357336
// MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
@@ -368,7 +347,29 @@ enum {
368347
MSG_MASTERNODE_QUORUM,
369348
MSG_MASTERNODE_ANNOUNCE,
370349
MSG_MASTERNODE_PING,
371-
MSG_DSTX
350+
MSG_DSTX,
351+
MSG_TYPE_MAX = MSG_DSTX
352+
};
353+
354+
/** inv message data */
355+
class CInv
356+
{
357+
public:
358+
CInv();
359+
CInv(int typeIn, const uint256& hashIn);
360+
361+
SERIALIZE_METHODS(CInv, obj) { READWRITE(obj.type, obj.hash); }
362+
363+
friend bool operator<(const CInv& a, const CInv& b);
364+
365+
bool IsMasterNodeType() const;
366+
std::string GetCommand() const;
367+
std::string ToString() const;
368+
369+
// TODO: make private (improve encapsulation)
370+
public:
371+
int type;
372+
uint256 hash;
372373
};
373374

374375
#endif // BITCOIN_PROTOCOL_H

0 commit comments

Comments
 (0)