Skip to content

Commit

Permalink
net: create generic functor accessors and move vNodes to CConnman
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni authored and Fuzzbawls committed Aug 25, 2020
1 parent 2e02467 commit dbde9be
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 94 deletions.
82 changes: 53 additions & 29 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3098,11 +3098,14 @@ bool ActivateBestChain(CValidationState& state, const CBlock* pblock, bool fAlre
// Relay inventory, but don't relay old inventory during initial block download.
int nBlockEstimate = Checkpoints::GetTotalBlocksEstimate();
{
LOCK(cs_vNodes);
for (CNode *pnode : vNodes)
if (chainActive.Height() >
(pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
pnode->PushInventory(CInv(MSG_BLOCK, hashNewTip));
if (connman) {
connman->ForEachNode([nBlockEstimate, hashNewTip](CNode* pnode) {
if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate)) {
pnode->PushInventory(CInv(MSG_BLOCK, hashNewTip));
}
return true;
});
}
}
// Notify external listeners about the new tip.
GetMainSignals().UpdatedBlockTip(pindexNewTip);
Expand Down Expand Up @@ -4972,6 +4975,46 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
return true;
}

static void RelayTransaction(const CTransaction& tx, CConnman& connman)
{
CInv inv(MSG_TX, tx.GetHash());
connman.ForEachNode([&inv](CNode* pnode)
{
pnode->PushInventory(inv);
return true;
});
}

static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connman)
{
int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)

// Relay to a limited number of other nodes
// Use deterministic randomness to send to the same nodes for 24 hours
// at a time so the addrKnowns of the chosen nodes prevent repeats
static const uint64_t salt0 = GetRand(std::numeric_limits<uint64_t>::max());
static const uint64_t salt1 = GetRand(std::numeric_limits<uint64_t>::max());
uint64_t hashAddr = addr.GetHash();
std::multimap<uint64_t, CNode*> mapMix;
const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));

auto sortfunc = [&mapMix, &hasher](CNode* pnode) {
if (pnode->nVersion >= CADDR_TIME_VERSION) {
uint64_t hashKey = CSipHasher(hasher).Write(pnode->id).Finalize();
mapMix.emplace(hashKey, pnode);
}
return true;
};

auto pushfunc = [&addr, &mapMix, &nRelayNodes] {
FastRandomContext insecure_rand;
for (auto mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
mi->second->PushAddress(addr, insecure_rand);
};

connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
}

void static ProcessGetData(CNode* pfrom)
{
AssertLockNotHeld(cs_main);
Expand Down Expand Up @@ -5397,26 +5440,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
bool fReachable = IsReachable(addr);
if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable()) {
// Relay to a limited number of other nodes
{
LOCK(cs_vNodes);
// Use deterministic randomness to send to the same nodes for 24 hours
// at a time so the addrKnowns of the chosen nodes prevent repeats
static const uint64_t salt0 = GetRand(std::numeric_limits<uint64_t>::max());
static const uint64_t salt1 = GetRand(std::numeric_limits<uint64_t>::max());
uint64_t hashAddr = addr.GetHash();
std::multimap<uint64_t, CNode*> mapMix;
const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
for (CNode* pnode : vNodes) {
if (pnode->nVersion < CADDR_TIME_VERSION)
continue;
uint64_t hashKey = CSipHasher(hasher).Write(pnode->id).Finalize();
mapMix.insert(std::make_pair(hashKey, pnode));
}
int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
FastRandomContext insecure_rand;
for (auto mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
((*mi).second)->PushAddress(addr, insecure_rand);
}
RelayAddress(addr, fReachable, connman);
}
// Do not store addresses outside our network
if (fReachable)
Expand Down Expand Up @@ -5605,7 +5629,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR

if (!tx.HasZerocoinSpendInputs() && AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs, false, ignoreFees)) {
mempool.check(pcoinsTip);
RelayTransaction(tx);
RelayTransaction(tx, connman);
vWorkQueue.push_back(inv.hash);

LogPrint(BCLog::MEMPOOL, "%s : peer=%d %s : accepted %s (poolsz %u txn, %u kB)\n",
Expand Down Expand Up @@ -5635,7 +5659,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
continue;
if(AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2)) {
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
RelayTransaction(orphanTx);
RelayTransaction(orphanTx, connman);
vWorkQueue.push_back(orphanHash);
vEraseQueue.push_back(orphanHash);
} else if(!fMissingInputs2) {
Expand All @@ -5662,7 +5686,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
} else if (tx.HasZerocoinSpendInputs() && AcceptToMemoryPool(mempool, state, tx, true, &fMissingZerocoinInputs, false, false, ignoreFees)) {
//Presstab: ZCoin has a bunch of code commented out here. Is this something that should have more going on?
//Also there is nothing that handles fMissingZerocoinInputs. Does there need to be?
RelayTransaction(tx);
RelayTransaction(tx, connman);
LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: Zerocoinspend peer=%d %s : accepted %s (poolsz %u)\n",
pfrom->id, pfrom->cleanSubVer,
tx.GetHash().ToString(),
Expand Down Expand Up @@ -5691,7 +5715,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
// FIXME: This includes invalid transactions, which means a
// whitelisted peer could get us banned! We may want to change
// that.
RelayTransaction(tx);
RelayTransaction(tx, connman);
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/masternode-budget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,11 +974,12 @@ void CBudgetManager::NewBlock()
ResetSync();
}

LOCK(cs_vNodes);
for (CNode* pnode : vNodes)
std::vector<CNode*> vNodesCopy = g_connman->CopyNodeVector();
for (CNode* pnode : vNodesCopy)
if (pnode->nVersion >= ActiveProtocol())
Sync(pnode, UINT256_ZERO, true);

g_connman->ReleaseNodeVector(vNodesCopy);
MarkSynced();
}

Expand Down Expand Up @@ -1761,7 +1762,7 @@ CBudgetProposalBroadcast::CBudgetProposalBroadcast(std::string strProposalNameIn
void CBudgetProposalBroadcast::Relay()
{
CInv inv(MSG_BUDGET_PROPOSAL, GetHash());
RelayInv(inv);
g_connman->RelayInv(inv);
}

CBudgetVote::CBudgetVote() :
Expand All @@ -1788,7 +1789,7 @@ CBudgetVote::CBudgetVote(CTxIn vinIn, uint256 nProposalHashIn, int nVoteIn) :
void CBudgetVote::Relay()
{
CInv inv(MSG_BUDGET_VOTE, GetHash());
RelayInv(inv);
g_connman->RelayInv(inv);
}

uint256 CBudgetVote::GetHash() const
Expand Down Expand Up @@ -2258,7 +2259,7 @@ CFinalizedBudgetBroadcast::CFinalizedBudgetBroadcast(std::string strBudgetNameIn
void CFinalizedBudgetBroadcast::Relay()
{
CInv inv(MSG_BUDGET_FINALIZED, GetHash());
RelayInv(inv);
g_connman->RelayInv(inv);
}

CFinalizedBudgetVote::CFinalizedBudgetVote() :
Expand All @@ -2283,7 +2284,7 @@ CFinalizedBudgetVote::CFinalizedBudgetVote(CTxIn vinIn, uint256 nBudgetHashIn) :
void CFinalizedBudgetVote::Relay()
{
CInv inv(MSG_BUDGET_FINALIZED_VOTE, GetHash());
RelayInv(inv);
g_connman->RelayInv(inv);
}

uint256 CFinalizedBudgetVote::GetHash() const
Expand Down
2 changes: 1 addition & 1 deletion src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ bool CMasternodePaymentWinner::IsValid(CNode* pnode, std::string& strError)
void CMasternodePaymentWinner::Relay()
{
CInv inv(MSG_MASTERNODE_WINNER, GetHash());
RelayInv(inv);
g_connman->RelayInv(inv);
}

void DumpMasternodePayments()
Expand Down
15 changes: 6 additions & 9 deletions src/masternode-sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,13 @@ void CMasternodeSync::ProcessMessage(CNode* pfrom, std::string& strCommand, CDat

void CMasternodeSync::ClearFulfilledRequest()
{
TRY_LOCK(cs_vNodes, lockRecv);
if (!lockRecv) return;

for (CNode* pnode : vNodes) {
g_connman->ForEachNode([](CNode* pnode) {
pnode->ClearFulfilledRequest("getspork");
pnode->ClearFulfilledRequest("mnsync");
pnode->ClearFulfilledRequest("mnwsync");
pnode->ClearFulfilledRequest("busync");
}
return true;
});
}

void CMasternodeSync::Process()
Expand Down Expand Up @@ -284,10 +282,8 @@ void CMasternodeSync::Process()
if (!isRegTestNet && !IsBlockchainSynced() &&
RequestedMasternodeAssets > MASTERNODE_SYNC_SPORKS) return;

TRY_LOCK(cs_vNodes, lockRecv);
if (!lockRecv) return;

for (CNode* pnode : vNodes) {
std::vector<CNode*> vNodesCopy = g_connman->CopyNodeVector();
for (CNode* pnode : vNodesCopy) {
if (isRegTestNet) {
if (RequestedMasternodeAttempt <= 2) {
pnode->PushMessage(NetMsgType::GETSPORKS); //get current network sporks
Expand Down Expand Up @@ -419,4 +415,5 @@ void CMasternodeSync::Process()
}
}
}
g_connman->ReleaseNodeVector(vNodesCopy);
}
4 changes: 2 additions & 2 deletions src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ bool CMasternodeBroadcast::CheckInputsAndAdd(int& nDoS)
void CMasternodeBroadcast::Relay()
{
CInv inv(MSG_MASTERNODE_ANNOUNCE, GetHash());
RelayInv(inv);
g_connman->RelayInv(inv);
}

uint256 CMasternodeBroadcast::GetHash() const
Expand Down Expand Up @@ -787,5 +787,5 @@ bool CMasternodePing::CheckAndUpdate(int& nDos, bool fRequireEnabled, bool fChec
void CMasternodePing::Relay()
{
CInv inv(MSG_MASTERNODE_PING, GetHash());
RelayInv(inv);
g_connman->RelayInv(inv);
}
10 changes: 6 additions & 4 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,11 @@ bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, Optional<CReserveKey>& r
return error("PIVXMiner : ProcessNewBlock, block not accepted");
}

for (CNode* node : vNodes) {
g_connman->ForEachNode([&pblock](CNode* node)
{
node->PushInventory(CInv(MSG_BLOCK, pblock->GetHash()));
}
return true;
});

return true;
}
Expand Down Expand Up @@ -595,7 +597,7 @@ void BitcoinMiner(CWallet* pwallet, bool fProofOfStake)
// update fStakeableCoins (5 minute check time);
CheckForCoins(pwallet, 5);

while ((vNodes.empty() && Params().MiningRequiresPeers()) || pwallet->IsLocked() || !fStakeableCoins ||
while ((g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0 && Params().MiningRequiresPeers()) || pwallet->IsLocked() || !fStakeableCoins ||
masternodeSync.NotCompleted()) {
MilliSleep(5000);
// Do a separate 1 minute check here to ensure fStakeableCoins is updated
Expand Down Expand Up @@ -703,7 +705,7 @@ void BitcoinMiner(CWallet* pwallet, bool fProofOfStake)

// Check for stop or if block needs to be rebuilt
boost::this_thread::interruption_point();
if ( (vNodes.empty() && Params().MiningRequiresPeers()) || // Regtest mode doesn't require peers
if ( (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0 && Params().MiningRequiresPeers()) || // Regtest mode doesn't require peers
(pblock->nNonce >= 0xffff0000) ||
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60) ||
(pindexPrev != chainActive.Tip())
Expand Down
Loading

0 comments on commit dbde9be

Please sign in to comment.