2011-08-11 12:14:53 -04:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2022-12-24 20:49:50 -03:00
|
|
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 10:02:28 -04:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2011-08-11 12:14:53 -04:00
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <protocol.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2023-05-08 05:32:13 -04:00
|
|
|
#include <common/system.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2024-10-26 18:44:15 -03:00
|
|
|
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* msg_type, unsigned int nMessageSizeIn)
|
2023-11-11 09:04:18 -03:00
|
|
|
: pchMessageStart{pchMessageStartIn}
|
2011-08-11 12:14:53 -04:00
|
|
|
{
|
2024-10-26 18:44:15 -03:00
|
|
|
// Copy the message type name
|
2019-09-30 07:27:27 -03:00
|
|
|
size_t i = 0;
|
2024-10-26 18:44:15 -03:00
|
|
|
for (; i < MESSAGE_TYPE_SIZE && msg_type[i] != 0; ++i) m_msg_type[i] = msg_type[i];
|
|
|
|
assert(msg_type[i] == 0); // Assert that the message type name passed in is not longer than MESSAGE_TYPE_SIZE
|
2019-09-30 07:27:27 -03:00
|
|
|
|
2011-08-11 12:14:53 -04:00
|
|
|
nMessageSize = nMessageSizeIn;
|
|
|
|
}
|
|
|
|
|
2024-10-26 18:44:15 -03:00
|
|
|
std::string CMessageHeader::GetMessageType() const
|
2011-08-11 12:14:53 -04:00
|
|
|
{
|
2024-10-26 18:44:15 -03:00
|
|
|
return std::string(m_msg_type, m_msg_type + strnlen(m_msg_type, MESSAGE_TYPE_SIZE));
|
2011-08-11 12:14:53 -04:00
|
|
|
}
|
|
|
|
|
2024-10-26 18:44:15 -03:00
|
|
|
bool CMessageHeader::IsMessageTypeValid() const
|
2011-08-11 12:14:53 -04:00
|
|
|
{
|
2024-10-26 18:44:15 -03:00
|
|
|
// Check the message type string for errors
|
|
|
|
for (const char* p1 = m_msg_type; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
|
2020-05-26 17:01:57 -04:00
|
|
|
if (*p1 == 0) {
|
2011-08-11 12:14:53 -04:00
|
|
|
// Must be all zeros after the first zero
|
2024-10-26 18:44:15 -03:00
|
|
|
for (; p1 < m_msg_type + MESSAGE_TYPE_SIZE; ++p1) {
|
2020-05-26 17:01:57 -04:00
|
|
|
if (*p1 != 0) {
|
2011-08-11 12:14:53 -04:00
|
|
|
return false;
|
2020-05-26 17:01:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (*p1 < ' ' || *p1 > 0x7E) {
|
2011-08-11 12:14:53 -04:00
|
|
|
return false;
|
2020-05-26 17:01:57 -04:00
|
|
|
}
|
2011-08-11 12:14:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-11 12:40:12 -04:00
|
|
|
|
2011-08-11 12:49:03 -04:00
|
|
|
CInv::CInv()
|
|
|
|
{
|
|
|
|
type = 0;
|
2014-12-15 05:11:16 -03:00
|
|
|
hash.SetNull();
|
2011-08-11 12:49:03 -04:00
|
|
|
}
|
|
|
|
|
2020-07-28 04:47:07 -04:00
|
|
|
CInv::CInv(uint32_t typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}
|
2011-08-11 12:49:03 -04:00
|
|
|
|
|
|
|
bool operator<(const CInv& a, const CInv& b)
|
|
|
|
{
|
|
|
|
return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
|
|
|
|
}
|
|
|
|
|
2024-10-26 18:44:15 -03:00
|
|
|
std::string CInv::GetMessageType() const
|
2011-08-11 12:49:03 -04:00
|
|
|
{
|
2015-11-05 21:32:04 -03:00
|
|
|
std::string cmd;
|
|
|
|
if (type & MSG_WITNESS_FLAG)
|
|
|
|
cmd.append("witness-");
|
|
|
|
int masked = type & MSG_TYPE_MASK;
|
|
|
|
switch (masked)
|
|
|
|
{
|
|
|
|
case MSG_TX: return cmd.append(NetMsgType::TX);
|
2020-01-30 11:35:00 -03:00
|
|
|
// WTX is not a message type, just an inv type
|
|
|
|
case MSG_WTX: return cmd.append("wtx");
|
2015-11-05 21:32:04 -03:00
|
|
|
case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
|
|
|
|
case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
|
|
|
|
case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
|
|
|
|
default:
|
2024-10-26 18:44:15 -03:00
|
|
|
throw std::out_of_range(strprintf("CInv::GetMessageType(): type=%d unknown type", type));
|
2015-11-05 21:32:04 -03:00
|
|
|
}
|
2011-08-11 12:49:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CInv::ToString() const
|
|
|
|
{
|
2016-11-09 07:09:16 -03:00
|
|
|
try {
|
2024-10-26 18:44:15 -03:00
|
|
|
return strprintf("%s %s", GetMessageType(), hash.ToString());
|
2016-11-09 07:09:16 -03:00
|
|
|
} catch(const std::out_of_range &) {
|
|
|
|
return strprintf("0x%08x %s", type, hash.ToString());
|
|
|
|
}
|
2011-08-11 12:49:03 -04:00
|
|
|
}
|
2015-12-07 11:31:32 -03:00
|
|
|
|
2020-05-29 12:52:59 -04:00
|
|
|
/**
|
|
|
|
* Convert a service flag (NODE_*) to a human readable string.
|
|
|
|
* It supports unknown service flags which will be returned as "UNKNOWN[...]".
|
|
|
|
* @param[in] bit the service flag is calculated as (1 << bit)
|
|
|
|
*/
|
|
|
|
static std::string serviceFlagToStr(size_t bit)
|
2020-02-16 22:53:13 -03:00
|
|
|
{
|
2020-05-29 12:49:26 -04:00
|
|
|
const uint64_t service_flag = 1ULL << bit;
|
|
|
|
switch ((ServiceFlags)service_flag) {
|
2020-02-16 22:53:13 -03:00
|
|
|
case NODE_NONE: abort(); // impossible
|
|
|
|
case NODE_NETWORK: return "NETWORK";
|
|
|
|
case NODE_BLOOM: return "BLOOM";
|
|
|
|
case NODE_WITNESS: return "WITNESS";
|
2020-05-04 11:13:13 -04:00
|
|
|
case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS";
|
2020-02-16 22:53:13 -03:00
|
|
|
case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
|
2023-08-22 17:50:59 -04:00
|
|
|
case NODE_P2P_V2: return "P2P_V2";
|
2020-02-16 22:53:13 -03:00
|
|
|
// Not using default, so we get warned when a case is missing
|
|
|
|
}
|
|
|
|
|
2022-08-20 09:01:57 -04:00
|
|
|
return strprintf("UNKNOWN[2^%u]", bit);
|
2020-02-16 22:53:13 -03:00
|
|
|
}
|
2020-05-29 12:52:59 -04:00
|
|
|
|
|
|
|
std::vector<std::string> serviceFlagsToStr(uint64_t flags)
|
|
|
|
{
|
|
|
|
std::vector<std::string> str_flags;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(flags) * 8; ++i) {
|
|
|
|
if (flags & (1ULL << i)) {
|
|
|
|
str_flags.emplace_back(serviceFlagToStr(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str_flags;
|
|
|
|
}
|
2020-07-22 20:17:00 -04:00
|
|
|
|
|
|
|
GenTxid ToGenTxid(const CInv& inv)
|
|
|
|
{
|
|
|
|
assert(inv.IsGenTxMsg());
|
2021-10-22 07:28:14 -03:00
|
|
|
return inv.IsMsgWtx() ? GenTxid::Wtxid(inv.hash) : GenTxid::Txid(inv.hash);
|
2020-07-22 20:17:00 -04:00
|
|
|
}
|