Skip to content

Commit 53b90df

Browse files
committed
Add txn_replaced to Added callback, remove lReplaced ATMP arg
1 parent 1520506 commit 53b90df

15 files changed

+66
-48
lines changed

src/init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ void Shutdown()
212212
// Because these depend on each-other, we make sure that neither can be
213213
// using the other before destroying them.
214214
if (peerLogic) UnregisterValidationInterface(peerLogic.get());
215+
if (peerLogic) UnregisterMempoolInterface(peerLogic.get());
215216
if (g_connman) g_connman->Stop();
216217
peerLogic.reset();
217218
g_connman.reset();
@@ -1305,6 +1306,7 @@ bool AppInitMain()
13051306

13061307
peerLogic.reset(new PeerLogicValidation(&connman, scheduler));
13071308
RegisterValidationInterface(peerLogic.get());
1309+
RegisterMempoolInterface(peerLogic.get());
13081310

13091311
// sanitize comments per BIP-0014, format user agent and check total size
13101312
std::vector<std::string> uacomments;

src/net_processing.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,15 @@ void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationSta
983983
mapBlockSource.erase(it);
984984
}
985985

986+
987+
void PeerLogicValidation::TransactionAddedToMempool(const CTransactionRef& ptx, const std::vector<CTransactionRef>& txn_replaced) {
988+
LOCK(g_cs_orphans);
989+
990+
for (const CTransactionRef& tx : txn_replaced) {
991+
AddToCompactExtraTransactions(tx);
992+
}
993+
}
994+
986995
//////////////////////////////////////////////////////////////////////////////
987996
//
988997
// Messages
@@ -2177,10 +2186,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21772186
pfrom->setAskFor.erase(inv.hash);
21782187
mapAlreadyAskedFor.erase(inv.hash);
21792188

2180-
std::list<CTransactionRef> lRemovedTxn;
2181-
21822189
if (!AlreadyHave(inv) &&
2183-
AcceptToMemoryPool(mempool, state, ptx, &fMissingInputs, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
2190+
AcceptToMemoryPool(mempool, state, ptx, &fMissingInputs, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
21842191
mempool.check(pcoinsTip.get());
21852192
RelayTransaction(tx, connman);
21862193
for (unsigned int i = 0; i < tx.vout.size(); i++) {
@@ -2218,7 +2225,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22182225

22192226
if (setMisbehaving.count(fromPeer))
22202227
continue;
2221-
if (AcceptToMemoryPool(mempool, stateDummy, porphanTx, &fMissingInputs2, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
2228+
if (AcceptToMemoryPool(mempool, stateDummy, porphanTx, &fMissingInputs2, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
22222229
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
22232230
RelayTransaction(orphanTx, connman);
22242231
for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
@@ -2318,9 +2325,6 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23182325
}
23192326
}
23202327

2321-
for (const CTransactionRef& removedTx : lRemovedTxn)
2322-
AddToCompactExtraTransactions(removedTx);
2323-
23242328
int nDoS = 0;
23252329
if (state.IsInvalid(nDoS))
23262330
{

src/net_processing.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45;
3535
/** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict, in seconds */
3636
static constexpr int64_t MINIMUM_CONNECT_TIME = 30;
3737

38-
class PeerLogicValidation final : public CValidationInterface, public NetEventsInterface {
38+
class PeerLogicValidation final : public CValidationInterface, public MempoolInterface, public NetEventsInterface {
3939
private:
4040
CConnman* const connman;
4141

@@ -58,6 +58,10 @@ class PeerLogicValidation final : public CValidationInterface, public NetEventsI
5858
* Overridden from CValidationInterface.
5959
*/
6060
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
61+
/**
62+
* Overridden from MempoolInterface.
63+
*/
64+
void TransactionAddedToMempool(const CTransactionRef& ptx, const std::vector<CTransactionRef>& txn_replaced) override;
6165

6266
/** Initialize a peer by adding it to mapNodeState and pushing a message requesting its version */
6367
void InitializeNode(CNode* pnode) override;

src/rpc/rawtransaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ UniValue sendrawtransaction(const JSONRPCRequest& request)
11361136
CValidationState state;
11371137
bool fMissingInputs;
11381138
if (!AcceptToMemoryPool(mempool, state, std::move(tx), &fMissingInputs,
1139-
nullptr /* plTxnReplaced */, false /* bypass_limits */, nMaxRawTxFee)) {
1139+
false /* bypass_limits */, nMaxRawTxFee)) {
11401140
if (state.IsInvalid()) {
11411141
throw JSONRPCError(RPC_TRANSACTION_REJECTED, FormatStateMessage(state));
11421142
} else {
@@ -1241,7 +1241,7 @@ UniValue testmempoolaccept(const JSONRPCRequest& request)
12411241
{
12421242
LOCK(cs_main);
12431243
test_accept_res = AcceptToMemoryPool(mempool, state, std::move(tx), &missing_inputs,
1244-
nullptr /* plTxnReplaced */, false /* bypass_limits */, max_raw_tx_fee, /* test_accept */ true);
1244+
false /* bypass_limits */, max_raw_tx_fee, /* test_accept */ true);
12451245
}
12461246
result_0.pushKV("allowed", test_accept_res);
12471247
if (!test_accept_res) {

src/test/txvalidation_tests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
4242
false,
4343
AcceptToMemoryPool(mempool, state, MakeTransactionRef(coinbaseTx),
4444
nullptr /* pfMissingInputs */,
45-
nullptr /* plTxnReplaced */,
4645
true /* bypass_limits */,
4746
0 /* nAbsurdFee */));
4847

src/test/txvalidationcache_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ToMemPool(CMutableTransaction& tx)
3030

3131
CValidationState state;
3232
return AcceptToMemoryPool(mempool, state, MakeTransactionRef(tx), nullptr /* pfMissingInputs */,
33-
nullptr /* plTxnReplaced */, true /* bypass_limits */, 0 /* nAbsurdFee */);
33+
true /* bypass_limits */, 0 /* nAbsurdFee */);
3434
}
3535

3636
BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)

src/txmempool.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,10 @@ bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry,
418418

419419
void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
420420
{
421-
if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT) {
422-
// BLOCK and CONFLICT callbacks are generated in removeForBlock
421+
if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT &&
422+
reason != MemPoolRemovalReason::REPLACED) {
423+
// BLOCK and CONFLICT callbacks are generated in removeForBlock; REPLACED
424+
// txn are included in TransactionAddedToMempool from AcceptToMemoryPool
423425
GetMainSignals().MempoolEntryRemoved(it->GetSharedTx(), reason);
424426
}
425427
const uint256 hash = it->GetTx().GetHash();

src/validation.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ void UpdateMempoolForReorg(DisconnectedBlockTransactions &disconnectpool, bool f
491491
CValidationState stateDummy;
492492
if (!fAddToMempool || (*it)->IsCoinBase() ||
493493
!AcceptToMemoryPool(mempool, stateDummy, *it, nullptr /* pfMissingInputs */,
494-
nullptr /* plTxnReplaced */, true /* bypass_limits */, 0 /* nAbsurdFee */)) {
494+
true /* bypass_limits */, 0 /* nAbsurdFee */)) {
495495
// If the transaction doesn't make it in to the mempool, remove any
496496
// transactions that depend on it (which would now be orphans).
497497
mempool.removeRecursive(**it, MemPoolRemovalReason::REORG);
@@ -551,8 +551,8 @@ static bool CheckInputsFromMempoolAndCache(const CTransaction& tx, CValidationSt
551551
}
552552

553553
static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool& pool, CValidationState& state, const CTransactionRef& ptx,
554-
bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
555-
bool bypass_limits, const CAmount& nAbsurdFee, std::vector<COutPoint>& coins_to_uncache, bool test_accept)
554+
bool* pfMissingInputs, int64_t nAcceptTime, bool bypass_limits,
555+
const CAmount& nAbsurdFee, std::vector<COutPoint>& coins_to_uncache, bool test_accept)
556556
{
557557
const CTransaction& tx = *ptx;
558558
const uint256 hash = tx.GetHash();
@@ -948,15 +948,16 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
948948
}
949949

950950
// Remove conflicting transactions from the mempool
951+
auto txn_replaced = std::make_shared<std::vector<CTransactionRef>>();
952+
txn_replaced->reserve(allConflicting.size());
951953
for (const CTxMemPool::txiter it : allConflicting)
952954
{
953955
LogPrint(BCLog::MEMPOOL, "replacing tx %s with %s for %s BTC additional fees, %d delta bytes\n",
954956
it->GetTx().GetHash().ToString(),
955957
hash.ToString(),
956958
FormatMoney(nModifiedFees - nConflictingFees),
957959
(int)nSize - (int)nConflictingSize);
958-
if (plTxnReplaced)
959-
plTxnReplaced->push_back(it->GetSharedTx());
960+
txn_replaced->push_back(it->GetSharedTx());
960961
}
961962
pool.RemoveStaged(allConflicting, false, MemPoolRemovalReason::REPLACED);
962963

@@ -977,18 +978,17 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
977978
return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool full");
978979
}
979980

980-
GetMainSignals().TransactionAddedToMempool(ptx);
981+
GetMainSignals().TransactionAddedToMempool(ptx, txn_replaced);
981982

982983
return true;
983984
}
984985

985986
/** (try to) add transaction to memory pool with a specified acceptance time **/
986987
static bool AcceptToMemoryPoolWithTime(const CChainParams& chainparams, CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx,
987-
bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
988-
bool bypass_limits, const CAmount nAbsurdFee, bool test_accept)
988+
bool* pfMissingInputs, int64_t nAcceptTime, bool bypass_limits, const CAmount nAbsurdFee, bool test_accept)
989989
{
990990
std::vector<COutPoint> coins_to_uncache;
991-
bool res = AcceptToMemoryPoolWorker(chainparams, pool, state, tx, pfMissingInputs, nAcceptTime, plTxnReplaced, bypass_limits, nAbsurdFee, coins_to_uncache, test_accept);
991+
bool res = AcceptToMemoryPoolWorker(chainparams, pool, state, tx, pfMissingInputs, nAcceptTime, bypass_limits, nAbsurdFee, coins_to_uncache, test_accept);
992992
if (!res) {
993993
for (const COutPoint& hashTx : coins_to_uncache)
994994
pcoinsTip->Uncache(hashTx);
@@ -1000,11 +1000,10 @@ static bool AcceptToMemoryPoolWithTime(const CChainParams& chainparams, CTxMemPo
10001000
}
10011001

10021002
bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx,
1003-
bool* pfMissingInputs, std::list<CTransactionRef>* plTxnReplaced,
1004-
bool bypass_limits, const CAmount nAbsurdFee, bool test_accept)
1003+
bool* pfMissingInputs, bool bypass_limits, const CAmount nAbsurdFee, bool test_accept)
10051004
{
10061005
const CChainParams& chainparams = Params();
1007-
return AcceptToMemoryPoolWithTime(chainparams, pool, state, tx, pfMissingInputs, GetTime(), plTxnReplaced, bypass_limits, nAbsurdFee, test_accept);
1006+
return AcceptToMemoryPoolWithTime(chainparams, pool, state, tx, pfMissingInputs, GetTime(), bypass_limits, nAbsurdFee, test_accept);
10081007
}
10091008

10101009
/**
@@ -4587,8 +4586,7 @@ bool LoadMempool(void)
45874586
if (nTime + nExpiryTimeout > nNow) {
45884587
LOCK(cs_main);
45894588
AcceptToMemoryPoolWithTime(chainparams, mempool, state, tx, nullptr /* pfMissingInputs */, nTime,
4590-
nullptr /* plTxnReplaced */, false /* bypass_limits */, 0 /* nAbsurdFee */,
4591-
false /* test_accept */);
4589+
false /* bypass_limits */, 0 /* nAbsurdFee */, false /* test_accept */);
45924590
if (state.IsValid()) {
45934591
++count;
45944592
} else {

src/validation.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ void PruneBlockFilesManual(int nManualPruneHeight);
308308
/** (try to) add transaction to memory pool
309309
* plTxnReplaced will be appended to with all transactions replaced from mempool **/
310310
bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx,
311-
bool* pfMissingInputs, std::list<CTransactionRef>* plTxnReplaced,
312-
bool bypass_limits, const CAmount nAbsurdFee, bool test_accept=false);
311+
bool* pfMissingInputs, bool bypass_limits, const CAmount nAbsurdFee, bool test_accept=false);
313312

314313
/** Convert CValidationState to a human-readable message for logging */
315314
std::string FormatStateMessage(const CValidationState &state);

src/validationinterface.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
struct MainSignalsInstance {
2323
boost::signals2::signal<void (const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)> UpdatedBlockTip;
24-
boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
24+
boost::signals2::signal<void (const CTransactionRef &, const std::vector<CTransactionRef> &)> TransactionAddedToMempool;
2525
boost::signals2::signal<void (const std::vector<CTransactionRef> &, const std::vector<CTransactionRef> &)> MempoolUpdatedForBlockConnect;
2626
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex)> BlockConnected;
2727
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
@@ -79,7 +79,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) {
7979
}
8080

8181
void RegisterMempoolInterface(MempoolInterface* listener) {
82-
g_signals.m_internals->TransactionAddedToMempool.connect(boost::bind(&MempoolInterface::TransactionAddedToMempool, listener, _1));
82+
g_signals.m_internals->TransactionAddedToMempool.connect(boost::bind(&MempoolInterface::TransactionAddedToMempool, listener, _1, _2));
8383
g_signals.m_internals->TransactionRemovedFromMempool.connect(boost::bind(&MempoolInterface::TransactionRemovedFromMempool, listener, _1, _2));
8484
g_signals.m_internals->MempoolUpdatedForBlockConnect.connect(boost::bind(&MempoolInterface::MempoolUpdatedForBlockConnect, listener, _1, _2));
8585
}
@@ -96,7 +96,7 @@ void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
9696
}
9797

9898
void UnregisterMempoolInterface(MempoolInterface* listener) {
99-
g_signals.m_internals->TransactionAddedToMempool.disconnect(boost::bind(&MempoolInterface::TransactionAddedToMempool, listener, _1));
99+
g_signals.m_internals->TransactionAddedToMempool.disconnect(boost::bind(&MempoolInterface::TransactionAddedToMempool, listener, _1, _2));
100100
g_signals.m_internals->TransactionRemovedFromMempool.disconnect(boost::bind(&MempoolInterface::TransactionRemovedFromMempool, listener, _1, _2));
101101
g_signals.m_internals->MempoolUpdatedForBlockConnect.disconnect(boost::bind(&MempoolInterface::MempoolUpdatedForBlockConnect, listener, _1, _2));
102102
}
@@ -156,9 +156,9 @@ void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockInd
156156
});
157157
}
158158

159-
void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
160-
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
161-
m_internals->TransactionAddedToMempool(ptx);
159+
void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx, const std::shared_ptr<std::vector<CTransactionRef>>& txn_replaced) {
160+
m_internals->m_schedulerClient.AddToProcessQueue([ptx, txn_replaced, this] {
161+
m_internals->TransactionAddedToMempool(ptx, *txn_replaced);
162162
});
163163
}
164164

0 commit comments

Comments
 (0)