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 : 135984 : CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn)
12 : 135984 : : pchMessageStart{pchMessageStartIn}
13 : : {
14 : : // Copy the message type name
15 : 135984 : size_t i = 0;
16 [ + - + + ]: 1009138 : for (; i < MESSAGE_TYPE_SIZE && msg_type[i] != 0; ++i) m_msg_type[i] = msg_type[i];
17 [ - + ]: 135984 : assert(msg_type[i] == 0); // Assert that the message type name passed in is not longer than MESSAGE_TYPE_SIZE
18 : :
19 : 135984 : nMessageSize = nMessageSizeIn;
20 : 135984 : }
21 : :
22 : 131049 : std::string CMessageHeader::GetMessageType() const
23 : : {
24 : 131049 : return std::string(m_msg_type, m_msg_type + strnlen(m_msg_type, MESSAGE_TYPE_SIZE));
25 : : }
26 : :
27 : 130964 : bool CMessageHeader::IsMessageTypeValid() const
28 : : {
29 : : // Check the message type string for errors
30 [ + + ]: 1087173 : for (const char* p1 = m_msg_type; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
31 [ + + ]: 956290 : if (*p1 == 0) {
32 : : // Must be all zeros after the first zero
33 [ + + ]: 876616 : for (; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
34 [ + + ]: 745744 : if (*p1 != 0) {
35 : : return false;
36 : : }
37 : : }
38 [ + + ]: 825417 : } else if (*p1 < ' ' || *p1 > 0x7E) {
39 : : return false;
40 : : }
41 : : }
42 : :
43 : : return true;
44 : : }
45 : :
46 : 170151 : CInv::CInv()
47 : : {
48 : 170151 : type = 0;
49 : 170151 : hash.SetNull();
50 : 170151 : }
51 : :
52 : 100236 : 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 : 59235 : std::string CInv::GetMessageType() const
60 : : {
61 [ + + ]: 59235 : std::string cmd;
62 [ + + ]: 59235 : if (type & MSG_WITNESS_FLAG)
63 [ + - ]: 16607 : cmd.append("witness-");
64 : 59235 : int masked = type & MSG_TYPE_MASK;
65 [ + + + + : 59235 : switch (masked)
+ + ]
66 : : {
67 [ + - - + ]: 64 : case MSG_TX: return cmd.append(NetMsgType::TX);
68 : : // WTX is not a message type, just an inv type
69 [ + - - + ]: 33993 : case MSG_WTX: return cmd.append("wtx");
70 [ + - - + ]: 24781 : case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
71 [ + - - + ]: 8 : case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
72 [ + - - + ]: 59622 : case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
73 : 1 : default:
74 [ + - + - ]: 2 : throw std::out_of_range(strprintf("CInv::GetMessageType(): type=%d unknown type", type));
75 : : }
76 : 59234 : }
77 : :
78 : 59235 : std::string CInv::ToString() const
79 : : {
80 : 59235 : try {
81 [ + - + + : 118470 : return strprintf("%s %s", GetMessageType(), hash.ToString());
+ - ]
82 [ - + ]: 1 : } catch(const std::out_of_range &) {
83 [ + - + - ]: 1 : return strprintf("0x%08x %s", type, hash.ToString());
84 : 1 : }
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 : 52033 : static std::string serviceFlagToStr(size_t bit)
93 : : {
94 : 52033 : const uint64_t service_flag = 1ULL << bit;
95 [ - + + + : 52033 : switch ((ServiceFlags)service_flag) {
+ + + + ]
96 : 0 : case NODE_NONE: AssertUnreachable();
97 : 18084 : case NODE_NETWORK: return "NETWORK";
98 : 10 : case NODE_BLOOM: return "BLOOM";
99 : 18953 : case NODE_WITNESS: return "WITNESS";
100 : 96 : case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS";
101 : 12742 : case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
102 : 2138 : case NODE_P2P_V2: return "P2P_V2";
103 : : // Not using default, so we get warned when a case is missing
104 : : }
105 : :
106 : 10 : return strprintf("UNKNOWN[2^%u]", bit);
107 : : }
108 : :
109 : 19830 : std::vector<std::string> serviceFlagsToStr(uint64_t flags)
110 : : {
111 : 19830 : std::vector<std::string> str_flags;
112 : :
113 [ + + ]: 1288950 : for (size_t i = 0; i < sizeof(flags) * 8; ++i) {
114 [ + + ]: 1269120 : if (flags & (1ULL << i)) {
115 [ + - + - ]: 104066 : str_flags.emplace_back(serviceFlagToStr(i));
116 : : }
117 : : }
118 : :
119 : 19830 : return str_flags;
120 : 0 : }
121 : :
122 : 42408 : GenTxid ToGenTxid(const CInv& inv)
123 : : {
124 [ + + ]: 42408 : assert(inv.IsGenTxMsg());
125 [ + + ]: 42408 : return inv.IsMsgWtx() ? GenTxid{Wtxid::FromUint256(inv.hash)} : GenTxid{Txid::FromUint256(inv.hash)};
126 : : }
|