Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
protocol.cpp
Go to the documentation of this file.
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
:
pchMessageStart
{pchMessageStartIn}
12
{
13
// Copy the message type name
14
size_t
i = 0;
15
for
(; i <
MESSAGE_TYPE_SIZE
&& msg_type[i] != 0; ++i)
m_msg_type
[i] = msg_type[i];
16
assert
(msg_type[i] == 0);
// Assert that the message type name passed in is not longer than MESSAGE_TYPE_SIZE
17
18
nMessageSize
= nMessageSizeIn;
19
}
20
21
std::string
CMessageHeader::GetMessageType
()
const
22
{
23
return
std::string(
m_msg_type
,
m_msg_type
+ strnlen(
m_msg_type
,
MESSAGE_TYPE_SIZE
));
24
}
25
26
bool
CMessageHeader::IsMessageTypeValid
()
const
27
{
28
// Check the message type string for errors
29
for
(
const
char
* p1 =
m_msg_type
; p1 <
m_msg_type
+
MESSAGE_TYPE_SIZE
; ++p1) {
30
if
(*p1 == 0) {
31
// Must be all zeros after the first zero
32
for
(; p1 <
m_msg_type
+
MESSAGE_TYPE_SIZE
; ++p1) {
33
if
(*p1 != 0) {
34
return
false
;
35
}
36
}
37
}
else
if
(*p1 <
' '
|| *p1 > 0x7E) {
38
return
false
;
39
}
40
}
41
42
return
true
;
43
}
44
45
CInv::CInv
()
46
{
47
type
= 0;
48
hash
.SetNull();
49
}
50
51
CInv::CInv
(uint32_t typeIn,
const
uint256
& hashIn) :
type
(typeIn),
hash
(hashIn) {}
52
53
bool
operator<
(
const
CInv
& a,
const
CInv
& b)
54
{
55
return
(a.
type
< b.
type
|| (a.
type
== b.
type
&& a.
hash
< b.
hash
));
56
}
57
58
std::string
CInv::GetMessageType
()
const
59
{
60
std::string
cmd
;
61
if
(
type
&
MSG_WITNESS_FLAG
)
62
cmd
.append(
"witness-"
);
63
int
masked =
type
&
MSG_TYPE_MASK
;
64
switch
(masked)
65
{
66
case
MSG_TX
:
return
cmd
.append(
NetMsgType::TX
);
67
// WTX is not a message type, just an inv type
68
case
MSG_WTX
:
return
cmd
.append(
"wtx"
);
69
case
MSG_BLOCK
:
return
cmd
.append(
NetMsgType::BLOCK
);
70
case
MSG_FILTERED_BLOCK
:
return
cmd
.append(
NetMsgType::MERKLEBLOCK
);
71
case
MSG_CMPCT_BLOCK
:
return
cmd
.append(
NetMsgType::CMPCTBLOCK
);
72
default
:
73
throw
std::out_of_range(
strprintf
(
"CInv::GetMessageType(): type=%d unknown type"
,
type
));
74
}
75
}
76
77
std::string
CInv::ToString
()
const
78
{
79
try
{
80
return
strprintf
(
"%s %s"
,
GetMessageType
(),
hash
.ToString());
81
}
catch
(
const
std::out_of_range &) {
82
return
strprintf
(
"0x%08x %s"
,
type
,
hash
.ToString());
83
}
84
}
85
91
static
std::string
serviceFlagToStr
(
size_t
bit)
92
{
93
const
uint64_t service_flag = 1ULL << bit;
94
switch
((
ServiceFlags
)service_flag) {
95
case
NODE_NONE
: abort();
// impossible
96
case
NODE_NETWORK
:
return
"NETWORK"
;
97
case
NODE_BLOOM
:
return
"BLOOM"
;
98
case
NODE_WITNESS
:
return
"WITNESS"
;
99
case
NODE_COMPACT_FILTERS
:
return
"COMPACT_FILTERS"
;
100
case
NODE_NETWORK_LIMITED
:
return
"NETWORK_LIMITED"
;
101
case
NODE_P2P_V2
:
return
"P2P_V2"
;
102
// Not using default, so we get warned when a case is missing
103
}
104
105
return
strprintf
(
"UNKNOWN[2^%u]"
, bit);
106
}
107
108
std::vector<std::string>
serviceFlagsToStr
(uint64_t
flags
)
109
{
110
std::vector<std::string> str_flags;
111
112
for
(
size_t
i = 0; i <
sizeof
(
flags
) * 8; ++i) {
113
if
(
flags
& (1ULL << i)) {
114
str_flags.emplace_back(
serviceFlagToStr
(i));
115
}
116
}
117
118
return
str_flags;
119
}
120
121
GenTxid
ToGenTxid
(
const
CInv
& inv)
122
{
123
assert
(inv.
IsGenTxMsg
());
124
return
inv.
IsMsgWtx
() ?
GenTxid
{
Wtxid::FromUint256
(inv.
hash
)} :
GenTxid
{
Txid::FromUint256
(inv.
hash
)};
125
}
flags
int flags
Definition
bitcoin-tx.cpp:529
cmd
const auto cmd
Definition
bitcoin-util.cpp:170
CInv
inv message data
Definition
protocol.h:494
CInv::GetMessageType
std::string GetMessageType() const
Definition
protocol.cpp:58
CInv::ToString
std::string ToString() const
Definition
protocol.cpp:77
CInv::IsMsgWtx
bool IsMsgWtx() const
Definition
protocol.h:509
CInv::CInv
CInv()
Definition
protocol.cpp:45
CInv::type
uint32_t type
Definition
protocol.h:524
CInv::IsGenTxMsg
bool IsGenTxMsg() const
Definition
protocol.h:515
CInv::hash
uint256 hash
Definition
protocol.h:525
CMessageHeader::MESSAGE_TYPE_SIZE
static constexpr size_t MESSAGE_TYPE_SIZE
Definition
protocol.h:31
CMessageHeader::IsMessageTypeValid
bool IsMessageTypeValid() const
Definition
protocol.cpp:26
CMessageHeader::CMessageHeader
CMessageHeader()=default
CMessageHeader::pchMessageStart
MessageStartChars pchMessageStart
Definition
protocol.h:50
CMessageHeader::GetMessageType
std::string GetMessageType() const
Definition
protocol.cpp:21
CMessageHeader::nMessageSize
uint32_t nMessageSize
Definition
protocol.h:52
CMessageHeader::m_msg_type
char m_msg_type[MESSAGE_TYPE_SIZE]
Definition
protocol.h:51
GenTxid
Definition
transaction_identifier.h:79
transaction_identifier< true >::FromUint256
static transaction_identifier FromUint256(const uint256 &id)
Definition
transaction_identifier.h:49
uint256
256-bit opaque blob.
Definition
uint256.h:195
MessageStartChars
std::array< uint8_t, 4 > MessageStartChars
Definition
messagestartchars.h:11
NetMsgType::CMPCTBLOCK
constexpr const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids".
Definition
protocol.h:206
NetMsgType::TX
constexpr const char * TX
The tx message transmits a single transaction.
Definition
protocol.h:117
NetMsgType::MERKLEBLOCK
constexpr const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition
protocol.h:102
NetMsgType::BLOCK
constexpr const char * BLOCK
The block message transmits a single serialized block.
Definition
protocol.h:127
operator<
bool operator<(const CInv &a, const CInv &b)
Definition
protocol.cpp:53
ToGenTxid
GenTxid ToGenTxid(const CInv &inv)
Convert a TX/WITNESS_TX/WTX CInv to a GenTxid.
Definition
protocol.cpp:121
serviceFlagsToStr
std::vector< std::string > serviceFlagsToStr(uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition
protocol.cpp:108
serviceFlagToStr
static std::string serviceFlagToStr(size_t bit)
Convert a service flag (NODE_*) to a human readable string.
Definition
protocol.cpp:91
protocol.h
MSG_WITNESS_FLAG
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition
protocol.h:470
MSG_TYPE_MASK
const uint32_t MSG_TYPE_MASK
Definition
protocol.h:471
MSG_TX
@ MSG_TX
Definition
protocol.h:479
MSG_FILTERED_BLOCK
@ MSG_FILTERED_BLOCK
Defined in BIP37.
Definition
protocol.h:483
MSG_WTX
@ MSG_WTX
Defined in BIP 339.
Definition
protocol.h:481
MSG_BLOCK
@ MSG_BLOCK
Definition
protocol.h:480
MSG_CMPCT_BLOCK
@ MSG_CMPCT_BLOCK
Defined in BIP152.
Definition
protocol.h:484
ServiceFlags
ServiceFlags
nServices flags
Definition
protocol.h:309
NODE_NONE
@ NODE_NONE
Definition
protocol.h:312
NODE_P2P_V2
@ NODE_P2P_V2
Definition
protocol.h:330
NODE_WITNESS
@ NODE_WITNESS
Definition
protocol.h:320
NODE_NETWORK_LIMITED
@ NODE_NETWORK_LIMITED
Definition
protocol.h:327
NODE_BLOOM
@ NODE_BLOOM
Definition
protocol.h:317
NODE_NETWORK
@ NODE_NETWORK
Definition
protocol.h:315
NODE_COMPACT_FILTERS
@ NODE_COMPACT_FILTERS
Definition
protocol.h:323
system.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition
tinyformat.h:1172
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.17.0