Bitcoin Forum
May 11, 2024, 01:49:13 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How does CTxMemPool::addUnchecked works ???  (Read 391 times)
btctousd81 (OP)
Sr. Member
****
Offline Offline

Activity: 434
Merit: 270


View Profile WWW
September 09, 2017, 09:04:06 AM
Merited by ABCbits (1)
 #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<uint256, CAmount>::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<uint256> 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.

1715392153
Hero Member
*
Offline Offline

Posts: 1715392153

View Profile Personal Message (Offline)

Ignore
1715392153
Reply with quote  #2

1715392153
Report to moderator
1715392153
Hero Member
*
Offline Offline

Posts: 1715392153

View Profile Personal Message (Offline)

Ignore
1715392153
Reply with quote  #2

1715392153
Report to moderator
The block chain is the main innovation of Bitcoin. It is the first distributed timestamping system.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715392153
Hero Member
*
Offline Offline

Posts: 1715392153

View Profile Personal Message (Offline)

Ignore
1715392153
Reply with quote  #2

1715392153
Report to moderator
1715392153
Hero Member
*
Offline Offline

Posts: 1715392153

View Profile Personal Message (Offline)

Ignore
1715392153
Reply with quote  #2

1715392153
Report to moderator
1715392153
Hero Member
*
Offline Offline

Posts: 1715392153

View Profile Personal Message (Offline)

Ignore
1715392153
Reply with quote  #2

1715392153
Report to moderator
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6635


Just writing some code


View Profile WWW
September 09, 2017, 04:45:33 PM
Merited by ABCbits (1)
 #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.

btctousd81 (OP)
Sr. Member
****
Offline Offline

Activity: 434
Merit: 270


View Profile WWW
September 09, 2017, 06: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

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!