Skip to content

Commit

Permalink
CValidationInterface: modify TransactionAddedToMempool callback par…
Browse files Browse the repository at this point in the history
…ameter

Creates a structure `NewMempoolTransactionInfo` which will contain a tx and additional informations
about the tx that we need for fee estimation.

Changes the `TransactionAddedToMempool` tx parameter type  to `NewMempoolTransactionInfo`.

This added informations will enable fee estimator to update from `CValidationInterface`'s
`TransactionAddedToMempool` notifications.

Co-authored-by: Matt Corallo <git@bluematt.me>
  • Loading branch information
ismaelsadeeq and TheBlueMatt committed Oct 14, 2023
1 parent 4986edb commit 1d116df
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 19 deletions.
17 changes: 17 additions & 0 deletions src/kernel/mempool_entry.h
Expand Up @@ -176,4 +176,21 @@ class CTxMemPoolEntry
mutable Epoch::Marker m_epoch_marker; //!< epoch when last touched, useful for graph algorithms
};

struct NewMempoolTransactionInfo {
CTransactionRef m_tx;
/* The fee the added transaction paid */
CAmount m_fee;
/**
* The virtual transaction size.
*
* This is a policy field which considers the sigop cost of the
* transaction as well as its weight, and reinterprets it as bytes.
*
* It is the primary metric by which the mining algorithm selects
* transactions.
*/
int64_t m_virtual_transaction_size;
unsigned int txHeight;
};

#endif // BITCOIN_KERNEL_MEMPOOL_ENTRY_H
4 changes: 2 additions & 2 deletions src/node/interfaces.cpp
Expand Up @@ -426,9 +426,9 @@ class NotificationsProxy : public CValidationInterface
explicit NotificationsProxy(std::shared_ptr<Chain::Notifications> notifications)
: m_notifications(std::move(notifications)) {}
virtual ~NotificationsProxy() = default;
void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) override
void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx_info, uint64_t mempool_sequence) override
{
m_notifications->transactionAddedToMempool(tx);
m_notifications->transactionAddedToMempool(tx_info.m_tx);
}
void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override
{
Expand Down
9 changes: 6 additions & 3 deletions src/test/fuzz/package_eval.cpp
Expand Up @@ -20,6 +20,8 @@

using node::NodeContext;

struct NewMempoolTransactionInfo;

namespace {

const TestingSetup* g_setup;
Expand Down Expand Up @@ -55,11 +57,12 @@ struct OutpointsUpdater final : public CValidationInterface {
explicit OutpointsUpdater(std::set<COutPoint>& r)
: m_mempool_outpoints{r} {}

void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t /* mempool_sequence */) override
void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx_info, uint64_t /* mempool_sequence */) override
{
// for coins spent we always want to be able to rbf so they're not removed

// outputs from this tx can now be spent
const CTransactionRef& tx = tx_info.m_tx;
for (uint32_t index{0}; index < tx->vout.size(); ++index) {
m_mempool_outpoints.insert(COutPoint{tx->GetHash(), index});
}
Expand All @@ -85,10 +88,10 @@ struct TransactionsDelta final : public CValidationInterface {
explicit TransactionsDelta(std::set<CTransactionRef>& a)
: m_added{a} {}

void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t /* mempool_sequence */) override
void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx_info, uint64_t /* mempool_sequence */) override
{
// Transactions may be entered and booted any number of times
m_added.insert(tx);
m_added.insert(tx_info.m_tx);
}

void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
Expand Down
6 changes: 4 additions & 2 deletions src/test/fuzz/tx_pool.cpp
Expand Up @@ -21,6 +21,8 @@
using node::BlockAssembler;
using node::NodeContext;

struct NewMempoolTransactionInfo;

namespace {

const TestingSetup* g_setup;
Expand Down Expand Up @@ -59,9 +61,9 @@ struct TransactionsDelta final : public CValidationInterface {
explicit TransactionsDelta(std::set<CTransactionRef>& r, std::set<CTransactionRef>& a)
: m_removed{r}, m_added{a} {}

void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t /* mempool_sequence */) override
void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx_info, uint64_t /* mempool_sequence */) override
{
Assert(m_added.insert(tx).second);
Assert(m_added.insert(tx_info.m_tx).second);
}

void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
Expand Down
14 changes: 12 additions & 2 deletions src/validation.cpp
Expand Up @@ -1214,7 +1214,12 @@ bool MemPoolAccept::SubmitPackage(const ATMPArgs& args, std::vector<Workspace>&
results.emplace(ws.m_ptx->GetWitnessHash(),
MempoolAcceptResult::Success(std::move(ws.m_replaced_transactions), ws.m_vsize,
ws.m_base_fees, effective_feerate, effective_feerate_wtxids));
GetMainSignals().TransactionAddedToMempool(ws.m_ptx, m_pool.GetAndIncrementSequence());
NewMempoolTransactionInfo tx_info = {
ws.m_ptx,
ws.m_base_fees,
ws.m_vsize,
ws.m_entry->GetHeight()};
GetMainSignals().TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());
}
return all_submitted;
}
Expand Down Expand Up @@ -1246,7 +1251,12 @@ MempoolAcceptResult MemPoolAccept::AcceptSingleTransaction(const CTransactionRef

if (!Finalize(args, ws)) return MempoolAcceptResult::Failure(ws.m_state);

GetMainSignals().TransactionAddedToMempool(ptx, m_pool.GetAndIncrementSequence());
NewMempoolTransactionInfo tx_info = {
ws.m_ptx,
ws.m_base_fees,
ws.m_vsize,
ws.m_entry->GetHeight()};
GetMainSignals().TransactionAddedToMempool(tx_info, m_pool.GetAndIncrementSequence());

return MempoolAcceptResult::Success(std::move(ws.m_replaced_transactions), ws.m_vsize, ws.m_base_fees,
effective_feerate, single_wtxid);
Expand Down
12 changes: 7 additions & 5 deletions src/validationinterface.cpp
Expand Up @@ -9,6 +9,7 @@
#include <chain.h>
#include <consensus/validation.h>
#include <kernel/chain.h>
#include <kernel/mempool_entry.h>
#include <logging.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
Expand Down Expand Up @@ -205,13 +206,14 @@ void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockInd
fInitialDownload);
}

void CMainSignals::TransactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) {
auto event = [tx, mempool_sequence, this] {
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionAddedToMempool(tx, mempool_sequence); });
void CMainSignals::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx_info, uint64_t mempool_sequence)
{
auto event = [tx_info, mempool_sequence, this] {
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionAddedToMempool(tx_info, mempool_sequence); });
};
ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
tx->GetHash().ToString(),
tx->GetWitnessHash().ToString());
tx_info.m_tx->GetHash().ToString(),
tx_info.m_tx->GetWitnessHash().ToString());
}

void CMainSignals::MempoolTransactionsRemovedForConnectedBlock(const std::vector<CTransactionRef>& txs_removed_for_block, unsigned int nBlockHeight)
Expand Down
5 changes: 3 additions & 2 deletions src/validationinterface.h
Expand Up @@ -21,6 +21,7 @@ struct CBlockLocator;
class CValidationInterface;
class CScheduler;
enum class MemPoolRemovalReason;
struct NewMempoolTransactionInfo;

/** Register subscriber */
void RegisterValidationInterface(CValidationInterface* callbacks);
Expand Down Expand Up @@ -96,7 +97,7 @@ class CValidationInterface {
*
* Called on a background thread.
*/
virtual void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) {}
virtual void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx_info, uint64_t mempool_sequence) {}

/**
* Notifies listeners of a transaction leaving mempool.
Expand Down Expand Up @@ -209,7 +210,7 @@ class CMainSignals {


void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
void TransactionAddedToMempool(const CTransactionRef&, uint64_t mempool_sequence);
void TransactionAddedToMempool(const NewMempoolTransactionInfo&, uint64_t mempool_sequence);
void TransactionRemovedFromMempool(const CTransactionRef&, MemPoolRemovalReason, uint64_t mempool_sequence);
void MempoolTransactionsRemovedForConnectedBlock(const std::vector<CTransactionRef>&, unsigned int nBlockHeight);
void BlockConnected(ChainstateRole, const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex);
Expand Down
6 changes: 4 additions & 2 deletions src/zmq/zmqnotificationinterface.cpp
Expand Up @@ -6,6 +6,7 @@

#include <common/args.h>
#include <kernel/chain.h>
#include <kernel/mempool_entry.h>
#include <logging.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
Expand Down Expand Up @@ -152,9 +153,10 @@ void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, co
});
}

void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef& ptx, uint64_t mempool_sequence)
void CZMQNotificationInterface::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx_info, uint64_t mempool_sequence)
{
const CTransaction& tx = *ptx;
const CTransactionRef& ptx = tx_info.m_tx;
const CTransaction& tx = *(ptx);

TryForEachAndRemoveFailed(notifiers, [&tx, mempool_sequence](CZMQAbstractNotifier* notifier) {
return notifier->NotifyTransaction(tx) && notifier->NotifyTransactionAcceptance(tx, mempool_sequence);
Expand Down
3 changes: 2 additions & 1 deletion src/zmq/zmqnotificationinterface.h
Expand Up @@ -16,6 +16,7 @@
class CBlock;
class CBlockIndex;
class CZMQAbstractNotifier;
struct NewMempoolTransactionInfo;

class CZMQNotificationInterface final : public CValidationInterface
{
Expand All @@ -31,7 +32,7 @@ class CZMQNotificationInterface final : public CValidationInterface
void Shutdown();

// CValidationInterface
void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) override;
void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx_info, uint64_t mempool_sequence) override;
void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override;
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected) override;
Expand Down

0 comments on commit 1d116df

Please sign in to comment.