Author

Topic: How does CTxMemPool::addUnchecked works ??? (Read 408 times)

sr. member
Activity: 434
Merit: 270
September 09, 2017, 01:16:42 PM
#3
Since CTxMempool uses a map, adding a duplicate entry would result in the new entry overwriting the original entry, but since they are duplicate, nothing would actually be changed. However that should not happen since addUnchecked is only used by AcceptToMemoryPoolWorker (which is an extension of AcceptToMemoryPool) which will do all of the checks for accepting a transaction such as making sure that we haven't already added the transaction to our mempool as well as all other transaction validity checks.
thank you, found in function

AcceptToMemoryPoolWorker

https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L446

on here https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L480

Code:
    // is it already in the memory pool?
    if (pool.exists(hash)) {
        return state.Invalid(false, REJECT_DUPLICATE, "txn-already-in-mempool");
    }


it rejects tx if its already in mempool.

thanks
staff
Activity: 3458
Merit: 6793
Just writing some code
September 09, 2017, 11:45:33 AM
#2
Since CTxMempool uses a map, adding a duplicate entry would result in the new entry overwriting the original entry, but since they are duplicate, nothing would actually be changed. However that should not happen since addUnchecked is only used by AcceptToMemoryPoolWorker (which is an extension of AcceptToMemoryPool) which will do all of the checks for accepting a transaction such as making sure that we haven't already added the transaction to our mempool as well as all other transaction validity checks.
sr. member
Activity: 434
Merit: 270
September 09, 2017, 04:04:06 AM
#1
from https://github.com/bitcoin/bitcoin/blob/master/src/txmempool.cpp#L360

i am trying to understand
Code:
CTxMemPool::addUnchecked
function.


Code:
bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool validFeeEstimate)
{
    NotifyEntryAdded(entry.GetSharedTx());
    // Add to memory pool without checking anything.
    // Used by AcceptToMemoryPool(), which DOES do
    // all the appropriate checks.
    LOCK(cs);
    indexed_transaction_set::iterator newit = mapTx.insert(entry).first;
    mapLinks.insert(make_pair(newit, TxLinks()));

    // Update transaction for any feeDelta created by PrioritiseTransaction
    // TODO: refactor so that the fee delta is calculated before inserting
    // into mapTx.
    std::map::const_iterator pos = mapDeltas.find(hash);
    if (pos != mapDeltas.end()) {
        const CAmount &delta = pos->second;
        if (delta) {
            mapTx.modify(newit, update_fee_delta(delta));
        }
    }

    // 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 setParentTransactions;
    for (unsigned int i = 0; i < tx.vin.size(); i++) {
        mapNextTx.insert(std::make_pair(&tx.vin[i].prevout, &tx));
        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
    for (const uint256 &phash : setParentTransactions) {
        txiter pit = mapTx.find(phash);
        if (pit != mapTx.end()) {
            UpdateParent(newit, pit, true);
        }
    }
    UpdateAncestorsOf(true, newit, setAncestors);
    UpdateEntryForAncestors(newit, setAncestors);

    nTransactionsUpdated++;
    totalTxSize += entry.GetTxSize();
    if (minerPolicyEstimator) {minerPolicyEstimator->processTransaction(entry, validFeeEstimate);}

    vTxHashes.emplace_back(tx.GetWitnessHash(), newit);
    newit->vTxHashesIdx = vTxHashes.size() - 1;

    return true;
}

lets say we already have a tx hash in our mempool, will
Code:
CTxMemPool::addUnchecked
add the already available tx hash in mempool again ?

or it will discard it ?

thanks for your time.
Jump to: