2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2009-2010 Satoshi Nakamoto
|
|
|
|
|
* Copyright (C) 2009-2015 The Bitcoin Core developers
|
2021-01-20 19:21:53 +01:00
|
|
|
* Copyright (C) 2017-2021 Tom Zander <tom@flowee.org>
|
2017-11-09 19:34:51 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2013-08-27 15:51:57 +10:00
|
|
|
|
2019-09-02 23:28:14 +02:00
|
|
|
#include "DoubleSpendProof.h"
|
|
|
|
|
#include "DoubleSpendProofStorage.h"
|
2013-08-27 15:51:57 +10:00
|
|
|
#include "txmempool.h"
|
2014-09-14 12:43:56 +02:00
|
|
|
|
2015-01-24 15:29:29 +01:00
|
|
|
#include "consensus/consensus.h"
|
2015-01-24 15:57:12 +01:00
|
|
|
#include "consensus/validation.h"
|
2014-11-11 21:06:15 -08:00
|
|
|
#include "main.h"
|
2019-03-11 14:47:12 +01:00
|
|
|
#include "streaming/streams.h"
|
2015-08-26 18:58:17 -07:00
|
|
|
#include "timedata.h"
|
2014-08-21 16:11:09 +02:00
|
|
|
#include "util.h"
|
2014-04-22 15:46:19 -07:00
|
|
|
#include "utilmoneystr.h"
|
2015-10-02 14:19:55 -07:00
|
|
|
#include "utiltime.h"
|
2018-07-23 18:05:43 +02:00
|
|
|
#include <core_memusage.h>
|
2013-08-27 15:51:57 +10:00
|
|
|
|
2018-07-23 18:05:43 +02:00
|
|
|
#include <utxo/UnspentOutputDatabase.h>
|
2018-01-15 15:26:12 +00:00
|
|
|
#include <validation/ValidationException.h>
|
2018-03-25 13:55:48 +02:00
|
|
|
#include <validationinterface.h>
|
2015-07-15 14:47:45 -04:00
|
|
|
|
2018-01-15 15:26:12 +00:00
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry(const Tx &tx)
|
|
|
|
|
: tx(tx),
|
|
|
|
|
nModFeesWithDescendants(0)
|
|
|
|
|
{
|
|
|
|
|
oldTx = tx.createOldTransaction();
|
|
|
|
|
nTime = ::GetTime();
|
|
|
|
|
nTxSize = tx.size();
|
|
|
|
|
nModSize = oldTx.CalculateModifiedSize(nTxSize);
|
|
|
|
|
nUsageSize = RecursiveDynamicUsage(oldTx);
|
2015-07-15 14:47:45 -04:00
|
|
|
nCountWithDescendants = 1;
|
|
|
|
|
nSizeWithDescendants = nTxSize;
|
2015-10-26 14:06:06 -04:00
|
|
|
|
|
|
|
|
feeDelta = 0;
|
2013-11-11 17:35:14 +10:00
|
|
|
}
|
|
|
|
|
|
2021-01-20 19:21:53 +01:00
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction &tx, int64_t _nFee,
|
2018-01-15 15:26:12 +00:00
|
|
|
int64_t _nTime, double _entryPriority, unsigned int _entryHeight,
|
2021-01-20 19:21:53 +01:00
|
|
|
bool poolHasNoInputsOf, int64_t _inChainInputValue,
|
2020-04-12 23:48:32 +02:00
|
|
|
bool _spendsCoinbase, LockPoints lp)
|
2018-01-15 15:26:12 +00:00
|
|
|
: CTxMemPoolEntry(Tx::fromOldTransaction(tx))
|
|
|
|
|
{
|
|
|
|
|
nFee = _nFee;
|
|
|
|
|
nModFeesWithDescendants = nFee;
|
|
|
|
|
nTime = _nTime;
|
|
|
|
|
entryPriority = _entryPriority;
|
|
|
|
|
entryHeight = _entryHeight;
|
|
|
|
|
hadNoDependencies = poolHasNoInputsOf;
|
|
|
|
|
inChainInputValue = _inChainInputValue;
|
|
|
|
|
spendsCoinbase = _spendsCoinbase;
|
|
|
|
|
lockPoints = lp;
|
|
|
|
|
|
|
|
|
|
assert(inChainInputValue <= oldTx.GetValueOut() + nFee);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-11 17:35:14 +10:00
|
|
|
CTxMemPoolEntry::CTxMemPoolEntry(const CTxMemPoolEntry& other)
|
|
|
|
|
{
|
|
|
|
|
*this = other;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double
|
|
|
|
|
CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
|
|
|
|
|
{
|
2015-11-13 10:05:21 -05:00
|
|
|
double deltaPriority = ((double)(currentHeight-entryHeight)*inChainInputValue)/nModSize;
|
2015-06-30 11:14:24 -04:00
|
|
|
double dResult = entryPriority + deltaPriority;
|
2015-11-13 10:05:21 -05:00
|
|
|
if (dResult < 0) // This should only happen if it was called with a height below entry height
|
|
|
|
|
dResult = 0;
|
2013-11-11 17:35:14 +10:00
|
|
|
return dResult;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-26 14:06:06 -04:00
|
|
|
void CTxMemPoolEntry::UpdateFeeDelta(int64_t newFeeDelta)
|
|
|
|
|
{
|
2015-11-19 11:18:28 -05:00
|
|
|
nModFeesWithDescendants += newFeeDelta - feeDelta;
|
2015-10-26 14:06:06 -04:00
|
|
|
feeDelta = newFeeDelta;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 15:01:22 -05:00
|
|
|
void CTxMemPoolEntry::UpdateLockPoints(const LockPoints& lp)
|
|
|
|
|
{
|
|
|
|
|
lockPoints = lp;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-15 14:47:45 -04:00
|
|
|
// Update the given tx for any in-mempool descendants.
|
|
|
|
|
// Assumes that setMemPoolChildren is correct for the given tx and all
|
|
|
|
|
// descendants.
|
|
|
|
|
bool CTxMemPool::UpdateForDescendants(txiter updateIt, int maxDescendantsToVisit, cacheMap &cachedDescendants, const std::set<uint256> &setExclude)
|
|
|
|
|
{
|
|
|
|
|
// Track the number of entries (outside setExclude) that we'd need to visit
|
|
|
|
|
// (will bail out if it exceeds maxDescendantsToVisit)
|
|
|
|
|
int nChildrenToVisit = 0;
|
|
|
|
|
|
|
|
|
|
setEntries stageEntries, setAllDescendants;
|
|
|
|
|
stageEntries = GetMemPoolChildren(updateIt);
|
|
|
|
|
|
|
|
|
|
while (!stageEntries.empty()) {
|
|
|
|
|
const txiter cit = *stageEntries.begin();
|
|
|
|
|
if (cit->IsDirty()) {
|
|
|
|
|
// Don't consider any more children if any descendant is dirty
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
setAllDescendants.insert(cit);
|
|
|
|
|
stageEntries.erase(cit);
|
|
|
|
|
const setEntries &setChildren = GetMemPoolChildren(cit);
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const txiter childEntry : setChildren) {
|
2015-07-15 14:47:45 -04:00
|
|
|
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.
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const txiter cacheEntry : cacheIt->second) {
|
2015-07-15 14:47:45 -04:00
|
|
|
// update visit count only for new child transactions
|
|
|
|
|
// (outside of setExclude and stageEntries)
|
|
|
|
|
if (setAllDescendants.insert(cacheEntry).second &&
|
|
|
|
|
!setExclude.count(cacheEntry->GetTx().GetHash()) &&
|
|
|
|
|
!stageEntries.count(cacheEntry)) {
|
|
|
|
|
nChildrenToVisit++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (!setAllDescendants.count(childEntry)) {
|
|
|
|
|
// Schedule for later processing and update our visit count
|
|
|
|
|
if (stageEntries.insert(childEntry).second && !setExclude.count(childEntry->GetTx().GetHash())) {
|
|
|
|
|
nChildrenToVisit++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (nChildrenToVisit > maxDescendantsToVisit) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// setAllDescendants now contains all in-mempool descendants of updateIt.
|
|
|
|
|
// Update and add to cached descendant map
|
|
|
|
|
int64_t modifySize = 0;
|
2021-01-20 19:21:53 +01:00
|
|
|
int64_t modifyFee = 0;
|
2015-07-15 14:47:45 -04:00
|
|
|
int64_t modifyCount = 0;
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter cit : setAllDescendants) {
|
2015-07-15 14:47:45 -04:00
|
|
|
if (!setExclude.count(cit->GetTx().GetHash())) {
|
|
|
|
|
modifySize += cit->GetTxSize();
|
2015-11-19 11:18:28 -05:00
|
|
|
modifyFee += cit->GetModifiedFee();
|
2015-07-15 14:47:45 -04:00
|
|
|
modifyCount++;
|
|
|
|
|
cachedDescendants[updateIt].insert(cit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mapTx.modify(updateIt, update_descendant_state(modifySize, modifyFee, modifyCount));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
std::map<COutPoint, CInPoint>::iterator iter = mapNextTx.lower_bound(COutPoint(hash, 0));
|
|
|
|
|
// First calculate the children, and update setMemPoolChildren to
|
|
|
|
|
// include them, and update their setMemPoolParents to include this tx.
|
|
|
|
|
for (; iter != mapNextTx.end() && iter->first.hash == hash; ++iter) {
|
|
|
|
|
const uint256 &childHash = iter->second.ptx->GetHash();
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!UpdateForDescendants(it, 100, mapMemPoolDescendantsToUpdate, setAlreadyIncluded)) {
|
|
|
|
|
// Mark as dirty if we can't do the calculation.
|
|
|
|
|
mapTx.modify(it, set_dirty());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 13:37:32 -04: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 */)
|
2015-07-15 14:47:45 -04:00
|
|
|
{
|
|
|
|
|
setEntries parentHashes;
|
|
|
|
|
const CTransaction &tx = entry.GetTx();
|
|
|
|
|
|
2015-09-23 13:37:32 -04: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 14:47:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
2015-09-23 13:37:32 -04: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 14:47:45 -04: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);
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const txiter &phash : setMemPoolParents) {
|
2015-07-15 14:47:45 -04:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-15 15:26:12 +00:00
|
|
|
void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, const setEntries &setAncestors)
|
2015-07-15 14:47:45 -04:00
|
|
|
{
|
|
|
|
|
setEntries parentIters = GetMemPoolParents(it);
|
|
|
|
|
// add or remove this tx as a child of each parent
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter piter : parentIters) {
|
2015-07-15 14:47:45 -04:00
|
|
|
UpdateChild(piter, it, add);
|
|
|
|
|
}
|
|
|
|
|
const int64_t updateCount = (add ? 1 : -1);
|
|
|
|
|
const int64_t updateSize = updateCount * it->GetTxSize();
|
2021-01-20 19:21:53 +01:00
|
|
|
const int64_t updateFee = updateCount * it->GetModifiedFee();
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter ancestorIt : setAncestors) {
|
2015-07-15 14:47:45 -04:00
|
|
|
mapTx.modify(ancestorIt, update_descendant_state(updateSize, updateFee, updateCount));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CTxMemPool::UpdateChildrenForRemoval(txiter it)
|
|
|
|
|
{
|
|
|
|
|
const setEntries &setMemPoolChildren = GetMemPoolChildren(it);
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter updateIt : setMemPoolChildren) {
|
2015-07-15 14:47:45 -04:00
|
|
|
UpdateParent(updateIt, it, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove)
|
|
|
|
|
{
|
|
|
|
|
// For each entry, walk back all ancestors and decrement size associated with this
|
|
|
|
|
// transaction
|
|
|
|
|
const uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter removeIt : entriesToRemove) {
|
2015-07-15 14:47:45 -04:00
|
|
|
setEntries setAncestors;
|
|
|
|
|
const CTxMemPoolEntry &entry = *removeIt;
|
|
|
|
|
std::string dummy;
|
2015-09-23 13:37:32 -04: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 14:47:45 -04:00
|
|
|
// Note that UpdateAncestorsOf severs the child links that point to
|
|
|
|
|
// removeIt in the entries for the parents of removeIt. This is
|
|
|
|
|
// fine since we don't need to use the mempool children of any entries
|
|
|
|
|
// to walk back over our ancestors (but we do need the mempool
|
|
|
|
|
// parents!)
|
|
|
|
|
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).
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter removeIt : entriesToRemove) {
|
2015-07-15 14:47:45 -04:00
|
|
|
UpdateChildrenForRemoval(removeIt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CTxMemPoolEntry::SetDirty()
|
|
|
|
|
{
|
|
|
|
|
nCountWithDescendants = 0;
|
|
|
|
|
nSizeWithDescendants = nTxSize;
|
2015-11-19 11:18:28 -05:00
|
|
|
nModFeesWithDescendants = GetModifiedFee();
|
2015-07-15 14:47:45 -04:00
|
|
|
}
|
|
|
|
|
|
2021-01-20 19:21:53 +01:00
|
|
|
void CTxMemPoolEntry::UpdateState(int64_t modifySize, int64_t modifyFee, int64_t modifyCount)
|
2015-07-15 14:47:45 -04:00
|
|
|
{
|
|
|
|
|
if (!IsDirty()) {
|
|
|
|
|
nSizeWithDescendants += modifySize;
|
|
|
|
|
assert(int64_t(nSizeWithDescendants) > 0);
|
2015-11-19 11:18:28 -05:00
|
|
|
nModFeesWithDescendants += modifyFee;
|
2015-07-15 14:47:45 -04:00
|
|
|
nCountWithDescendants += modifyCount;
|
|
|
|
|
assert(int64_t(nCountWithDescendants) > 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 12:38:46 +02:00
|
|
|
CTxMemPool::CTxMemPool() :
|
2018-07-23 18:05:43 +02:00
|
|
|
nTransactionsUpdated(0),
|
2019-09-02 23:28:14 +02:00
|
|
|
m_utxo(nullptr),
|
|
|
|
|
m_dspStorage(new DoubleSpendProofStorage())
|
2013-08-27 15:51:57 +10:00
|
|
|
{
|
2015-10-26 14:55:17 +01:00
|
|
|
_clear(); //lock free clear
|
2014-03-17 08:19:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CTxMemPool::~CTxMemPool()
|
|
|
|
|
{
|
2019-09-02 23:28:14 +02:00
|
|
|
delete m_dspStorage;
|
2013-08-27 15:51:57 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned int CTxMemPool::GetTransactionsUpdated() const
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
return nTransactionsUpdated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CTxMemPool::AddTransactionsUpdated(unsigned int n)
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
nTransactionsUpdated += n;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 15:25:15 +01:00
|
|
|
void CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, const setEntries &setAncestors)
|
2013-08-27 15:51:57 +10:00
|
|
|
{
|
|
|
|
|
// Add to memory pool without checking anything.
|
2018-01-15 15:26:12 +00:00
|
|
|
// Used by insertTx via TxValidationState which DOES do
|
2013-08-27 15:51:57 +10:00
|
|
|
// all the appropriate checks.
|
|
|
|
|
LOCK(cs);
|
2015-07-15 14:47:45 -04:00
|
|
|
indexed_transaction_set::iterator newit = mapTx.insert(entry).first;
|
|
|
|
|
mapLinks.insert(make_pair(newit, TxLinks()));
|
|
|
|
|
|
2015-11-19 11:18:28 -05:00
|
|
|
// Update transaction for any feeDelta created by PrioritiseTransaction
|
|
|
|
|
// TODO: refactor so that the fee delta is calculated before inserting
|
|
|
|
|
// into mapTx.
|
2021-01-20 19:21:53 +01:00
|
|
|
std::map<uint256, std::pair<double, int64_t> >::const_iterator pos = mapDeltas.find(hash);
|
2015-11-19 11:18:28 -05:00
|
|
|
if (pos != mapDeltas.end()) {
|
2021-01-20 19:21:53 +01:00
|
|
|
const std::pair<double, int64_t> &deltas = pos->second;
|
2015-11-19 11:18:28 -05:00
|
|
|
if (deltas.second) {
|
|
|
|
|
mapTx.modify(newit, update_fee_delta(deltas.second));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-15 14:47:45 -04: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++) {
|
2018-03-25 13:55:48 +02:00
|
|
|
mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, entry.tx, i);
|
2015-07-15 14:47:45 -04: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
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const uint256 &phash : setParentTransactions) {
|
2015-07-15 14:47:45 -04:00
|
|
|
txiter pit = mapTx.find(phash);
|
|
|
|
|
if (pit != mapTx.end()) {
|
|
|
|
|
UpdateParent(newit, pit, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
UpdateAncestorsOf(true, newit, setAncestors);
|
|
|
|
|
|
2014-08-26 16:28:32 -04:00
|
|
|
nTransactionsUpdated++;
|
|
|
|
|
totalTxSize += entry.GetTxSize();
|
2018-01-15 15:26:12 +00:00
|
|
|
}
|
2014-08-26 16:28:32 -04:00
|
|
|
|
2019-09-02 23:28:14 +02:00
|
|
|
bool CTxMemPool::insertTx(CTxMemPoolEntry &entry)
|
2018-01-15 15:26:12 +00:00
|
|
|
{
|
2018-07-23 18:05:43 +02:00
|
|
|
assert(m_utxo);
|
2019-09-02 23:28:14 +02:00
|
|
|
assert(entry.dsproof == -1);
|
2018-01-15 15:26:12 +00:00
|
|
|
LOCK(cs);
|
|
|
|
|
|
2020-01-11 13:06:58 +01:00
|
|
|
uint256 hash = entry.tx.createHash();
|
|
|
|
|
|
|
|
|
|
if (exists(hash))
|
2018-01-15 15:26:12 +00:00
|
|
|
return false;
|
|
|
|
|
|
2020-01-12 17:03:13 +01:00
|
|
|
std::list<std::pair<int, int> > rescuedOrphans;
|
2020-01-11 13:06:58 +01:00
|
|
|
for (const CTxIn &txin : entry.oldTx.vin) { // find double spends.
|
|
|
|
|
auto orphans = m_dspStorage->findOrphans(txin.prevout);
|
|
|
|
|
if (!orphans.empty()) {
|
|
|
|
|
for (auto o : orphans)
|
|
|
|
|
rescuedOrphans.push_back(o);
|
|
|
|
|
// if we find this here, AS AN ORPHAN, then nothing has entered the mempool yet
|
|
|
|
|
// that claimed it. As such we don't have to check for conflicts.
|
2020-01-12 17:03:13 +01:00
|
|
|
assert(mapNextTx.find(txin.prevout) == mapNextTx.end()); // Check anyway
|
2020-01-11 13:06:58 +01:00
|
|
|
continue;
|
2019-09-02 23:28:14 +02:00
|
|
|
}
|
2018-03-25 13:55:48 +02:00
|
|
|
auto oldTx = mapNextTx.find(txin.prevout);
|
|
|
|
|
if (oldTx != mapNextTx.end()) { // double spend detected!
|
2019-09-02 23:28:14 +02:00
|
|
|
auto iter = mapTx.find(oldTx->second.tx.createHash());
|
|
|
|
|
assert(mapTx.end() != iter);
|
2020-01-15 17:10:01 +01:00
|
|
|
int newProofId = -1;
|
2020-01-14 12:36:19 +01:00
|
|
|
try {
|
|
|
|
|
if (iter->dsproof == -1) { // no DS proof exists, lets make one.
|
|
|
|
|
auto item = *iter;
|
|
|
|
|
logWarning(Log::DSProof) << "Double spend found, creating double spend proof"
|
|
|
|
|
<< oldTx->second.tx.createHash()
|
|
|
|
|
<< entry.tx.createHash();
|
|
|
|
|
item.dsproof = m_dspStorage->add(DoubleSpendProof::create(oldTx->second.tx, entry.tx));
|
2020-09-29 17:01:52 +02:00
|
|
|
if (item.dsproof == -1) // DSP-hash already known
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-01-14 12:36:19 +01:00
|
|
|
mapTx.replace(iter, item);
|
|
|
|
|
newProofId = item.dsproof;
|
2019-09-04 14:11:53 +02:00
|
|
|
#ifndef NDEBUG
|
2020-01-14 12:36:19 +01:00
|
|
|
auto newIter = mapTx.find(oldTx->second.tx.createHash());
|
|
|
|
|
assert(newIter->dsproof == newProofId);
|
2019-09-04 14:11:53 +02:00
|
|
|
#endif
|
2020-01-14 12:36:19 +01:00
|
|
|
}
|
|
|
|
|
} catch (const std::runtime_error &e) {
|
|
|
|
|
// we don't support 100% of the types of transactions yet, failures are possible.
|
2020-08-10 10:10:09 +02:00
|
|
|
logWarning(Log::DSProof) << "Failed creating a proof:" << e;
|
2020-01-15 17:10:01 +01:00
|
|
|
throw Validation::Exception("Tx double spends another", 0);
|
2019-09-02 23:28:14 +02:00
|
|
|
}
|
2020-01-15 17:10:01 +01:00
|
|
|
throw Validation::DoubleSpendException(oldTx->second.tx, newProofId);
|
2018-03-25 13:55:48 +02:00
|
|
|
}
|
2018-01-15 15:26:12 +00:00
|
|
|
|
|
|
|
|
auto iter = mapTx.find(txin.prevout.hash);
|
|
|
|
|
if (iter != mapTx.end()) {
|
|
|
|
|
const CTransaction &prevTx = iter->GetTx();
|
|
|
|
|
if (txin.prevout.n < prevTx.vout.size() && !prevTx.vout[txin.prevout.n].IsNull())
|
|
|
|
|
continue; // found it in mempool.
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-23 18:05:43 +02:00
|
|
|
const UnspentOutput uo = m_utxo->find(txin.prevout.hash, txin.prevout.n);
|
|
|
|
|
if (!uo.isValid())
|
2018-01-15 15:26:12 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 13:06:58 +01:00
|
|
|
addUnchecked(hash, entry);
|
|
|
|
|
for (auto i = rescuedOrphans.begin(); i != rescuedOrphans.end(); ++i) {
|
2020-01-12 17:03:13 +01:00
|
|
|
const int proofId = i->first;
|
2020-01-11 13:06:58 +01:00
|
|
|
auto dsp = m_dspStorage->proof(proofId);
|
|
|
|
|
logDebug(Log::DSProof) << "Rescued a DSP orphan" << dsp.createHash();
|
|
|
|
|
auto rc = dsp.validate(mempool);
|
|
|
|
|
|
2020-01-12 17:03:13 +01:00
|
|
|
// it can't be missing utxo or transaction, assert we are internally consistent.
|
2020-01-11 13:06:58 +01:00
|
|
|
assert(rc == DoubleSpendProof::Valid
|
|
|
|
|
|| rc == DoubleSpendProof::Invalid);
|
|
|
|
|
|
|
|
|
|
if (rc == DoubleSpendProof::Valid) {
|
|
|
|
|
logDebug(Log::DSProof) << " Using it, it validated just fine";
|
2020-01-13 11:27:47 +01:00
|
|
|
m_dspStorage->claimOrphan(proofId);
|
2020-01-11 13:06:58 +01:00
|
|
|
entry.dsproof = proofId;
|
|
|
|
|
txiter iter = mapTx.find(hash);
|
|
|
|
|
mapTx.replace(iter, entry);
|
|
|
|
|
|
|
|
|
|
while (++i != rescuedOrphans.end()) {
|
|
|
|
|
logDebug(Log::DSProof) << "Killing orphans, we don't need more than one";
|
2020-01-12 17:03:13 +01:00
|
|
|
m_dspStorage->remove(i->first);
|
2020-01-11 13:06:58 +01:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
logDebug(Log::DSProof) << " DSP didn't validate!" << dsp.createHash();
|
|
|
|
|
m_dspStorage->remove(proofId);
|
2020-01-12 17:03:13 +01:00
|
|
|
|
|
|
|
|
LOCK(cs_main);
|
|
|
|
|
Misbehaving(i->second, 10);
|
2020-01-11 13:06:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-27 15:51:57 +10:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-15 14:47:45 -04:00
|
|
|
void CTxMemPool::removeUnchecked(txiter it)
|
|
|
|
|
{
|
2019-09-04 14:11:53 +02:00
|
|
|
if (it->dsproof != -1)
|
|
|
|
|
m_dspStorage->remove(it->dsproof);
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const CTxIn& txin : it->GetTx().vin)
|
2015-07-15 14:47:45 -04:00
|
|
|
mapNextTx.erase(txin.prevout);
|
|
|
|
|
|
|
|
|
|
totalTxSize -= it->GetTxSize();
|
|
|
|
|
cachedInnerUsage -= it->DynamicMemoryUsage();
|
|
|
|
|
cachedInnerUsage -= memusage::DynamicUsage(mapLinks[it].parents) + memusage::DynamicUsage(mapLinks[it].children);
|
|
|
|
|
mapLinks.erase(it);
|
|
|
|
|
mapTx.erase(it);
|
|
|
|
|
nTransactionsUpdated++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const txiter &childiter : setChildren) {
|
2015-07-15 14:47:45 -04:00
|
|
|
if (!setDescendants.count(childiter)) {
|
|
|
|
|
stage.insert(childiter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-27 15:51:57 +10:00
|
|
|
|
2014-11-18 14:57:20 -08:00
|
|
|
void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
|
2013-08-27 15:51:57 +10:00
|
|
|
{
|
|
|
|
|
// Remove transaction from memory pool
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
2015-07-15 14:47:45 -04:00
|
|
|
setEntries txToRemove;
|
|
|
|
|
txiter origit = mapTx.find(origTx.GetHash());
|
|
|
|
|
if (origit != mapTx.end()) {
|
|
|
|
|
txToRemove.insert(origit);
|
|
|
|
|
} else if (fRecursive) {
|
2015-03-25 13:13:09 -04:00
|
|
|
// If recursively removing but origTx isn't in the mempool
|
|
|
|
|
// 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++) {
|
|
|
|
|
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(origTx.GetHash(), i));
|
|
|
|
|
if (it == mapNextTx.end())
|
|
|
|
|
continue;
|
2015-07-15 14:47:45 -04:00
|
|
|
txiter nextit = mapTx.find(it->second.ptx->GetHash());
|
|
|
|
|
assert(nextit != mapTx.end());
|
|
|
|
|
txToRemove.insert(nextit);
|
2015-03-25 13:13:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
2015-07-15 14:47:45 -04:00
|
|
|
setEntries setAllRemoves;
|
|
|
|
|
if (fRecursive) {
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter it : txToRemove) {
|
2015-07-15 14:47:45 -04:00
|
|
|
CalculateDescendants(it, setAllRemoves);
|
2014-11-18 14:57:20 -08:00
|
|
|
}
|
2015-07-15 14:47:45 -04:00
|
|
|
} else {
|
|
|
|
|
setAllRemoves.swap(txToRemove);
|
2013-08-27 15:51:57 +10:00
|
|
|
}
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter it : setAllRemoves) {
|
2015-07-15 14:47:45 -04:00
|
|
|
removed.push_back(it->GetTx());
|
|
|
|
|
}
|
|
|
|
|
RemoveStaged(setAllRemoves);
|
2013-08-27 15:51:57 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-15 15:26:12 +00:00
|
|
|
void CTxMemPool::removeForReorg(unsigned int nMemPoolHeight, int flags)
|
2014-11-11 20:57:54 -08:00
|
|
|
{
|
2015-09-05 21:40:21 -07:00
|
|
|
// Remove transactions spending a coinbase which are now immature and no-longer-final transactions
|
2014-11-11 20:57:54 -08:00
|
|
|
LOCK(cs);
|
2017-08-04 19:02:56 +05:30
|
|
|
std::list<CTransaction> transactionsToRemove;
|
2015-06-24 03:32:20 -05:00
|
|
|
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
|
|
|
|
|
const CTransaction& tx = it->GetTx();
|
2015-12-04 15:01:22 -05:00
|
|
|
LockPoints lp = it->GetLockPoints();
|
|
|
|
|
bool validLP = TestLockPointValidity(&lp);
|
2026-04-15 21:35:08 +02:00
|
|
|
if (!CheckFinalTx(it->tx, flags) || !CheckSequenceLocks(*this, tx, flags, &lp, validLP)) {
|
2015-12-04 15:01:22 -05:00
|
|
|
// 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.
|
2015-08-26 18:58:17 -07:00
|
|
|
transactionsToRemove.push_back(tx);
|
2015-10-29 14:06:13 -04:00
|
|
|
} else if (it->GetSpendsCoinbase()) {
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const CTxIn& txin : tx.vin) {
|
2015-08-26 18:58:17 -07:00
|
|
|
indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
|
|
|
|
|
if (it2 != mapTx.end())
|
|
|
|
|
continue;
|
2018-07-23 18:05:43 +02:00
|
|
|
|
|
|
|
|
const UnspentOutput uo = m_utxo->find(txin.prevout.hash, txin.prevout.n);
|
|
|
|
|
if (!uo.isValid() || (uo.isCoinbase() && ((signed long)nMemPoolHeight) - uo.blockHeight() < COINBASE_MATURITY)) {
|
2015-08-26 18:58:17 -07:00
|
|
|
transactionsToRemove.push_back(tx);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-11-11 20:57:54 -08:00
|
|
|
}
|
|
|
|
|
}
|
2015-12-04 15:01:22 -05:00
|
|
|
if (!validLP) {
|
|
|
|
|
mapTx.modify(it, update_lock_points(lp));
|
|
|
|
|
}
|
2014-11-11 20:57:54 -08:00
|
|
|
}
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const CTransaction& tx : transactionsToRemove) {
|
2017-08-04 19:02:56 +05:30
|
|
|
std::list<CTransaction> removed;
|
2014-11-11 20:57:54 -08:00
|
|
|
remove(tx, removed, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 16:38:28 -05:00
|
|
|
void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
|
2013-08-27 15:51:57 +10:00
|
|
|
{
|
|
|
|
|
// Remove transactions which depend on inputs of tx, recursively
|
|
|
|
|
LOCK(cs);
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const CTxIn &txin : tx.vin) {
|
2013-08-27 15:51:57 +10:00
|
|
|
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout);
|
|
|
|
|
if (it != mapNextTx.end()) {
|
|
|
|
|
const CTransaction &txConflict = *it->second.ptx;
|
2019-06-28 12:38:46 +02:00
|
|
|
if (txConflict != tx) {
|
2014-02-15 16:38:28 -05:00
|
|
|
remove(txConflict, removed, true);
|
2015-08-03 15:49:01 -04:00
|
|
|
ClearPrioritisation(txConflict.GetHash());
|
2014-02-15 16:38:28 -05:00
|
|
|
}
|
2013-08-27 15:51:57 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-17 10:29:09 +08:00
|
|
|
/**
|
|
|
|
|
* Called when a block is connected. Removes from mempool and updates the miner fee estimator.
|
|
|
|
|
*/
|
2019-06-28 12:38:46 +02:00
|
|
|
void CTxMemPool::removeForBlock(const std::vector<CTransaction> &vtx, std::list<CTransaction> &conflicts)
|
2014-03-17 08:19:54 -04:00
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
2018-07-23 18:05:43 +02:00
|
|
|
for (const CTransaction& tx : vtx) {
|
2014-03-17 08:19:54 -04:00
|
|
|
std::list<CTransaction> dummy;
|
|
|
|
|
remove(tx, dummy, false);
|
|
|
|
|
removeConflicts(tx, conflicts);
|
2012-07-11 18:52:41 +00:00
|
|
|
ClearPrioritisation(tx.GetHash());
|
2014-03-17 08:19:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-26 14:55:17 +01:00
|
|
|
void CTxMemPool::_clear()
|
2013-08-27 15:51:57 +10:00
|
|
|
{
|
2015-07-15 14:47:45 -04:00
|
|
|
mapLinks.clear();
|
2013-08-27 15:51:57 +10:00
|
|
|
mapTx.clear();
|
|
|
|
|
mapNextTx.clear();
|
2014-08-06 23:58:19 -04:00
|
|
|
totalTxSize = 0;
|
2015-07-09 13:56:31 -04:00
|
|
|
cachedInnerUsage = 0;
|
2013-08-27 15:51:57 +10:00
|
|
|
++nTransactionsUpdated;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-26 14:55:17 +01:00
|
|
|
void CTxMemPool::clear()
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
_clear();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 19:02:56 +05:30
|
|
|
void CTxMemPool::queryHashes(std::vector<uint256>& vtxid)
|
2013-08-27 15:51:57 +10:00
|
|
|
{
|
|
|
|
|
vtxid.clear();
|
|
|
|
|
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
vtxid.reserve(mapTx.size());
|
2015-06-24 03:32:20 -05:00
|
|
|
for (indexed_transaction_set::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
|
|
|
|
|
vtxid.push_back(mi->GetTx().GetHash());
|
2013-08-27 15:51:57 +10:00
|
|
|
}
|
|
|
|
|
|
2019-03-14 21:10:02 +01:00
|
|
|
bool CTxMemPool::lookup(const uint256 &hash, CTransaction &result) const
|
2013-08-27 15:51:57 +10:00
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
2015-06-24 03:32:20 -05:00
|
|
|
indexed_transaction_set::const_iterator i = mapTx.find(hash);
|
2013-08-27 15:51:57 +10:00
|
|
|
if (i == mapTx.end()) return false;
|
2015-06-24 03:32:20 -05:00
|
|
|
result = i->GetTx();
|
2013-08-27 15:51:57 +10:00
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2020-09-14 15:26:55 +02:00
|
|
|
bool CTxMemPool::lookup(const uint256 &hash, Tx& result) const
|
2018-07-23 18:05:43 +02:00
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
indexed_transaction_set::const_iterator i = mapTx.find(hash);
|
|
|
|
|
if (i == mapTx.end()) return false;
|
|
|
|
|
result = i->tx;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 23:28:14 +02:00
|
|
|
bool CTxMemPool::lookup(const COutPoint &outpoint, Tx& result) const
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
|
|
auto oldTx = mapNextTx.find(outpoint);
|
|
|
|
|
if (oldTx == mapNextTx.end())
|
|
|
|
|
return false;
|
|
|
|
|
result = oldTx->second.tx;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-20 19:21:53 +01:00
|
|
|
void CTxMemPool::PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const int64_t& nFeeDelta)
|
2012-07-11 18:52:41 +00:00
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
2021-01-20 19:21:53 +01:00
|
|
|
std::pair<double, int64_t> &deltas = mapDeltas[hash];
|
2012-07-11 18:52:41 +00:00
|
|
|
deltas.first += dPriorityDelta;
|
|
|
|
|
deltas.second += nFeeDelta;
|
2015-10-26 14:06:06 -04:00
|
|
|
txiter it = mapTx.find(hash);
|
|
|
|
|
if (it != mapTx.end()) {
|
|
|
|
|
mapTx.modify(it, update_fee_delta(deltas.second));
|
2015-11-19 11:18:28 -05: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);
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter ancestorIt : setAncestors) {
|
2015-11-19 11:18:28 -05:00
|
|
|
mapTx.modify(ancestorIt, update_descendant_state(0, nFeeDelta, 0));
|
|
|
|
|
}
|
2015-10-26 14:06:06 -04:00
|
|
|
}
|
2012-07-11 18:52:41 +00:00
|
|
|
}
|
2014-04-22 15:46:19 -07:00
|
|
|
LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash, dPriorityDelta, FormatMoney(nFeeDelta));
|
2012-07-11 18:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-20 19:21:53 +01:00
|
|
|
void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, int64_t &nFeeDelta) const
|
2012-07-11 18:52:41 +00:00
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
2021-01-20 19:21:53 +01:00
|
|
|
std::map<uint256, std::pair<double, int64_t> >::const_iterator pos = mapDeltas.find(hash);
|
2012-07-11 18:52:41 +00:00
|
|
|
if (pos == mapDeltas.end())
|
|
|
|
|
return;
|
2021-01-20 19:21:53 +01:00
|
|
|
const std::pair<double, int64_t> &deltas = pos->second;
|
2012-07-11 18:52:41 +00:00
|
|
|
dPriorityDelta += deltas.first;
|
|
|
|
|
nFeeDelta += deltas.second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CTxMemPool::ClearPrioritisation(const uint256 hash)
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
mapDeltas.erase(hash);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-26 16:28:32 -04: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 08:19:54 -04:00
|
|
|
|
2015-07-09 13:56:31 -04:00
|
|
|
size_t CTxMemPool::DynamicMemoryUsage() const {
|
|
|
|
|
LOCK(cs);
|
2015-10-26 14:06:06 -04:00
|
|
|
// Estimate the overhead of mapTx to be 12 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
|
|
|
|
|
return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 12 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(mapLinks) + cachedInnerUsage;
|
2015-07-15 14:47:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CTxMemPool::RemoveStaged(setEntries &stage) {
|
|
|
|
|
AssertLockHeld(cs);
|
|
|
|
|
UpdateForRemoveFromMempool(stage);
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const txiter& it : stage) {
|
2015-07-15 14:47:45 -04:00
|
|
|
removeUnchecked(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-02 14:43:30 -07:00
|
|
|
int CTxMemPool::Expire(int64_t time) {
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
indexed_transaction_set::nth_index<2>::type::iterator it = mapTx.get<2>().begin();
|
|
|
|
|
setEntries toremove;
|
|
|
|
|
while (it != mapTx.get<2>().end() && it->GetTime() < time) {
|
|
|
|
|
toremove.insert(mapTx.project<0>(it));
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
setEntries stage;
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter removeit : toremove) {
|
2015-10-02 14:43:30 -07:00
|
|
|
CalculateDescendants(removeit, stage);
|
|
|
|
|
}
|
|
|
|
|
RemoveStaged(stage);
|
|
|
|
|
return stage.size();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 15:25:15 +01:00
|
|
|
void CTxMemPool::addUnchecked(const uint256&hash, const CTxMemPoolEntry &entry)
|
2015-07-15 14:47:45 -04:00
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
setEntries setAncestors;
|
|
|
|
|
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
|
|
|
|
|
std::string dummy;
|
|
|
|
|
CalculateMemPoolAncestors(entry, setAncestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy);
|
2020-01-04 15:25:15 +01:00
|
|
|
addUnchecked(hash, entry, setAncestors);
|
2015-07-15 14:47:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 13:56:31 -04:00
|
|
|
}
|
2015-10-02 14:19:55 -07:00
|
|
|
|
2018-07-23 18:05:43 +02:00
|
|
|
void CTxMemPool::setUtxo(UnspentOutputDatabase *utxo)
|
2018-01-15 15:26:12 +00:00
|
|
|
{
|
2018-07-23 18:05:43 +02:00
|
|
|
assert(utxo);
|
|
|
|
|
m_utxo = utxo;
|
2018-01-15 15:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-02 23:28:14 +02:00
|
|
|
Tx CTxMemPool::addDoubleSpendProof(const DoubleSpendProof &proof)
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
auto oldTx = mapNextTx.find(COutPoint(proof.prevTxId(), proof.prevOutIndex()));
|
|
|
|
|
if (oldTx == mapNextTx.end())
|
|
|
|
|
return Tx();
|
|
|
|
|
|
|
|
|
|
auto iter = mapTx.find(oldTx->second.tx.createHash());
|
|
|
|
|
assert(mapTx.end() != iter);
|
|
|
|
|
if (iter->dsproof != -1) // A DSProof already exists for this tx.
|
|
|
|
|
return Tx(); // don't propagate new one.
|
|
|
|
|
|
|
|
|
|
auto item = *iter;
|
|
|
|
|
item.dsproof = m_dspStorage->add(proof);
|
|
|
|
|
mapTx.replace(iter, item);
|
|
|
|
|
|
|
|
|
|
return oldTx->second.tx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DoubleSpendProofStorage *CTxMemPool::doubleSpendProofStorage() const
|
|
|
|
|
{
|
|
|
|
|
return m_dspStorage;
|
|
|
|
|
}
|
2018-01-15 15:26:12 +00:00
|
|
|
|
2020-03-19 22:03:08 +01:00
|
|
|
bool CTxMemPool::doubleSpendProofFor(const uint256 &txid, DoubleSpendProof &dsp)
|
|
|
|
|
{
|
|
|
|
|
LOCK(cs);
|
|
|
|
|
indexed_transaction_set::const_iterator i = mapTx.find(txid);
|
|
|
|
|
if (i == mapTx.end())
|
|
|
|
|
return false;
|
|
|
|
|
if (i->dsproof == -1)
|
|
|
|
|
return false;
|
|
|
|
|
dsp = doubleSpendProofStorage()->proof(i->dsproof);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 12:38:46 +02:00
|
|
|
CFeeRate CTxMemPool::GetMinFee() const
|
|
|
|
|
{
|
|
|
|
|
return CFeeRate();
|
2015-10-02 14:19:55 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-21 17:44:00 -07:00
|
|
|
void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<uint256>* pvNoSpendsRemaining) {
|
2015-10-02 14:19:55 -07:00
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
|
|
unsigned nTxnRemoved = 0;
|
|
|
|
|
while (DynamicMemoryUsage() > sizelimit) {
|
|
|
|
|
indexed_transaction_set::nth_index<1>::type::iterator it = mapTx.get<1>().begin();
|
|
|
|
|
setEntries stage;
|
|
|
|
|
CalculateDescendants(mapTx.project<0>(it), stage);
|
|
|
|
|
nTxnRemoved += stage.size();
|
2015-10-21 17:44:00 -07:00
|
|
|
|
|
|
|
|
std::vector<CTransaction> txn;
|
|
|
|
|
if (pvNoSpendsRemaining) {
|
|
|
|
|
txn.reserve(stage.size());
|
2018-12-30 15:33:11 +01:00
|
|
|
for (txiter it : stage)
|
2015-10-21 17:44:00 -07:00
|
|
|
txn.push_back(it->GetTx());
|
|
|
|
|
}
|
|
|
|
|
RemoveStaged(stage);
|
|
|
|
|
if (pvNoSpendsRemaining) {
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const CTransaction& tx : txn) {
|
|
|
|
|
for (const CTxIn& txin : tx.vin) {
|
2015-10-21 17:44:00 -07:00
|
|
|
if (exists(txin.prevout.hash))
|
|
|
|
|
continue;
|
|
|
|
|
std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(txin.prevout.hash, 0));
|
|
|
|
|
if (it == mapNextTx.end() || it->first.hash != txin.prevout.hash)
|
|
|
|
|
pvNoSpendsRemaining->push_back(txin.prevout.hash);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-02 14:19:55 -07:00
|
|
|
}
|
|
|
|
|
}
|