Skip to content

Commit 0f6d20e

Browse files
committed
Merge bitcoin/bitcoin#31163: scripted-diff: get rid of remaining "command" terminology in protocol.{h,cpp}
4120c75 scripted-diff: get rid of remaining "command" terminology in protocol.{h,cpp} (Sebastian Falbesoner) Pull request description: The confusing "command" terminology for the 12-byte field in the (v1) p2p message header was replaced with the more proper term "message type" in other modules already years ago, see eg #18533, #18937, #24078, #24141. This PR does the same for the protocol.{h,cpp} module to complete the replacements. Note that "GetCommand" is a method name also used in the `ArgsManager` (there it makes much more sense), so the scripted-diff lists for this replacement the files explicitly, rather than using `$(git grep -l ...)`. ACKs for top commit: maflcko: review ACK 4120c75 🛒 fjahr: Code review ACK 4120c75 rkrux: tACK 4120c75 Tree-SHA512: 7b4dd30136392a145da95d2f3ba181c18c155ba6f3158e49e622d76811c6a45ef9b5c7539a979a04d8404faf18bb27f11457aa436d4e2998ece3deb2c9e59748
2 parents 5acd5e7 + 4120c75 commit 0f6d20e

File tree

11 files changed

+53
-53
lines changed

11 files changed

+53
-53
lines changed

src/net.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ int V1Transport::readHeader(Span<const uint8_t> msg_bytes)
739739

740740
// reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
741741
if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
742-
LogDebug(BCLog::NET, "Header error: Size too large (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetCommand()), hdr.nMessageSize, m_node_id);
742+
LogDebug(BCLog::NET, "Header error: Size too large (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetMessageType()), hdr.nMessageSize, m_node_id);
743743
return -1;
744744
}
745745

@@ -786,7 +786,7 @@ CNetMessage V1Transport::GetReceivedMessage(const std::chrono::microseconds time
786786
CNetMessage msg(std::move(vRecv));
787787

788788
// store message type string, time, and sizes
789-
msg.m_type = hdr.GetCommand();
789+
msg.m_type = hdr.GetMessageType();
790790
msg.m_time = time;
791791
msg.m_message_size = hdr.nMessageSize;
792792
msg.m_raw_message_size = hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
@@ -804,9 +804,9 @@ CNetMessage V1Transport::GetReceivedMessage(const std::chrono::microseconds time
804804
HexStr(hdr.pchChecksum),
805805
m_node_id);
806806
reject_message = true;
807-
} else if (!hdr.IsCommandValid()) {
807+
} else if (!hdr.IsMessageTypeValid()) {
808808
LogDebug(BCLog::NET, "Header error: Invalid message type (%s, %u bytes), peer=%d\n",
809-
SanitizeString(hdr.GetCommand()), msg.m_message_size, m_node_id);
809+
SanitizeString(hdr.GetMessageType()), msg.m_message_size, m_node_id);
810810
reject_message = true;
811811
}
812812

@@ -1188,7 +1188,7 @@ bool V2Transport::ProcessReceivedPacketBytes() noexcept
11881188
// - 12 bytes of message type
11891189
// - payload
11901190
static constexpr size_t MAX_CONTENTS_LEN =
1191-
1 + CMessageHeader::COMMAND_SIZE +
1191+
1 + CMessageHeader::MESSAGE_TYPE_SIZE +
11921192
std::min<size_t>(MAX_SIZE, MAX_PROTOCOL_MESSAGE_LENGTH);
11931193

11941194
if (m_recv_buffer.size() == BIP324Cipher::LENGTH_LEN) {
@@ -1403,26 +1403,26 @@ std::optional<std::string> V2Transport::GetMessageType(Span<const uint8_t>& cont
14031403
}
14041404
}
14051405

1406-
if (contents.size() < CMessageHeader::COMMAND_SIZE) {
1406+
if (contents.size() < CMessageHeader::MESSAGE_TYPE_SIZE) {
14071407
return std::nullopt; // Long encoding needs 12 message type bytes.
14081408
}
14091409

14101410
size_t msg_type_len{0};
1411-
while (msg_type_len < CMessageHeader::COMMAND_SIZE && contents[msg_type_len] != 0) {
1411+
while (msg_type_len < CMessageHeader::MESSAGE_TYPE_SIZE && contents[msg_type_len] != 0) {
14121412
// Verify that message type bytes before the first 0x00 are in range.
14131413
if (contents[msg_type_len] < ' ' || contents[msg_type_len] > 0x7F) {
14141414
return {};
14151415
}
14161416
++msg_type_len;
14171417
}
14181418
std::string ret{reinterpret_cast<const char*>(contents.data()), msg_type_len};
1419-
while (msg_type_len < CMessageHeader::COMMAND_SIZE) {
1419+
while (msg_type_len < CMessageHeader::MESSAGE_TYPE_SIZE) {
14201420
// Verify that message type bytes after the first 0x00 are also 0x00.
14211421
if (contents[msg_type_len] != 0) return {};
14221422
++msg_type_len;
14231423
}
14241424
// Strip message type bytes of contents.
1425-
contents = contents.subspan(CMessageHeader::COMMAND_SIZE);
1425+
contents = contents.subspan(CMessageHeader::MESSAGE_TYPE_SIZE);
14261426
return ret;
14271427
}
14281428

@@ -1474,9 +1474,9 @@ bool V2Transport::SetMessageToSend(CSerializedNetMsg& msg) noexcept
14741474
} else {
14751475
// Initialize with zeroes, and then write the message type string starting at offset 1.
14761476
// This means contents[0] and the unused positions in contents[1..13] remain 0x00.
1477-
contents.resize(1 + CMessageHeader::COMMAND_SIZE + msg.data.size(), 0);
1477+
contents.resize(1 + CMessageHeader::MESSAGE_TYPE_SIZE + msg.data.size(), 0);
14781478
std::copy(msg.m_type.begin(), msg.m_type.end(), contents.data() + 1);
1479-
std::copy(msg.data.begin(), msg.data.end(), contents.begin() + 1 + CMessageHeader::COMMAND_SIZE);
1479+
std::copy(msg.data.begin(), msg.data.end(), contents.begin() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE);
14801480
}
14811481
// Construct ciphertext in send buffer.
14821482
m_send_buffer.resize(contents.size() + BIP324Cipher::EXPANSION);
@@ -3940,7 +3940,7 @@ static void CaptureMessageToFile(const CAddress& addr,
39403940

39413941
ser_writedata64(f, now.count());
39423942
f << Span{msg_type};
3943-
for (auto i = msg_type.length(); i < CMessageHeader::COMMAND_SIZE; ++i) {
3943+
for (auto i = msg_type.length(); i < CMessageHeader::MESSAGE_TYPE_SIZE; ++i) {
39443944
f << uint8_t{'\0'};
39453945
}
39463946
uint32_t size = data.size();

src/protocol.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77

88
#include <common/system.h>
99

10-
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
10+
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn)
1111
: pchMessageStart{pchMessageStartIn}
1212
{
13-
// Copy the command name
13+
// Copy the message type name
1414
size_t i = 0;
15-
for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
16-
assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE
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
1717

1818
nMessageSize = nMessageSizeIn;
1919
}
2020

21-
std::string CMessageHeader::GetCommand() const
21+
std::string CMessageHeader::GetMessageType() const
2222
{
23-
return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
23+
return std::string(m_msg_type, m_msg_type + strnlen(m_msg_type, MESSAGE_TYPE_SIZE));
2424
}
2525

26-
bool CMessageHeader::IsCommandValid() const
26+
bool CMessageHeader::IsMessageTypeValid() const
2727
{
28-
// Check the command string for errors
29-
for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; ++p1) {
28+
// Check the message type string for errors
29+
for (const char* p1 = m_msg_type; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
3030
if (*p1 == 0) {
3131
// Must be all zeros after the first zero
32-
for (; p1 < pchCommand + COMMAND_SIZE; ++p1) {
32+
for (; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
3333
if (*p1 != 0) {
3434
return false;
3535
}
@@ -55,7 +55,7 @@ bool operator<(const CInv& a, const CInv& b)
5555
return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
5656
}
5757

58-
std::string CInv::GetCommand() const
58+
std::string CInv::GetMessageType() const
5959
{
6060
std::string cmd;
6161
if (type & MSG_WITNESS_FLAG)
@@ -70,14 +70,14 @@ std::string CInv::GetCommand() const
7070
case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
7171
case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
7272
default:
73-
throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
73+
throw std::out_of_range(strprintf("CInv::GetMessageType(): type=%d unknown type", type));
7474
}
7575
}
7676

7777
std::string CInv::ToString() const
7878
{
7979
try {
80-
return strprintf("%s %s", GetCommand(), hash.ToString());
80+
return strprintf("%s %s", GetMessageType(), hash.ToString());
8181
} catch(const std::out_of_range &) {
8282
return strprintf("0x%08x %s", type, hash.ToString());
8383
}

src/protocol.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,34 @@
2121

2222
/** Message header.
2323
* (4) message start.
24-
* (12) command.
24+
* (12) message type.
2525
* (4) size.
2626
* (4) checksum.
2727
*/
2828
class CMessageHeader
2929
{
3030
public:
31-
static constexpr size_t COMMAND_SIZE = 12;
31+
static constexpr size_t MESSAGE_TYPE_SIZE = 12;
3232
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
3333
static constexpr size_t CHECKSUM_SIZE = 4;
34-
static constexpr size_t MESSAGE_SIZE_OFFSET = std::tuple_size_v<MessageStartChars> + COMMAND_SIZE;
34+
static constexpr size_t MESSAGE_SIZE_OFFSET = std::tuple_size_v<MessageStartChars> + MESSAGE_TYPE_SIZE;
3535
static constexpr size_t CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
36-
static constexpr size_t HEADER_SIZE = std::tuple_size_v<MessageStartChars> + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
36+
static constexpr size_t HEADER_SIZE = std::tuple_size_v<MessageStartChars> + MESSAGE_TYPE_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
3737

3838
explicit CMessageHeader() = default;
3939

40-
/** Construct a P2P message header from message-start characters, a command and the size of the message.
41-
* @note Passing in a `pszCommand` longer than COMMAND_SIZE will result in a run-time assertion error.
40+
/** Construct a P2P message header from message-start characters, a message type and the size of the message.
41+
* @note Passing in a `msg_type` longer than MESSAGE_TYPE_SIZE will result in a run-time assertion error.
4242
*/
43-
CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
43+
CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn);
4444

45-
std::string GetCommand() const;
46-
bool IsCommandValid() const;
45+
std::string GetMessageType() const;
46+
bool IsMessageTypeValid() const;
4747

48-
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
48+
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.m_msg_type, obj.nMessageSize, obj.pchChecksum); }
4949

5050
MessageStartChars pchMessageStart{};
51-
char pchCommand[COMMAND_SIZE]{};
51+
char m_msg_type[MESSAGE_TYPE_SIZE]{};
5252
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
5353
uint8_t pchChecksum[CHECKSUM_SIZE]{};
5454
};
@@ -500,7 +500,7 @@ class CInv
500500

501501
friend bool operator<(const CInv& a, const CInv& b);
502502

503-
std::string GetCommand() const;
503+
std::string GetMessageType() const;
504504
std::string ToString() const;
505505

506506
// Single-message helper methods

src/rpc/net.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ static RPCHelpMan sendmsgtopeer()
10241024
"This RPC is for testing only.",
10251025
{
10261026
{"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer to send the message to."},
1027-
{"msg_type", RPCArg::Type::STR, RPCArg::Optional::NO, strprintf("The message type (maximum length %i)", CMessageHeader::COMMAND_SIZE)},
1027+
{"msg_type", RPCArg::Type::STR, RPCArg::Optional::NO, strprintf("The message type (maximum length %i)", CMessageHeader::MESSAGE_TYPE_SIZE)},
10281028
{"msg", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The serialized message body to send, in hex, without a message header"},
10291029
},
10301030
RPCResult{RPCResult::Type::OBJ, "", "", std::vector<RPCResult>{}},
@@ -1033,8 +1033,8 @@ static RPCHelpMan sendmsgtopeer()
10331033
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
10341034
const NodeId peer_id{request.params[0].getInt<int64_t>()};
10351035
const std::string& msg_type{request.params[1].get_str()};
1036-
if (msg_type.size() > CMessageHeader::COMMAND_SIZE) {
1037-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error: msg_type too long, max length is %i", CMessageHeader::COMMAND_SIZE));
1036+
if (msg_type.size() > CMessageHeader::MESSAGE_TYPE_SIZE) {
1037+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error: msg_type too long, max length is %i", CMessageHeader::MESSAGE_TYPE_SIZE));
10381038
}
10391039
auto msg{TryParseHex<unsigned char>(request.params[2].get_str())};
10401040
if (!msg.has_value()) {

src/test/fuzz/connman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ FUZZ_TARGET(connman, .init = initialize_connman)
130130
},
131131
[&] {
132132
CSerializedNetMsg serialized_net_msg;
133-
serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::COMMAND_SIZE);
133+
serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::MESSAGE_TYPE_SIZE);
134134
serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
135135
connman.PushMessage(&random_node, std::move(serialized_net_msg));
136136
},

src/test/fuzz/deserialize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ FUZZ_TARGET(service_deserialize, .init = initialize_deserialize)
263263
FUZZ_TARGET_DESERIALIZE(messageheader_deserialize, {
264264
CMessageHeader mh;
265265
DeserializeFromFuzzingInput(buffer, mh);
266-
(void)mh.IsCommandValid();
266+
(void)mh.IsMessageTypeValid();
267267
})
268268
FUZZ_TARGET(address_deserialize, .init = initialize_deserialize)
269269
{

src/test/fuzz/p2p_transport_serialization.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serial
8181
const std::chrono::microseconds m_time{std::numeric_limits<int64_t>::max()};
8282
bool reject_message{false};
8383
CNetMessage msg = recv_transport.GetReceivedMessage(m_time, reject_message);
84-
assert(msg.m_type.size() <= CMessageHeader::COMMAND_SIZE);
84+
assert(msg.m_type.size() <= CMessageHeader::MESSAGE_TYPE_SIZE);
8585
assert(msg.m_raw_message_size <= mutable_msg_bytes.size());
8686
assert(msg.m_raw_message_size == CMessageHeader::HEADER_SIZE + msg.m_message_size);
8787
assert(msg.m_time == m_time);
@@ -139,9 +139,9 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa
139139
// If v is 0xFF, construct a valid (but possibly unknown) message type from the fuzz
140140
// data.
141141
std::string ret;
142-
while (ret.size() < CMessageHeader::COMMAND_SIZE) {
142+
while (ret.size() < CMessageHeader::MESSAGE_TYPE_SIZE) {
143143
char c = provider.ConsumeIntegral<char>();
144-
// Match the allowed characters in CMessageHeader::IsCommandValid(). Any other
144+
// Match the allowed characters in CMessageHeader::IsMessageTypeValid(). Any other
145145
// character is interpreted as end.
146146
if (c < ' ' || c > 0x7E) break;
147147
ret += c;

src/test/fuzz/process_message.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ FUZZ_TARGET(process_message, .init = initialize_process_message)
6161

6262
LOCK(NetEventsInterface::g_msgproc_mutex);
6363

64-
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
64+
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
6565
if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
6666
return;
6767
}

src/test/fuzz/process_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages)
6464

6565
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
6666
{
67-
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
67+
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
6868

6969
const auto mock_time = ConsumeTime(fuzzed_data_provider);
7070
SetMockTime(mock_time);

src/test/fuzz/protocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ FUZZ_TARGET(protocol)
2020
return;
2121
}
2222
try {
23-
(void)inv->GetCommand();
23+
(void)inv->GetMessageType();
2424
} catch (const std::out_of_range&) {
2525
}
2626
(void)inv->ToString();

0 commit comments

Comments
 (0)