Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made protocol.h more similar to bitcoin #1688

Merged
merged 5 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ bool CAddrMan::Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimeP
pinfo->nTime = max((int64_t)0, addr.nTime - nTimePenalty);

// add services
pinfo->nServices |= addr.nServices;
pinfo->nServices = ServiceFlags(pinfo->nServices | addr.nServices);

// do not update if no new information is present
if (!addr.nTime || (pinfo->nTime && addr.nTime <= pinfo->nTime))
Expand Down
10 changes: 6 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5397,12 +5397,14 @@ bool ProcessMessages(CNode* pfrom)
// Checksum
CDataStream& vRecv = msg.vRecv;
uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
unsigned int nChecksum = 0;
memcpy(&nChecksum, &hash, sizeof(nChecksum));
if (nChecksum != hdr.nChecksum)
// TODO: hardcoded checksum size;
// will no longer be used once we adopt CNetMessage from Bitcoin
uint8_t nChecksum[4];
memcpy(&nChecksum, &hash, 4);
jamescowens marked this conversation as resolved.
Show resolved Hide resolved
if (!std::equal(std::begin(nChecksum), std::end(nChecksum), std::begin(hdr.pchChecksum)))
{
LogPrintf("ProcessMessages(%s, %u bytes) : CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x",
strCommand, nMessageSize, nChecksum, hdr.nChecksum);
strCommand, nMessageSize, nChecksum, hdr.pchChecksum);
continue;
}

Expand Down
8 changes: 5 additions & 3 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct LocalServiceInfo {
//
bool fDiscover = true;
bool fUseUPnP = false;
uint64_t nLocalServices = NODE_NETWORK;
ServiceFlags nLocalServices = NODE_NETWORK;
static CCriticalSection cs_mapLocalHost;
static map<CNetAddr, LocalServiceInfo> mapLocalHost;
static bool vfReachable[NET_MAX] = {};
Expand Down Expand Up @@ -159,7 +159,8 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
// get best local address for a particular peer as a CAddress
CAddress GetLocalAddress(const CNetAddr *paddrPeer)
{
CAddress ret(CService("0.0.0.0",0),0);
ServiceFlags nLocalServices = NODE_NETWORK;
CAddress ret(CService("0.0.0.0",0), nLocalServices);
CService addr;
if (GetLocal(addr, paddrPeer))
{
Expand Down Expand Up @@ -727,8 +728,9 @@ void CNode::PushVersion()
std::string mycpid;
std::string acid;

//TODO: change `PushMessage()` to use ServiceFlags so we don't need to cast nLocalServices
PushMessage("aries", PROTOCOL_VERSION, nonce, pw1,
mycpid, mycpid, acid, nLocalServices, nTime, addrYou, addrMe,
mycpid, mycpid, acid, (uint64_t) nLocalServices, nTime, addrYou, addrMe,
nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>()),
nBestHeight);

Expand Down
2 changes: 1 addition & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class CRequestTracker
extern bool fDiscover;
void Discover(boost::thread_group& threadGroup);
extern bool fUseUPnP;
extern uint64_t nLocalServices;
extern ServiceFlags nLocalServices;
extern uint64_t nLocalHostNonce;
extern CAddress addrSeenByPeer;
extern CAddrMan addrman;
Expand Down
39 changes: 10 additions & 29 deletions src/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,22 @@ CMessageHeader::CMessageHeader()
{
memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
memset(pchCommand, 0, sizeof(pchCommand));
pchCommand[1] = 1;
nMessageSize = -1;
nChecksum = 0;
memset(pchChecksum, 0, CHECKSUM_SIZE);
}

CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
{
memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
strncpy(pchCommand, pszCommand, COMMAND_SIZE);

// Copy the command name, zero-padding to COMMAND_SIZE bytes
size_t i = 0;
for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE
for (; i < COMMAND_SIZE; ++i) pchCommand[i] = 0;

nMessageSize = nMessageSizeIn;
nChecksum = 0;
memset(pchChecksum, 0, CHECKSUM_SIZE);
}

std::string CMessageHeader::GetCommand() const
Expand Down Expand Up @@ -75,37 +80,13 @@ bool CMessageHeader::IsValid() const
return true;
}



CAddress::CAddress() : CService()
{
Init();
}

CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
{
Init();
nServices = nServicesIn;
}

void CAddress::Init()
{
nServices = NODE_NETWORK;
nTime = 100000000;
nLastTry = 0;
}

CInv::CInv()
{
type = 0;
hash.SetNull();
}

CInv::CInv(int typeIn, const uint256& hashIn)
{
type = typeIn;
hash = hashIn;
}
CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}

CInv::CInv(const std::string& strType, const uint256& hashIn)
{
Expand Down
93 changes: 29 additions & 64 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,92 +35,65 @@ extern unsigned char pchMessageStart[4];
class CMessageHeader
{
public:
static constexpr size_t MESSAGE_START_SIZE = 4;
static constexpr size_t COMMAND_SIZE = 12;
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
static constexpr size_t CHECKSUM_SIZE = 4;
static constexpr size_t MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE;
static constexpr size_t CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;

CMessageHeader();
CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);

std::string GetCommand() const;
bool IsValid() const;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(pchMessageStart);
READWRITE(pchCommand);
READWRITE(nMessageSize);
READWRITE(nChecksum);
}
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }

// TODO: make private (improves encapsulation)
//HALFORD: 12-26-2014 - Add Encryption to messages - Increase size by 32 for checksum + delimiters + 10 for timestamp = 50 = 62 (vs 12)
public:
enum {
MESSAGE_START_SIZE=sizeof(::pchMessageStart),
COMMAND_SIZE=12,
MESSAGE_SIZE_SIZE=sizeof(int),
CHECKSUM_SIZE=sizeof(int),

MESSAGE_SIZE_OFFSET=MESSAGE_START_SIZE+COMMAND_SIZE,
CHECKSUM_OFFSET=MESSAGE_SIZE_OFFSET+MESSAGE_SIZE_SIZE,
HEADER_SIZE=MESSAGE_START_SIZE+COMMAND_SIZE+MESSAGE_SIZE_SIZE+CHECKSUM_SIZE
};
char pchMessageStart[MESSAGE_START_SIZE];
char pchCommand[COMMAND_SIZE];
unsigned int nMessageSize;
unsigned int nChecksum;
uint32_t nMessageSize;
uint8_t pchChecksum[CHECKSUM_SIZE];
};

/** nServices flags */
enum
{
enum ServiceFlags : uint64_t {
// NODE_NETWORK means that the node is capable of serving the complete block chain.
NODE_NETWORK = (1 << 0),
};

/** A CService with information about it as peer */
class CAddress : public CService
{
public:
CAddress();
explicit CAddress(CService ipIn, uint64_t nServicesIn=NODE_NETWORK);

void Init();
static constexpr uint32_t TIME_INIT{100000000};

ADD_SERIALIZE_METHODS;
public:
CAddress() : CService{} {};
explicit CAddress(CService ipIn, ServiceFlags nServicesIn=NODE_NETWORK): CService{ipIn}, nServices{nServicesIn} {};

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
SERIALIZE_METHODS(CAddress, obj)
{
if (ser_action.ForRead()) {
Init();
}

SER_READ(obj, obj.nTime = TIME_INIT);
int nVersion = s.GetVersion();
if (s.GetType() & SER_DISK) {
READWRITE(nVersion);
}

if ((s.GetType() & SER_DISK)
|| (nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH)))
{
READWRITE(nTime);
if ((s.GetType() & SER_DISK) ||
(nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH))) {
READWRITE(obj.nTime);
}

READWRITE(nServices);
READWRITEAS(CService, *this);
READWRITE(Using<CustomUintFormatter<8>>(obj.nServices));
READWRITEAS(CService, obj);
}

void print() const;

// TODO: make private (improves encapsulation)
public:
uint64_t nServices;

ServiceFlags nServices{NODE_NETWORK};
// disk and network only
unsigned int nTime;

uint32_t nTime{TIME_INIT};
// memory only
int64_t nLastTry;
int64_t nLastTry = 0;
};

/** inv message data */
Expand All @@ -131,14 +104,7 @@ class CInv
CInv(int typeIn, const uint256& hashIn);
CInv(const std::string& strType, const uint256& hashIn);

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(type);
READWRITE(hash);
}
SERIALIZE_METHODS(CInv, obj) { READWRITE(obj.type, obj.hash); }

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

Expand All @@ -147,10 +113,9 @@ class CInv
std::string ToString() const;
void print() const;

// TODO: make private (improves encapsulation)
public:
int type;
uint256 hash;
};


#endif // __INCLUDED_PROTOCOL_H__
Loading