mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 02:33:24 -03:00
Merge bitcoin/bitcoin#31163: scripted-diff: get rid of remaining "command" terminology in protocol.{h,cpp}
4120c7543e
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 ACK4120c7543e
🛒 fjahr: Code review ACK4120c7543e
rkrux: tACK4120c7543e
Tree-SHA512: 7b4dd30136392a145da95d2f3ba181c18c155ba6f3158e49e622d76811c6a45ef9b5c7539a979a04d8404faf18bb27f11457aa436d4e2998ece3deb2c9e59748
This commit is contained in:
commit
0f6d20e43f
11 changed files with 53 additions and 53 deletions
24
src/net.cpp
24
src/net.cpp
|
@ -739,7 +739,7 @@ int V1Transport::readHeader(Span<const uint8_t> msg_bytes)
|
||||||
|
|
||||||
// reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
|
// reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
|
||||||
if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
|
if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
|
||||||
LogDebug(BCLog::NET, "Header error: Size too large (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetCommand()), hdr.nMessageSize, m_node_id);
|
LogDebug(BCLog::NET, "Header error: Size too large (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetMessageType()), hdr.nMessageSize, m_node_id);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -786,7 +786,7 @@ CNetMessage V1Transport::GetReceivedMessage(const std::chrono::microseconds time
|
||||||
CNetMessage msg(std::move(vRecv));
|
CNetMessage msg(std::move(vRecv));
|
||||||
|
|
||||||
// store message type string, time, and sizes
|
// store message type string, time, and sizes
|
||||||
msg.m_type = hdr.GetCommand();
|
msg.m_type = hdr.GetMessageType();
|
||||||
msg.m_time = time;
|
msg.m_time = time;
|
||||||
msg.m_message_size = hdr.nMessageSize;
|
msg.m_message_size = hdr.nMessageSize;
|
||||||
msg.m_raw_message_size = hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
|
msg.m_raw_message_size = hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
|
||||||
|
@ -804,9 +804,9 @@ CNetMessage V1Transport::GetReceivedMessage(const std::chrono::microseconds time
|
||||||
HexStr(hdr.pchChecksum),
|
HexStr(hdr.pchChecksum),
|
||||||
m_node_id);
|
m_node_id);
|
||||||
reject_message = true;
|
reject_message = true;
|
||||||
} else if (!hdr.IsCommandValid()) {
|
} else if (!hdr.IsMessageTypeValid()) {
|
||||||
LogDebug(BCLog::NET, "Header error: Invalid message type (%s, %u bytes), peer=%d\n",
|
LogDebug(BCLog::NET, "Header error: Invalid message type (%s, %u bytes), peer=%d\n",
|
||||||
SanitizeString(hdr.GetCommand()), msg.m_message_size, m_node_id);
|
SanitizeString(hdr.GetMessageType()), msg.m_message_size, m_node_id);
|
||||||
reject_message = true;
|
reject_message = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1188,7 +1188,7 @@ bool V2Transport::ProcessReceivedPacketBytes() noexcept
|
||||||
// - 12 bytes of message type
|
// - 12 bytes of message type
|
||||||
// - payload
|
// - payload
|
||||||
static constexpr size_t MAX_CONTENTS_LEN =
|
static constexpr size_t MAX_CONTENTS_LEN =
|
||||||
1 + CMessageHeader::COMMAND_SIZE +
|
1 + CMessageHeader::MESSAGE_TYPE_SIZE +
|
||||||
std::min<size_t>(MAX_SIZE, MAX_PROTOCOL_MESSAGE_LENGTH);
|
std::min<size_t>(MAX_SIZE, MAX_PROTOCOL_MESSAGE_LENGTH);
|
||||||
|
|
||||||
if (m_recv_buffer.size() == BIP324Cipher::LENGTH_LEN) {
|
if (m_recv_buffer.size() == BIP324Cipher::LENGTH_LEN) {
|
||||||
|
@ -1403,12 +1403,12 @@ std::optional<std::string> V2Transport::GetMessageType(Span<const uint8_t>& cont
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contents.size() < CMessageHeader::COMMAND_SIZE) {
|
if (contents.size() < CMessageHeader::MESSAGE_TYPE_SIZE) {
|
||||||
return std::nullopt; // Long encoding needs 12 message type bytes.
|
return std::nullopt; // Long encoding needs 12 message type bytes.
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t msg_type_len{0};
|
size_t msg_type_len{0};
|
||||||
while (msg_type_len < CMessageHeader::COMMAND_SIZE && contents[msg_type_len] != 0) {
|
while (msg_type_len < CMessageHeader::MESSAGE_TYPE_SIZE && contents[msg_type_len] != 0) {
|
||||||
// Verify that message type bytes before the first 0x00 are in range.
|
// Verify that message type bytes before the first 0x00 are in range.
|
||||||
if (contents[msg_type_len] < ' ' || contents[msg_type_len] > 0x7F) {
|
if (contents[msg_type_len] < ' ' || contents[msg_type_len] > 0x7F) {
|
||||||
return {};
|
return {};
|
||||||
|
@ -1416,13 +1416,13 @@ std::optional<std::string> V2Transport::GetMessageType(Span<const uint8_t>& cont
|
||||||
++msg_type_len;
|
++msg_type_len;
|
||||||
}
|
}
|
||||||
std::string ret{reinterpret_cast<const char*>(contents.data()), msg_type_len};
|
std::string ret{reinterpret_cast<const char*>(contents.data()), msg_type_len};
|
||||||
while (msg_type_len < CMessageHeader::COMMAND_SIZE) {
|
while (msg_type_len < CMessageHeader::MESSAGE_TYPE_SIZE) {
|
||||||
// Verify that message type bytes after the first 0x00 are also 0x00.
|
// Verify that message type bytes after the first 0x00 are also 0x00.
|
||||||
if (contents[msg_type_len] != 0) return {};
|
if (contents[msg_type_len] != 0) return {};
|
||||||
++msg_type_len;
|
++msg_type_len;
|
||||||
}
|
}
|
||||||
// Strip message type bytes of contents.
|
// Strip message type bytes of contents.
|
||||||
contents = contents.subspan(CMessageHeader::COMMAND_SIZE);
|
contents = contents.subspan(CMessageHeader::MESSAGE_TYPE_SIZE);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1474,9 +1474,9 @@ bool V2Transport::SetMessageToSend(CSerializedNetMsg& msg) noexcept
|
||||||
} else {
|
} else {
|
||||||
// Initialize with zeroes, and then write the message type string starting at offset 1.
|
// Initialize with zeroes, and then write the message type string starting at offset 1.
|
||||||
// This means contents[0] and the unused positions in contents[1..13] remain 0x00.
|
// This means contents[0] and the unused positions in contents[1..13] remain 0x00.
|
||||||
contents.resize(1 + CMessageHeader::COMMAND_SIZE + msg.data.size(), 0);
|
contents.resize(1 + CMessageHeader::MESSAGE_TYPE_SIZE + msg.data.size(), 0);
|
||||||
std::copy(msg.m_type.begin(), msg.m_type.end(), contents.data() + 1);
|
std::copy(msg.m_type.begin(), msg.m_type.end(), contents.data() + 1);
|
||||||
std::copy(msg.data.begin(), msg.data.end(), contents.begin() + 1 + CMessageHeader::COMMAND_SIZE);
|
std::copy(msg.data.begin(), msg.data.end(), contents.begin() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE);
|
||||||
}
|
}
|
||||||
// Construct ciphertext in send buffer.
|
// Construct ciphertext in send buffer.
|
||||||
m_send_buffer.resize(contents.size() + BIP324Cipher::EXPANSION);
|
m_send_buffer.resize(contents.size() + BIP324Cipher::EXPANSION);
|
||||||
|
@ -3940,7 +3940,7 @@ static void CaptureMessageToFile(const CAddress& addr,
|
||||||
|
|
||||||
ser_writedata64(f, now.count());
|
ser_writedata64(f, now.count());
|
||||||
f << Span{msg_type};
|
f << Span{msg_type};
|
||||||
for (auto i = msg_type.length(); i < CMessageHeader::COMMAND_SIZE; ++i) {
|
for (auto i = msg_type.length(); i < CMessageHeader::MESSAGE_TYPE_SIZE; ++i) {
|
||||||
f << uint8_t{'\0'};
|
f << uint8_t{'\0'};
|
||||||
}
|
}
|
||||||
uint32_t size = data.size();
|
uint32_t size = data.size();
|
||||||
|
|
|
@ -7,29 +7,29 @@
|
||||||
|
|
||||||
#include <common/system.h>
|
#include <common/system.h>
|
||||||
|
|
||||||
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
|
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn)
|
||||||
: pchMessageStart{pchMessageStartIn}
|
: pchMessageStart{pchMessageStartIn}
|
||||||
{
|
{
|
||||||
// Copy the command name
|
// Copy the message type name
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
|
for (; i < MESSAGE_TYPE_SIZE && msg_type[i] != 0; ++i) m_msg_type[i] = msg_type[i];
|
||||||
assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE
|
assert(msg_type[i] == 0); // Assert that the message type name passed in is not longer than MESSAGE_TYPE_SIZE
|
||||||
|
|
||||||
nMessageSize = nMessageSizeIn;
|
nMessageSize = nMessageSizeIn;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string CMessageHeader::GetCommand() const
|
std::string CMessageHeader::GetMessageType() const
|
||||||
{
|
{
|
||||||
return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
|
return std::string(m_msg_type, m_msg_type + strnlen(m_msg_type, MESSAGE_TYPE_SIZE));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CMessageHeader::IsCommandValid() const
|
bool CMessageHeader::IsMessageTypeValid() const
|
||||||
{
|
{
|
||||||
// Check the command string for errors
|
// Check the message type string for errors
|
||||||
for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; ++p1) {
|
for (const char* p1 = m_msg_type; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
|
||||||
if (*p1 == 0) {
|
if (*p1 == 0) {
|
||||||
// Must be all zeros after the first zero
|
// Must be all zeros after the first zero
|
||||||
for (; p1 < pchCommand + COMMAND_SIZE; ++p1) {
|
for (; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
|
||||||
if (*p1 != 0) {
|
if (*p1 != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ bool operator<(const CInv& a, const CInv& b)
|
||||||
return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
|
return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string CInv::GetCommand() const
|
std::string CInv::GetMessageType() const
|
||||||
{
|
{
|
||||||
std::string cmd;
|
std::string cmd;
|
||||||
if (type & MSG_WITNESS_FLAG)
|
if (type & MSG_WITNESS_FLAG)
|
||||||
|
@ -70,14 +70,14 @@ std::string CInv::GetCommand() const
|
||||||
case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
|
case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
|
||||||
case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
|
case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
|
||||||
default:
|
default:
|
||||||
throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
|
throw std::out_of_range(strprintf("CInv::GetMessageType(): type=%d unknown type", type));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string CInv::ToString() const
|
std::string CInv::ToString() const
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return strprintf("%s %s", GetCommand(), hash.ToString());
|
return strprintf("%s %s", GetMessageType(), hash.ToString());
|
||||||
} catch(const std::out_of_range &) {
|
} catch(const std::out_of_range &) {
|
||||||
return strprintf("0x%08x %s", type, hash.ToString());
|
return strprintf("0x%08x %s", type, hash.ToString());
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,34 +21,34 @@
|
||||||
|
|
||||||
/** Message header.
|
/** Message header.
|
||||||
* (4) message start.
|
* (4) message start.
|
||||||
* (12) command.
|
* (12) message type.
|
||||||
* (4) size.
|
* (4) size.
|
||||||
* (4) checksum.
|
* (4) checksum.
|
||||||
*/
|
*/
|
||||||
class CMessageHeader
|
class CMessageHeader
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr size_t COMMAND_SIZE = 12;
|
static constexpr size_t MESSAGE_TYPE_SIZE = 12;
|
||||||
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
|
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
|
||||||
static constexpr size_t CHECKSUM_SIZE = 4;
|
static constexpr size_t CHECKSUM_SIZE = 4;
|
||||||
static constexpr size_t MESSAGE_SIZE_OFFSET = std::tuple_size_v<MessageStartChars> + COMMAND_SIZE;
|
static constexpr size_t MESSAGE_SIZE_OFFSET = std::tuple_size_v<MessageStartChars> + MESSAGE_TYPE_SIZE;
|
||||||
static constexpr size_t CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
|
static constexpr size_t CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
|
||||||
static constexpr size_t HEADER_SIZE = std::tuple_size_v<MessageStartChars> + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
|
static constexpr size_t HEADER_SIZE = std::tuple_size_v<MessageStartChars> + MESSAGE_TYPE_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
|
||||||
|
|
||||||
explicit CMessageHeader() = default;
|
explicit CMessageHeader() = default;
|
||||||
|
|
||||||
/** Construct a P2P message header from message-start characters, a command and the size of the message.
|
/** Construct a P2P message header from message-start characters, a message type and the size of the message.
|
||||||
* @note Passing in a `pszCommand` longer than COMMAND_SIZE will result in a run-time assertion error.
|
* @note Passing in a `msg_type` longer than MESSAGE_TYPE_SIZE will result in a run-time assertion error.
|
||||||
*/
|
*/
|
||||||
CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
|
CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn);
|
||||||
|
|
||||||
std::string GetCommand() const;
|
std::string GetMessageType() const;
|
||||||
bool IsCommandValid() const;
|
bool IsMessageTypeValid() const;
|
||||||
|
|
||||||
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
|
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.m_msg_type, obj.nMessageSize, obj.pchChecksum); }
|
||||||
|
|
||||||
MessageStartChars pchMessageStart{};
|
MessageStartChars pchMessageStart{};
|
||||||
char pchCommand[COMMAND_SIZE]{};
|
char m_msg_type[MESSAGE_TYPE_SIZE]{};
|
||||||
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
|
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
|
||||||
uint8_t pchChecksum[CHECKSUM_SIZE]{};
|
uint8_t pchChecksum[CHECKSUM_SIZE]{};
|
||||||
};
|
};
|
||||||
|
@ -500,7 +500,7 @@ public:
|
||||||
|
|
||||||
friend bool operator<(const CInv& a, const CInv& b);
|
friend bool operator<(const CInv& a, const CInv& b);
|
||||||
|
|
||||||
std::string GetCommand() const;
|
std::string GetMessageType() const;
|
||||||
std::string ToString() const;
|
std::string ToString() const;
|
||||||
|
|
||||||
// Single-message helper methods
|
// Single-message helper methods
|
||||||
|
|
|
@ -1024,7 +1024,7 @@ static RPCHelpMan sendmsgtopeer()
|
||||||
"This RPC is for testing only.",
|
"This RPC is for testing only.",
|
||||||
{
|
{
|
||||||
{"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer to send the message to."},
|
{"peer_id", RPCArg::Type::NUM, RPCArg::Optional::NO, "The peer to send the message to."},
|
||||||
{"msg_type", RPCArg::Type::STR, RPCArg::Optional::NO, strprintf("The message type (maximum length %i)", CMessageHeader::COMMAND_SIZE)},
|
{"msg_type", RPCArg::Type::STR, RPCArg::Optional::NO, strprintf("The message type (maximum length %i)", CMessageHeader::MESSAGE_TYPE_SIZE)},
|
||||||
{"msg", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The serialized message body to send, in hex, without a message header"},
|
{"msg", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The serialized message body to send, in hex, without a message header"},
|
||||||
},
|
},
|
||||||
RPCResult{RPCResult::Type::OBJ, "", "", std::vector<RPCResult>{}},
|
RPCResult{RPCResult::Type::OBJ, "", "", std::vector<RPCResult>{}},
|
||||||
|
@ -1033,8 +1033,8 @@ static RPCHelpMan sendmsgtopeer()
|
||||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
|
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
|
||||||
const NodeId peer_id{request.params[0].getInt<int64_t>()};
|
const NodeId peer_id{request.params[0].getInt<int64_t>()};
|
||||||
const std::string& msg_type{request.params[1].get_str()};
|
const std::string& msg_type{request.params[1].get_str()};
|
||||||
if (msg_type.size() > CMessageHeader::COMMAND_SIZE) {
|
if (msg_type.size() > CMessageHeader::MESSAGE_TYPE_SIZE) {
|
||||||
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error: msg_type too long, max length is %i", CMessageHeader::COMMAND_SIZE));
|
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error: msg_type too long, max length is %i", CMessageHeader::MESSAGE_TYPE_SIZE));
|
||||||
}
|
}
|
||||||
auto msg{TryParseHex<unsigned char>(request.params[2].get_str())};
|
auto msg{TryParseHex<unsigned char>(request.params[2].get_str())};
|
||||||
if (!msg.has_value()) {
|
if (!msg.has_value()) {
|
||||||
|
|
|
@ -130,7 +130,7 @@ FUZZ_TARGET(connman, .init = initialize_connman)
|
||||||
},
|
},
|
||||||
[&] {
|
[&] {
|
||||||
CSerializedNetMsg serialized_net_msg;
|
CSerializedNetMsg serialized_net_msg;
|
||||||
serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::COMMAND_SIZE);
|
serialized_net_msg.m_type = fuzzed_data_provider.ConsumeRandomLengthString(CMessageHeader::MESSAGE_TYPE_SIZE);
|
||||||
serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
|
serialized_net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
|
||||||
connman.PushMessage(&random_node, std::move(serialized_net_msg));
|
connman.PushMessage(&random_node, std::move(serialized_net_msg));
|
||||||
},
|
},
|
||||||
|
|
|
@ -263,7 +263,7 @@ FUZZ_TARGET(service_deserialize, .init = initialize_deserialize)
|
||||||
FUZZ_TARGET_DESERIALIZE(messageheader_deserialize, {
|
FUZZ_TARGET_DESERIALIZE(messageheader_deserialize, {
|
||||||
CMessageHeader mh;
|
CMessageHeader mh;
|
||||||
DeserializeFromFuzzingInput(buffer, mh);
|
DeserializeFromFuzzingInput(buffer, mh);
|
||||||
(void)mh.IsCommandValid();
|
(void)mh.IsMessageTypeValid();
|
||||||
})
|
})
|
||||||
FUZZ_TARGET(address_deserialize, .init = initialize_deserialize)
|
FUZZ_TARGET(address_deserialize, .init = initialize_deserialize)
|
||||||
{
|
{
|
||||||
|
|
|
@ -81,7 +81,7 @@ FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serial
|
||||||
const std::chrono::microseconds m_time{std::numeric_limits<int64_t>::max()};
|
const std::chrono::microseconds m_time{std::numeric_limits<int64_t>::max()};
|
||||||
bool reject_message{false};
|
bool reject_message{false};
|
||||||
CNetMessage msg = recv_transport.GetReceivedMessage(m_time, reject_message);
|
CNetMessage msg = recv_transport.GetReceivedMessage(m_time, reject_message);
|
||||||
assert(msg.m_type.size() <= CMessageHeader::COMMAND_SIZE);
|
assert(msg.m_type.size() <= CMessageHeader::MESSAGE_TYPE_SIZE);
|
||||||
assert(msg.m_raw_message_size <= mutable_msg_bytes.size());
|
assert(msg.m_raw_message_size <= mutable_msg_bytes.size());
|
||||||
assert(msg.m_raw_message_size == CMessageHeader::HEADER_SIZE + msg.m_message_size);
|
assert(msg.m_raw_message_size == CMessageHeader::HEADER_SIZE + msg.m_message_size);
|
||||||
assert(msg.m_time == m_time);
|
assert(msg.m_time == m_time);
|
||||||
|
@ -139,9 +139,9 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa
|
||||||
// If v is 0xFF, construct a valid (but possibly unknown) message type from the fuzz
|
// If v is 0xFF, construct a valid (but possibly unknown) message type from the fuzz
|
||||||
// data.
|
// data.
|
||||||
std::string ret;
|
std::string ret;
|
||||||
while (ret.size() < CMessageHeader::COMMAND_SIZE) {
|
while (ret.size() < CMessageHeader::MESSAGE_TYPE_SIZE) {
|
||||||
char c = provider.ConsumeIntegral<char>();
|
char c = provider.ConsumeIntegral<char>();
|
||||||
// Match the allowed characters in CMessageHeader::IsCommandValid(). Any other
|
// Match the allowed characters in CMessageHeader::IsMessageTypeValid(). Any other
|
||||||
// character is interpreted as end.
|
// character is interpreted as end.
|
||||||
if (c < ' ' || c > 0x7E) break;
|
if (c < ' ' || c > 0x7E) break;
|
||||||
ret += c;
|
ret += c;
|
||||||
|
|
|
@ -61,7 +61,7 @@ FUZZ_TARGET(process_message, .init = initialize_process_message)
|
||||||
|
|
||||||
LOCK(NetEventsInterface::g_msgproc_mutex);
|
LOCK(NetEventsInterface::g_msgproc_mutex);
|
||||||
|
|
||||||
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
|
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
|
||||||
if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
|
if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,7 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages)
|
||||||
|
|
||||||
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
|
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
|
||||||
{
|
{
|
||||||
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
|
const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
|
||||||
|
|
||||||
const auto mock_time = ConsumeTime(fuzzed_data_provider);
|
const auto mock_time = ConsumeTime(fuzzed_data_provider);
|
||||||
SetMockTime(mock_time);
|
SetMockTime(mock_time);
|
||||||
|
|
|
@ -20,7 +20,7 @@ FUZZ_TARGET(protocol)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
(void)inv->GetCommand();
|
(void)inv->GetMessageType();
|
||||||
} catch (const std::out_of_range&) {
|
} catch (const std::out_of_range&) {
|
||||||
}
|
}
|
||||||
(void)inv->ToString();
|
(void)inv->ToString();
|
||||||
|
|
|
@ -1281,7 +1281,7 @@ public:
|
||||||
void ReceiveMessage(const std::string& m_type, Span<const uint8_t> payload)
|
void ReceiveMessage(const std::string& m_type, Span<const uint8_t> payload)
|
||||||
{
|
{
|
||||||
auto ret = ReceivePacket();
|
auto ret = ReceivePacket();
|
||||||
BOOST_REQUIRE(ret.size() == payload.size() + 1 + CMessageHeader::COMMAND_SIZE);
|
BOOST_REQUIRE(ret.size() == payload.size() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE);
|
||||||
BOOST_CHECK(ret[0] == 0);
|
BOOST_CHECK(ret[0] == 0);
|
||||||
for (unsigned i = 0; i < 12; ++i) {
|
for (unsigned i = 0; i < 12; ++i) {
|
||||||
if (i < m_type.size()) {
|
if (i < m_type.size()) {
|
||||||
|
@ -1290,7 +1290,7 @@ public:
|
||||||
BOOST_CHECK(ret[1 + i] == 0);
|
BOOST_CHECK(ret[1 + i] == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BOOST_CHECK(std::ranges::equal(Span{ret}.subspan(1 + CMessageHeader::COMMAND_SIZE), payload));
|
BOOST_CHECK(std::ranges::equal(Span{ret}.subspan(1 + CMessageHeader::MESSAGE_TYPE_SIZE), payload));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Schedule an encrypted packet with specified message type and payload to be sent to
|
/** Schedule an encrypted packet with specified message type and payload to be sent to
|
||||||
|
@ -1298,9 +1298,9 @@ public:
|
||||||
void SendMessage(std::string mtype, Span<const uint8_t> payload)
|
void SendMessage(std::string mtype, Span<const uint8_t> payload)
|
||||||
{
|
{
|
||||||
// Construct contents consisting of 0x00 + 12-byte message type + payload.
|
// Construct contents consisting of 0x00 + 12-byte message type + payload.
|
||||||
std::vector<uint8_t> contents(1 + CMessageHeader::COMMAND_SIZE + payload.size());
|
std::vector<uint8_t> contents(1 + CMessageHeader::MESSAGE_TYPE_SIZE + payload.size());
|
||||||
std::copy(mtype.begin(), mtype.end(), reinterpret_cast<char*>(contents.data() + 1));
|
std::copy(mtype.begin(), mtype.end(), reinterpret_cast<char*>(contents.data() + 1));
|
||||||
std::copy(payload.begin(), payload.end(), contents.begin() + 1 + CMessageHeader::COMMAND_SIZE);
|
std::copy(payload.begin(), payload.end(), contents.begin() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE);
|
||||||
// Send a packet with that as contents.
|
// Send a packet with that as contents.
|
||||||
SendPacket(contents);
|
SendPacket(contents);
|
||||||
}
|
}
|
||||||
|
@ -1459,7 +1459,7 @@ BOOST_AUTO_TEST_CASE(v2transport_test)
|
||||||
auto msg_data_2 = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
|
auto msg_data_2 = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
|
||||||
tester.SendMessage(uint8_t(13), msg_data_2); // headers short id
|
tester.SendMessage(uint8_t(13), msg_data_2); // headers short id
|
||||||
// Send invalidly-encoded message
|
// Send invalidly-encoded message
|
||||||
tester.SendMessage(std::string("blocktxn\x00\x00\x00a", CMessageHeader::COMMAND_SIZE), {});
|
tester.SendMessage(std::string("blocktxn\x00\x00\x00a", CMessageHeader::MESSAGE_TYPE_SIZE), {});
|
||||||
tester.SendMessage("foobar", {}); // test receiving unknown message type
|
tester.SendMessage("foobar", {}); // test receiving unknown message type
|
||||||
tester.AddMessage("barfoo", {}); // test sending unknown message type
|
tester.AddMessage("barfoo", {}); // test sending unknown message type
|
||||||
ret = tester.Interact();
|
ret = tester.Interact();
|
||||||
|
|
Loading…
Add table
Reference in a new issue