Skip to content

Commit

Permalink
net: Use C++11 member initialization in protocol
Browse files Browse the repository at this point in the history
Summary:
```
This change removes Init from the constructors and instead uses C++11
member initialization. This removes a bunch of boilerplate, makes the
code easier to read. Also, C++11 member initialization avoids accidental
uninitialized members.
```

Backport of core [[bitcoin/bitcoin#19020 | PR19020]].

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D9190
  • Loading branch information
MarcoFalke authored and Fabcien committed Feb 9, 2021
1 parent 3705100 commit fcf518b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/compat/assumptions.h
Expand Up @@ -52,6 +52,7 @@ static_assert(sizeof(double) == 8, "64-bit double assumed");
// code.
static_assert(sizeof(short) == 2, "16-bit short assumed");
static_assert(sizeof(int) == 4, "32-bit int assumed");
static_assert(sizeof(unsigned) == 4, "32-bit unsigned assumed");

// Assumption: We assume 8-bit bytes, because 32-bit int and 16-bit short are
// assumed.
Expand Down
14 changes: 0 additions & 14 deletions src/protocol.cpp
Expand Up @@ -213,20 +213,6 @@ void SetServiceFlagsIBDCache(bool state) {
g_initial_block_download_completed = state;
}

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

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

void CAddress::Init() {
nServices = NODE_NONE;
nTime = 100000000;
}

std::string CInv::GetCommand() const {
std::string cmd;
switch (GetKind()) {
Expand Down
13 changes: 8 additions & 5 deletions src/protocol.h
Expand Up @@ -420,14 +420,17 @@ static inline bool MayHaveUsefulAddressDB(ServiceFlags services) {
* A CService with information about it as peer.
*/
class CAddress : public CService {
static constexpr uint32_t TIME_INIT{100000000};

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

void Init();

SERIALIZE_METHODS(CAddress, obj) {
SER_READ(obj, obj.Init());
SER_READ(obj, obj.nTime = TIME_INIT);
int nVersion = s.GetVersion();
if (s.GetType() & SER_DISK) {
READWRITE(nVersion);
Expand All @@ -446,10 +449,10 @@ class CAddress : public CService {
READWRITEAS(CService, obj);
}

ServiceFlags nServices;
ServiceFlags nServices{NODE_NONE};

// disk and network only
unsigned int nTime;
uint32_t nTime{TIME_INIT};
};

/** getdata message type flags */
Expand Down

0 comments on commit fcf518b

Please sign in to comment.