diff --git a/src/gtest/test_mempoollimit.cpp b/src/gtest/test_mempoollimit.cpp index b211e6992..856834a45 100644 --- a/src/gtest/test_mempoollimit.cpp +++ b/src/gtest/test_mempoollimit.cpp @@ -59,26 +59,26 @@ TEST(MempoolLimitTests, WeightedTxTreeCheckSizeAfterDropping) int trialNum = 0; while (testedDropping.size() < 3) { WeightedTxTree tree(MIN_TX_WEIGHT * 2); - EXPECT_EQ(0, tree.getTotalWeight().weight); - EXPECT_EQ(0, tree.getTotalWeight().lowFeePenaltyWeight); + EXPECT_EQ(0, tree.getTotalWeight().cost); + EXPECT_EQ(0, tree.getTotalWeight().evictionWeight); tree.add(WeightedTxInfo(TX_ID1, TxWeight(MIN_TX_WEIGHT, MIN_TX_WEIGHT))); - EXPECT_EQ(4000, tree.getTotalWeight().weight); - EXPECT_EQ(4000, tree.getTotalWeight().lowFeePenaltyWeight); + EXPECT_EQ(4000, tree.getTotalWeight().cost); + EXPECT_EQ(4000, tree.getTotalWeight().evictionWeight); tree.add(WeightedTxInfo(TX_ID2, TxWeight(MIN_TX_WEIGHT, MIN_TX_WEIGHT))); - EXPECT_EQ(8000, tree.getTotalWeight().weight); - EXPECT_EQ(8000, tree.getTotalWeight().lowFeePenaltyWeight); + EXPECT_EQ(8000, tree.getTotalWeight().cost); + EXPECT_EQ(8000, tree.getTotalWeight().evictionWeight); EXPECT_FALSE(tree.maybeDropRandom().is_initialized()); tree.add(WeightedTxInfo(TX_ID3, TxWeight(MIN_TX_WEIGHT, MIN_TX_WEIGHT + LOW_FEE_PENALTY))); - EXPECT_EQ(12000, tree.getTotalWeight().weight); - EXPECT_EQ(12000 + LOW_FEE_PENALTY, tree.getTotalWeight().lowFeePenaltyWeight); + EXPECT_EQ(12000, tree.getTotalWeight().cost); + EXPECT_EQ(12000 + LOW_FEE_PENALTY, tree.getTotalWeight().evictionWeight); boost::optional drop = tree.maybeDropRandom(); ASSERT_TRUE(drop.is_initialized()); uint256 txid = drop.get(); std::cerr << "Trial " << trialNum++ << ": dropped " << txid.ToString() << std::endl; testedDropping.insert(txid); // Do not continue to test if a particular trial fails - ASSERT_EQ(8000, tree.getTotalWeight().weight); - ASSERT_EQ(txid == TX_ID3 ? 8000 : 8000 + LOW_FEE_PENALTY, tree.getTotalWeight().lowFeePenaltyWeight); + ASSERT_EQ(8000, tree.getTotalWeight().cost); + ASSERT_EQ(txid == TX_ID3 ? 8000 : 8000 + LOW_FEE_PENALTY, tree.getTotalWeight().evictionWeight); } std::cerr << "All 3 scenarios tested in " << trialNum << " trials" << std::endl; } @@ -99,8 +99,8 @@ TEST(MempoolLimitTests, WeightedTxInfoFromTx) builder.AddSaplingOutput(sk.full_viewing_key().ovk, sk.default_address(), 25000, {}); WeightedTxInfo info = WeightedTxInfo::from(builder.Build().GetTxOrThrow(), 10000); - EXPECT_EQ(MIN_TX_WEIGHT, info.txWeight.weight); - EXPECT_EQ(MIN_TX_WEIGHT, info.txWeight.lowFeePenaltyWeight); + EXPECT_EQ(MIN_TX_WEIGHT, info.txWeight.cost); + EXPECT_EQ(MIN_TX_WEIGHT, info.txWeight.evictionWeight); } // Lower than standard fee @@ -111,8 +111,8 @@ TEST(MempoolLimitTests, WeightedTxInfoFromTx) builder.SetFee(9999); WeightedTxInfo info = WeightedTxInfo::from(builder.Build().GetTxOrThrow(), 9999); - EXPECT_EQ(MIN_TX_WEIGHT, info.txWeight.weight); - EXPECT_EQ(MIN_TX_WEIGHT + LOW_FEE_PENALTY, info.txWeight.lowFeePenaltyWeight); + EXPECT_EQ(MIN_TX_WEIGHT, info.txWeight.cost); + EXPECT_EQ(MIN_TX_WEIGHT + LOW_FEE_PENALTY, info.txWeight.evictionWeight); } // Larger Tx @@ -129,8 +129,8 @@ TEST(MempoolLimitTests, WeightedTxInfoFromTx) std::cerr << result.GetError() << std::endl; } WeightedTxInfo info = WeightedTxInfo::from(result.GetTxOrThrow(), 10000); - EXPECT_EQ(5124, info.txWeight.weight); - EXPECT_EQ(5124, info.txWeight.lowFeePenaltyWeight); + EXPECT_EQ(5124, info.txWeight.cost); + EXPECT_EQ(5124, info.txWeight.evictionWeight); } RegtestDeactivateSapling(); diff --git a/src/init.cpp b/src/init.cpp index 1447ac139..f8e9ed871 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -385,7 +385,7 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageOpt("-maxreceivebuffer=", strprintf(_("Maximum per-connection receive buffer, *1000 bytes (default: %u)"), 5000)); strUsage += HelpMessageOpt("-maxsendbuffer=", strprintf(_("Maximum per-connection send buffer, *1000 bytes (default: %u)"), 1000)); strUsage += HelpMessageOpt("-mempoolevictionmemoryminutes=", strprintf(_("The number of minutes before allowing rejected transactions to re-enter the mempool. (default: %u)"), DEFAULT_MEMPOOL_EVICTION_MEMORY_MINUTES)); - strUsage += HelpMessageOpt("-mempooltotalcostlimit=",strprintf(_("An upper bound on the maximum size in bytes of all transactions in the mempool. (default: %s)"), DEFAULT_MEMPOOL_TOTAL_WEIGHT_LIMIT)); + strUsage += HelpMessageOpt("-mempooltotalcostlimit=",strprintf(_("An upper bound on the maximum size in bytes of all transactions in the mempool. (default: %s)"), DEFAULT_MEMPOOL_TOTAL_COST_LIMIT)); strUsage += HelpMessageOpt("-onion=", strprintf(_("Use separate SOCKS5 proxy to reach peers via Tor hidden services (default: %s)"), "-proxy")); strUsage += HelpMessageOpt("-onlynet=", _("Only connect to nodes in network (ipv4, ipv6 or onion)")); strUsage += HelpMessageOpt("-permitbaremultisig", strprintf(_("Relay non-P2SH multisig (default: %u)"), 1)); @@ -978,7 +978,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) mempool.setSanityCheck(1.0 / ratio); } - int64_t mempoolTotalCostLimit = GetArg("-mempooltotalcostlimit", DEFAULT_MEMPOOL_TOTAL_WEIGHT_LIMIT); + int64_t mempoolTotalCostLimit = GetArg("-mempooltotalcostlimit", DEFAULT_MEMPOOL_TOTAL_COST_LIMIT); int64_t mempoolEvictionMemorySeconds = GetArg("-mempoolevictionmemoryminutes", DEFAULT_MEMPOOL_EVICTION_MEMORY_MINUTES) * 60; mempool.SetMempoolCostLimit(mempoolTotalCostLimit, mempoolEvictionMemorySeconds); diff --git a/src/mempoollimit.cpp b/src/mempoollimit.cpp index 594154d31..d20e805a6 100644 --- a/src/mempoollimit.cpp +++ b/src/mempoollimit.cpp @@ -59,20 +59,20 @@ void WeightedTxTree::backPropagate(size_t fromIndex, const TxWeight& weightDelta } } -size_t WeightedTxTree::findByWeight(size_t fromIndex, int64_t weightToFind) const +size_t WeightedTxTree::findByEvictionWeight(size_t fromIndex, int64_t weightToFind) const { - int leftWeight = getWeightAt(fromIndex * 2 + 1).lowFeePenaltyWeight; - int rightWeight = getWeightAt(fromIndex).lowFeePenaltyWeight - getWeightAt(fromIndex * 2 + 2).lowFeePenaltyWeight; + int leftWeight = getWeightAt(fromIndex * 2 + 1).evictionWeight; + int rightWeight = getWeightAt(fromIndex).evictionWeight - getWeightAt(fromIndex * 2 + 2).evictionWeight; // On Left if (weightToFind < leftWeight) { - return findByWeight(fromIndex * 2 + 1, weightToFind); + return findByEvictionWeight(fromIndex * 2 + 1, weightToFind); } // Found if (weightToFind < rightWeight) { return fromIndex; } // On Right - return findByWeight(fromIndex * 2 + 2, weightToFind - rightWeight); + return findByEvictionWeight(fromIndex * 2 + 2, weightToFind - rightWeight); } TxWeight WeightedTxTree::getTotalWeight() const @@ -124,15 +124,15 @@ void WeightedTxTree::remove(const uint256& txId) boost::optional WeightedTxTree::maybeDropRandom() { - int64_t totalPenaltyWeight = getTotalWeight().lowFeePenaltyWeight; + int64_t totalPenaltyWeight = getTotalWeight().evictionWeight; if (totalPenaltyWeight <= capacity) { return boost::none; } LogPrint("mempool", "Mempool cost limit exceeded (cost=%d, limit=%d)\n", totalPenaltyWeight, capacity); int randomWeight = GetRand(totalPenaltyWeight); - WeightedTxInfo drop = txIdAndWeights[findByWeight(0, randomWeight)]; - LogPrint("mempool", "Evicting transaction (txid=%s, cost=%d, penaltyCost=%d)\n", - drop.txId.ToString(), drop.txWeight.weight, drop.txWeight.lowFeePenaltyWeight); + WeightedTxInfo drop = txIdAndWeights[findByEvictionWeight(0, randomWeight)]; + LogPrint("mempool", "Evicting transaction (txid=%s, cost=%d, evictionWeight=%d)\n", + drop.txId.ToString(), drop.txWeight.cost, drop.txWeight.evictionWeight); remove(drop.txId); return drop.txId; } @@ -140,12 +140,12 @@ boost::optional WeightedTxTree::maybeDropRandom() TxWeight TxWeight::add(const TxWeight& other) const { - return TxWeight(weight + other.weight, lowFeePenaltyWeight + other.lowFeePenaltyWeight); + return TxWeight(cost + other.cost, evictionWeight + other.evictionWeight); } TxWeight TxWeight::negate() const { - return TxWeight(-weight, -lowFeePenaltyWeight); + return TxWeight(-cost, -evictionWeight); } @@ -156,9 +156,9 @@ WeightedTxInfo WeightedTxInfo::from(const CTransaction& tx, const CAmount& fee) memUsage += tx.vShieldedOutput.size() * OUTPUTDESCRIPTION_SIZE; memUsage += tx.vShieldedSpend.size() * SPENDDESCRIPTION_SIZE; int64_t cost = std::max(memUsage, MIN_TX_WEIGHT); - int64_t lowFeePenaltyCost = cost; + int64_t evictionWeight = cost; if (fee < DEFAULT_FEE) { - lowFeePenaltyCost += LOW_FEE_PENALTY; + evictionWeight += LOW_FEE_PENALTY; } - return WeightedTxInfo(tx.GetHash(), TxWeight(cost, lowFeePenaltyCost)); + return WeightedTxInfo(tx.GetHash(), TxWeight(cost, evictionWeight)); } diff --git a/src/mempoollimit.h b/src/mempoollimit.h index 985d1db55..7000b90b1 100644 --- a/src/mempoollimit.h +++ b/src/mempoollimit.h @@ -13,7 +13,7 @@ #include "primitives/transaction.h" #include "uint256.h" -const size_t DEFAULT_MEMPOOL_TOTAL_WEIGHT_LIMIT = 80000000; +const size_t DEFAULT_MEMPOOL_TOTAL_COST_LIMIT = 80000000; const int64_t DEFAULT_MEMPOOL_EVICTION_MEMORY_MINUTES = 60; const size_t RECENTLY_EVICTED_SIZE = 40000; @@ -46,11 +46,11 @@ public: struct TxWeight { - int64_t weight; - int64_t lowFeePenaltyWeight; + int64_t cost; + int64_t evictionWeight; - TxWeight(int64_t weight_, int64_t lowFeePenaltyWeight_) - : weight(weight_), lowFeePenaltyWeight(lowFeePenaltyWeight_) {} + TxWeight(int64_t cost_, int64_t evictionWeight_) + : cost(cost_), evictionWeight(evictionWeight_) {} TxWeight add(const TxWeight& other) const; TxWeight negate() const; @@ -78,7 +78,7 @@ class WeightedTxTree TxWeight getWeightAt(size_t index) const; void backPropagate(size_t fromIndex, const TxWeight& weightDelta); - size_t findByWeight(size_t fromIndex, int64_t weightToFind) const; + size_t findByEvictionWeight(size_t fromIndex, int64_t weightToFind) const; public: WeightedTxTree(int64_t capacity_) : capacity(capacity_) {