Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #include <protocol.h>
7 : :
8 : : #include <common/system.h>
9 : : #include <util/check.h>
10 : :
11 : 39 : CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn)
12 : 39 : : pchMessageStart{pchMessageStartIn}
13 : : {
14 : : // Copy the message type name
15 : 39 : size_t i = 0;
16 [ + - + + ]: 277 : for (; i < MESSAGE_TYPE_SIZE && msg_type[i] != 0; ++i) m_msg_type[i] = msg_type[i];
17 [ - + ]: 39 : assert(msg_type[i] == 0); // Assert that the message type name passed in is not longer than MESSAGE_TYPE_SIZE
18 : :
19 : 39 : nMessageSize = nMessageSizeIn;
20 : 39 : }
21 : :
22 : 21 : std::string CMessageHeader::GetMessageType() const
23 : : {
24 : 21 : return std::string(m_msg_type, m_msg_type + strnlen(m_msg_type, MESSAGE_TYPE_SIZE));
25 : : }
26 : :
27 : 21 : bool CMessageHeader::IsMessageTypeValid() const
28 : : {
29 : : // Check the message type string for errors
30 [ + + ]: 136 : for (const char* p1 = m_msg_type; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
31 [ + + ]: 115 : if (*p1 == 0) {
32 : : // Must be all zeros after the first zero
33 [ + + ]: 179 : for (; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
34 [ + - ]: 158 : if (*p1 != 0) {
35 : : return false;
36 : : }
37 : : }
38 [ + - ]: 94 : } else if (*p1 < ' ' || *p1 > 0x7E) {
39 : : return false;
40 : : }
41 : : }
42 : :
43 : : return true;
44 : : }
45 : :
46 : 0 : CInv::CInv()
47 : : {
48 : 0 : type = 0;
49 : 0 : hash.SetNull();
50 : 0 : }
51 : :
52 : 0 : CInv::CInv(uint32_t typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}
53 : :
54 : 0 : bool operator<(const CInv& a, const CInv& b)
55 : : {
56 [ # # # # : 0 : return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
# # ]
57 : : }
58 : :
59 : 0 : std::string CInv::GetMessageType() const
60 : : {
61 [ # # ]: 0 : std::string cmd;
62 [ # # ]: 0 : if (type & MSG_WITNESS_FLAG)
63 [ # # ]: 0 : cmd.append("witness-");
64 : 0 : int masked = type & MSG_TYPE_MASK;
65 [ # # # # : 0 : switch (masked)
# # ]
66 : : {
67 [ # # # # ]: 0 : case MSG_TX: return cmd.append(NetMsgType::TX);
68 : : // WTX is not a message type, just an inv type
69 [ # # # # ]: 0 : case MSG_WTX: return cmd.append("wtx");
70 [ # # # # ]: 0 : case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
71 [ # # # # ]: 0 : case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
72 [ # # # # ]: 0 : case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
73 : 0 : default:
74 [ # # # # ]: 0 : throw std::out_of_range(strprintf("CInv::GetMessageType(): type=%d unknown type", type));
75 : : }
76 : 0 : }
77 : :
78 : 0 : std::string CInv::ToString() const
79 : : {
80 : 0 : try {
81 [ # # # # : 0 : return strprintf("%s %s", GetMessageType(), hash.ToString());
# # ]
82 [ - - ]: 0 : } catch(const std::out_of_range &) {
83 [ - - - - ]: 0 : return strprintf("0x%08x %s", type, hash.ToString());
84 : 0 : }
85 : : }
86 : :
87 : : /**
88 : : * Convert a service flag (NODE_*) to a human readable string.
89 : : * It supports unknown service flags which will be returned as "UNKNOWN[...]".
90 : : * @param[in] bit the service flag is calculated as (1 << bit)
91 : : */
92 : 0 : static std::string serviceFlagToStr(size_t bit)
93 : : {
94 : 0 : const uint64_t service_flag = 1ULL << bit;
95 [ # # # # : 0 : switch ((ServiceFlags)service_flag) {
# # # # ]
96 : 0 : case NODE_NONE: AssertUnreachable();
97 : 0 : case NODE_NETWORK: return "NETWORK";
98 : 0 : case NODE_BLOOM: return "BLOOM";
99 : 0 : case NODE_WITNESS: return "WITNESS";
100 : 0 : case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS";
101 : 0 : case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
102 : 0 : case NODE_P2P_V2: return "P2P_V2";
103 : : // Not using default, so we get warned when a case is missing
104 : : }
105 : :
106 : 0 : return strprintf("UNKNOWN[2^%u]", bit);
107 : : }
108 : :
109 : 3 : std::vector<std::string> serviceFlagsToStr(uint64_t flags)
110 : : {
111 : 3 : std::vector<std::string> str_flags;
112 : :
113 [ + + ]: 195 : for (size_t i = 0; i < sizeof(flags) * 8; ++i) {
114 [ - + ]: 192 : if (flags & (1ULL << i)) {
115 [ # # # # ]: 0 : str_flags.emplace_back(serviceFlagToStr(i));
116 : : }
117 : : }
118 : :
119 : 3 : return str_flags;
120 : 0 : }
121 : :
122 : 0 : GenTxid ToGenTxid(const CInv& inv)
123 : : {
124 [ # # ]: 0 : assert(inv.IsGenTxMsg());
125 [ # # ]: 0 : return inv.IsMsgWtx() ? GenTxid{Wtxid::FromUint256(inv.hash)} : GenTxid{Txid::FromUint256(inv.hash)};
126 : : }
|