Skip to content

Commit 1531652

Browse files
committed
Keep shared_ptrs to recently-replaced txn for compact blocks
1 parent edded80 commit 1531652

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ std::string HelpMessage(HelpMessageMode mode)
346346
strUsage += HelpMessageOpt("-maxorphantx=<n>", strprintf(_("Keep at most <n> unconnectable transactions in memory (default: %u)"), DEFAULT_MAX_ORPHAN_TRANSACTIONS));
347347
strUsage += HelpMessageOpt("-maxmempool=<n>", strprintf(_("Keep the transaction memory pool below <n> megabytes (default: %u)"), DEFAULT_MAX_MEMPOOL_SIZE));
348348
strUsage += HelpMessageOpt("-mempoolexpiry=<n>", strprintf(_("Do not keep transactions in the mempool longer than <n> hours (default: %u)"), DEFAULT_MEMPOOL_EXPIRY));
349+
strUsage += HelpMessageOpt("-blockreconstructionextratxn=<n>", strprintf(_("Extra transactions to keep in memory for compact block reconstructions (default: %u)"), DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN));
349350
strUsage += HelpMessageOpt("-par=<n>", strprintf(_("Set the number of script verification threads (%u to %d, 0 = auto, <0 = leave that many cores free, default: %d)"),
350351
-GetNumCores(), MAX_SCRIPTCHECK_THREADS, DEFAULT_SCRIPTCHECK_THREADS));
351352
#ifndef WIN32

src/net_processing.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);
5959
map<COutPoint, set<map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(cs_main);
6060
void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
6161

62+
static size_t vExtraTxnForCompactIt = 0;
63+
static std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(cs_main);
64+
6265
static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; // SHA256("main address relay")[0:8]
6366

6467
// Internal stuff
@@ -586,6 +589,17 @@ void UnregisterNodeSignals(CNodeSignals& nodeSignals)
586589
// mapOrphanTransactions
587590
//
588591

592+
void AddToCompactExtraTransactions(const CTransactionRef& tx)
593+
{
594+
size_t max_extra_txn = GetArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
595+
if (max_extra_txn <= 0)
596+
return;
597+
if (!vExtraTxnForCompact.size())
598+
vExtraTxnForCompact.resize(max_extra_txn);
599+
vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
600+
vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn;
601+
}
602+
589603
bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
590604
{
591605
const uint256& hash = tx->GetHash();
@@ -1615,7 +1629,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
16151629
pfrom->setAskFor.erase(inv.hash);
16161630
mapAlreadyAskedFor.erase(inv.hash);
16171631

1618-
if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, ptx, true, &fMissingInputs)) {
1632+
std::list<CTransactionRef> lRemovedTxn;
1633+
1634+
if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, ptx, true, &fMissingInputs, &lRemovedTxn)) {
16191635
mempool.check(pcoinsTip);
16201636
RelayTransaction(tx, connman);
16211637
for (unsigned int i = 0; i < tx.vout.size(); i++) {
@@ -1653,7 +1669,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
16531669

16541670
if (setMisbehaving.count(fromPeer))
16551671
continue;
1656-
if (AcceptToMemoryPool(mempool, stateDummy, porphanTx, true, &fMissingInputs2)) {
1672+
if (AcceptToMemoryPool(mempool, stateDummy, porphanTx, true, &fMissingInputs2, &lRemovedTxn)) {
16571673
LogPrint("mempool", " accepted orphan tx %s\n", orphanHash.ToString());
16581674
RelayTransaction(orphanTx, connman);
16591675
for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
@@ -1743,6 +1759,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
17431759
}
17441760
}
17451761
}
1762+
1763+
for (const CTransactionRef& tx : lRemovedTxn)
1764+
AddToCompactExtraTransactions(tx);
1765+
17461766
int nDoS = 0;
17471767
if (state.IsInvalid(nDoS))
17481768
{

src/net_processing.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
1515
static const int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
1616
/** Minimum time between orphan transactions expire time checks in seconds */
1717
static const int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
18+
/** Default number of orphan+recently-replaced txn to keep around for block reconstruction */
19+
static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN = 100;
1820

1921
/** Register with a network node to receive its signals */
2022
void RegisterNodeSignals(CNodeSignals& nodeSignals);

0 commit comments

Comments
 (0)