Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/dashpay/dash into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
PastaPastaPasta committed Jan 30, 2025
2 parents cf11108 + 78f242b commit e589460
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 85 deletions.
44 changes: 26 additions & 18 deletions src/bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class CBLSWrapper
*(static_cast<C*>(this)) = C();
}

void SetByteVector(Span<const uint8_t> vecBytes, const bool specificLegacyScheme)
void SetBytes(Span<const uint8_t> vecBytes, const bool specificLegacyScheme)
{
if (vecBytes.size() != SerSize) {
Reset();
Expand Down Expand Up @@ -127,6 +127,14 @@ class CBLSWrapper
return impl.Serialize(specificLegacyScheme);
}

std::array<uint8_t, SerSize> ToBytes(const bool specificLegacyScheme) const
{
if (!fValid) {
return std::array<uint8_t, SerSize>{};
}
return impl.SerializeToArray(specificLegacyScheme);
}

const uint256& GetHash() const
{
if (cachedHash.IsNull()) {
Expand All @@ -146,7 +154,7 @@ class CBLSWrapper
Reset();
return false;
}
SetByteVector(b, specificLegacyScheme);
SetBytes(b, specificLegacyScheme);
return IsValid();
}

Expand All @@ -158,7 +166,8 @@ class CBLSWrapper
template <typename Stream>
inline void Serialize(Stream& s, const bool specificLegacyScheme) const
{
s.write(AsBytes(Span{ToByteVector(specificLegacyScheme).data(), SerSize}));
const auto bytes{ToBytes(specificLegacyScheme)};
s.write(AsBytes(Span{bytes.data(), SerSize}));
}

template <typename Stream>
Expand All @@ -172,12 +181,12 @@ class CBLSWrapper
{
std::array<uint8_t, SerSize> vecBytes{};
s.read(AsWritableBytes(Span{vecBytes.data(), SerSize}));
SetByteVector(vecBytes, specificLegacyScheme);
SetBytes(vecBytes, specificLegacyScheme);

if (!CheckMalleable(vecBytes, specificLegacyScheme)) {
// If CheckMalleable failed with specificLegacyScheme, we need to try again with the opposite scheme.
// Probably we received the BLS object sent with legacy scheme, but in the meanwhile the fork activated.
SetByteVector(vecBytes, !specificLegacyScheme);
SetBytes(vecBytes, !specificLegacyScheme);
if (!CheckMalleable(vecBytes, !specificLegacyScheme)) {
// Both attempts failed
throw std::ios_base::failure("malleable BLS object");
Expand All @@ -197,7 +206,8 @@ class CBLSWrapper

inline bool CheckMalleable(Span<uint8_t> vecBytes, const bool specificLegacyScheme) const
{
if (memcmp(vecBytes.data(), ToByteVector(specificLegacyScheme).data(), SerSize)) {
const auto bytes{ToBytes(specificLegacyScheme)};
if (memcmp(vecBytes.data(), bytes.data(), SerSize)) {
// TODO not sure if this is actually possible with the BLS libs. I'm assuming here that somewhere deep inside
// these libs masking might happen, so that 2 different binary representations could result in the same object
// representation
Expand All @@ -208,7 +218,7 @@ class CBLSWrapper

inline std::string ToString(const bool specificLegacyScheme) const
{
std::vector<uint8_t> buf = ToByteVector(specificLegacyScheme);
auto buf = ToBytes(specificLegacyScheme);
return HexStr(buf);
}

Expand All @@ -235,6 +245,7 @@ struct CBLSIdImplicit : public uint256
{
return {begin(), end()};
}
[[nodiscard]] std::array<uint8_t, 32> SerializeToArray(const bool fLegacy) const { return m_data; }
};

class CBLSId : public CBLSWrapper<CBLSIdImplicit, BLS_CURVE_ID_SIZE, CBLSId>
Expand Down Expand Up @@ -262,7 +273,7 @@ class CBLSSecretKey : public CBLSWrapper<bls::PrivateKey, BLS_CURVE_SECKEY_SIZE,
explicit CBLSSecretKey(Span<const unsigned char> vecBytes)
{
// The second param here is not 'is_legacy', but `modOrder`
SetByteVector(vecBytes, false);
SetBytes(vecBytes, false);
}
CBLSSecretKey(const CBLSSecretKey&) = default;
CBLSSecretKey& operator=(const CBLSSecretKey&) = default;
Expand Down Expand Up @@ -334,7 +345,7 @@ class CBLSSignature : public CBLSWrapper<bls::G2Element, BLS_CURVE_SIG_SIZE, CBL
CBLSSignature() = default;
explicit CBLSSignature(Span<const unsigned char> bytes, bool is_serialized_legacy)
{
SetByteVector(bytes, is_serialized_legacy);
SetBytes(bytes, is_serialized_legacy);
}
CBLSSignature(const CBLSSignature&) = default;
CBLSSignature& operator=(const CBLSSignature&) = default;
Expand Down Expand Up @@ -379,7 +390,7 @@ class CBLSLazyWrapper
private:
mutable std::mutex mutex;

mutable std::vector<uint8_t> vecBytes;
mutable std::array<uint8_t, BLSObject::SerSize> vecBytes;
mutable bool bufValid{false};
mutable bool bufLegacyScheme{true};

Expand All @@ -390,8 +401,8 @@ class CBLSLazyWrapper

public:
CBLSLazyWrapper() :
vecBytes(BLSObject::SerSize, 0),
bufLegacyScheme(bls::bls_legacy_scheme.load())
vecBytes{},
bufLegacyScheme(bls::bls_legacy_scheme.load())
{}

explicit CBLSLazyWrapper(const CBLSLazyWrapper& r)
Expand All @@ -408,7 +419,6 @@ class CBLSLazyWrapper
if (r.bufValid) {
vecBytes = r.vecBytes;
} else {
vecBytes.resize(BLSObject::SerSize);
std::fill(vecBytes.begin(), vecBytes.end(), 0);
}
objInitialized = r.objInitialized;
Expand All @@ -431,10 +441,9 @@ class CBLSLazyWrapper
{
std::unique_lock<std::mutex> l(mutex);
if (!objInitialized && !bufValid) {
vecBytes.resize(BLSObject::SerSize);
std::fill(vecBytes.begin(), vecBytes.end(), 0);
} else if (!bufValid || (bufLegacyScheme != specificLegacyScheme)) {
vecBytes = obj.ToByteVector(specificLegacyScheme);
vecBytes = obj.ToBytes(specificLegacyScheme);
bufValid = true;
bufLegacyScheme = specificLegacyScheme;
hash.SetNull();
Expand Down Expand Up @@ -482,7 +491,7 @@ class CBLSLazyWrapper
return invalidObj;
}
if (!objInitialized) {
obj.SetByteVector(vecBytes, bufLegacyScheme);
obj.SetBytes(vecBytes, bufLegacyScheme);
if (!obj.IsValid()) {
bufValid = false;
return invalidObj;
Expand Down Expand Up @@ -516,11 +525,10 @@ class CBLSLazyWrapper
{
std::unique_lock<std::mutex> l(mutex);
if (!objInitialized && !bufValid) {
vecBytes.resize(BLSObject::SerSize);
std::fill(vecBytes.begin(), vecBytes.end(), 0);
hash.SetNull();
} else if (!bufValid) {
vecBytes = obj.ToByteVector(bufLegacyScheme);
vecBytes = obj.ToBytes(bufLegacyScheme);
bufValid = true;
hash.SetNull();
}
Expand Down
4 changes: 0 additions & 4 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ class CMainParams : public CChainParams {
fRequireRoutableExternalIP = true;
m_is_test_chain = false;
fAllowMultipleAddressesFromGroup = false;
fAllowMultiplePorts = false;
nLLMQConnectionRetryTimeout = 60;
m_is_mockable_chain = false;

Expand Down Expand Up @@ -476,7 +475,6 @@ class CTestNetParams : public CChainParams {
fRequireRoutableExternalIP = true;
m_is_test_chain = true;
fAllowMultipleAddressesFromGroup = false;
fAllowMultiplePorts = true;
nLLMQConnectionRetryTimeout = 60;
m_is_mockable_chain = false;

Expand Down Expand Up @@ -659,7 +657,6 @@ class CDevNetParams : public CChainParams {
fRequireRoutableExternalIP = true;
m_is_test_chain = true;
fAllowMultipleAddressesFromGroup = true;
fAllowMultiplePorts = true;
nLLMQConnectionRetryTimeout = 60;
m_is_mockable_chain = false;

Expand Down Expand Up @@ -861,7 +858,6 @@ class CRegTestParams : public CChainParams {
fRequireRoutableExternalIP = false;
m_is_test_chain = true;
fAllowMultipleAddressesFromGroup = true;
fAllowMultiplePorts = true;
nLLMQConnectionRetryTimeout = 1; // must be lower then the LLMQ signing session timeout so that tests have control over failing behavior
m_is_mockable_chain = true;

Expand Down
3 changes: 0 additions & 3 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ class CChainParams
bool MineBlocksOnDemand() const { return consensus.fPowNoRetargeting; }
/** Allow multiple addresses to be selected from the same network group (e.g. 192.168.x.x) */
bool AllowMultipleAddressesFromGroup() const { return fAllowMultipleAddressesFromGroup; }
/** Allow nodes with the same address and multiple ports */
bool AllowMultiplePorts() const { return fAllowMultiplePorts; }
/** How long to wait until we allow retrying of a LLMQ connection */
int LLMQConnectionRetryTimeout() const { return nLLMQConnectionRetryTimeout; }
/** Return the network string */
Expand Down Expand Up @@ -176,7 +174,6 @@ class CChainParams
bool fRequireRoutableExternalIP;
bool m_is_test_chain;
bool fAllowMultipleAddressesFromGroup;
bool fAllowMultiplePorts;
bool m_is_mockable_chain;
int nLLMQConnectionRetryTimeout;
CCheckpointData checkpointData;
Expand Down
2 changes: 1 addition & 1 deletion src/governance/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ bool CGovernanceObject::Sign(const CActiveMasternodeManager& mn_activeman)
bool CGovernanceObject::CheckSignature(const CBLSPublicKey& pubKey) const
{
CBLSSignature sig;
sig.SetByteVector(m_obj.vchSig, false);
sig.SetBytes(m_obj.vchSig, false);
if (!sig.VerifyInsecure(pubKey, GetSignatureHash(), false)) {
LogPrintf("CGovernanceObject::CheckSignature -- VerifyInsecure() failed\n");
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/governance/vote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ bool CGovernanceVote::Sign(const CActiveMasternodeManager& mn_activeman)
bool CGovernanceVote::CheckSignature(const CBLSPublicKey& pubKey) const
{
CBLSSignature sig;
sig.SetByteVector(vchSig, false);
sig.SetBytes(vchSig, false);
if (!sig.VerifyInsecure(pubKey, GetSignatureHash(), false)) {
LogPrintf("CGovernanceVote::CheckSignature -- VerifyInsecure() failed\n");
return false;
Expand Down
8 changes: 4 additions & 4 deletions src/llmq/dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,13 +1019,13 @@ void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages, PeerManag
qc.quorumSig = skShare.Sign(commitmentHash, m_use_legacy_bls);

if (lieType == 3) {
std::vector<uint8_t> buf = qc.sig.ToByteVector(m_use_legacy_bls);
auto buf = qc.sig.ToBytes(m_use_legacy_bls);
buf[5]++;
qc.sig.SetByteVector(buf, m_use_legacy_bls);
qc.sig.SetBytes(buf, m_use_legacy_bls);
} else if (lieType == 4) {
std::vector<uint8_t> buf = qc.quorumSig.ToByteVector(m_use_legacy_bls);
auto buf = qc.quorumSig.ToBytes(m_use_legacy_bls);
buf[5]++;
qc.quorumSig.SetByteVector(buf, m_use_legacy_bls);
qc.quorumSig.SetBytes(buf, m_use_legacy_bls);
}

t3.stop();
Expand Down
69 changes: 21 additions & 48 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
assert(conn_type != ConnectionType::INBOUND);

if (pszDest == nullptr) {
bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
if (!fAllowLocal && IsLocal(addrConnect)) {
if (addrConnect.GetPort() == GetListenPort() && IsLocal(addrConnect)) {
return nullptr;
}

Expand Down Expand Up @@ -3508,25 +3507,18 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
std::tie(addr, addr_last_try) = addrman.Select(false, preferred_net);
}

auto dmn = mnList.GetMNByService(addr);
bool isMasternode = dmn != nullptr;

// Require outbound IPv4/IPv6 connections, other than feelers, to be to distinct network groups
if (!fFeeler && outbound_ipv46_peer_netgroups.count(m_netgroupman.GetGroup(addr))) {
continue;
}

// if we selected an invalid address, restart
if (!addr.IsValid() || outbound_ipv46_peer_netgroups.count(m_netgroupman.GetGroup(addr)))
break;

// don't try to connect to masternodes that we already have a connection to (most likely inbound)
if (isMasternode && setConnectedMasternodes.count(dmn->proTxHash))
if (!addr.IsValid()) {
break;
}

// if we selected a local address, restart (local addresses are allowed in regtest and devnet)
bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
if (!fAllowLocal && IsLocal(addrConnect)) {
// don't connect to ourselves
if (addr.GetPort() == GetListenPort() && IsLocal(addr)) {
break;
}

Expand All @@ -3542,32 +3534,14 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
// for non-feelers, require all the services we'll want,
// for feelers, only require they be a full node (only because most
// SPV clients don't have a good address DB available)
if (!isMasternode && !fFeeler && !HasAllDesirableServiceFlags(addr.nServices)) {
if (!fFeeler && !HasAllDesirableServiceFlags(addr.nServices)) {
continue;
} else if (!isMasternode && fFeeler && !MayHaveUsefulAddressDB(addr.nServices)) {
} else if (fFeeler && !MayHaveUsefulAddressDB(addr.nServices)) {
continue;
}

// Port validation in Dash has additional rules. Some networks are prohibited
// from using a non-default port while others allow any arbitary port so long
// it isn't a bad port (and in the case of masternodes, it matches its listen
// port)
const bool is_prohibited_port = [this, &addr, &isMasternode](){
if (!Params().AllowMultiplePorts()) {
const uint16_t default_port{Params().GetDefaultPort(addr.GetNetwork())};
assert(!IsBadPort(default_port)); // Make sure we never set the default port to a bad port
return addr.GetPort() != default_port;
}
const bool is_bad_port{IsBadPort(addr.GetPort())};
if (isMasternode) {
return addr.GetPort() != GetListenPort() || is_bad_port;
} else {
return is_bad_port;
}
}();

// Do not connect to prohibited ports, unless 50 invalid addresses have been selected already.
if (nTries < 50 && is_prohibited_port) {
if (nTries < 50 && IsBadPort(addr.GetPort())) {
continue;
}

Expand All @@ -3582,6 +3556,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
continue;
}

// don't try to connect to masternodes that we already have a connection to (most likely inbound)
if (auto dmn = mnList.GetMNByService(addr); dmn && setConnectedMasternodes.count(dmn->proTxHash)) {
continue;
}

addrConnect = addr;
break;
}
Expand Down Expand Up @@ -3918,20 +3897,8 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
// banned, discouraged or exact match?
if ((m_banman && (m_banman->IsDiscouraged(addrConnect) || m_banman->IsBanned(addrConnect))) || AlreadyConnectedToAddress(addrConnect))
return;
// local and not a connection to itself?
bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
if (!fAllowLocal && IsLocal(addrConnect))
return;
// Search for IP:PORT match:
// - if multiple ports for the same IP are allowed,
// - for probe connections
// Search for IP-only match otherwise
bool searchIPPort = Params().AllowMultiplePorts() || masternode_probe_connection == MasternodeProbeConn::IsConnection;
bool skip = searchIPPort ?
FindNode(static_cast<CService>(addrConnect)) :
FindNode(static_cast<CNetAddr>(addrConnect));
if (skip) {
LogPrintf("CConnman::%s -- Failed to open new connection to %s, already connected\n", __func__, getIpStr());
// connecting to ourselves?
if (addrConnect.GetPort() == GetListenPort() && IsLocal(addrConnect)) {
return;
}
} else if (FindNode(std::string(pszDest)))
Expand Down Expand Up @@ -4228,6 +4195,12 @@ CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, AddrMan& addrman_in,
, nSeed0(nSeed0In)
, nSeed1(nSeed1In)
{
// Make sure we never set the default port to a bad port
for (int n = 0; n < NET_MAX; ++n) {
const bool is_bad_port = IsBadPort(Params().GetDefaultPort(static_cast<Network>(n)));
assert(!is_bad_port);
}

SetTryNewOutboundPeer(false);

Options connOptions;
Expand Down
Loading

0 comments on commit e589460

Please sign in to comment.