2013-08-27 07:51:57 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-11-17 03:29:09 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-08-27 07:51:57 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "txmempool.h"
|
2014-09-14 12:43:56 +02:00
|
|
|
|
2014-10-29 02:33:23 +01:00
|
|
|
#include "clientversion.h"
|
2015-01-24 15:29:29 +01:00
|
|
|
#include "consensus/consensus.h"
|
2015-01-24 15:57:12 +01:00
|
|
|
#include "consensus/validation.h"
|
2016-12-02 01:06:41 +01:00
|
|
|
#include "validation.h"
|
2016-01-03 18:54:50 +01:00
|
|
|
#include "policy/policy.h"
|
2014-08-26 22:28:32 +02:00
|
|
|
#include "policy/fees.h"
|
2014-10-22 21:08:30 +02:00
|
|
|
#include "streams.h"
|
2015-08-27 03:58:17 +02:00
|
|
|
#include "timedata.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include "util.h"
|
2014-04-23 00:46:19 +02:00
|
|
|
#include "utilmoneystr.h"
|
2015-10-02 23:19:55 +02:00
|
|
|
#include "utiltime.h"
|
2014-09-25 05:32:36 +02:00
|
|
|
#include "version.h"
|
2013-08-27 07:51:57 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
|
2015-11-13 16:05:21 +01:00
|
|
|
int64_t _nTime, double _entryPriority, unsigned int _entryHeight,
|
2015-10-29 19:06:13 +01:00
|
|
|
bool poolHasNoInputsOf, CAmount _inChainInputValue,
|
2016-01-03 18:54:50 +01:00
|
|
|
bool _spendsCoinbase, int64_t _sigOpsCost, LockPoints lp):
|
2016-11-11 02:34:17 +01:00
|
|
|
tx(MakeTransactionRef(_tx)), nFee(_nFee), nTime(_nTime), entryPriority(_entryPriority), entryHeight(_entryHeight),
|
2015-10-29 19:06:13 +01:00
|
|
|
hadNoDependencies(poolHasNoInputsOf), inChainInputValue(_inChainInputValue),
|
2016-01-03 18:54:50 +01:00
|
|
|
spendsCoinbase(_spendsCoinbase), sigOpCost(_sigOpsCost), lockPoints(lp)
|
2013-11-11 08:35:14 +01:00
|
|
|
{
|
2016-07-18 19:28:26 +02:00
|
|
|
nTxWeight = GetTransactionWeight(_tx);
|
2016-01-03 18:54:50 +01:00
|
|
|
nModSize = _tx.CalculateModifiedSize(GetTxSize());
|
2016-05-30 17:06:24 +02:00
|
|
|
nUsageSize = RecursiveDynamicUsage(*tx) + memusage::DynamicUsage(tx);
|
2015-07-15 20:47:45 +02:00
|
|
|
|
|
|
|
nCountWithDescendants = 1;
|
2016-01-03 18:54:50 +01:00
|
|
|
nSizeWithDescendants = GetTxSize();
|
2015-11-19 17:18:28 +01:00
|
|
|
nModFeesWithDescendants = nFee;
|
2016-05-30 17:06:24 +02:00
|
|
|
CAmount nValueIn = _tx.GetValueOut()+nFee;
|
2015-11-13 16:05:21 +01:00
|
|
|
assert(inChainInputValue <= nValueIn);
|
2015-10-26 19:06:06 +01:00
|
|
|
|
|
|
|
feeDelta = 0;
|
2015-10-19 16:54:28 +02:00
|
|
|
|
|
|
|
nCountWithAncestors = 1;
|
2016-01-03 18:54:50 +01:00
|
|
|
nSizeWithAncestors = GetTxSize();
|
2015-10-19 16:54:28 +02:00
|
|
|
nModFeesWithAncestors = nFee;
|
2016-01-03 18:54:50 +01:00
|
|
|
nSigOpCostWithAncestors = sigOpCost;
|
2013-11-11 08:35:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other)
|
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
|
|
|
|
{
|
2015-11-13 16:05:21 +01:00
|
|
|
double deltaPriority = ((double)(currentHeight-entryHeight)*inChainInputValue)/nModSize;
|
2015-06-30 17:14:24 +02:00
|
|
|
double dResult = entryPriority + deltaPriority;
|
2015-11-13 16:05:21 +01:00
|
|
|
if (dResult < 0) // This should only happen if it was called with a height below entry height
|
|
|
|
dResult = 0;
|
2013-11-11 08:35:14 +01:00
|
|
|
return dResult;
|
|
|
|
}
|
|
|
|
|
2015-10-26 19:06:06 +01:00
|
|
|
void CTxMemPoolEntry::UpdateFeeDelta(int64_t newFeeDelta)
|
|
|
|
{
|
2015-11-19 17:18:28 +01:00
|
|
|
nModFeesWithDescendants += newFeeDelta - feeDelta;
|
2015-10-19 16:54:28 +02:00
|
|
|
nModFeesWithAncestors += newFeeDelta - feeDelta;
|
2015-10-26 19:06:06 +01:00
|
|
|
feeDelta = newFeeDelta;
|
|
|
|
}
|
|
|
|
|
2015-12-04 21:01:22 +01:00
|
|
|
void CTxMemPoolEntry::UpdateLockPoints(const LockPoints& lp)
|
|
|
|
{
|
|
|
|
lockPoints = lp;
|
|
|
|
}
|
|
|
|
|
2016-01-03 18:54:50 +01:00
|
|
|
size_t CTxMemPoolEntry::GetTxSize() const
|
|
|
|
{
|
2016-07-18 20:57:20 +02:00
|
|
|
return GetVirtualTransactionSize(nTxWeight, sigOpCost);
|
2016-01-03 18:54:50 +01:00
|
|
|
}
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
// Update the given tx for any in-mempool descendants.
|
|
|
|
// Assumes that setMemPoolChildren is correct for the given tx and all
|
|
|
|
// descendants.
|
2015-10-21 16:18:24 +02:00
|
|
|
void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set<uint256> &setExclude)
|
2015-07-15 20:47:45 +02:00
|
|
|
{
|
|
|
|
setEntries stageEntries, setAllDescendants;
|
|
|
|
stageEntries = GetMemPoolChildren(updateIt);
|
|
|
|
|
|
|
|
while (!stageEntries.empty()) {
|
|
|
|
const txiter cit = *stageEntries.begin();
|
|
|
|
setAllDescendants.insert(cit);
|
|
|
|
stageEntries.erase(cit);
|
|
|
|
const setEntries &setChildren = GetMemPoolChildren(cit);
|
|
|
|
BOOST_FOREACH(const txiter childEntry, setChildren) {
|
|
|
|
cacheMap::iterator cacheIt = cachedDescendants.find(childEntry);
|
|
|
|
if (cacheIt != cachedDescendants.end()) {
|
|
|
|
// We've already calculated this one, just add the entries for this set
|
|
|
|
// but don't traverse again.
|
|
|
|
BOOST_FOREACH(const txiter cacheEntry, cacheIt->second) {
|
2015-10-21 16:18:24 +02:00
|
|
|
setAllDescendants.insert(cacheEntry);
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
} else if (!setAllDescendants.count(childEntry)) {
|
2015-10-21 16:18:24 +02:00
|
|
|
// Schedule for later processing
|
|
|
|
stageEntries.insert(childEntry);
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// setAllDescendants now contains all in-mempool descendants of updateIt.
|
|
|
|
// Update and add to cached descendant map
|
|
|
|
int64_t modifySize = 0;
|
|
|
|
CAmount modifyFee = 0;
|
|
|
|
int64_t modifyCount = 0;
|
|
|
|
BOOST_FOREACH(txiter cit, setAllDescendants) {
|
|
|
|
if (!setExclude.count(cit->GetTx().GetHash())) {
|
|
|
|
modifySize += cit->GetTxSize();
|
2015-11-19 17:18:28 +01:00
|
|
|
modifyFee += cit->GetModifiedFee();
|
2015-07-15 20:47:45 +02:00
|
|
|
modifyCount++;
|
|
|
|
cachedDescendants[updateIt].insert(cit);
|
2015-10-19 16:54:28 +02:00
|
|
|
// Update ancestor state for each descendant
|
2016-01-03 18:54:50 +01:00
|
|
|
mapTx.modify(cit, update_ancestor_state(updateIt->GetTxSize(), updateIt->GetModifiedFee(), 1, updateIt->GetSigOpCost()));
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mapTx.modify(updateIt, update_descendant_state(modifySize, modifyFee, modifyCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
// vHashesToUpdate is the set of transaction hashes from a disconnected block
|
|
|
|
// which has been re-added to the mempool.
|
|
|
|
// for each entry, look for descendants that are outside hashesToUpdate, and
|
|
|
|
// add fee/size information for such descendants to the parent.
|
2015-10-19 16:54:28 +02:00
|
|
|
// for each such descendant, also update the ancestor state to include the parent.
|
2015-07-15 20:47:45 +02:00
|
|
|
void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256> &vHashesToUpdate)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
// For each entry in vHashesToUpdate, store the set of in-mempool, but not
|
|
|
|
// in-vHashesToUpdate transactions, so that we don't have to recalculate
|
|
|
|
// descendants when we come across a previously seen entry.
|
|
|
|
cacheMap mapMemPoolDescendantsToUpdate;
|
|
|
|
|
|
|
|
// Use a set for lookups into vHashesToUpdate (these entries are already
|
|
|
|
// accounted for in the state of their ancestors)
|
|
|
|
std::set<uint256> setAlreadyIncluded(vHashesToUpdate.begin(), vHashesToUpdate.end());
|
|
|
|
|
|
|
|
// Iterate in reverse, so that whenever we are looking at at a transaction
|
|
|
|
// we are sure that all in-mempool descendants have already been processed.
|
|
|
|
// This maximizes the benefit of the descendant cache and guarantees that
|
|
|
|
// setMemPoolChildren will be updated, an assumption made in
|
|
|
|
// UpdateForDescendants.
|
|
|
|
BOOST_REVERSE_FOREACH(const uint256 &hash, vHashesToUpdate) {
|
|
|
|
// we cache the in-mempool children to avoid duplicate updates
|
|
|
|
setEntries setChildren;
|
|
|
|
// calculate children from mapNextTx
|
|
|
|
txiter it = mapTx.find(hash);
|
|
|
|
if (it == mapTx.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-01 06:45:26 +02:00
|
|
|
auto iter = mapNextTx.lower_bound(COutPoint(hash, 0));
|
2015-07-15 20:47:45 +02:00
|
|
|
// First calculate the children, and update setMemPoolChildren to
|
|
|
|
// include them, and update their setMemPoolParents to include this tx.
|
2016-05-01 06:45:26 +02:00
|
|
|
for (; iter != mapNextTx.end() && iter->first->hash == hash; ++iter) {
|
|
|
|
const uint256 &childHash = iter->second->GetHash();
|
2015-07-15 20:47:45 +02:00
|
|
|
txiter childIter = mapTx.find(childHash);
|
|
|
|
assert(childIter != mapTx.end());
|
|
|
|
// We can skip updating entries we've encountered before or that
|
|
|
|
// are in the block (which are already accounted for).
|
|
|
|
if (setChildren.insert(childIter).second && !setAlreadyIncluded.count(childHash)) {
|
|
|
|
UpdateChild(it, childIter, true);
|
|
|
|
UpdateParent(childIter, it, true);
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 16:18:24 +02:00
|
|
|
UpdateForDescendants(it, mapMemPoolDescendantsToUpdate, setAlreadyIncluded);
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 21:49:26 +01:00
|
|
|
bool CTxMemPool::CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents /* = true */) const
|
2015-07-15 20:47:45 +02:00
|
|
|
{
|
|
|
|
setEntries parentHashes;
|
|
|
|
const CTransaction &tx = entry.GetTx();
|
|
|
|
|
2015-09-23 19:37:32 +02:00
|
|
|
if (fSearchForParents) {
|
|
|
|
// Get parents of this transaction that are in the mempool
|
|
|
|
// GetMemPoolParents() is only valid for entries in the mempool, so we
|
|
|
|
// iterate mapTx to find parents.
|
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++) {
|
|
|
|
txiter piter = mapTx.find(tx.vin[i].prevout.hash);
|
|
|
|
if (piter != mapTx.end()) {
|
|
|
|
parentHashes.insert(piter);
|
|
|
|
if (parentHashes.size() + 1 > limitAncestorCount) {
|
|
|
|
errString = strprintf("too many unconfirmed parents [limit: %u]", limitAncestorCount);
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 19:37:32 +02:00
|
|
|
} else {
|
|
|
|
// If we're not searching for parents, we require this to be an
|
|
|
|
// entry in the mempool already.
|
|
|
|
txiter it = mapTx.iterator_to(entry);
|
|
|
|
parentHashes = GetMemPoolParents(it);
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t totalSizeWithAncestors = entry.GetTxSize();
|
|
|
|
|
|
|
|
while (!parentHashes.empty()) {
|
|
|
|
txiter stageit = *parentHashes.begin();
|
|
|
|
|
|
|
|
setAncestors.insert(stageit);
|
|
|
|
parentHashes.erase(stageit);
|
|
|
|
totalSizeWithAncestors += stageit->GetTxSize();
|
|
|
|
|
|
|
|
if (stageit->GetSizeWithDescendants() + entry.GetTxSize() > limitDescendantSize) {
|
|
|
|
errString = strprintf("exceeds descendant size limit for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantSize);
|
|
|
|
return false;
|
|
|
|
} else if (stageit->GetCountWithDescendants() + 1 > limitDescendantCount) {
|
|
|
|
errString = strprintf("too many descendants for tx %s [limit: %u]", stageit->GetTx().GetHash().ToString(), limitDescendantCount);
|
|
|
|
return false;
|
|
|
|
} else if (totalSizeWithAncestors > limitAncestorSize) {
|
|
|
|
errString = strprintf("exceeds ancestor size limit [limit: %u]", limitAncestorSize);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const setEntries & setMemPoolParents = GetMemPoolParents(stageit);
|
|
|
|
BOOST_FOREACH(const txiter &phash, setMemPoolParents) {
|
|
|
|
// If this is a new ancestor, add it.
|
|
|
|
if (setAncestors.count(phash) == 0) {
|
|
|
|
parentHashes.insert(phash);
|
|
|
|
}
|
|
|
|
if (parentHashes.size() + setAncestors.size() + 1 > limitAncestorCount) {
|
|
|
|
errString = strprintf("too many unconfirmed ancestors [limit: %u]", limitAncestorCount);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors)
|
|
|
|
{
|
|
|
|
setEntries parentIters = GetMemPoolParents(it);
|
|
|
|
// add or remove this tx as a child of each parent
|
|
|
|
BOOST_FOREACH(txiter piter, parentIters) {
|
|
|
|
UpdateChild(piter, it, add);
|
|
|
|
}
|
|
|
|
const int64_t updateCount = (add ? 1 : -1);
|
|
|
|
const int64_t updateSize = updateCount * it->GetTxSize();
|
2015-11-19 17:18:28 +01:00
|
|
|
const CAmount updateFee = updateCount * it->GetModifiedFee();
|
2015-07-15 20:47:45 +02:00
|
|
|
BOOST_FOREACH(txiter ancestorIt, setAncestors) {
|
|
|
|
mapTx.modify(ancestorIt, update_descendant_state(updateSize, updateFee, updateCount));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:54:28 +02:00
|
|
|
void CTxMemPool::UpdateEntryForAncestors(txiter it, const setEntries &setAncestors)
|
|
|
|
{
|
|
|
|
int64_t updateCount = setAncestors.size();
|
|
|
|
int64_t updateSize = 0;
|
|
|
|
CAmount updateFee = 0;
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t updateSigOpsCost = 0;
|
2015-10-19 16:54:28 +02:00
|
|
|
BOOST_FOREACH(txiter ancestorIt, setAncestors) {
|
|
|
|
updateSize += ancestorIt->GetTxSize();
|
|
|
|
updateFee += ancestorIt->GetModifiedFee();
|
2016-01-03 18:54:50 +01:00
|
|
|
updateSigOpsCost += ancestorIt->GetSigOpCost();
|
2015-10-19 16:54:28 +02:00
|
|
|
}
|
2016-01-03 18:54:50 +01:00
|
|
|
mapTx.modify(it, update_ancestor_state(updateSize, updateFee, updateCount, updateSigOpsCost));
|
2015-10-19 16:54:28 +02:00
|
|
|
}
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
void CTxMemPool::UpdateChildrenForRemoval(txiter it)
|
|
|
|
{
|
|
|
|
const setEntries &setMemPoolChildren = GetMemPoolChildren(it);
|
|
|
|
BOOST_FOREACH(txiter updateIt, setMemPoolChildren) {
|
|
|
|
UpdateParent(updateIt, it, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:54:28 +02:00
|
|
|
void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants)
|
2015-07-15 20:47:45 +02:00
|
|
|
{
|
|
|
|
// For each entry, walk back all ancestors and decrement size associated with this
|
|
|
|
// transaction
|
|
|
|
const uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
|
2015-10-19 16:54:28 +02:00
|
|
|
if (updateDescendants) {
|
|
|
|
// updateDescendants should be true whenever we're not recursively
|
|
|
|
// removing a tx and all its descendants, eg when a transaction is
|
|
|
|
// confirmed in a block.
|
|
|
|
// Here we only update statistics and not data in mapLinks (which
|
|
|
|
// we need to preserve until we're finished with all operations that
|
|
|
|
// need to traverse the mempool).
|
|
|
|
BOOST_FOREACH(txiter removeIt, entriesToRemove) {
|
|
|
|
setEntries setDescendants;
|
|
|
|
CalculateDescendants(removeIt, setDescendants);
|
|
|
|
setDescendants.erase(removeIt); // don't update state for self
|
|
|
|
int64_t modifySize = -((int64_t)removeIt->GetTxSize());
|
|
|
|
CAmount modifyFee = -removeIt->GetModifiedFee();
|
2016-01-03 18:54:50 +01:00
|
|
|
int modifySigOps = -removeIt->GetSigOpCost();
|
2015-10-19 16:54:28 +02:00
|
|
|
BOOST_FOREACH(txiter dit, setDescendants) {
|
|
|
|
mapTx.modify(dit, update_ancestor_state(modifySize, modifyFee, -1, modifySigOps));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-15 20:47:45 +02:00
|
|
|
BOOST_FOREACH(txiter removeIt, entriesToRemove) {
|
|
|
|
setEntries setAncestors;
|
|
|
|
const CTxMemPoolEntry &entry = *removeIt;
|
|
|
|
std::string dummy;
|
2015-09-23 19:37:32 +02:00
|
|
|
// Since this is a tx that is already in the mempool, we can call CMPA
|
|
|
|
// with fSearchForParents = false. If the mempool is in a consistent
|
|
|
|
// state, then using true or false should both be correct, though false
|
|
|
|
// should be a bit faster.
|
|
|
|
// However, if we happen to be in the middle of processing a reorg, then
|
|
|
|
// the mempool can be in an inconsistent state. In this case, the set
|
|
|
|
// of ancestors reachable via mapLinks will be the same as the set of
|
|
|
|
// ancestors whose packages include this transaction, because when we
|
|
|
|
// add a new transaction to the mempool in addUnchecked(), we assume it
|
|
|
|
// has no children, and in the case of a reorg where that assumption is
|
|
|
|
// false, the in-mempool children aren't linked to the in-block tx's
|
|
|
|
// until UpdateTransactionsFromBlock() is called.
|
|
|
|
// So if we're being called during a reorg, ie before
|
|
|
|
// UpdateTransactionsFromBlock() has been called, then mapLinks[] will
|
|
|
|
// differ from the set of mempool parents we'd calculate by searching,
|
|
|
|
// and it's important that we use the mapLinks[] notion of ancestor
|
|
|
|
// transactions as the set of things to update for removal.
|
|
|
|
CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
|
2015-07-15 20:47:45 +02:00
|
|
|
// Note that UpdateAncestorsOf severs the child links that point to
|
2015-10-19 16:54:28 +02:00
|
|
|
// removeIt in the entries for the parents of removeIt.
|
2015-07-15 20:47:45 +02:00
|
|
|
UpdateAncestorsOf(false, removeIt, setAncestors);
|
|
|
|
}
|
|
|
|
// After updating all the ancestor sizes, we can now sever the link between each
|
|
|
|
// transaction being removed and any mempool children (ie, update setMemPoolParents
|
|
|
|
// for each direct child of a transaction being removed).
|
|
|
|
BOOST_FOREACH(txiter removeIt, entriesToRemove) {
|
|
|
|
UpdateChildrenForRemoval(removeIt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 16:54:28 +02:00
|
|
|
void CTxMemPoolEntry::UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
|
2015-07-15 20:47:45 +02:00
|
|
|
{
|
2015-10-21 16:18:24 +02:00
|
|
|
nSizeWithDescendants += modifySize;
|
|
|
|
assert(int64_t(nSizeWithDescendants) > 0);
|
|
|
|
nModFeesWithDescendants += modifyFee;
|
|
|
|
nCountWithDescendants += modifyCount;
|
|
|
|
assert(int64_t(nCountWithDescendants) > 0);
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
|
2015-10-19 16:54:28 +02:00
|
|
|
void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int modifySigOps)
|
2015-07-15 20:47:45 +02:00
|
|
|
{
|
2015-10-19 16:54:28 +02:00
|
|
|
nSizeWithAncestors += modifySize;
|
|
|
|
assert(int64_t(nSizeWithAncestors) > 0);
|
|
|
|
nModFeesWithAncestors += modifyFee;
|
|
|
|
nCountWithAncestors += modifyCount;
|
|
|
|
assert(int64_t(nCountWithAncestors) > 0);
|
2016-01-03 18:54:50 +01:00
|
|
|
nSigOpCostWithAncestors += modifySigOps;
|
|
|
|
assert(int(nSigOpCostWithAncestors) >= 0);
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
|
2015-10-13 09:57:41 +02:00
|
|
|
CTxMemPool::CTxMemPool(const CFeeRate& _minReasonableRelayFee) :
|
2014-08-26 22:28:32 +02:00
|
|
|
nTransactionsUpdated(0)
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
2015-10-26 14:55:17 +01:00
|
|
|
_clear(); //lock free clear
|
2015-10-02 23:19:55 +02:00
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
// Sanity checks off by default for performance, because otherwise
|
|
|
|
// accepting transactions becomes O(N^2) where N is the number
|
|
|
|
// of transactions in the pool
|
2015-10-07 23:34:55 +02:00
|
|
|
nCheckFrequency = 0;
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2015-10-13 09:57:41 +02:00
|
|
|
minerPolicyEstimator = new CBlockPolicyEstimator(_minReasonableRelayFee);
|
|
|
|
minReasonableRelayFee = _minReasonableRelayFee;
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CTxMemPool::~CTxMemPool()
|
|
|
|
{
|
|
|
|
delete minerPolicyEstimator;
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
2016-05-01 06:45:26 +02:00
|
|
|
auto it = mapNextTx.lower_bound(COutPoint(hashTx, 0));
|
2013-08-27 07:51:57 +02:00
|
|
|
|
|
|
|
// iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx
|
2016-05-01 06:45:26 +02:00
|
|
|
while (it != mapNextTx.end() && it->first->hash == hashTx) {
|
|
|
|
coins.Spend(it->first->n); // and remove those outputs from coins
|
2013-08-27 07:51:57 +02:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CTxMemPool::GetTransactionsUpdated() const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return nTransactionsUpdated;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTxMemPool::AddTransactionsUpdated(unsigned int n)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
nTransactionsUpdated += n;
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool fCurrentEstimate)
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
// Add to memory pool without checking anything.
|
|
|
|
// Used by main.cpp AcceptToMemoryPool(), which DOES do
|
|
|
|
// all the appropriate checks.
|
|
|
|
LOCK(cs);
|
2015-07-15 20:47:45 +02:00
|
|
|
indexed_transaction_set::iterator newit = mapTx.insert(entry).first;
|
|
|
|
mapLinks.insert(make_pair(newit, TxLinks()));
|
|
|
|
|
2015-11-19 17:18:28 +01:00
|
|
|
// Update transaction for any feeDelta created by PrioritiseTransaction
|
|
|
|
// TODO: refactor so that the fee delta is calculated before inserting
|
|
|
|
// into mapTx.
|
|
|
|
std::map<uint256, std::pair<double, CAmount> >::const_iterator pos = mapDeltas.find(hash);
|
|
|
|
if (pos != mapDeltas.end()) {
|
|
|
|
const std::pair<double, CAmount> &deltas = pos->second;
|
|
|
|
if (deltas.second) {
|
|
|
|
mapTx.modify(newit, update_fee_delta(deltas.second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
// Update cachedInnerUsage to include contained transaction's usage.
|
|
|
|
// (When we update the entry for in-mempool parents, memory usage will be
|
|
|
|
// further updated.)
|
|
|
|
cachedInnerUsage += entry.DynamicMemoryUsage();
|
|
|
|
|
|
|
|
const CTransaction& tx = newit->GetTx();
|
|
|
|
std::set<uint256> setParentTransactions;
|
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++) {
|
2016-05-01 06:45:26 +02:00
|
|
|
mapNextTx.insert(std::make_pair(&tx.vin[i].prevout, &tx));
|
2015-07-15 20:47:45 +02:00
|
|
|
setParentTransactions.insert(tx.vin[i].prevout.hash);
|
|
|
|
}
|
|
|
|
// Don't bother worrying about child transactions of this one.
|
|
|
|
// Normal case of a new transaction arriving is that there can't be any
|
|
|
|
// children, because such children would be orphans.
|
|
|
|
// An exception to that is if a transaction enters that used to be in a block.
|
|
|
|
// In that case, our disconnect block logic will call UpdateTransactionsFromBlock
|
|
|
|
// to clean up the mess we're leaving here.
|
|
|
|
|
|
|
|
// Update ancestors with information about this tx
|
|
|
|
BOOST_FOREACH (const uint256 &phash, setParentTransactions) {
|
|
|
|
txiter pit = mapTx.find(phash);
|
|
|
|
if (pit != mapTx.end()) {
|
|
|
|
UpdateParent(newit, pit, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UpdateAncestorsOf(true, newit, setAncestors);
|
2015-10-19 16:54:28 +02:00
|
|
|
UpdateEntryForAncestors(newit, setAncestors);
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
nTransactionsUpdated++;
|
|
|
|
totalTxSize += entry.GetTxSize();
|
|
|
|
minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
|
|
|
|
|
2016-06-25 19:17:45 +02:00
|
|
|
vTxHashes.emplace_back(tx.GetWitnessHash(), newit);
|
2016-06-15 08:59:03 +02:00
|
|
|
newit->vTxHashesIdx = vTxHashes.size() - 1;
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
void CTxMemPool::removeUnchecked(txiter it)
|
|
|
|
{
|
|
|
|
const uint256 hash = it->GetTx().GetHash();
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, it->GetTx().vin)
|
|
|
|
mapNextTx.erase(txin.prevout);
|
|
|
|
|
2016-06-15 08:59:03 +02:00
|
|
|
if (vTxHashes.size() > 1) {
|
|
|
|
vTxHashes[it->vTxHashesIdx] = std::move(vTxHashes.back());
|
|
|
|
vTxHashes[it->vTxHashesIdx].second->vTxHashesIdx = it->vTxHashesIdx;
|
|
|
|
vTxHashes.pop_back();
|
|
|
|
if (vTxHashes.size() * 2 < vTxHashes.capacity())
|
|
|
|
vTxHashes.shrink_to_fit();
|
|
|
|
} else
|
|
|
|
vTxHashes.clear();
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
totalTxSize -= it->GetTxSize();
|
|
|
|
cachedInnerUsage -= it->DynamicMemoryUsage();
|
|
|
|
cachedInnerUsage -= memusage::DynamicUsage(mapLinks[it].parents) + memusage::DynamicUsage(mapLinks[it].children);
|
|
|
|
mapLinks.erase(it);
|
|
|
|
mapTx.erase(it);
|
|
|
|
nTransactionsUpdated++;
|
|
|
|
minerPolicyEstimator->removeTx(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculates descendants of entry that are not already in setDescendants, and adds to
|
|
|
|
// setDescendants. Assumes entryit is already a tx in the mempool and setMemPoolChildren
|
|
|
|
// is correct for tx and all descendants.
|
|
|
|
// Also assumes that if an entry is in setDescendants already, then all
|
|
|
|
// in-mempool descendants of it are already in setDescendants as well, so that we
|
|
|
|
// can save time by not iterating over those entries.
|
|
|
|
void CTxMemPool::CalculateDescendants(txiter entryit, setEntries &setDescendants)
|
|
|
|
{
|
|
|
|
setEntries stage;
|
|
|
|
if (setDescendants.count(entryit) == 0) {
|
|
|
|
stage.insert(entryit);
|
|
|
|
}
|
|
|
|
// Traverse down the children of entry, only adding children that are not
|
|
|
|
// accounted for in setDescendants already (because those children have either
|
|
|
|
// already been walked, or will be walked in this iteration).
|
|
|
|
while (!stage.empty()) {
|
|
|
|
txiter it = *stage.begin();
|
|
|
|
setDescendants.insert(it);
|
|
|
|
stage.erase(it);
|
|
|
|
|
|
|
|
const setEntries &setChildren = GetMemPoolChildren(it);
|
|
|
|
BOOST_FOREACH(const txiter &childiter, setChildren) {
|
|
|
|
if (!setDescendants.count(childiter)) {
|
|
|
|
stage.insert(childiter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2016-11-11 02:34:17 +01:00
|
|
|
void CTxMemPool::removeRecursive(const CTransaction &origTx, std::vector<CTransactionRef>* removed)
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
// Remove transaction from memory pool
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2015-07-15 20:47:45 +02:00
|
|
|
setEntries txToRemove;
|
|
|
|
txiter origit = mapTx.find(origTx.GetHash());
|
|
|
|
if (origit != mapTx.end()) {
|
|
|
|
txToRemove.insert(origit);
|
2015-10-19 18:43:38 +02:00
|
|
|
} else {
|
|
|
|
// When recursively removing but origTx isn't in the mempool
|
2015-03-25 18:13:09 +01:00
|
|
|
// be sure to remove any children that are in the pool. This can
|
|
|
|
// happen during chain re-orgs if origTx isn't re-accepted into
|
|
|
|
// the mempool for any reason.
|
|
|
|
for (unsigned int i = 0; i < origTx.vout.size(); i++) {
|
2016-05-01 06:45:26 +02:00
|
|
|
auto it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
|
2015-03-25 18:13:09 +01:00
|
|
|
if (it == mapNextTx.end())
|
|
|
|
continue;
|
2016-05-01 06:45:26 +02:00
|
|
|
txiter nextit = mapTx.find(it->second->GetHash());
|
2015-07-15 20:47:45 +02:00
|
|
|
assert(nextit != mapTx.end());
|
|
|
|
txToRemove.insert(nextit);
|
2015-03-25 18:13:09 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-15 20:47:45 +02:00
|
|
|
setEntries setAllRemoves;
|
2015-10-19 18:43:38 +02:00
|
|
|
BOOST_FOREACH(txiter it, txToRemove) {
|
|
|
|
CalculateDescendants(it, setAllRemoves);
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
2016-08-15 13:10:57 +02:00
|
|
|
if (removed) {
|
|
|
|
BOOST_FOREACH(txiter it, setAllRemoves) {
|
2016-08-15 12:57:10 +02:00
|
|
|
removed->emplace_back(it->GetSharedTx());
|
2016-08-15 13:10:57 +02:00
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
2015-10-19 16:54:28 +02:00
|
|
|
RemoveStaged(setAllRemoves, false);
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 22:06:12 +01:00
|
|
|
void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
|
2014-11-12 05:57:54 +01:00
|
|
|
{
|
2015-09-06 06:40:21 +02:00
|
|
|
// Remove transactions spending a coinbase which are now immature and no-longer-final transactions
|
2014-11-12 05:57:54 +01:00
|
|
|
LOCK(cs);
|
2016-08-15 12:20:13 +02:00
|
|
|
setEntries txToRemove;
|
2015-06-24 10:32:20 +02:00
|
|
|
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
|
|
|
|
const CTransaction& tx = it->GetTx();
|
2015-12-04 21:01:22 +01:00
|
|
|
LockPoints lp = it->GetLockPoints();
|
|
|
|
bool validLP = TestLockPointValidity(&lp);
|
|
|
|
if (!CheckFinalTx(tx, flags) || !CheckSequenceLocks(tx, flags, &lp, validLP)) {
|
|
|
|
// Note if CheckSequenceLocks fails the LockPoints may still be invalid
|
|
|
|
// So it's critical that we remove the tx and not depend on the LockPoints.
|
2016-08-15 12:20:13 +02:00
|
|
|
txToRemove.insert(it);
|
2015-10-29 19:06:13 +01:00
|
|
|
} else if (it->GetSpendsCoinbase()) {
|
2015-08-27 03:58:17 +02:00
|
|
|
BOOST_FOREACH(const CTxIn& txin, tx.vin) {
|
|
|
|
indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
|
|
|
|
if (it2 != mapTx.end())
|
|
|
|
continue;
|
|
|
|
const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
|
2016-08-15 12:20:13 +02:00
|
|
|
if (nCheckFrequency != 0) assert(coins);
|
2015-08-27 03:58:17 +02:00
|
|
|
if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
|
2016-08-15 12:20:13 +02:00
|
|
|
txToRemove.insert(it);
|
2015-08-27 03:58:17 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-11-12 05:57:54 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 21:01:22 +01:00
|
|
|
if (!validLP) {
|
|
|
|
mapTx.modify(it, update_lock_points(lp));
|
|
|
|
}
|
2014-11-12 05:57:54 +01:00
|
|
|
}
|
2016-08-15 12:20:13 +02:00
|
|
|
setEntries setAllRemoves;
|
|
|
|
for (txiter it : txToRemove) {
|
|
|
|
CalculateDescendants(it, setAllRemoves);
|
2014-11-12 05:57:54 +01:00
|
|
|
}
|
2016-08-15 12:20:13 +02:00
|
|
|
RemoveStaged(setAllRemoves, false);
|
2014-11-12 05:57:54 +01:00
|
|
|
}
|
|
|
|
|
2016-11-11 02:34:17 +01:00
|
|
|
void CTxMemPool::removeConflicts(const CTransaction &tx, std::vector<CTransactionRef>* removed)
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
// Remove transactions which depend on inputs of tx, recursively
|
|
|
|
LOCK(cs);
|
|
|
|
BOOST_FOREACH(const CTxIn &txin, tx.vin) {
|
2016-05-01 06:45:26 +02:00
|
|
|
auto it = mapNextTx.find(txin.prevout);
|
2013-08-27 07:51:57 +02:00
|
|
|
if (it != mapNextTx.end()) {
|
2016-05-01 06:45:26 +02:00
|
|
|
const CTransaction &txConflict = *it->second;
|
2013-08-27 07:51:57 +02:00
|
|
|
if (txConflict != tx)
|
2014-02-15 22:38:28 +01:00
|
|
|
{
|
2015-10-19 18:43:38 +02:00
|
|
|
removeRecursive(txConflict, removed);
|
2015-08-03 21:49:01 +02:00
|
|
|
ClearPrioritisation(txConflict.GetHash());
|
2014-02-15 22:38:28 +01:00
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 03:29:09 +01:00
|
|
|
/**
|
|
|
|
* Called when a block is connected. Removes from mempool and updates the miner fee estimator.
|
|
|
|
*/
|
2016-11-11 02:34:17 +01:00
|
|
|
void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight,
|
|
|
|
std::vector<CTransactionRef>* conflicts, bool fCurrentEstimate)
|
2014-03-17 13:19:54 +01:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
std::vector<CTxMemPoolEntry> entries;
|
2016-11-11 02:26:00 +01:00
|
|
|
for (const auto& tx : vtx)
|
2014-03-17 13:19:54 +01:00
|
|
|
{
|
2016-11-11 02:26:00 +01:00
|
|
|
uint256 hash = tx->GetHash();
|
2015-06-24 10:32:20 +02:00
|
|
|
|
|
|
|
indexed_transaction_set::iterator i = mapTx.find(hash);
|
|
|
|
if (i != mapTx.end())
|
|
|
|
entries.push_back(*i);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
2016-11-11 02:26:00 +01:00
|
|
|
for (const auto& tx : vtx)
|
2014-03-17 13:19:54 +01:00
|
|
|
{
|
2016-11-11 02:26:00 +01:00
|
|
|
txiter it = mapTx.find(tx->GetHash());
|
2015-10-19 18:42:42 +02:00
|
|
|
if (it != mapTx.end()) {
|
|
|
|
setEntries stage;
|
|
|
|
stage.insert(it);
|
2015-10-19 16:54:28 +02:00
|
|
|
RemoveStaged(stage, true);
|
2015-10-19 18:42:42 +02:00
|
|
|
}
|
2016-11-11 02:26:00 +01:00
|
|
|
removeConflicts(*tx, conflicts);
|
|
|
|
ClearPrioritisation(tx->GetHash());
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
2014-08-26 22:28:32 +02:00
|
|
|
// After the txs in the new block have been removed from the mempool, update policy estimates
|
|
|
|
minerPolicyEstimator->processBlock(nBlockHeight, entries, fCurrentEstimate);
|
2015-10-02 23:19:55 +02:00
|
|
|
lastRollingFeeUpdate = GetTime();
|
|
|
|
blockSinceLastRollingFeeBump = true;
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
|
|
|
|
2015-10-26 14:55:17 +01:00
|
|
|
void CTxMemPool::_clear()
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
2015-07-15 20:47:45 +02:00
|
|
|
mapLinks.clear();
|
2013-08-27 07:51:57 +02:00
|
|
|
mapTx.clear();
|
|
|
|
mapNextTx.clear();
|
2014-08-07 05:58:19 +02:00
|
|
|
totalTxSize = 0;
|
2015-07-09 19:56:31 +02:00
|
|
|
cachedInnerUsage = 0;
|
2015-10-02 23:19:55 +02:00
|
|
|
lastRollingFeeUpdate = GetTime();
|
|
|
|
blockSinceLastRollingFeeBump = false;
|
|
|
|
rollingMinimumFeeRate = 0;
|
2013-08-27 07:51:57 +02:00
|
|
|
++nTransactionsUpdated;
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:55:17 +01:00
|
|
|
void CTxMemPool::clear()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
_clear();
|
|
|
|
}
|
|
|
|
|
2014-07-19 17:14:23 +02:00
|
|
|
void CTxMemPool::check(const CCoinsViewCache *pcoins) const
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
2015-10-07 23:34:55 +02:00
|
|
|
if (nCheckFrequency == 0)
|
|
|
|
return;
|
|
|
|
|
2016-10-13 16:19:20 +02:00
|
|
|
if (GetRand(std::numeric_limits<uint32_t>::max()) >= nCheckFrequency)
|
2013-08-27 07:51:57 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
|
|
|
|
|
2014-08-07 05:58:19 +02:00
|
|
|
uint64_t checkTotal = 0;
|
2015-07-09 19:56:31 +02:00
|
|
|
uint64_t innerUsage = 0;
|
2014-08-07 05:58:19 +02:00
|
|
|
|
2014-11-12 06:06:15 +01:00
|
|
|
CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
|
2016-07-16 20:57:42 +02:00
|
|
|
const int64_t nSpendHeight = GetSpendHeight(mempoolDuplicate);
|
2014-11-12 06:06:15 +01:00
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
LOCK(cs);
|
2014-11-12 06:06:15 +01:00
|
|
|
list<const CTxMemPoolEntry*> waitingOnDependants;
|
2015-06-24 10:32:20 +02:00
|
|
|
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
|
2013-08-27 07:51:57 +02:00
|
|
|
unsigned int i = 0;
|
2015-06-24 10:32:20 +02:00
|
|
|
checkTotal += it->GetTxSize();
|
|
|
|
innerUsage += it->DynamicMemoryUsage();
|
|
|
|
const CTransaction& tx = it->GetTx();
|
2015-07-15 20:47:45 +02:00
|
|
|
txlinksMap::const_iterator linksiter = mapLinks.find(it);
|
|
|
|
assert(linksiter != mapLinks.end());
|
|
|
|
const TxLinks &links = linksiter->second;
|
|
|
|
innerUsage += memusage::DynamicUsage(links.parents) + memusage::DynamicUsage(links.children);
|
2014-11-12 06:06:15 +01:00
|
|
|
bool fDependsWait = false;
|
2015-07-15 20:47:45 +02:00
|
|
|
setEntries setParentCheck;
|
2015-10-19 16:54:28 +02:00
|
|
|
int64_t parentSizes = 0;
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t parentSigOpCost = 0;
|
2013-11-11 08:35:14 +01:00
|
|
|
BOOST_FOREACH(const CTxIn &txin, tx.vin) {
|
2013-08-27 07:51:57 +02:00
|
|
|
// Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
|
2015-06-24 10:32:20 +02:00
|
|
|
indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
|
2013-08-27 07:51:57 +02:00
|
|
|
if (it2 != mapTx.end()) {
|
2015-06-24 10:32:20 +02:00
|
|
|
const CTransaction& tx2 = it2->GetTx();
|
2013-11-11 08:35:14 +01:00
|
|
|
assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
|
2014-11-12 06:06:15 +01:00
|
|
|
fDependsWait = true;
|
2015-10-19 16:54:28 +02:00
|
|
|
if (setParentCheck.insert(it2).second) {
|
|
|
|
parentSizes += it2->GetTxSize();
|
2016-01-03 18:54:50 +01:00
|
|
|
parentSigOpCost += it2->GetSigOpCost();
|
2015-10-19 16:54:28 +02:00
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
} else {
|
2014-09-02 21:21:15 +02:00
|
|
|
const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
|
|
|
|
assert(coins && coins->IsAvailable(txin.prevout.n));
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
// Check whether its inputs are marked in mapNextTx.
|
2016-05-01 06:45:26 +02:00
|
|
|
auto it3 = mapNextTx.find(txin.prevout);
|
2013-08-27 07:51:57 +02:00
|
|
|
assert(it3 != mapNextTx.end());
|
2016-05-01 06:45:26 +02:00
|
|
|
assert(it3->first == &txin.prevout);
|
|
|
|
assert(it3->second == &tx);
|
2013-08-27 07:51:57 +02:00
|
|
|
i++;
|
|
|
|
}
|
2015-07-15 20:47:45 +02:00
|
|
|
assert(setParentCheck == GetMemPoolParents(it));
|
2016-03-08 21:49:26 +01:00
|
|
|
// Verify ancestor state is correct.
|
|
|
|
setEntries setAncestors;
|
|
|
|
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
|
|
|
|
std::string dummy;
|
|
|
|
CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
|
|
|
|
uint64_t nCountCheck = setAncestors.size() + 1;
|
|
|
|
uint64_t nSizeCheck = it->GetTxSize();
|
|
|
|
CAmount nFeesCheck = it->GetModifiedFee();
|
2016-01-03 18:54:50 +01:00
|
|
|
int64_t nSigOpCheck = it->GetSigOpCost();
|
2016-03-08 21:49:26 +01:00
|
|
|
|
|
|
|
BOOST_FOREACH(txiter ancestorIt, setAncestors) {
|
|
|
|
nSizeCheck += ancestorIt->GetTxSize();
|
|
|
|
nFeesCheck += ancestorIt->GetModifiedFee();
|
2016-01-03 18:54:50 +01:00
|
|
|
nSigOpCheck += ancestorIt->GetSigOpCost();
|
2016-03-08 21:49:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(it->GetCountWithAncestors() == nCountCheck);
|
|
|
|
assert(it->GetSizeWithAncestors() == nSizeCheck);
|
2016-01-03 18:54:50 +01:00
|
|
|
assert(it->GetSigOpCostWithAncestors() == nSigOpCheck);
|
2016-03-08 21:49:26 +01:00
|
|
|
assert(it->GetModFeesWithAncestors() == nFeesCheck);
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
// Check children against mapNextTx
|
|
|
|
CTxMemPool::setEntries setChildrenCheck;
|
2016-05-01 06:45:26 +02:00
|
|
|
auto iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetHash(), 0));
|
2015-07-15 20:47:45 +02:00
|
|
|
int64_t childSizes = 0;
|
2016-05-01 06:45:26 +02:00
|
|
|
for (; iter != mapNextTx.end() && iter->first->hash == it->GetTx().GetHash(); ++iter) {
|
|
|
|
txiter childit = mapTx.find(iter->second->GetHash());
|
2015-07-15 20:47:45 +02:00
|
|
|
assert(childit != mapTx.end()); // mapNextTx points to in-mempool transactions
|
|
|
|
if (setChildrenCheck.insert(childit).second) {
|
|
|
|
childSizes += childit->GetTxSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(setChildrenCheck == GetMemPoolChildren(it));
|
2015-11-19 17:18:28 +01:00
|
|
|
// Also check to make sure size is greater than sum with immediate children.
|
2015-07-15 20:47:45 +02:00
|
|
|
// just a sanity check, not definitive that this calc is correct...
|
2015-10-21 16:18:24 +02:00
|
|
|
assert(it->GetSizeWithDescendants() >= childSizes + it->GetTxSize());
|
2015-07-15 20:47:45 +02:00
|
|
|
|
2014-11-12 06:06:15 +01:00
|
|
|
if (fDependsWait)
|
2015-06-24 10:32:20 +02:00
|
|
|
waitingOnDependants.push_back(&(*it));
|
2014-11-12 06:06:15 +01:00
|
|
|
else {
|
2014-10-27 16:07:50 +01:00
|
|
|
CValidationState state;
|
2016-07-16 20:57:42 +02:00
|
|
|
bool fCheckResult = tx.IsCoinBase() ||
|
|
|
|
Consensus::CheckTxInputs(tx, state, mempoolDuplicate, nSpendHeight);
|
|
|
|
assert(fCheckResult);
|
2016-04-30 05:45:20 +02:00
|
|
|
UpdateCoins(tx, mempoolDuplicate, 1000000);
|
2014-11-12 06:06:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned int stepsSinceLastRemove = 0;
|
|
|
|
while (!waitingOnDependants.empty()) {
|
|
|
|
const CTxMemPoolEntry* entry = waitingOnDependants.front();
|
|
|
|
waitingOnDependants.pop_front();
|
|
|
|
CValidationState state;
|
|
|
|
if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
|
|
|
|
waitingOnDependants.push_back(entry);
|
|
|
|
stepsSinceLastRemove++;
|
|
|
|
assert(stepsSinceLastRemove < waitingOnDependants.size());
|
|
|
|
} else {
|
2016-07-16 20:57:42 +02:00
|
|
|
bool fCheckResult = entry->GetTx().IsCoinBase() ||
|
|
|
|
Consensus::CheckTxInputs(entry->GetTx(), state, mempoolDuplicate, nSpendHeight);
|
|
|
|
assert(fCheckResult);
|
2016-04-30 05:45:20 +02:00
|
|
|
UpdateCoins(entry->GetTx(), mempoolDuplicate, 1000000);
|
2014-11-12 06:06:15 +01:00
|
|
|
stepsSinceLastRemove = 0;
|
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
2016-05-01 06:45:26 +02:00
|
|
|
for (auto it = mapNextTx.cbegin(); it != mapNextTx.cend(); it++) {
|
|
|
|
uint256 hash = it->second->GetHash();
|
2015-06-24 10:32:20 +02:00
|
|
|
indexed_transaction_set::const_iterator it2 = mapTx.find(hash);
|
|
|
|
const CTransaction& tx = it2->GetTx();
|
2013-08-27 07:51:57 +02:00
|
|
|
assert(it2 != mapTx.end());
|
2016-05-01 06:45:26 +02:00
|
|
|
assert(&tx == it->second);
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
2014-08-07 05:58:19 +02:00
|
|
|
|
|
|
|
assert(totalTxSize == checkTotal);
|
2015-07-09 19:56:31 +02:00
|
|
|
assert(innerUsage == cachedInnerUsage);
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 04:36:47 +02:00
|
|
|
bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
indexed_transaction_set::const_iterator i = mapTx.find(hasha);
|
|
|
|
if (i == mapTx.end()) return false;
|
|
|
|
indexed_transaction_set::const_iterator j = mapTx.find(hashb);
|
|
|
|
if (j == mapTx.end()) return true;
|
|
|
|
uint64_t counta = i->GetCountWithAncestors();
|
|
|
|
uint64_t countb = j->GetCountWithAncestors();
|
|
|
|
if (counta == countb) {
|
|
|
|
return CompareTxMemPoolEntryByScore()(*i, *j);
|
|
|
|
}
|
|
|
|
return counta < countb;
|
|
|
|
}
|
|
|
|
|
2016-04-10 15:33:05 +02:00
|
|
|
namespace {
|
|
|
|
class DepthAndScoreComparator
|
|
|
|
{
|
|
|
|
public:
|
2016-05-30 17:06:24 +02:00
|
|
|
bool operator()(const CTxMemPool::indexed_transaction_set::const_iterator& a, const CTxMemPool::indexed_transaction_set::const_iterator& b)
|
|
|
|
{
|
|
|
|
uint64_t counta = a->GetCountWithAncestors();
|
|
|
|
uint64_t countb = b->GetCountWithAncestors();
|
|
|
|
if (counta == countb) {
|
|
|
|
return CompareTxMemPoolEntryByScore()(*a, *b);
|
|
|
|
}
|
|
|
|
return counta < countb;
|
|
|
|
}
|
2016-04-10 15:33:05 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-30 17:06:24 +02:00
|
|
|
std::vector<CTxMemPool::indexed_transaction_set::const_iterator> CTxMemPool::GetSortedDepthAndScore() const
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
2016-05-30 17:06:24 +02:00
|
|
|
std::vector<indexed_transaction_set::const_iterator> iters;
|
|
|
|
AssertLockHeld(cs);
|
|
|
|
|
|
|
|
iters.reserve(mapTx.size());
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2016-05-30 17:06:24 +02:00
|
|
|
for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi) {
|
|
|
|
iters.push_back(mi);
|
|
|
|
}
|
|
|
|
std::sort(iters.begin(), iters.end(), DepthAndScoreComparator());
|
|
|
|
return iters;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTxMemPool::queryHashes(vector<uint256>& vtxid)
|
|
|
|
{
|
2013-08-27 07:51:57 +02:00
|
|
|
LOCK(cs);
|
2016-05-30 17:06:24 +02:00
|
|
|
auto iters = GetSortedDepthAndScore();
|
|
|
|
|
|
|
|
vtxid.clear();
|
2013-08-27 07:51:57 +02:00
|
|
|
vtxid.reserve(mapTx.size());
|
2016-04-10 15:33:05 +02:00
|
|
|
|
2016-05-30 17:06:24 +02:00
|
|
|
for (auto it : iters) {
|
|
|
|
vtxid.push_back(it->GetTx().GetHash());
|
|
|
|
}
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
|
|
|
|
2016-07-31 20:53:17 +02:00
|
|
|
static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it) {
|
|
|
|
return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), CFeeRate(it->GetFee(), it->GetTxSize()), it->GetModifiedFee() - it->GetFee()};
|
|
|
|
}
|
|
|
|
|
2016-05-30 17:06:24 +02:00
|
|
|
std::vector<TxMempoolInfo> CTxMemPool::infoAll() const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
auto iters = GetSortedDepthAndScore();
|
|
|
|
|
|
|
|
std::vector<TxMempoolInfo> ret;
|
|
|
|
ret.reserve(mapTx.size());
|
|
|
|
for (auto it : iters) {
|
2016-07-31 20:53:17 +02:00
|
|
|
ret.push_back(GetInfo(it));
|
2016-05-30 17:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-20 18:19:26 +02:00
|
|
|
|
2016-11-11 02:34:17 +01:00
|
|
|
CTransactionRef CTxMemPool::get(const uint256& hash) const
|
2013-08-27 07:51:57 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
2015-06-24 10:32:20 +02:00
|
|
|
indexed_transaction_set::const_iterator i = mapTx.find(hash);
|
2016-05-30 17:06:24 +02:00
|
|
|
if (i == mapTx.end())
|
|
|
|
return nullptr;
|
|
|
|
return i->GetSharedTx();
|
2013-08-27 07:51:57 +02:00
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2016-05-30 17:06:24 +02:00
|
|
|
TxMempoolInfo CTxMemPool::info(const uint256& hash) const
|
2016-02-12 21:57:15 +01:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
indexed_transaction_set::const_iterator i = mapTx.find(hash);
|
|
|
|
if (i == mapTx.end())
|
2016-05-30 17:06:24 +02:00
|
|
|
return TxMempoolInfo();
|
2016-07-31 20:53:17 +02:00
|
|
|
return GetInfo(i);
|
2016-02-12 21:57:15 +01:00
|
|
|
}
|
|
|
|
|
2014-03-17 13:19:54 +01:00
|
|
|
CFeeRate CTxMemPool::estimateFee(int nBlocks) const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return minerPolicyEstimator->estimateFee(nBlocks);
|
|
|
|
}
|
2015-11-16 21:10:22 +01:00
|
|
|
CFeeRate CTxMemPool::estimateSmartFee(int nBlocks, int *answerFoundAtBlocks) const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2015-11-24 14:53:14 +01:00
|
|
|
return minerPolicyEstimator->estimateSmartFee(nBlocks, answerFoundAtBlocks, *this);
|
2015-11-16 21:10:22 +01:00
|
|
|
}
|
2014-03-17 13:19:54 +01:00
|
|
|
double CTxMemPool::estimatePriority(int nBlocks) const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return minerPolicyEstimator->estimatePriority(nBlocks);
|
|
|
|
}
|
2015-11-16 21:10:22 +01:00
|
|
|
double CTxMemPool::estimateSmartPriority(int nBlocks, int *answerFoundAtBlocks) const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2015-11-24 14:53:14 +01:00
|
|
|
return minerPolicyEstimator->estimateSmartPriority(nBlocks, answerFoundAtBlocks, *this);
|
2015-11-16 21:10:22 +01:00
|
|
|
}
|
2014-03-17 13:19:54 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
CTxMemPool::WriteFeeEstimates(CAutoFile& fileout) const
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
LOCK(cs);
|
2016-03-21 18:04:40 +01:00
|
|
|
fileout << 139900; // version required to read: 0.13.99 or later
|
2014-03-17 13:19:54 +01:00
|
|
|
fileout << CLIENT_VERSION; // version that wrote the file
|
|
|
|
minerPolicyEstimator->Write(fileout);
|
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const std::exception&) {
|
2015-07-31 16:41:06 +02:00
|
|
|
LogPrintf("CTxMemPool::WriteFeeEstimates(): unable to write policy estimator data (non-fatal)\n");
|
2014-03-17 13:19:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
int nVersionRequired, nVersionThatWrote;
|
|
|
|
filein >> nVersionRequired >> nVersionThatWrote;
|
|
|
|
if (nVersionRequired > CLIENT_VERSION)
|
2015-01-08 11:44:25 +01:00
|
|
|
return error("CTxMemPool::ReadFeeEstimates(): up-version (%d) fee estimate file", nVersionRequired);
|
2014-03-17 13:19:54 +01:00
|
|
|
LOCK(cs);
|
2016-03-21 18:04:40 +01:00
|
|
|
minerPolicyEstimator->Read(filein, nVersionThatWrote);
|
2014-03-17 13:19:54 +01:00
|
|
|
}
|
2014-12-07 13:29:06 +01:00
|
|
|
catch (const std::exception&) {
|
2015-07-31 16:41:06 +02:00
|
|
|
LogPrintf("CTxMemPool::ReadFeeEstimates(): unable to read policy estimator data (non-fatal)\n");
|
2014-03-17 13:19:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, const CAmount& nFeeDelta)
|
2012-07-11 20:52:41 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
2014-04-23 00:46:19 +02:00
|
|
|
std::pair<double, CAmount> &deltas = mapDeltas[hash];
|
2012-07-11 20:52:41 +02:00
|
|
|
deltas.first += dPriorityDelta;
|
|
|
|
deltas.second += nFeeDelta;
|
2015-10-26 19:06:06 +01:00
|
|
|
txiter it = mapTx.find(hash);
|
|
|
|
if (it != mapTx.end()) {
|
|
|
|
mapTx.modify(it, update_fee_delta(deltas.second));
|
2015-11-19 17:18:28 +01:00
|
|
|
// Now update all ancestors' modified fees with descendants
|
|
|
|
setEntries setAncestors;
|
|
|
|
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
|
|
|
|
std::string dummy;
|
|
|
|
CalculateMemPoolAncestors(*it, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
|
|
|
|
BOOST_FOREACH(txiter ancestorIt, setAncestors) {
|
|
|
|
mapTx.modify(ancestorIt, update_descendant_state(0, nFeeDelta, 0));
|
|
|
|
}
|
2015-10-26 19:06:06 +01:00
|
|
|
}
|
2012-07-11 20:52:41 +02:00
|
|
|
}
|
2014-04-23 00:46:19 +02:00
|
|
|
LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
|
2012-07-11 20:52:41 +02:00
|
|
|
}
|
|
|
|
|
2015-10-02 23:17:27 +02:00
|
|
|
void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) const
|
2012-07-11 20:52:41 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
2015-10-02 23:17:27 +02:00
|
|
|
std::map<uint256, std::pair<double, CAmount> >::const_iterator pos = mapDeltas.find(hash);
|
2012-07-11 20:52:41 +02:00
|
|
|
if (pos == mapDeltas.end())
|
|
|
|
return;
|
2014-04-23 00:46:19 +02:00
|
|
|
const std::pair<double, CAmount> &deltas = pos->second;
|
2012-07-11 20:52:41 +02:00
|
|
|
dPriorityDelta += deltas.first;
|
|
|
|
nFeeDelta += deltas.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTxMemPool::ClearPrioritisation(const uint256 hash)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
mapDeltas.erase(hash);
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
bool CTxMemPool::HasNoInputsOf(const CTransaction &tx) const
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++)
|
|
|
|
if (exists(tx.vin[i].prevout.hash))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2016-04-30 19:25:00 +02:00
|
|
|
CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) const {
|
2014-07-23 15:28:45 +02:00
|
|
|
// If an entry in the mempool exists, always return that one, as it's guaranteed to never
|
|
|
|
// conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
|
|
|
|
// transactions. First checking the underlying cache risks returning a pruned entry instead.
|
2016-11-11 02:34:17 +01:00
|
|
|
CTransactionRef ptx = mempool.get(txid);
|
2016-06-07 13:44:56 +02:00
|
|
|
if (ptx) {
|
|
|
|
coins = CCoins(*ptx, MEMPOOL_HEIGHT);
|
2013-11-05 02:47:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-07-23 15:28:45 +02:00
|
|
|
return (base->GetCoins(txid, coins) && !coins.IsPruned());
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
|
2013-11-05 02:47:07 +01:00
|
|
|
return mempool.exists(txid) || base->HaveCoins(txid);
|
|
|
|
}
|
2015-07-09 19:56:31 +02:00
|
|
|
|
|
|
|
size_t CTxMemPool::DynamicMemoryUsage() const {
|
|
|
|
LOCK(cs);
|
2015-10-19 21:15:12 +02:00
|
|
|
// Estimate the overhead of mapTx to be 15 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
|
2016-06-15 08:59:03 +02:00
|
|
|
return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 15 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(mapLinks) + memusage::DynamicUsage(vTxHashes) + cachedInnerUsage;
|
2015-07-15 20:47:45 +02:00
|
|
|
}
|
|
|
|
|
2015-10-19 16:54:28 +02:00
|
|
|
void CTxMemPool::RemoveStaged(setEntries &stage, bool updateDescendants) {
|
2015-07-15 20:47:45 +02:00
|
|
|
AssertLockHeld(cs);
|
2015-10-19 16:54:28 +02:00
|
|
|
UpdateForRemoveFromMempool(stage, updateDescendants);
|
2015-07-15 20:47:45 +02:00
|
|
|
BOOST_FOREACH(const txiter& it, stage) {
|
|
|
|
removeUnchecked(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 23:43:30 +02:00
|
|
|
int CTxMemPool::Expire(int64_t time) {
|
|
|
|
LOCK(cs);
|
2016-02-16 18:10:12 +01:00
|
|
|
indexed_transaction_set::index<entry_time>::type::iterator it = mapTx.get<entry_time>().begin();
|
2015-10-02 23:43:30 +02:00
|
|
|
setEntries toremove;
|
2016-02-16 18:10:12 +01:00
|
|
|
while (it != mapTx.get<entry_time>().end() && it->GetTime() < time) {
|
2015-10-02 23:43:30 +02:00
|
|
|
toremove.insert(mapTx.project<0>(it));
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
setEntries stage;
|
|
|
|
BOOST_FOREACH(txiter removeit, toremove) {
|
|
|
|
CalculateDescendants(removeit, stage);
|
|
|
|
}
|
2015-10-19 16:54:28 +02:00
|
|
|
RemoveStaged(stage, false);
|
2015-10-02 23:43:30 +02:00
|
|
|
return stage.size();
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
bool CTxMemPool::addUnchecked(const uint256&hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
setEntries setAncestors;
|
|
|
|
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
|
|
|
|
std::string dummy;
|
|
|
|
CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
|
|
|
|
return addUnchecked(hash, entry, setAncestors, fCurrentEstimate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTxMemPool::UpdateChild(txiter entry, txiter child, bool add)
|
|
|
|
{
|
|
|
|
setEntries s;
|
|
|
|
if (add && mapLinks[entry].children.insert(child).second) {
|
|
|
|
cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
|
|
|
|
} else if (!add && mapLinks[entry].children.erase(child)) {
|
|
|
|
cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTxMemPool::UpdateParent(txiter entry, txiter parent, bool add)
|
|
|
|
{
|
|
|
|
setEntries s;
|
|
|
|
if (add && mapLinks[entry].parents.insert(parent).second) {
|
|
|
|
cachedInnerUsage += memusage::IncrementalDynamicUsage(s);
|
|
|
|
} else if (!add && mapLinks[entry].parents.erase(parent)) {
|
|
|
|
cachedInnerUsage -= memusage::IncrementalDynamicUsage(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CTxMemPool::setEntries & CTxMemPool::GetMemPoolParents(txiter entry) const
|
|
|
|
{
|
|
|
|
assert (entry != mapTx.end());
|
|
|
|
txlinksMap::const_iterator it = mapLinks.find(entry);
|
|
|
|
assert(it != mapLinks.end());
|
|
|
|
return it->second.parents;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CTxMemPool::setEntries & CTxMemPool::GetMemPoolChildren(txiter entry) const
|
|
|
|
{
|
|
|
|
assert (entry != mapTx.end());
|
|
|
|
txlinksMap::const_iterator it = mapLinks.find(entry);
|
|
|
|
assert(it != mapLinks.end());
|
|
|
|
return it->second.children;
|
2015-07-09 19:56:31 +02:00
|
|
|
}
|
2015-10-02 23:19:55 +02:00
|
|
|
|
|
|
|
CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const {
|
|
|
|
LOCK(cs);
|
|
|
|
if (!blockSinceLastRollingFeeBump || rollingMinimumFeeRate == 0)
|
|
|
|
return CFeeRate(rollingMinimumFeeRate);
|
|
|
|
|
|
|
|
int64_t time = GetTime();
|
|
|
|
if (time > lastRollingFeeUpdate + 10) {
|
|
|
|
double halflife = ROLLING_FEE_HALFLIFE;
|
|
|
|
if (DynamicMemoryUsage() < sizelimit / 4)
|
|
|
|
halflife /= 4;
|
|
|
|
else if (DynamicMemoryUsage() < sizelimit / 2)
|
|
|
|
halflife /= 2;
|
|
|
|
|
|
|
|
rollingMinimumFeeRate = rollingMinimumFeeRate / pow(2.0, (time - lastRollingFeeUpdate) / halflife);
|
|
|
|
lastRollingFeeUpdate = time;
|
|
|
|
|
2015-10-14 21:44:18 +02:00
|
|
|
if (rollingMinimumFeeRate < minReasonableRelayFee.GetFeePerK() / 2) {
|
2015-10-02 23:19:55 +02:00
|
|
|
rollingMinimumFeeRate = 0;
|
2015-10-14 21:44:18 +02:00
|
|
|
return CFeeRate(0);
|
|
|
|
}
|
2015-10-02 23:19:55 +02:00
|
|
|
}
|
|
|
|
return std::max(CFeeRate(rollingMinimumFeeRate), minReasonableRelayFee);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTxMemPool::trackPackageRemoved(const CFeeRate& rate) {
|
|
|
|
AssertLockHeld(cs);
|
|
|
|
if (rate.GetFeePerK() > rollingMinimumFeeRate) {
|
|
|
|
rollingMinimumFeeRate = rate.GetFeePerK();
|
|
|
|
blockSinceLastRollingFeeBump = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 02:44:00 +02:00
|
|
|
void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<uint256>* pvNoSpendsRemaining) {
|
2015-10-02 23:19:55 +02:00
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
unsigned nTxnRemoved = 0;
|
|
|
|
CFeeRate maxFeeRateRemoved(0);
|
2016-06-18 18:16:36 +02:00
|
|
|
while (!mapTx.empty() && DynamicMemoryUsage() > sizelimit) {
|
2016-02-16 18:10:12 +01:00
|
|
|
indexed_transaction_set::index<descendant_score>::type::iterator it = mapTx.get<descendant_score>().begin();
|
2015-10-02 23:19:55 +02:00
|
|
|
|
2015-10-19 11:40:25 +02:00
|
|
|
// We set the new mempool min fee to the feerate of the removed set, plus the
|
|
|
|
// "minimum reasonable fee rate" (ie some value under which we consider txn
|
|
|
|
// to have 0 fee). This way, we don't allow txn to enter mempool with feerate
|
|
|
|
// equal to txn which were removed with no block in between.
|
2015-11-19 17:18:28 +01:00
|
|
|
CFeeRate removed(it->GetModFeesWithDescendants(), it->GetSizeWithDescendants());
|
2015-10-02 23:19:55 +02:00
|
|
|
removed += minReasonableRelayFee;
|
|
|
|
trackPackageRemoved(removed);
|
|
|
|
maxFeeRateRemoved = std::max(maxFeeRateRemoved, removed);
|
|
|
|
|
|
|
|
setEntries stage;
|
|
|
|
CalculateDescendants(mapTx.project<0>(it), stage);
|
|
|
|
nTxnRemoved += stage.size();
|
2015-10-22 02:44:00 +02:00
|
|
|
|
|
|
|
std::vector<CTransaction> txn;
|
|
|
|
if (pvNoSpendsRemaining) {
|
|
|
|
txn.reserve(stage.size());
|
2016-09-02 18:19:01 +02:00
|
|
|
BOOST_FOREACH(txiter iter, stage)
|
|
|
|
txn.push_back(iter->GetTx());
|
2015-10-22 02:44:00 +02:00
|
|
|
}
|
2015-10-19 16:54:28 +02:00
|
|
|
RemoveStaged(stage, false);
|
2015-10-22 02:44:00 +02:00
|
|
|
if (pvNoSpendsRemaining) {
|
|
|
|
BOOST_FOREACH(const CTransaction& tx, txn) {
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, tx.vin) {
|
|
|
|
if (exists(txin.prevout.hash))
|
|
|
|
continue;
|
2016-09-02 18:19:01 +02:00
|
|
|
auto iter = mapNextTx.lower_bound(COutPoint(txin.prevout.hash, 0));
|
|
|
|
if (iter == mapNextTx.end() || iter->first->hash != txin.prevout.hash)
|
2015-10-22 02:44:00 +02:00
|
|
|
pvNoSpendsRemaining->push_back(txin.prevout.hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 23:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (maxFeeRateRemoved > CFeeRate(0))
|
|
|
|
LogPrint("mempool", "Removed %u txn, rolling minimum fee bumped to %s\n", nTxnRemoved, maxFeeRateRemoved.ToString());
|
|
|
|
}
|