Check that tx_relay is initialized before access

This commit is contained in:
Suhas Daftuar 2019-03-08 15:48:41 -05:00
parent c4aa2ba822
commit e75c39cd42
3 changed files with 138 additions and 121 deletions

View file

@ -499,9 +499,11 @@ void CNode::copyStats(CNodeStats &stats)
X(nServices); X(nServices);
X(addr); X(addr);
X(addrBind); X(addrBind);
{ if (m_tx_relay != nullptr) {
LOCK(m_tx_relay->cs_filter); LOCK(m_tx_relay->cs_filter);
stats.fRelayTxes = m_tx_relay->fRelayTxes; stats.fRelayTxes = m_tx_relay->fRelayTxes;
} else {
stats.fRelayTxes = false;
} }
X(nLastSend); X(nLastSend);
X(nLastRecv); X(nLastRecv);
@ -528,9 +530,11 @@ void CNode::copyStats(CNodeStats &stats)
} }
X(m_legacyWhitelisted); X(m_legacyWhitelisted);
X(m_permissionFlags); X(m_permissionFlags);
{ if (m_tx_relay != nullptr) {
LOCK(m_tx_relay->cs_feeFilter); LOCK(m_tx_relay->cs_feeFilter);
stats.minFeeFilter = m_tx_relay->minFeeFilter; stats.minFeeFilter = m_tx_relay->minFeeFilter;
} else {
stats.minFeeFilter = 0;
} }
// It is common for nodes with good ping times to suddenly become lagged, // It is common for nodes with good ping times to suddenly become lagged,
@ -818,11 +822,17 @@ bool CConnman::AttemptToEvictConnection()
continue; continue;
if (node->fDisconnect) if (node->fDisconnect)
continue; continue;
LOCK(node->m_tx_relay->cs_filter); bool peer_relay_txes = false;
bool peer_filter_not_null = false;
if (node->m_tx_relay != nullptr) {
LOCK(node->m_tx_relay->cs_filter);
peer_relay_txes = node->m_tx_relay->fRelayTxes;
peer_filter_not_null = node->m_tx_relay->pfilter != nullptr;
}
NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime, NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime,
node->nLastBlockTime, node->nLastTXTime, node->nLastBlockTime, node->nLastTXTime,
HasAllDesirableServiceFlags(node->nServices), HasAllDesirableServiceFlags(node->nServices),
node->m_tx_relay->fRelayTxes, node->m_tx_relay->pfilter != nullptr, node->addr, node->nKeyedNetGroup, peer_relay_txes, peer_filter_not_null, node->addr, node->nKeyedNetGroup,
node->m_prefer_evict}; node->m_prefer_evict};
vEvictionCandidates.push_back(candidate); vEvictionCandidates.push_back(candidate);
} }

View file

@ -848,7 +848,7 @@ public:
void AddInventoryKnown(const CInv& inv) void AddInventoryKnown(const CInv& inv)
{ {
{ if (m_tx_relay != nullptr) {
LOCK(m_tx_relay->cs_tx_inventory); LOCK(m_tx_relay->cs_tx_inventory);
m_tx_relay->filterInventoryKnown.insert(inv.hash); m_tx_relay->filterInventoryKnown.insert(inv.hash);
} }
@ -856,7 +856,7 @@ public:
void PushInventory(const CInv& inv) void PushInventory(const CInv& inv)
{ {
if (inv.type == MSG_TX) { if (inv.type == MSG_TX && m_tx_relay != nullptr) {
LOCK(m_tx_relay->cs_tx_inventory); LOCK(m_tx_relay->cs_tx_inventory);
if (!m_tx_relay->filterInventoryKnown.contains(inv.hash)) { if (!m_tx_relay->filterInventoryKnown.contains(inv.hash)) {
m_tx_relay->setInventoryTxToSend.insert(inv.hash); m_tx_relay->setInventoryTxToSend.insert(inv.hash);

View file

@ -1448,7 +1448,7 @@ void static ProcessGetBlockData(CNode* pfrom, const CChainParams& chainparams, c
{ {
bool sendMerkleBlock = false; bool sendMerkleBlock = false;
CMerkleBlock merkleBlock; CMerkleBlock merkleBlock;
{ if (pfrom->m_tx_relay != nullptr) {
LOCK(pfrom->m_tx_relay->cs_filter); LOCK(pfrom->m_tx_relay->cs_filter);
if (pfrom->m_tx_relay->pfilter) { if (pfrom->m_tx_relay->pfilter) {
sendMerkleBlock = true; sendMerkleBlock = true;
@ -1512,7 +1512,7 @@ void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnm
std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin(); std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
std::vector<CInv> vNotFound; std::vector<CInv> vNotFound;
const CNetMsgMaker msgMaker(pfrom->GetSendVersion()); const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
{ if (pfrom->m_tx_relay != nullptr) {
LOCK(cs_main); LOCK(cs_main);
while (it != pfrom->vRecvGetData.end() && (it->type == MSG_TX || it->type == MSG_WITNESS_TX)) { while (it != pfrom->vRecvGetData.end() && (it->type == MSG_TX || it->type == MSG_WITNESS_TX)) {
@ -1995,7 +1995,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
// set nodes not capable of serving the complete blockchain history as "limited nodes" // set nodes not capable of serving the complete blockchain history as "limited nodes"
pfrom->m_limited_node = (!(nServices & NODE_NETWORK) && (nServices & NODE_NETWORK_LIMITED)); pfrom->m_limited_node = (!(nServices & NODE_NETWORK) && (nServices & NODE_NETWORK_LIMITED));
{ if (pfrom->m_tx_relay != nullptr) {
LOCK(pfrom->m_tx_relay->cs_filter); LOCK(pfrom->m_tx_relay->cs_filter);
pfrom->m_tx_relay->fRelayTxes = fRelay; // set to true after we get the first filter* message pfrom->m_tx_relay->fRelayTxes = fRelay; // set to true after we get the first filter* message
} }
@ -3030,8 +3030,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
return true; return true;
} }
LOCK(pfrom->m_tx_relay->cs_tx_inventory); if (pfrom->m_tx_relay != nullptr) {
pfrom->m_tx_relay->fSendMempool = true; LOCK(pfrom->m_tx_relay->cs_tx_inventory);
pfrom->m_tx_relay->fSendMempool = true;
}
return true; return true;
} }
@ -3122,7 +3124,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
LOCK(cs_main); LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100); Misbehaving(pfrom->GetId(), 100);
} }
else else if (pfrom->m_tx_relay != nullptr)
{ {
LOCK(pfrom->m_tx_relay->cs_filter); LOCK(pfrom->m_tx_relay->cs_filter);
pfrom->m_tx_relay->pfilter.reset(new CBloomFilter(filter)); pfrom->m_tx_relay->pfilter.reset(new CBloomFilter(filter));
@ -3141,7 +3143,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
bool bad = false; bool bad = false;
if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) { if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
bad = true; bad = true;
} else { } else if (pfrom->m_tx_relay != nullptr) {
LOCK(pfrom->m_tx_relay->cs_filter); LOCK(pfrom->m_tx_relay->cs_filter);
if (pfrom->m_tx_relay->pfilter) { if (pfrom->m_tx_relay->pfilter) {
pfrom->m_tx_relay->pfilter->insert(vData); pfrom->m_tx_relay->pfilter->insert(vData);
@ -3157,6 +3159,9 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
} }
if (strCommand == NetMsgType::FILTERCLEAR) { if (strCommand == NetMsgType::FILTERCLEAR) {
if (pfrom->m_tx_relay == nullptr) {
return true;
}
LOCK(pfrom->m_tx_relay->cs_filter); LOCK(pfrom->m_tx_relay->cs_filter);
if (pfrom->GetLocalServices() & NODE_BLOOM) { if (pfrom->GetLocalServices() & NODE_BLOOM) {
pfrom->m_tx_relay->pfilter.reset(new CBloomFilter()); pfrom->m_tx_relay->pfilter.reset(new CBloomFilter());
@ -3169,7 +3174,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
CAmount newFeeFilter = 0; CAmount newFeeFilter = 0;
vRecv >> newFeeFilter; vRecv >> newFeeFilter;
if (MoneyRange(newFeeFilter)) { if (MoneyRange(newFeeFilter)) {
{ if (pfrom->m_tx_relay != nullptr) {
LOCK(pfrom->m_tx_relay->cs_feeFilter); LOCK(pfrom->m_tx_relay->cs_feeFilter);
pfrom->m_tx_relay->minFeeFilter = newFeeFilter; pfrom->m_tx_relay->minFeeFilter = newFeeFilter;
} }
@ -3791,121 +3796,123 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
} }
pto->vInventoryBlockToSend.clear(); pto->vInventoryBlockToSend.clear();
LOCK(pto->m_tx_relay->cs_tx_inventory); if (pto->m_tx_relay != nullptr) {
// Check whether periodic sends should happen LOCK(pto->m_tx_relay->cs_tx_inventory);
bool fSendTrickle = pto->HasPermission(PF_NOBAN); // Check whether periodic sends should happen
if (pto->m_tx_relay->nNextInvSend < nNow) { bool fSendTrickle = pto->HasPermission(PF_NOBAN);
fSendTrickle = true; if (pto->m_tx_relay->nNextInvSend < nNow) {
if (pto->fInbound) { fSendTrickle = true;
pto->m_tx_relay->nNextInvSend = connman->PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL); if (pto->fInbound) {
} else { pto->m_tx_relay->nNextInvSend = connman->PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL);
// Use half the delay for outbound peers, as there is less privacy concern for them. } else {
pto->m_tx_relay->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> 1); // Use half the delay for outbound peers, as there is less privacy concern for them.
} pto->m_tx_relay->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> 1);
}
// Time to send but the peer has requested we not relay transactions.
if (fSendTrickle) {
LOCK(pto->m_tx_relay->cs_filter);
if (!pto->m_tx_relay->fRelayTxes) pto->m_tx_relay->setInventoryTxToSend.clear();
}
// Respond to BIP35 mempool requests
if (fSendTrickle && pto->m_tx_relay->fSendMempool) {
auto vtxinfo = mempool.infoAll();
pto->m_tx_relay->fSendMempool = false;
CAmount filterrate = 0;
{
LOCK(pto->m_tx_relay->cs_feeFilter);
filterrate = pto->m_tx_relay->minFeeFilter;
}
LOCK(pto->m_tx_relay->cs_filter);
for (const auto& txinfo : vtxinfo) {
const uint256& hash = txinfo.tx->GetHash();
CInv inv(MSG_TX, hash);
pto->m_tx_relay->setInventoryTxToSend.erase(hash);
if (filterrate) {
if (txinfo.feeRate.GetFeePerK() < filterrate)
continue;
}
if (pto->m_tx_relay->pfilter) {
if (!pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
}
pto->m_tx_relay->filterInventoryKnown.insert(hash);
vInv.push_back(inv);
if (vInv.size() == MAX_INV_SZ) {
connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
vInv.clear();
} }
} }
pto->m_tx_relay->timeLastMempoolReq = GetTime();
}
// Determine transactions to relay // Time to send but the peer has requested we not relay transactions.
if (fSendTrickle) { if (fSendTrickle) {
// Produce a vector with all candidates for sending LOCK(pto->m_tx_relay->cs_filter);
std::vector<std::set<uint256>::iterator> vInvTx; if (!pto->m_tx_relay->fRelayTxes) pto->m_tx_relay->setInventoryTxToSend.clear();
vInvTx.reserve(pto->m_tx_relay->setInventoryTxToSend.size());
for (std::set<uint256>::iterator it = pto->m_tx_relay->setInventoryTxToSend.begin(); it != pto->m_tx_relay->setInventoryTxToSend.end(); it++) {
vInvTx.push_back(it);
} }
CAmount filterrate = 0;
{ // Respond to BIP35 mempool requests
LOCK(pto->m_tx_relay->cs_feeFilter); if (fSendTrickle && pto->m_tx_relay->fSendMempool) {
filterrate = pto->m_tx_relay->minFeeFilter; auto vtxinfo = mempool.infoAll();
} pto->m_tx_relay->fSendMempool = false;
// Topologically and fee-rate sort the inventory we send for privacy and priority reasons. CAmount filterrate = 0;
// A heap is used so that not all items need sorting if only a few are being sent.
CompareInvMempoolOrder compareInvMempoolOrder(&mempool);
std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
// No reason to drain out at many times the network's capacity,
// especially since we have many peers and some will draw much shorter delays.
unsigned int nRelayedTransactions = 0;
LOCK(pto->m_tx_relay->cs_filter);
while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
// Fetch the top element from the heap
std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
std::set<uint256>::iterator it = vInvTx.back();
vInvTx.pop_back();
uint256 hash = *it;
// Remove it from the to-be-sent set
pto->m_tx_relay->setInventoryTxToSend.erase(it);
// Check if not in the filter already
if (pto->m_tx_relay->filterInventoryKnown.contains(hash)) {
continue;
}
// Not in the mempool anymore? don't bother sending it.
auto txinfo = mempool.info(hash);
if (!txinfo.tx) {
continue;
}
if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) {
continue;
}
if (pto->m_tx_relay->pfilter && !pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
// Send
vInv.push_back(CInv(MSG_TX, hash));
nRelayedTransactions++;
{ {
// Expire old relay messages LOCK(pto->m_tx_relay->cs_feeFilter);
while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow) filterrate = pto->m_tx_relay->minFeeFilter;
{ }
mapRelay.erase(vRelayExpiration.front().second);
vRelayExpiration.pop_front();
}
auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx))); LOCK(pto->m_tx_relay->cs_filter);
if (ret.second) {
vRelayExpiration.push_back(std::make_pair(nNow + 15 * 60 * 1000000, ret.first)); for (const auto& txinfo : vtxinfo) {
const uint256& hash = txinfo.tx->GetHash();
CInv inv(MSG_TX, hash);
pto->m_tx_relay->setInventoryTxToSend.erase(hash);
if (filterrate) {
if (txinfo.feeRate.GetFeePerK() < filterrate)
continue;
}
if (pto->m_tx_relay->pfilter) {
if (!pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
}
pto->m_tx_relay->filterInventoryKnown.insert(hash);
vInv.push_back(inv);
if (vInv.size() == MAX_INV_SZ) {
connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
vInv.clear();
} }
} }
if (vInv.size() == MAX_INV_SZ) { pto->m_tx_relay->timeLastMempoolReq = GetTime();
connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv)); }
vInv.clear();
// Determine transactions to relay
if (fSendTrickle) {
// Produce a vector with all candidates for sending
std::vector<std::set<uint256>::iterator> vInvTx;
vInvTx.reserve(pto->m_tx_relay->setInventoryTxToSend.size());
for (std::set<uint256>::iterator it = pto->m_tx_relay->setInventoryTxToSend.begin(); it != pto->m_tx_relay->setInventoryTxToSend.end(); it++) {
vInvTx.push_back(it);
}
CAmount filterrate = 0;
{
LOCK(pto->m_tx_relay->cs_feeFilter);
filterrate = pto->m_tx_relay->minFeeFilter;
}
// Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
// A heap is used so that not all items need sorting if only a few are being sent.
CompareInvMempoolOrder compareInvMempoolOrder(&mempool);
std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
// No reason to drain out at many times the network's capacity,
// especially since we have many peers and some will draw much shorter delays.
unsigned int nRelayedTransactions = 0;
LOCK(pto->m_tx_relay->cs_filter);
while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
// Fetch the top element from the heap
std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
std::set<uint256>::iterator it = vInvTx.back();
vInvTx.pop_back();
uint256 hash = *it;
// Remove it from the to-be-sent set
pto->m_tx_relay->setInventoryTxToSend.erase(it);
// Check if not in the filter already
if (pto->m_tx_relay->filterInventoryKnown.contains(hash)) {
continue;
}
// Not in the mempool anymore? don't bother sending it.
auto txinfo = mempool.info(hash);
if (!txinfo.tx) {
continue;
}
if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) {
continue;
}
if (pto->m_tx_relay->pfilter && !pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
// Send
vInv.push_back(CInv(MSG_TX, hash));
nRelayedTransactions++;
{
// Expire old relay messages
while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
{
mapRelay.erase(vRelayExpiration.front().second);
vRelayExpiration.pop_front();
}
auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
if (ret.second) {
vRelayExpiration.push_back(std::make_pair(nNow + 15 * 60 * 1000000, ret.first));
}
}
if (vInv.size() == MAX_INV_SZ) {
connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
vInv.clear();
}
pto->m_tx_relay->filterInventoryKnown.insert(hash);
} }
pto->m_tx_relay->filterInventoryKnown.insert(hash);
} }
} }
} }
@ -4066,7 +4073,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
// Message: feefilter // Message: feefilter
// //
// We don't want white listed peers to filter txs to us if we have -whitelistforcerelay // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
if (pto->nVersion >= FEEFILTER_VERSION && gArgs.GetBoolArg("-feefilter", DEFAULT_FEEFILTER) && if (pto->m_tx_relay != nullptr && pto->nVersion >= FEEFILTER_VERSION && gArgs.GetBoolArg("-feefilter", DEFAULT_FEEFILTER) &&
!pto->HasPermission(PF_FORCERELAY)) { !pto->HasPermission(PF_FORCERELAY)) {
CAmount currentFilter = mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK(); CAmount currentFilter = mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
int64_t timeNow = GetTimeMicros(); int64_t timeNow = GetTimeMicros();