Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ BITCOIN_CORE_H = \
utilasmap.h \
utilmemory.h \
utilmoneystr.h \
utilstring.h \
utiltime.h \
validation.h \
validationinterface.h \
Expand Down Expand Up @@ -587,6 +588,7 @@ libdash_util_a_SOURCES = \
utilmoneystr.cpp \
utilstrencodings.cpp \
utiltime.cpp \
utilstring.cpp \
$(BITCOIN_CORE_H)

if GLIBC_BACK_COMPAT
Expand Down
8 changes: 8 additions & 0 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <hash.h>
#include <uint256.h>
#include <utilstrencodings.h>
#include <utilstring.h>

#include <assert.h>
#include <stdint.h>
Expand Down Expand Up @@ -127,6 +129,9 @@ std::string EncodeBase58(const std::vector<unsigned char>& vch)

bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
{
if (!ValidAsCString(str)) {
return false;
}
return DecodeBase58(str.c_str(), vchRet);
}

Expand Down Expand Up @@ -158,6 +163,9 @@ bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)

bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
{
if (!ValidAsCString(str)) {
return false;
}
return DecodeBase58Check(str.c_str(), vchRet);
}

7 changes: 7 additions & 0 deletions src/crypto/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ void static inline WriteLE64(unsigned char* ptr, uint64_t x)
memcpy(ptr, (char*)&v, 8);
}

uint16_t static inline ReadBE16(const unsigned char* ptr)
{
uint16_t x;
memcpy((char*)&x, ptr, 2);
return be16toh(x);
}

uint32_t static inline ReadBE32(const unsigned char* ptr)
{
uint32_t x;
Expand Down
13 changes: 12 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,12 @@ class CNode
bool fClient;
bool m_limited_node; //after BIP159
const bool fInbound;

/**
* Whether the peer has signaled support for receiving ADDRv2 (BIP155)
* messages, implying a preference to receive ADDRv2 instead of ADDR ones.
*/
std::atomic_bool m_wants_addrv2{false};
std::atomic_bool fSuccessfullyConnected;
std::atomic_bool fDisconnect;
std::atomic<int64_t> nDisconnectLingerTime{0};
Expand Down Expand Up @@ -1063,10 +1069,15 @@ class CNode

void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand)
{
// Whether the peer supports the address in `_addr`. For example,
// nodes that do not implement BIP155 cannot receive Tor v3 addresses
// because they require ADDRv2 (BIP155) encoding.
const bool addr_format_supported = m_wants_addrv2 || _addr.IsAddrV1Compatible();

// Known checking here is only to save space from duplicates.
// SendMessages will filter it again for knowns that were added
// after addresses were pushed.
if (_addr.IsValid() && !addrKnown.contains(_addr.GetKey())) {
if (_addr.IsValid() && !addrKnown.contains(_addr.GetKey()) && addr_format_supported) {
if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
vAddrToSend[insecure_rand.randrange(vAddrToSend.size())] = _addr;
} else {
Expand Down
43 changes: 37 additions & 6 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <random.h>
#include <reverse_iterator.h>
#include <scheduler.h>
#include <streams.h>
#include <tinyformat.h>
#include <txdb.h>
#include <txmempool.h>
Expand Down Expand Up @@ -2248,7 +2249,11 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
}
}

connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK));
const CNetMsgMaker msg_maker(INIT_PROTO_VERSION);
connman->PushMessage(pfrom, msg_maker.Make(NetMsgType::VERACK));

// Signal ADDRv2 support (BIP155).
connman->PushMessage(pfrom, msg_maker.Make(NetMsgType::SENDADDRV2));

pfrom->nServices = nServices;
pfrom->SetAddrLocal(addrMe);
Expand Down Expand Up @@ -2409,17 +2414,27 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
}
}

if (strCommand == NetMsgType::ADDR) {
if (strCommand == NetMsgType::ADDR || strCommand == NetMsgType::ADDRV2) {
int stream_version = vRecv.GetVersion();
if (strCommand == NetMsgType::ADDRV2) {
// Add ADDRV2_FORMAT to the version so that the CNetAddr and CAddress
// unserialize methods know that an address in v2 format is coming.
stream_version |= ADDRV2_FORMAT;
}

OverrideStream<CDataStream> s(&vRecv, vRecv.GetType(), stream_version);
std::vector<CAddress> vAddr;
vRecv >> vAddr;

s >> vAddr;

// Don't want addr from older versions unless seeding
if (pfrom->nVersion < CADDR_TIME_VERSION && connman->GetAddressCount() > 1000)
return true;

if (vAddr.size() > 1000)
{
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 20, strprintf("message addr size() = %u", vAddr.size()));
Misbehaving(pfrom->GetId(), 20, strprintf("%s message size = %u", strCommand, vAddr.size()));
return false;
}

Expand Down Expand Up @@ -2459,6 +2474,11 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
return true;
}

if (strCommand == NetMsgType::SENDADDRV2) {
pfrom->m_wants_addrv2 = true;
return true;
}

if (strCommand == NetMsgType::SENDHEADERS) {
LOCK(cs_main);
State(pfrom->GetId())->fPreferHeaders = true;
Expand Down Expand Up @@ -3963,6 +3983,17 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
std::vector<CAddress> vAddr;
vAddr.reserve(pto->vAddrToSend.size());

const char* strCommand;
int make_flags;
if (pto->m_wants_addrv2) {
strCommand = NetMsgType::ADDRV2;
make_flags = ADDRV2_FORMAT;
} else {
strCommand = NetMsgType::ADDR;
make_flags = 0;
}

for (const CAddress& addr : pto->vAddrToSend)
{
if (!pto->addrKnown.contains(addr.GetKey()))
Expand All @@ -3972,14 +4003,14 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
// receiver rejects addr messages larger than 1000
if (vAddr.size() >= 1000)
{
connman->PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
connman->PushMessage(pto, msgMaker.Make(make_flags, strCommand, vAddr));
vAddr.clear();
}
}
}
pto->vAddrToSend.clear();
if (!vAddr.empty())
connman->PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
connman->PushMessage(pto, msgMaker.Make(make_flags, strCommand, vAddr));
// we only send the big addr message once
if (pto->vAddrToSend.capacity() > 40)
pto->vAddrToSend.shrink_to_fit();
Expand Down
Loading