txgraph: Introduce TxGraphImpl observer tracking (preparation)

This is preparation for a next commit which will introduce a class whose
objects hold references to internals in TxGraphImpl, which disallows
modifications to the graph while such objects exist.
This commit is contained in:
Pieter Wuille 2024-11-25 11:31:02 -05:00
parent 63e44512e2
commit 9c2af16cd7

View file

@ -308,6 +308,8 @@ private:
/** Index of ChunkData objects, indexing the last transaction in each chunk in the main
* graph. */
ChunkIndex m_main_chunkindex;
/** Number of index-observing objects in existence. */
size_t m_main_chunkindex_observers{0};
/** A Locator that describes whether, where, and in which Cluster an Entry appears.
* Every Entry has MAX_LEVELS locators, as it may appear in one Cluster per level.
@ -443,6 +445,7 @@ public:
{
auto& entry = m_entries[idx];
Assume(entry.m_ref != nullptr);
Assume(m_main_chunkindex_observers == 0 || !entry.m_locator[0].IsPresent());
entry.m_ref = nullptr;
// Mark the transaction as to be removed in all levels where it explicitly or implicitly
// exists.
@ -571,6 +574,7 @@ void TxGraphImpl::ClearLocator(int level, GraphIndex idx) noexcept
}
}
if (level == 0 && entry.m_main_chunkindex_iterator != m_main_chunkindex.end()) {
Assume(m_main_chunkindex_observers == 0);
m_main_chunkindex.erase(entry.m_main_chunkindex_iterator);
entry.m_main_chunkindex_iterator = m_main_chunkindex.end();
}
@ -584,6 +588,7 @@ void Cluster::Updated(TxGraphImpl& graph) noexcept
if (m_level == 0 && entry.m_main_chunkindex_iterator != graph.m_main_chunkindex.end()) {
// Destroy any potential ChunkData prior to modifying the Cluster (as that could
// invalidate its ordering).
Assume(graph.m_main_chunkindex_observers == 0);
graph.m_main_chunkindex.erase(entry.m_main_chunkindex_iterator);
entry.m_main_chunkindex_iterator = graph.m_main_chunkindex.end();
}
@ -877,6 +882,7 @@ void Cluster::Merge(TxGraphImpl& graph, Cluster& other) noexcept
if (m_level == 0 && entry.m_main_chunkindex_iterator != graph.m_main_chunkindex.end()) {
// Destroy any potential ChunkData prior to modifying the Cluster (as that could
// invalidate its ordering).
Assume(graph.m_main_chunkindex_observers == 0);
graph.m_main_chunkindex.erase(entry.m_main_chunkindex_iterator);
entry.m_main_chunkindex_iterator = graph.m_main_chunkindex.end();
}
@ -1501,6 +1507,7 @@ Cluster::Cluster(uint64_t sequence, TxGraphImpl& graph, const FeePerWeight& feer
TxGraph::Ref TxGraphImpl::AddTransaction(const FeePerWeight& feerate) noexcept
{
Assume(m_main_chunkindex_observers == 0 || GetTopLevel() != 0);
// Construct a new Ref.
Ref ret;
// Construct a new Entry, and link it with the Ref.
@ -1529,6 +1536,7 @@ void TxGraphImpl::RemoveTransaction(const Ref& arg) noexcept
// having been removed).
if (GetRefGraph(arg) == nullptr) return;
Assume(GetRefGraph(arg) == this);
Assume(m_main_chunkindex_observers == 0 || GetTopLevel() != 0);
// Find the Cluster the transaction is in, and stop if it isn't in any.
int level = GetTopLevel();
auto cluster = FindCluster(GetRefIndex(arg), level);
@ -1547,6 +1555,7 @@ void TxGraphImpl::AddDependency(const Ref& parent, const Ref& child) noexcept
// removed).
if (GetRefGraph(parent) == nullptr || GetRefGraph(child) == nullptr) return;
Assume(GetRefGraph(parent) == this && GetRefGraph(child) == this);
Assume(m_main_chunkindex_observers == 0 || GetTopLevel() != 0);
// Don't do anything if this is a dependency on self.
if (GetRefIndex(parent) == GetRefIndex(child)) return;
// Find the Cluster the parent and child transaction are in, and stop if either appears to be
@ -1885,6 +1894,7 @@ void TxGraphImpl::CommitStaging() noexcept
{
// Staging must exist.
Assume(m_staging_clusterset.has_value());
Assume(m_main_chunkindex_observers == 0);
// Delete all conflicting Clusters in main, to make place for moving the staging ones
// there. All of these have been copied to staging in PullIn().
auto conflicts = GetConflicts();
@ -1937,6 +1947,7 @@ void TxGraphImpl::SetTransactionFee(const Ref& ref, int64_t fee) noexcept
// Don't do anything if the passed Ref is empty.
if (GetRefGraph(ref) == nullptr) return;
Assume(GetRefGraph(ref) == this);
Assume(m_main_chunkindex_observers == 0);
// Find the entry, its locator, and inform its Cluster about the new feerate, if any.
auto& entry = m_entries[GetRefIndex(ref)];
for (int level = 0; level < MAX_LEVELS; ++level) {
@ -2245,7 +2256,9 @@ void TxGraphImpl::SanityCheck() const
void TxGraphImpl::DoWork() noexcept
{
for (int level = 0; level <= GetTopLevel(); ++level) {
MakeAllAcceptable(level);
if (level > 0 || m_main_chunkindex_observers == 0) {
MakeAllAcceptable(level);
}
}
}