Skip to content

Commit

Permalink
refactor: Use uint16_t instead of unsigned short
Browse files Browse the repository at this point in the history
Plus included cnode_listen_port unit test.

Adaptation of btc@1cabbddbca615b26aa4510c75f459c28d6fe0afd with few more changes.
  • Loading branch information
furszy committed Jul 28, 2021
1 parent 1e4fa87 commit a7b9fd9
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 32 deletions.
4 changes: 3 additions & 1 deletion src/addrdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "tinyformat.h"
#include "util/system.h"

#include <cstdint>

namespace {

template <typename Stream, typename Data>
Expand All @@ -37,7 +39,7 @@ template <typename Data>
bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data)
{
// Generate random temporary filename
unsigned short randv = 0;
uint16_t randv = 0;
GetRandBytes((unsigned char*)&randv, sizeof(randv));
std::string tmpfn = strprintf("%s.%04x", prefix, randv);

Expand Down
6 changes: 4 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <ifaddrs.h>
#endif

#include <cstdint>

#include <math.h>

// Dump addresses to peers.dat and banlist.dat every 15 minutes (900s)
Expand Down Expand Up @@ -78,9 +80,9 @@ void CConnman::AddOneShot(const std::string& strDest)
vOneShots.push_back(strDest);
}

unsigned short GetListenPort()
uint16_t GetListenPort()
{
return (unsigned short)(gArgs.GetArg("-port", Params().GetDefaultPort()));
return (uint16_t)(gArgs.GetArg("-port", Params().GetDefaultPort()));
}

// find 'best' local address for a particular peer
Expand Down
4 changes: 2 additions & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "threadinterrupt.h"

#include <atomic>
#include <cstdint>
#include <deque>
#include <stdint.h>
#include <thread>
#include <memory>
#include <condition_variable>
Expand Down Expand Up @@ -383,7 +383,7 @@ class CConnman
};
extern std::unique_ptr<CConnman> g_connman;
void Discover();
unsigned short GetListenPort();
uint16_t GetListenPort();
bool BindListenPort(const CService& bindAddr, std::string& strError, bool fWhitelisted = false);
void CheckOffsetDisconnectedPeers(const CNetAddr& ip);

Expand Down
15 changes: 6 additions & 9 deletions src/netaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "utilstrencodings.h"
#include "tinyformat.h"

#include <cstdint>

static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};

Expand Down Expand Up @@ -479,15 +481,15 @@ CService::CService()
Init();
}

CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
{
}

CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn)
CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
{
}

CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn)
CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
{
}

Expand Down Expand Up @@ -515,7 +517,7 @@ bool CService::SetSockAddr(const struct sockaddr *paddr)
}
}

unsigned short CService::GetPort() const
uint16_t CService::GetPort() const
{
return port;
}
Expand Down Expand Up @@ -594,11 +596,6 @@ std::string CService::ToString() const
return ToStringIPPort();
}

void CService::SetPort(unsigned short portIn)
{
port = portIn;
}

CSubNet::CSubNet():
valid(false)
{
Expand Down
11 changes: 5 additions & 6 deletions src/netaddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "serialize.h"
#include "span.h"

#include <stdint.h>
#include <cstdint>
#include <string>
#include <vector>

Expand Down Expand Up @@ -133,12 +133,11 @@ class CService : public CNetAddr

public:
CService();
CService(const CNetAddr& ip, unsigned short port);
CService(const struct in_addr& ipv4Addr, unsigned short port);
CService(const CNetAddr& ip, uint16_t port);
CService(const struct in_addr& ipv4Addr, uint16_t port);
CService(const struct sockaddr_in& addr);
void Init();
void SetPort(unsigned short portIn);
unsigned short GetPort() const;
uint16_t GetPort() const;
bool GetSockAddr(struct sockaddr* paddr, socklen_t* addrlen) const;
bool SetSockAddr(const struct sockaddr* paddr);
friend bool operator==(const CService& a, const CService& b);
Expand All @@ -149,7 +148,7 @@ class CService : public CNetAddr
std::string ToStringPort() const;
std::string ToStringIPPort() const;

CService(const struct in6_addr& ipv6Addr, unsigned short port);
CService(const struct in6_addr& ipv6Addr, uint16_t port);
CService(const struct sockaddr_in6& addr);

SERIALIZE_METHODS(CService, obj) { READWRITE(obj.ip, Using<BigEndianFormatter<2>>(obj.port)); }
Expand Down
7 changes: 5 additions & 2 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "utilstrencodings.h"

#include <atomic>
#include <cstdint>

#ifndef WIN32
#include <fcntl.h>
Expand Down Expand Up @@ -613,11 +614,13 @@ static bool ConnectThroughProxy(const proxyType &proxy, const std::string strDes
ProxyCredentials random_auth;
static std::atomic_int counter;
random_auth.username = random_auth.password = strprintf("%i", counter++);
if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket))
if (!Socks5(strDest, (uint16_t)port, &random_auth, hSocket)) {
return false;
}
} else {
if (!Socks5(strDest, (unsigned short)port, 0, hSocket))
if (!Socks5(strDest, (uint16_t)port, 0, hSocket)) {
return false;
}
}

hSocketRet = hSocket;
Expand Down
1 change: 1 addition & 0 deletions src/qt/optionsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define BITCOIN_QT_OPTIONSMODEL_H

#include "amount.h"
#include <cstdint>

#include <QAbstractListModel>
#include <QSettings>
Expand Down
16 changes: 6 additions & 10 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
#include <array>
#include <assert.h>
#include <cstring>
#include <cstdint>
#include <ios>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <stdint.h>
#include <string.h>
#include <string>
#include <utility>
Expand Down Expand Up @@ -308,14 +308,10 @@ inline void Unserialize(Stream& s, SporkId& sporkID)
*/
inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
{
if (nSize < 253)
return sizeof(unsigned char);
else if (nSize <= std::numeric_limits<unsigned short>::max())
return sizeof(unsigned char) + sizeof(unsigned short);
else if (nSize <= std::numeric_limits<unsigned int>::max())
return sizeof(unsigned char) + sizeof(unsigned int);
else
return sizeof(unsigned char) + sizeof(uint64_t);
if (nSize < 253) return sizeof(unsigned char);
else if (nSize <= std::numeric_limits<uint16_t>::max()) return sizeof(unsigned char) + sizeof(uint16_t);
else if (nSize <= std::numeric_limits<unsigned int>::max()) return sizeof(unsigned char) + sizeof(unsigned int);
else return sizeof(unsigned char) + sizeof(uint64_t);
}

inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize);
Expand All @@ -325,7 +321,7 @@ void WriteCompactSize(Stream& os, uint64_t nSize)
{
if (nSize < 253) {
ser_writedata8(os, nSize);
} else if (nSize <= std::numeric_limits<unsigned short>::max()) {
} else if (nSize <= std::numeric_limits<uint16_t>::max()) {
ser_writedata8(os, 253);
ser_writedata16(os, nSize);
} else if (nSize <= std::numeric_limits<unsigned int>::max()) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/net_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "serialize.h"
#include "streams.h"

#include <cstdint>

#include "test/test_pivx.h"

#include <string>
Expand Down Expand Up @@ -76,6 +78,18 @@ CDataStream AddrmanToStream(CAddrManSerializationMock& addrman)

BOOST_FIXTURE_TEST_SUITE(net_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(cnode_listen_port)
{
// test default
uint16_t port = GetListenPort();
BOOST_CHECK(port == Params().GetDefaultPort());
// test set port
uint16_t altPort = 12345;
BOOST_CHECK(gArgs.SoftSetArg("-port", std::to_string(altPort)));
port = GetListenPort();
BOOST_CHECK(port == altPort);
}

BOOST_AUTO_TEST_CASE(caddrdb_read)
{
CAddrManUncorrupted addrmanUncorrupted;
Expand Down

0 comments on commit a7b9fd9

Please sign in to comment.