/Users/eugenesiegel/btc/bitcoin/src/protocol.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-2022 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 | | |
10 | | CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn) |
11 | 7.32M | : pchMessageStart{pchMessageStartIn} |
12 | 7.32M | { |
13 | | // Copy the message type name |
14 | 7.32M | size_t i = 0; |
15 | 60.6M | for (; i < MESSAGE_TYPE_SIZE && msg_type[i] != 0; ++i53.2M ) m_msg_type[i] = msg_type[i]53.2M ; |
16 | 7.32M | assert(msg_type[i] == 0); // Assert that the message type name passed in is not longer than MESSAGE_TYPE_SIZE |
17 | | |
18 | 7.32M | nMessageSize = nMessageSizeIn; |
19 | 7.32M | } |
20 | | |
21 | | std::string CMessageHeader::GetMessageType() const |
22 | 6.47M | { |
23 | 6.47M | return std::string(m_msg_type, m_msg_type + strnlen(m_msg_type, MESSAGE_TYPE_SIZE)); |
24 | 6.47M | } |
25 | | |
26 | | bool CMessageHeader::IsMessageTypeValid() const |
27 | 6.47M | { |
28 | | // Check the message type string for errors |
29 | 59.2M | for (const char* p1 = m_msg_type; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p152.7M ) { |
30 | 52.7M | if (*p1 == 0) { |
31 | | // Must be all zeros after the first zero |
32 | 37.9M | for (; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p131.4M ) { |
33 | 31.4M | if (*p1 != 0) { |
34 | 0 | return false; |
35 | 0 | } |
36 | 31.4M | } |
37 | 46.3M | } else if (*p1 < ' ' || *p1 > 0x7E) { |
38 | 0 | return false; |
39 | 0 | } |
40 | 52.7M | } |
41 | | |
42 | 6.47M | return true; |
43 | 6.47M | } |
44 | | |
45 | | CInv::CInv() |
46 | 165k | { |
47 | 165k | type = 0; |
48 | 165k | hash.SetNull(); |
49 | 165k | } |
50 | | |
51 | 283k | CInv::CInv(uint32_t typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {} |
52 | | |
53 | | bool operator<(const CInv& a, const CInv& b) |
54 | 0 | { |
55 | 0 | return (a.type < b.type || (a.type == b.type && a.hash < b.hash)); |
56 | 0 | } |
57 | | |
58 | | std::string CInv::GetMessageType() const |
59 | 0 | { |
60 | 0 | std::string cmd; |
61 | 0 | if (type & MSG_WITNESS_FLAG) |
62 | 0 | cmd.append("witness-"); |
63 | 0 | int masked = type & MSG_TYPE_MASK; |
64 | 0 | switch (masked) |
65 | 0 | { |
66 | 0 | case MSG_TX: return cmd.append(NetMsgType::TX); |
67 | | // WTX is not a message type, just an inv type |
68 | 0 | case MSG_WTX: return cmd.append("wtx"); |
69 | 0 | case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK); |
70 | 0 | case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK); |
71 | 0 | case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK); |
72 | 0 | default: |
73 | 0 | throw std::out_of_range(strprintf("CInv::GetMessageType(): type=%d unknown type", type)); Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | | std::string CInv::ToString() const |
78 | 0 | { |
79 | 0 | try { |
80 | 0 | return strprintf("%s %s", GetMessageType(), hash.ToString()); Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
81 | 0 | } catch(const std::out_of_range &) { |
82 | 0 | return strprintf("0x%08x %s", type, hash.ToString()); Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | /** |
87 | | * Convert a service flag (NODE_*) to a human readable string. |
88 | | * It supports unknown service flags which will be returned as "UNKNOWN[...]". |
89 | | * @param[in] bit the service flag is calculated as (1 << bit) |
90 | | */ |
91 | | static std::string serviceFlagToStr(size_t bit) |
92 | 0 | { |
93 | 0 | const uint64_t service_flag = 1ULL << bit; |
94 | 0 | switch ((ServiceFlags)service_flag) { |
95 | 0 | case NODE_NONE: abort(); // impossible |
96 | 0 | case NODE_NETWORK: return "NETWORK"; |
97 | 0 | case NODE_BLOOM: return "BLOOM"; |
98 | 0 | case NODE_WITNESS: return "WITNESS"; |
99 | 0 | case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS"; |
100 | 0 | case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED"; |
101 | 0 | case NODE_P2P_V2: return "P2P_V2"; |
102 | | // Not using default, so we get warned when a case is missing |
103 | 0 | } |
104 | | |
105 | 0 | return strprintf("UNKNOWN[2^%u]", bit); Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
106 | 0 | } |
107 | | |
108 | | std::vector<std::string> serviceFlagsToStr(uint64_t flags) |
109 | 0 | { |
110 | 0 | std::vector<std::string> str_flags; |
111 | |
|
112 | 0 | for (size_t i = 0; i < sizeof(flags) * 8; ++i) { |
113 | 0 | if (flags & (1ULL << i)) { |
114 | 0 | str_flags.emplace_back(serviceFlagToStr(i)); |
115 | 0 | } |
116 | 0 | } |
117 | |
|
118 | 0 | return str_flags; |
119 | 0 | } |
120 | | |
121 | | GenTxid ToGenTxid(const CInv& inv) |
122 | 0 | { |
123 | 0 | assert(inv.IsGenTxMsg()); |
124 | 0 | return inv.IsMsgWtx() ? GenTxid{Wtxid::FromUint256(inv.hash)} : GenTxid{Txid::FromUint256(inv.hash)}; |
125 | 0 | } |