Merge remote-tracking branch 'trim-cnode' into hotfix-5.3.3

This commit is contained in:
Kris Nuttycombe 2023-02-20 19:30:48 -07:00
commit 169e890f1e
6 changed files with 120 additions and 33 deletions

View File

@ -187,15 +187,7 @@ CRollingBloomFilter::CRollingBloomFilter(const unsigned int nElements, const dou
* => nFilterBits = -nHashFuncs * nMaxElements / log(1.0 - pow(fpRate, 1.0 / nHashFuncs))
* => nFilterBits = -nHashFuncs * nMaxElements / log(1.0 - exp(logFpRate / nHashFuncs))
*/
uint32_t nFilterBits = (uint32_t)ceil(-1.0 * nHashFuncs * nMaxElements / log(1.0 - exp(logFpRate / nHashFuncs)));
data.clear();
/* For each data element we need to store 2 bits. If both bits are 0, the
* bit is treated as unset. If the bits are (01), (10), or (11), the bit is
* treated as set in generation 1, 2, or 3 respectively.
* These bits are stored in separate integers: position P corresponds to bit
* (P & 63) of the integers data[(P >> 6) * 2] and data[(P >> 6) * 2 + 1]. */
data.resize(((nFilterBits + 63) / 64) << 1);
reset();
nFilterBits = (uint32_t)ceil(-1.0 * nHashFuncs * nMaxElements / log(1.0 - exp(logFpRate / nHashFuncs)));
}
/* Similar to CBloomFilter::Hash */
@ -213,6 +205,9 @@ static inline uint32_t FastMod(uint32_t x, size_t n) {
void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
{
if (data.empty()) {
initialize();
}
if (nEntriesThisGeneration == nEntriesPerGeneration) {
nEntriesThisGeneration = 0;
nGeneration++;
@ -250,6 +245,9 @@ void CRollingBloomFilter::insert(const uint256& hash)
bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
{
if (data.empty()) {
return false;
}
for (int n = 0; n < nHashFuncs; n++) {
uint32_t h = RollingBloomHash(n, nTweak, vKey);
int bit = h & 0x3F;
@ -268,8 +266,19 @@ bool CRollingBloomFilter::contains(const uint256& hash) const
return contains(vData);
}
void CRollingBloomFilter::reset()
void CRollingBloomFilter::reset() {
std::vector<uint64_t>().swap(data);
}
void CRollingBloomFilter::initialize()
{
/* For each data element we need to store 2 bits. If both bits are 0, the
* bit is treated as unset. If the bits are (01), (10), or (11), the bit is
* treated as set in generation 1, 2, or 3 respectively.
* These bits are stored in separate integers: position P corresponds to bit
* (P & 63) of the integers data[(P >> 6) * 2] and data[(P >> 6) * 2 + 1]. */
data.resize(((nFilterBits + 63) / 64) << 1);
nTweak = GetRand(std::numeric_limits<unsigned int>::max());
nEntriesThisGeneration = 0;
nGeneration = 1;

View File

@ -131,7 +131,13 @@ public:
void reset();
protected:
bool is_data_empty() const { return data.empty(); }
private:
void initialize();
uint32_t nFilterBits;
int nEntriesPerGeneration;
int nEntriesThisGeneration;
int nGeneration;

View File

@ -6659,7 +6659,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
addr.nTime = nNow - 5 * 24 * 60 * 60;
pfrom->AddAddressKnown(addr);
pfrom->AddAddressIfNotAlreadyKnown(addr);
bool fReachable = IsReachable(addr);
if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
{
@ -6754,7 +6754,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
}
else
{
pfrom->AddKnownTx(WTxId(inv.hash, inv.hashAux));
pfrom->AddKnownWTxId(WTxId(inv.hash, inv.hashAux));
if (fBlocksOnly)
LogPrint("net", "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->id);
else if (!fAlreadyHave && !IsInitialBlockDownload(chainparams.GetConsensus()))
@ -6900,7 +6900,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
LOCK(cs_main);
pfrom->AddKnownTx(wtxid);
pfrom->AddKnownWTxId(wtxid);
bool fMissingInputs = false;
CValidationState state;
@ -7597,9 +7597,8 @@ bool SendMessages(const Consensus::Params& params, CNode* pto)
vAddr.reserve(pto->vAddrToSend.size());
for (const CAddress& addr : pto->vAddrToSend)
{
if (!pto->addrKnown.contains(addr.GetKey()))
if (pto->AddAddressIfNotAlreadyKnown(addr))
{
pto->addrKnown.insert(addr.GetKey());
vAddr.push_back(addr);
// receiver rejects addr messages larger than 1000
if (vAddr.size() >= 1000)
@ -7672,6 +7671,12 @@ bool SendMessages(const Consensus::Params& params, CNode* pto)
vector<CInv> vInv;
{
LOCK(pto->cs_inventory);
// Avoid possibly adding to pto->filterInventoryKnown after it has been reset in CloseSocketDisconnect.
if (pto->fDisconnect) {
// We can safely return here because SendMessages would, in any case, do nothing after
// this block if pto->fDisconnect is set.
return true;
}
vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
// Add blocks
@ -7719,7 +7724,7 @@ bool SendMessages(const Consensus::Params& params, CNode* pto)
if (pto->pfilter) {
if (!pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
}
pto->filterInventoryKnown.insert(hash);
pto->AddKnownTxId(hash);
vInv.push_back(inv);
if (vInv.size() == MAX_INV_SZ) {
pto->PushMessage("inv", vInv);
@ -7754,7 +7759,7 @@ bool SendMessages(const Consensus::Params& params, CNode* pto)
// Remove it from the to-be-sent set
pto->setInventoryTxToSend.erase(it);
// Check if not in the filter already
if (pto->filterInventoryKnown.contains(hash)) {
if (pto->HasKnownTxId(hash)) {
continue;
}
// Not in the mempool anymore? don't bother sending it.
@ -7789,7 +7794,7 @@ bool SendMessages(const Consensus::Params& params, CNode* pto)
pto->PushMessage("inv", vInv);
vInv.clear();
}
pto->filterInventoryKnown.insert(hash);
pto->AddKnownTxId(hash);
}
}
}

View File

@ -440,6 +440,14 @@ void CNode::CloseSocketDisconnect()
CloseSocket(hSocket);
}
}
{
LOCK(cs_addrKnown);
addrKnown.reset();
}
{
LOCK(cs_inventory);
filterInventoryKnown.reset();
}
// in case this fails, we'll empty the recv buffer when the CNode is deleted
TRY_LOCK(cs_vRecvMsg, lockRecv);
@ -2221,7 +2229,6 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
nSendOffset = 0;
hashContinue = uint256();
nStartingHeight = -1;
filterInventoryKnown.reset();
fSendMempool = false;
fGetAddr = false;
nNextLocalAddrSend = 0;

View File

@ -304,6 +304,12 @@ public:
CBloomFilter* pfilter;
NodeId id;
std::atomic<int> nRefCount;
CRollingBloomFilter addrKnown;
mutable CCriticalSection cs_addrKnown;
// Inventory based relay
// This filter is protected by cs_inventory and contains both txids and wtxids.
CRollingBloomFilter filterInventoryKnown;
const uint64_t nKeyedNetGroup;
@ -333,22 +339,19 @@ public:
// flood relay
std::vector<CAddress> vAddrToSend;
CRollingBloomFilter addrKnown;
bool fGetAddr;
std::set<uint256> setKnown;
int64_t nNextAddrSend;
int64_t nNextLocalAddrSend;
// inventory based relay
CRollingBloomFilter filterInventoryKnown;
// Set of transaction ids we still have to announce.
// They are sorted by the mempool before relay, so the order is not important.
std::set<uint256> setInventoryTxToSend;
// List of block ids we still have announce.
// List of block ids we still have to announce.
// There is no final sorting before sending, as they are always sent immediately
// and in the order requested.
std::vector<uint256> vInventoryBlockToSend;
CCriticalSection cs_inventory;
mutable CCriticalSection cs_inventory;
std::set<WTxId> setAskFor;
std::multimap<int64_t, CInv> mapAskFor;
int64_t nNextInvSend;
@ -448,10 +451,25 @@ public:
}
void AddAddressKnown(const CAddress& addr)
bool AddAddressIfNotAlreadyKnown(const CAddress& addr)
{
LOCK(cs_addrKnown);
// Avoid adding to addrKnown after it has been reset in CloseSocketDisconnect.
if (fDisconnect) {
return false;
}
if (!addrKnown.contains(addr.GetKey())) {
addrKnown.insert(addr.GetKey());
return true;
} else {
return false;
}
}
bool IsAddressKnown(const CAddress& addr) const
{
LOCK(cs_addrKnown);
return addrKnown.contains(addr.GetKey());
}
void PushAddress(const CAddress& addr, FastRandomContext &insecure_rand)
@ -459,7 +477,7 @@ public:
// 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() && !IsAddressKnown(addr)) {
if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
vAddrToSend[insecure_rand.randrange(vAddrToSend.size())] = addr;
} else {
@ -469,18 +487,32 @@ public:
}
void AddKnownTx(const WTxId& wtxid)
{
void AddKnownWTxId(const WTxId& wtxid)
{
LOCK(cs_inventory);
if (!fDisconnect) {
filterInventoryKnown.insert(wtxid.ToBytes());
}
}
void AddKnownTxId(const uint256& txid)
{
LOCK(cs_inventory);
if (!fDisconnect) {
filterInventoryKnown.insert(txid);
}
}
bool HasKnownTxId(const uint256& txid) const
{
LOCK(cs_inventory);
return filterInventoryKnown.contains(txid);
}
void PushTxInventory(const WTxId& wtxid)
{
LOCK(cs_inventory);
if (!filterInventoryKnown.contains(wtxid.ToBytes())) {
if (!fDisconnect && !filterInventoryKnown.contains(wtxid.ToBytes())) {
setInventoryTxToSend.insert(wtxid.hash);
}
}
@ -488,8 +520,10 @@ public:
void PushBlockInventory(const uint256& hash)
{
LOCK(cs_inventory);
if (!fDisconnect) {
vInventoryBlockToSend.push_back(hash);
}
}
void AskFor(const CInv& inv);

View File

@ -538,4 +538,30 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
}
}
BOOST_AUTO_TEST_CASE(rolling_bloom_reset)
{
struct TestRollingBloomFilter : CRollingBloomFilter
{
TestRollingBloomFilter() : CRollingBloomFilter(100, 0.01) {}
bool is_data_empty() const { return CRollingBloomFilter::is_data_empty(); }
};
TestRollingBloomFilter rb;
BOOST_CHECK(rb.is_data_empty());
std::vector<unsigned char> d = RandomData();
rb.insert(d);
BOOST_CHECK(!rb.is_data_empty());
BOOST_CHECK(rb.contains(d));
// reset() should ensure minimal memory usage.
rb.reset();
BOOST_CHECK(rb.is_data_empty());
BOOST_CHECK(!rb.contains(d));
rb.insert(d);
BOOST_CHECK(!rb.is_data_empty());
BOOST_CHECK(rb.contains(d));
}
BOOST_AUTO_TEST_SUITE_END()