Changeset View
Changeset View
Standalone View
Standalone View
src/net_processing.cpp
Show First 20 Lines • Show All 720 Lines • ▼ Show 20 Lines | void AddToCompactExtraTransactions(const CTransactionRef &tx) { | ||||
vExtraTxnForCompact[vExtraTxnForCompactIt] = | vExtraTxnForCompact[vExtraTxnForCompactIt] = | ||||
std::make_pair(tx->GetHash(), tx); | std::make_pair(tx->GetHash(), tx); | ||||
vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn; | vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn; | ||||
} | } | ||||
bool AddOrphanTx(const CTransactionRef &tx, NodeId peer) | bool AddOrphanTx(const CTransactionRef &tx, NodeId peer) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | ||||
const uint256 &txid = tx->GetId(); | const uint256 &txid = tx->GetHash(); | ||||
if (mapOrphanTransactions.count(txid)) { | if (mapOrphanTransactions.count(txid)) { | ||||
return false; | return false; | ||||
} | } | ||||
// Ignore big transactions, to avoid a send-big-orphans memory exhaustion | // Ignore big transactions, to avoid a send-big-orphans memory exhaustion | ||||
// attack. If a peer has a legitimate large transaction with a missing | // attack. If a peer has a legitimate large transaction with a missing | ||||
// parent then we assume it will rebroadcast it later, after the parent | // parent then we assume it will rebroadcast it later, after the parent | ||||
// transaction(s) have been mined or received. | // transaction(s) have been mined or received. | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | |||||
void EraseOrphansFor(NodeId peer) { | void EraseOrphansFor(NodeId peer) { | ||||
int nErased = 0; | int nErased = 0; | ||||
std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin(); | std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin(); | ||||
while (iter != mapOrphanTransactions.end()) { | while (iter != mapOrphanTransactions.end()) { | ||||
// Increment to avoid iterator becoming invalid. | // Increment to avoid iterator becoming invalid. | ||||
std::map<uint256, COrphanTx>::iterator maybeErase = iter++; | std::map<uint256, COrphanTx>::iterator maybeErase = iter++; | ||||
if (maybeErase->second.fromPeer == peer) { | if (maybeErase->second.fromPeer == peer) { | ||||
nErased += EraseOrphanTx(maybeErase->second.tx->GetId()); | nErased += EraseOrphanTx(maybeErase->second.tx->GetHash()); | ||||
} | } | ||||
} | } | ||||
if (nErased > 0) { | if (nErased > 0) { | ||||
LogPrint("mempool", "Erased %d orphan tx from peer=%d\n", nErased, | LogPrint("mempool", "Erased %d orphan tx from peer=%d\n", nErased, | ||||
peer); | peer); | ||||
} | } | ||||
} | } | ||||
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) | unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | ||||
unsigned int nEvicted = 0; | unsigned int nEvicted = 0; | ||||
static int64_t nNextSweep; | static int64_t nNextSweep; | ||||
int64_t nNow = GetTime(); | int64_t nNow = GetTime(); | ||||
if (nNextSweep <= nNow) { | if (nNextSweep <= nNow) { | ||||
// Sweep out expired orphan pool entries: | // Sweep out expired orphan pool entries: | ||||
int nErased = 0; | int nErased = 0; | ||||
int64_t nMinExpTime = | int64_t nMinExpTime = | ||||
nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL; | nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL; | ||||
std::map<uint256, COrphanTx>::iterator iter = | std::map<uint256, COrphanTx>::iterator iter = | ||||
mapOrphanTransactions.begin(); | mapOrphanTransactions.begin(); | ||||
while (iter != mapOrphanTransactions.end()) { | while (iter != mapOrphanTransactions.end()) { | ||||
std::map<uint256, COrphanTx>::iterator maybeErase = iter++; | std::map<uint256, COrphanTx>::iterator maybeErase = iter++; | ||||
if (maybeErase->second.nTimeExpire <= nNow) { | if (maybeErase->second.nTimeExpire <= nNow) { | ||||
nErased += EraseOrphanTx(maybeErase->second.tx->GetId()); | nErased += EraseOrphanTx(maybeErase->second.tx->GetHash()); | ||||
} else { | } else { | ||||
nMinExpTime = | nMinExpTime = | ||||
std::min(maybeErase->second.nTimeExpire, nMinExpTime); | std::min(maybeErase->second.nTimeExpire, nMinExpTime); | ||||
} | } | ||||
} | } | ||||
// Sweep again 5 minutes after the next entry that expires in order to | // Sweep again 5 minutes after the next entry that expires in order to | ||||
// batch the linear scan. | // batch the linear scan. | ||||
nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL; | nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL; | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | void PeerLogicValidation::SyncTransaction(const CTransaction &tx, | ||||
for (size_t j = 0; j < tx.vin.size(); j++) { | for (size_t j = 0; j < tx.vin.size(); j++) { | ||||
auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout); | auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout); | ||||
if (itByPrev == mapOrphanTransactionsByPrev.end()) { | if (itByPrev == mapOrphanTransactionsByPrev.end()) { | ||||
continue; | continue; | ||||
} | } | ||||
for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); | for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); | ||||
++mi) { | ++mi) { | ||||
const CTransaction &orphanTx = *(*mi)->second.tx; | const CTransaction &orphanTx = *(*mi)->second.tx; | ||||
const uint256 &orphanId = orphanTx.GetId(); | const uint256 &orphanId = orphanTx.GetHash(); | ||||
vOrphanErase.push_back(orphanId); | vOrphanErase.push_back(orphanId); | ||||
} | } | ||||
} | } | ||||
// Erase orphan transactions include or precluded by this block | // Erase orphan transactions include or precluded by this block | ||||
if (vOrphanErase.size()) { | if (vOrphanErase.size()) { | ||||
int nErased = 0; | int nErased = 0; | ||||
for (uint256 &orphanId : vOrphanErase) { | for (uint256 &orphanId : vOrphanErase) { | ||||
▲ Show 20 Lines • Show All 168 Lines • ▼ Show 20 Lines | switch (inv.type) { | ||||
case MSG_BLOCK: | case MSG_BLOCK: | ||||
return mapBlockIndex.count(inv.hash); | return mapBlockIndex.count(inv.hash); | ||||
} | } | ||||
// Don't know what it is, just say we already got one | // Don't know what it is, just say we already got one | ||||
return true; | return true; | ||||
} | } | ||||
static void RelayTransaction(const CTransaction &tx, CConnman &connman) { | static void RelayTransaction(const CTransaction &tx, CConnman &connman) { | ||||
CInv inv(MSG_TX, tx.GetId()); | CInv inv(MSG_TX, tx.GetHash()); | ||||
connman.ForEachNode([&inv](CNode *pnode) { pnode->PushInventory(inv); }); | connman.ForEachNode([&inv](CNode *pnode) { pnode->PushInventory(inv); }); | ||||
} | } | ||||
static void RelayAddress(const CAddress &addr, bool fReachable, | static void RelayAddress(const CAddress &addr, bool fReachable, | ||||
CConnman &connman) { | CConnman &connman) { | ||||
// Limited relaying of addresses outside our network(s) | // Limited relaying of addresses outside our network(s) | ||||
unsigned int nRelayNodes = fReachable ? 2 : 1; | unsigned int nRelayNodes = fReachable ? 2 : 1; | ||||
▲ Show 20 Lines • Show All 967 Lines • ▼ Show 20 Lines | else if (strCommand == NetMsgType::TX) { | ||||
} | } | ||||
std::deque<COutPoint> vWorkQueue; | std::deque<COutPoint> vWorkQueue; | ||||
std::vector<uint256> vEraseQueue; | std::vector<uint256> vEraseQueue; | ||||
CTransactionRef ptx; | CTransactionRef ptx; | ||||
vRecv >> ptx; | vRecv >> ptx; | ||||
const CTransaction &tx = *ptx; | const CTransaction &tx = *ptx; | ||||
CInv inv(MSG_TX, tx.GetId()); | CInv inv(MSG_TX, tx.GetHash()); | ||||
pfrom->AddInventoryKnown(inv); | pfrom->AddInventoryKnown(inv); | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
bool fMissingInputs = false; | bool fMissingInputs = false; | ||||
CValidationState state; | CValidationState state; | ||||
pfrom->setAskFor.erase(inv.hash); | pfrom->setAskFor.erase(inv.hash); | ||||
Show All 9 Lines | else if (strCommand == NetMsgType::TX) { | ||||
for (size_t i = 0; i < tx.vout.size(); i++) { | for (size_t i = 0; i < tx.vout.size(); i++) { | ||||
vWorkQueue.emplace_back(inv.hash, i); | vWorkQueue.emplace_back(inv.hash, i); | ||||
} | } | ||||
pfrom->nLastTXTime = GetTime(); | pfrom->nLastTXTime = GetTime(); | ||||
LogPrint("mempool", "AcceptToMemoryPool: peer=%d: accepted %s " | LogPrint("mempool", "AcceptToMemoryPool: peer=%d: accepted %s " | ||||
"(poolsz %u txn, %u kB)\n", | "(poolsz %u txn, %u kB)\n", | ||||
pfrom->id, tx.GetId().ToString(), mempool.size(), | pfrom->id, tx.GetHash().ToString(), mempool.size(), | ||||
mempool.DynamicMemoryUsage() / 1000); | mempool.DynamicMemoryUsage() / 1000); | ||||
// Recursively process any orphan transactions that depended on this | // Recursively process any orphan transactions that depended on this | ||||
// one | // one | ||||
std::set<NodeId> setMisbehaving; | std::set<NodeId> setMisbehaving; | ||||
while (!vWorkQueue.empty()) { | while (!vWorkQueue.empty()) { | ||||
auto itByPrev = | auto itByPrev = | ||||
mapOrphanTransactionsByPrev.find(vWorkQueue.front()); | mapOrphanTransactionsByPrev.find(vWorkQueue.front()); | ||||
vWorkQueue.pop_front(); | vWorkQueue.pop_front(); | ||||
if (itByPrev == mapOrphanTransactionsByPrev.end()) { | if (itByPrev == mapOrphanTransactionsByPrev.end()) { | ||||
continue; | continue; | ||||
} | } | ||||
for (auto mi = itByPrev->second.begin(); | for (auto mi = itByPrev->second.begin(); | ||||
mi != itByPrev->second.end(); ++mi) { | mi != itByPrev->second.end(); ++mi) { | ||||
const CTransactionRef &porphanTx = (*mi)->second.tx; | const CTransactionRef &porphanTx = (*mi)->second.tx; | ||||
const CTransaction &orphanTx = *porphanTx; | const CTransaction &orphanTx = *porphanTx; | ||||
const uint256 &orphanId = orphanTx.GetId(); | const uint256 &orphanId = orphanTx.GetHash(); | ||||
NodeId fromPeer = (*mi)->second.fromPeer; | NodeId fromPeer = (*mi)->second.fromPeer; | ||||
bool fMissingInputs2 = false; | bool fMissingInputs2 = false; | ||||
// Use a dummy CValidationState so someone can't setup nodes | // Use a dummy CValidationState so someone can't setup nodes | ||||
// to counter-DoS based on orphan resolution (that is, | // to counter-DoS based on orphan resolution (that is, | ||||
// feeding people an invalid transaction based on LegitTxX | // feeding people an invalid transaction based on LegitTxX | ||||
// in order to get anyone relaying LegitTxX banned) | // in order to get anyone relaying LegitTxX banned) | ||||
CValidationState stateDummy; | CValidationState stateDummy; | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | else if (strCommand == NetMsgType::TX) { | ||||
unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx); | unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx); | ||||
if (nEvicted > 0) { | if (nEvicted > 0) { | ||||
LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", | LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", | ||||
nEvicted); | nEvicted); | ||||
} | } | ||||
} else { | } else { | ||||
LogPrint("mempool", | LogPrint("mempool", | ||||
"not keeping orphan with rejected parents %s\n", | "not keeping orphan with rejected parents %s\n", | ||||
tx.GetId().ToString()); | tx.GetHash().ToString()); | ||||
// We will continue to reject this tx since it has rejected | // We will continue to reject this tx since it has rejected | ||||
// parents so avoid re-requesting it from other peers. | // parents so avoid re-requesting it from other peers. | ||||
recentRejects->insert(tx.GetId()); | recentRejects->insert(tx.GetHash()); | ||||
} | } | ||||
} else { | } else { | ||||
if (!state.CorruptionPossible()) { | if (!state.CorruptionPossible()) { | ||||
// Do not use rejection cache for witness transactions or | // Do not use rejection cache for witness transactions or | ||||
// witness-stripped transactions, as they can have been | // witness-stripped transactions, as they can have been | ||||
// malleated. See https://github.com/bitcoin/bitcoin/issues/8279 | // malleated. See https://github.com/bitcoin/bitcoin/issues/8279 | ||||
// for details. | // for details. | ||||
assert(recentRejects); | assert(recentRejects); | ||||
recentRejects->insert(tx.GetId()); | recentRejects->insert(tx.GetHash()); | ||||
if (RecursiveDynamicUsage(*ptx) < 100000) { | if (RecursiveDynamicUsage(*ptx) < 100000) { | ||||
AddToCompactExtraTransactions(ptx); | AddToCompactExtraTransactions(ptx); | ||||
} | } | ||||
} | } | ||||
if (pfrom->fWhitelisted && | if (pfrom->fWhitelisted && | ||||
GetBoolArg("-whitelistforcerelay", | GetBoolArg("-whitelistforcerelay", | ||||
DEFAULT_WHITELISTFORCERELAY)) { | DEFAULT_WHITELISTFORCERELAY)) { | ||||
// Always relay transactions received from whitelisted peers, | // Always relay transactions received from whitelisted peers, | ||||
// even if they were already in the mempool or rejected from it | // even if they were already in the mempool or rejected from it | ||||
// due to policy, allowing the node to function as a gateway for | // due to policy, allowing the node to function as a gateway for | ||||
// nodes hidden behind it. | // nodes hidden behind it. | ||||
// | // | ||||
// Never relay transactions that we would assign a non-zero DoS | // Never relay transactions that we would assign a non-zero DoS | ||||
// score for, as we expect peers to do the same with us in that | // score for, as we expect peers to do the same with us in that | ||||
// case. | // case. | ||||
int nDoS = 0; | int nDoS = 0; | ||||
if (!state.IsInvalid(nDoS) || nDoS == 0) { | if (!state.IsInvalid(nDoS) || nDoS == 0) { | ||||
LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", | LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", | ||||
tx.GetId().ToString(), pfrom->id); | tx.GetHash().ToString(), pfrom->id); | ||||
RelayTransaction(tx, connman); | RelayTransaction(tx, connman); | ||||
} else { | } else { | ||||
LogPrintf("Not relaying invalid transaction %s from " | LogPrintf("Not relaying invalid transaction %s from " | ||||
"whitelisted peer=%d (%s)\n", | "whitelisted peer=%d (%s)\n", | ||||
tx.GetId().ToString(), pfrom->id, | tx.GetHash().ToString(), pfrom->id, | ||||
FormatStateMessage(state)); | FormatStateMessage(state)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
for (const CTransactionRef &removedTx : lRemovedTxn) { | for (const CTransactionRef &removedTx : lRemovedTxn) { | ||||
AddToCompactExtraTransactions(removedTx); | AddToCompactExtraTransactions(removedTx); | ||||
} | } | ||||
int nDoS = 0; | int nDoS = 0; | ||||
if (state.IsInvalid(nDoS)) { | if (state.IsInvalid(nDoS)) { | ||||
LogPrint("mempoolrej", "%s from peer=%d was not accepted: %s\n", | LogPrint("mempoolrej", "%s from peer=%d was not accepted: %s\n", | ||||
tx.GetId().ToString(), pfrom->id, | tx.GetHash().ToString(), pfrom->id, | ||||
FormatStateMessage(state)); | FormatStateMessage(state)); | ||||
// Never send AcceptToMemoryPool's internal codes over P2P. | // Never send AcceptToMemoryPool's internal codes over P2P. | ||||
if (state.GetRejectCode() < REJECT_INTERNAL) { | if (state.GetRejectCode() < REJECT_INTERNAL) { | ||||
connman.PushMessage( | connman.PushMessage( | ||||
pfrom, msgMaker.Make(NetMsgType::REJECT, strCommand, | pfrom, msgMaker.Make(NetMsgType::REJECT, strCommand, | ||||
uint8_t(state.GetRejectCode()), | uint8_t(state.GetRejectCode()), | ||||
state.GetRejectReason().substr( | state.GetRejectReason().substr( | ||||
0, MAX_REJECT_MESSAGE_LENGTH), | 0, MAX_REJECT_MESSAGE_LENGTH), | ||||
▲ Show 20 Lines • Show All 1,320 Lines • ▼ Show 20 Lines | std::vector<CInv> vInv; | ||||
{ | { | ||||
LOCK(pto->cs_feeFilter); | LOCK(pto->cs_feeFilter); | ||||
filterrate = pto->minFeeFilter; | filterrate = pto->minFeeFilter; | ||||
} | } | ||||
LOCK(pto->cs_filter); | LOCK(pto->cs_filter); | ||||
for (const auto &txinfo : vtxinfo) { | for (const auto &txinfo : vtxinfo) { | ||||
const uint256 &txid = txinfo.tx->GetId(); | const uint256 &txid = txinfo.tx->GetHash(); | ||||
CInv inv(MSG_TX, txid); | CInv inv(MSG_TX, txid); | ||||
pto->setInventoryTxToSend.erase(txid); | pto->setInventoryTxToSend.erase(txid); | ||||
if (filterrate != 0) { | if (filterrate != 0) { | ||||
if (txinfo.feeRate.GetFeePerK() < filterrate) { | if (txinfo.feeRate.GetFeePerK() < filterrate) { | ||||
continue; | continue; | ||||
} | } | ||||
} | } | ||||
if (pto->pfilter) { | if (pto->pfilter) { | ||||
▲ Show 20 Lines • Show All 252 Lines • Show Last 20 Lines |