Coverage Report

Created: 2026-06-17 15:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/protocol.cpp
Line
Count
Source
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
10
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn)
11
162k
    : pchMessageStart{pchMessageStartIn}
12
162k
{
13
    // Copy the message type name
14
162k
    size_t i = 0;
15
1.12M
    for (; i < MESSAGE_TYPE_SIZE && msg_type[i] != 0; ++i) m_msg_type[i] = msg_type[i];
16
162k
    assert(msg_type[i] == 0); // Assert that the message type name passed in is not longer than MESSAGE_TYPE_SIZE
17
18
162k
    nMessageSize = nMessageSizeIn;
19
162k
}
20
21
std::string CMessageHeader::GetMessageType() const
22
154k
{
23
154k
    return std::string(m_msg_type, m_msg_type + strnlen(m_msg_type, MESSAGE_TYPE_SIZE));
24
154k
}
25
26
bool CMessageHeader::IsMessageTypeValid() const
27
154k
{
28
    // Check the message type string for errors
29
1.22M
    for (const char* p1 = m_msg_type; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
30
1.07M
        if (*p1 == 0) {
31
            // Must be all zeros after the first zero
32
1.08M
            for (; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
33
933k
                if (*p1 != 0) {
34
1
                    return false;
35
1
                }
36
933k
            }
37
919k
        } else if (*p1 < ' ' || *p1 > 0x7E) {
38
80
            return false;
39
80
        }
40
1.07M
    }
41
42
154k
    return true;
43
154k
}
44
45
CInv::CInv()
46
180k
{
47
180k
    type = 0;
48
180k
    hash.SetNull();
49
180k
}
50
51
92.5k
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
73.9k
{
60
73.9k
    std::string cmd;
61
73.9k
    if (type & MSG_WITNESS_FLAG)
62
27.4k
        cmd.append("witness-");
63
73.9k
    int masked = type & MSG_TYPE_MASK;
64
73.9k
    switch (masked)
65
73.9k
    {
66
47
    case MSG_TX:             return cmd.append(NetMsgType::TX);
67
    // WTX is not a message type, just an inv type
68
35.8k
    case MSG_WTX:            return cmd.append("wtx");
69
37.5k
    case MSG_BLOCK:          return cmd.append(NetMsgType::BLOCK);
70
7
    case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
71
467
    case MSG_CMPCT_BLOCK:    return cmd.append(NetMsgType::CMPCTBLOCK);
72
1
    default:
73
1
        throw std::out_of_range(strprintf("CInv::GetMessageType(): type=%d unknown type", type));
74
73.9k
    }
75
73.9k
}
76
77
std::string CInv::ToString() const
78
73.9k
{
79
73.9k
    try {
80
73.9k
        return strprintf("%s %s", GetMessageType(), hash.ToString());
81
73.9k
    } catch(const std::out_of_range &) {
82
1
        return strprintf("0x%08x %s", type, hash.ToString());
83
1
    }
84
73.9k
}
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
40.1k
{
93
40.1k
    const uint64_t service_flag = 1ULL << bit;
94
40.1k
    switch ((ServiceFlags)service_flag) {
95
0
    case NODE_NONE: abort();  // impossible
96
14.3k
    case NODE_NETWORK:         return "NETWORK";
97
10
    case NODE_BLOOM:           return "BLOOM";
98
14.6k
    case NODE_WITNESS:         return "WITNESS";
99
32
    case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS";
100
8.67k
    case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
101
2.36k
    case NODE_P2P_V2:          return "P2P_V2";
102
    // Not using default, so we get warned when a case is missing
103
40.1k
    }
104
105
8
    return strprintf("UNKNOWN[2^%u]", bit);
106
40.1k
}
107
108
std::vector<std::string> serviceFlagsToStr(uint64_t flags)
109
15.4k
{
110
15.4k
    std::vector<std::string> str_flags;
111
112
1.00M
    for (size_t i = 0; i < sizeof(flags) * 8; ++i) {
113
985k
        if (flags & (1ULL << i)) {
114
40.1k
            str_flags.emplace_back(serviceFlagToStr(i));
115
40.1k
        }
116
985k
    }
117
118
15.4k
    return str_flags;
119
15.4k
}
120
121
GenTxid ToGenTxid(const CInv& inv)
122
41.4k
{
123
41.4k
    assert(inv.IsGenTxMsg());
124
41.4k
    return inv.IsMsgWtx() ? GenTxid{Wtxid::FromUint256(inv.hash)} : GenTxid{Txid::FromUint256(inv.hash)};
125
41.4k
}