2023-12-15 12:17:23 +00:00
|
|
|
// Copyright (c) 2022 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2024-06-11 13:06:21 +01:00
|
|
|
#include <policy/truc_policy.h>
|
2023-12-15 12:17:23 +00:00
|
|
|
|
|
|
|
#include <coins.h>
|
|
|
|
#include <consensus/amount.h>
|
|
|
|
#include <logging.h>
|
|
|
|
#include <tinyformat.h>
|
|
|
|
#include <util/check.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <numeric>
|
|
|
|
#include <vector>
|
|
|
|
|
2024-06-11 14:01:34 +01:00
|
|
|
/** Helper for PackageTRUCChecks: Returns a vector containing the indices of transactions (within
|
2023-12-15 12:17:23 +00:00
|
|
|
* package) that are direct parents of ptx. */
|
|
|
|
std::vector<size_t> FindInPackageParents(const Package& package, const CTransactionRef& ptx)
|
|
|
|
{
|
|
|
|
std::vector<size_t> in_package_parents;
|
|
|
|
|
|
|
|
std::set<Txid> possible_parents;
|
|
|
|
for (auto &input : ptx->vin) {
|
|
|
|
possible_parents.insert(input.prevout.hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i{0}; i < package.size(); ++i) {
|
|
|
|
const auto& tx = package.at(i);
|
|
|
|
// We assume the package is sorted, so that we don't need to continue
|
|
|
|
// looking past the transaction itself.
|
|
|
|
if (&(*tx) == &(*ptx)) break;
|
|
|
|
if (possible_parents.count(tx->GetHash())) {
|
|
|
|
in_package_parents.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return in_package_parents;
|
|
|
|
}
|
|
|
|
|
2024-06-11 14:01:34 +01:00
|
|
|
/** Helper for PackageTRUCChecks, storing info for a mempool or package parent. */
|
2023-12-15 12:17:23 +00:00
|
|
|
struct ParentInfo {
|
|
|
|
/** Txid used to identify this parent by prevout */
|
|
|
|
const Txid& m_txid;
|
|
|
|
/** Wtxid used for debug string */
|
|
|
|
const Wtxid& m_wtxid;
|
2024-06-11 13:37:05 +01:00
|
|
|
/** version used to check inheritance of TRUC and non-TRUC */
|
2024-01-26 15:27:13 -05:00
|
|
|
decltype(CTransaction::version) m_version;
|
2023-12-15 12:17:23 +00:00
|
|
|
/** If parent is in mempool, whether it has any descendants in mempool. */
|
|
|
|
bool m_has_mempool_descendant;
|
|
|
|
|
|
|
|
ParentInfo() = delete;
|
2024-01-26 15:27:13 -05:00
|
|
|
ParentInfo(const Txid& txid, const Wtxid& wtxid, decltype(CTransaction::version) version, bool has_mempool_descendant) :
|
2023-12-15 12:17:23 +00:00
|
|
|
m_txid{txid}, m_wtxid{wtxid}, m_version{version},
|
|
|
|
m_has_mempool_descendant{has_mempool_descendant}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2024-06-11 14:01:34 +01:00
|
|
|
std::optional<std::string> PackageTRUCChecks(const CTransactionRef& ptx, int64_t vsize,
|
2023-12-15 12:17:23 +00:00
|
|
|
const Package& package,
|
|
|
|
const CTxMemPool::setEntries& mempool_ancestors)
|
|
|
|
{
|
|
|
|
// This function is specialized for these limits, and must be reimplemented if they ever change.
|
2024-06-11 14:01:34 +01:00
|
|
|
static_assert(TRUC_ANCESTOR_LIMIT == 2);
|
|
|
|
static_assert(TRUC_DESCENDANT_LIMIT == 2);
|
2023-12-15 12:17:23 +00:00
|
|
|
|
|
|
|
const auto in_package_parents{FindInPackageParents(package, ptx)};
|
|
|
|
|
2024-06-11 13:37:05 +01:00
|
|
|
// Now we have all ancestors, so we can start checking TRUC rules.
|
2024-01-26 15:27:13 -05:00
|
|
|
if (ptx->version == TRUC_VERSION) {
|
2024-06-11 14:01:34 +01:00
|
|
|
// SingleTRUCChecks should have checked this already.
|
|
|
|
if (!Assume(vsize <= TRUC_MAX_VSIZE)) {
|
2024-07-02 11:56:13 +01:00
|
|
|
return strprintf("version=3 tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
|
2024-06-11 14:01:34 +01:00
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_MAX_VSIZE);
|
2024-04-15 11:45:15 +01:00
|
|
|
}
|
|
|
|
|
2024-06-11 14:01:34 +01:00
|
|
|
if (mempool_ancestors.size() + in_package_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
|
2023-12-15 12:17:23 +00:00
|
|
|
return strprintf("tx %s (wtxid=%s) would have too many ancestors",
|
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool has_parent{mempool_ancestors.size() + in_package_parents.size() > 0};
|
|
|
|
if (has_parent) {
|
2024-06-11 13:37:05 +01:00
|
|
|
// A TRUC child cannot be too large.
|
2024-06-11 14:01:34 +01:00
|
|
|
if (vsize > TRUC_CHILD_MAX_VSIZE) {
|
2024-07-02 11:56:13 +01:00
|
|
|
return strprintf("version=3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
|
2023-12-15 12:17:23 +00:00
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
|
2024-06-11 14:01:34 +01:00
|
|
|
vsize, TRUC_CHILD_MAX_VSIZE);
|
2023-12-15 12:17:23 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 22:06:26 +00:00
|
|
|
// Exactly 1 parent exists, either in mempool or package. Find it.
|
2023-12-15 12:17:23 +00:00
|
|
|
const auto parent_info = [&] {
|
|
|
|
if (mempool_ancestors.size() > 0) {
|
|
|
|
auto& mempool_parent = *mempool_ancestors.begin();
|
|
|
|
return ParentInfo{mempool_parent->GetTx().GetHash(),
|
|
|
|
mempool_parent->GetTx().GetWitnessHash(),
|
2024-01-26 15:27:13 -05:00
|
|
|
mempool_parent->GetTx().version,
|
2023-12-15 12:17:23 +00:00
|
|
|
/*has_mempool_descendant=*/mempool_parent->GetCountWithDescendants() > 1};
|
|
|
|
} else {
|
|
|
|
auto& parent_index = in_package_parents.front();
|
|
|
|
auto& package_parent = package.at(parent_index);
|
|
|
|
return ParentInfo{package_parent->GetHash(),
|
|
|
|
package_parent->GetWitnessHash(),
|
2024-01-26 15:27:13 -05:00
|
|
|
package_parent->version,
|
2023-12-15 12:17:23 +00:00
|
|
|
/*has_mempool_descendant=*/false};
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
// If there is a parent, it must have the right version.
|
2024-05-23 14:39:04 +01:00
|
|
|
if (parent_info.m_version != TRUC_VERSION) {
|
2024-07-02 11:56:13 +01:00
|
|
|
return strprintf("version=3 tx %s (wtxid=%s) cannot spend from non-version=3 tx %s (wtxid=%s)",
|
2023-12-15 12:17:23 +00:00
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
|
|
|
|
parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& package_tx : package) {
|
|
|
|
// Skip same tx.
|
|
|
|
if (&(*package_tx) == &(*ptx)) continue;
|
|
|
|
|
|
|
|
for (auto& input : package_tx->vin) {
|
|
|
|
// Fail if we find another tx with the same parent. We don't check whether the
|
2024-06-11 14:01:34 +01:00
|
|
|
// sibling is to-be-replaced (done in SingleTRUCChecks) because these transactions
|
2023-12-15 12:17:23 +00:00
|
|
|
// are within the same package.
|
|
|
|
if (input.prevout.hash == parent_info.m_txid) {
|
|
|
|
return strprintf("tx %s (wtxid=%s) would exceed descendant count limit",
|
|
|
|
parent_info.m_txid.ToString(),
|
|
|
|
parent_info.m_wtxid.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This tx can't have both a parent and an in-package child.
|
|
|
|
if (input.prevout.hash == ptx->GetHash()) {
|
|
|
|
return strprintf("tx %s (wtxid=%s) would have too many ancestors",
|
|
|
|
package_tx->GetHash().ToString(), package_tx->GetWitnessHash().ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 15:28:37 -04:00
|
|
|
if (parent_info.m_has_mempool_descendant) {
|
2024-02-14 14:15:48 +00:00
|
|
|
return strprintf("tx %s (wtxid=%s) would exceed descendant count limit",
|
|
|
|
parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
|
2023-12-15 12:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-06-11 13:37:05 +01:00
|
|
|
// Non-TRUC transactions cannot have TRUC parents.
|
2023-12-15 12:17:23 +00:00
|
|
|
for (auto it : mempool_ancestors) {
|
2024-01-26 15:27:13 -05:00
|
|
|
if (it->GetTx().version == TRUC_VERSION) {
|
2024-07-02 11:56:13 +01:00
|
|
|
return strprintf("non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
|
2023-12-15 12:17:23 +00:00
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
|
|
|
|
it->GetSharedTx()->GetHash().ToString(), it->GetSharedTx()->GetWitnessHash().ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto& index: in_package_parents) {
|
2024-01-26 15:27:13 -05:00
|
|
|
if (package.at(index)->version == TRUC_VERSION) {
|
2024-07-02 11:56:13 +01:00
|
|
|
return strprintf("non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
|
2023-12-15 12:17:23 +00:00
|
|
|
ptx->GetHash().ToString(),
|
|
|
|
ptx->GetWitnessHash().ToString(),
|
|
|
|
package.at(index)->GetHash().ToString(),
|
|
|
|
package.at(index)->GetWitnessHash().ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2024-06-11 14:01:34 +01:00
|
|
|
std::optional<std::pair<std::string, CTransactionRef>> SingleTRUCChecks(const CTransactionRef& ptx,
|
2023-12-15 12:17:23 +00:00
|
|
|
const CTxMemPool::setEntries& mempool_ancestors,
|
|
|
|
const std::set<Txid>& direct_conflicts,
|
|
|
|
int64_t vsize)
|
|
|
|
{
|
2024-06-11 13:37:05 +01:00
|
|
|
// Check TRUC and non-TRUC inheritance.
|
2023-12-15 12:17:23 +00:00
|
|
|
for (const auto& entry : mempool_ancestors) {
|
2024-01-26 15:27:13 -05:00
|
|
|
if (ptx->version != TRUC_VERSION && entry->GetTx().version == TRUC_VERSION) {
|
2024-07-02 11:56:13 +01:00
|
|
|
return std::make_pair(strprintf("non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
|
2023-12-15 12:17:23 +00:00
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
|
2024-01-23 10:30:39 +00:00
|
|
|
entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
|
|
|
|
nullptr);
|
2024-01-26 15:27:13 -05:00
|
|
|
} else if (ptx->version == TRUC_VERSION && entry->GetTx().version != TRUC_VERSION) {
|
2024-07-02 11:56:13 +01:00
|
|
|
return std::make_pair(strprintf("version=3 tx %s (wtxid=%s) cannot spend from non-version=3 tx %s (wtxid=%s)",
|
2023-12-15 12:17:23 +00:00
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
|
2024-01-23 10:30:39 +00:00
|
|
|
entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
|
|
|
|
nullptr);
|
2023-12-15 12:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is specialized for these limits, and must be reimplemented if they ever change.
|
2024-06-11 14:01:34 +01:00
|
|
|
static_assert(TRUC_ANCESTOR_LIMIT == 2);
|
|
|
|
static_assert(TRUC_DESCENDANT_LIMIT == 2);
|
2023-12-15 12:17:23 +00:00
|
|
|
|
2024-01-26 15:27:13 -05:00
|
|
|
// The rest of the rules only apply to transactions with version=3.
|
|
|
|
if (ptx->version != TRUC_VERSION) return std::nullopt;
|
2023-12-15 12:17:23 +00:00
|
|
|
|
2024-06-11 14:01:34 +01:00
|
|
|
if (vsize > TRUC_MAX_VSIZE) {
|
2024-07-02 11:56:13 +01:00
|
|
|
return std::make_pair(strprintf("version=3 tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
|
2024-06-11 14:01:34 +01:00
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_MAX_VSIZE),
|
2024-04-15 11:45:15 +01:00
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
2024-06-11 14:01:34 +01:00
|
|
|
// Check that TRUC_ANCESTOR_LIMIT would not be violated.
|
|
|
|
if (mempool_ancestors.size() + 1 > TRUC_ANCESTOR_LIMIT) {
|
2024-01-23 10:30:39 +00:00
|
|
|
return std::make_pair(strprintf("tx %s (wtxid=%s) would have too many ancestors",
|
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString()),
|
|
|
|
nullptr);
|
2023-12-15 12:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remaining checks only pertain to transactions with unconfirmed ancestors.
|
|
|
|
if (mempool_ancestors.size() > 0) {
|
2024-06-11 13:37:05 +01:00
|
|
|
// If this transaction spends TRUC parents, it cannot be too large.
|
2024-06-11 14:01:34 +01:00
|
|
|
if (vsize > TRUC_CHILD_MAX_VSIZE) {
|
2024-07-02 11:56:13 +01:00
|
|
|
return std::make_pair(strprintf("version=3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
|
2024-06-11 14:01:34 +01:00
|
|
|
ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_CHILD_MAX_VSIZE),
|
2024-01-23 10:30:39 +00:00
|
|
|
nullptr);
|
2023-12-15 12:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the descendant counts of in-mempool ancestors.
|
|
|
|
const auto& parent_entry = *mempool_ancestors.begin();
|
|
|
|
// If there are any ancestors, this is the only child allowed. The parent cannot have any
|
|
|
|
// other descendants. We handle the possibility of multiple children as that case is
|
|
|
|
// possible through a reorg.
|
|
|
|
const auto& children = parent_entry->GetMemPoolChildrenConst();
|
|
|
|
// Don't double-count a transaction that is going to be replaced. This logic assumes that
|
2024-06-11 13:37:05 +01:00
|
|
|
// any descendant of the TRUC transaction is a direct child, which makes sense because a
|
|
|
|
// TRUC transaction can only have 1 descendant.
|
2023-12-15 12:17:23 +00:00
|
|
|
const bool child_will_be_replaced = !children.empty() &&
|
|
|
|
std::any_of(children.cbegin(), children.cend(),
|
|
|
|
[&direct_conflicts](const CTxMemPoolEntry& child){return direct_conflicts.count(child.GetTx().GetHash()) > 0;});
|
2024-06-11 14:01:34 +01:00
|
|
|
if (parent_entry->GetCountWithDescendants() + 1 > TRUC_DESCENDANT_LIMIT && !child_will_be_replaced) {
|
2024-06-11 13:37:05 +01:00
|
|
|
// Allow sibling eviction for TRUC transaction: if another child already exists, even if
|
|
|
|
// we don't conflict inputs with it, consider evicting it under RBF rules. We rely on TRUC rules
|
2024-02-14 14:15:48 +00:00
|
|
|
// only permitting 1 descendant, as otherwise we would need to have logic for deciding
|
|
|
|
// which descendant to evict. Skip if this isn't true, e.g. if the transaction has
|
|
|
|
// multiple children or the sibling also has descendants due to a reorg.
|
|
|
|
const bool consider_sibling_eviction{parent_entry->GetCountWithDescendants() == 2 &&
|
|
|
|
children.begin()->get().GetCountWithAncestors() == 2};
|
|
|
|
|
|
|
|
// Return the sibling if its eviction can be considered. Provide the "descendant count
|
|
|
|
// limit" string either way, as the caller may decide not to do sibling eviction.
|
2024-01-23 10:30:39 +00:00
|
|
|
return std::make_pair(strprintf("tx %u (wtxid=%s) would exceed descendant count limit",
|
2024-02-14 14:15:48 +00:00
|
|
|
parent_entry->GetSharedTx()->GetHash().ToString(),
|
|
|
|
parent_entry->GetSharedTx()->GetWitnessHash().ToString()),
|
|
|
|
consider_sibling_eviction ? children.begin()->get().GetSharedTx() : nullptr);
|
2023-12-15 12:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|