Class: Mempool

mempool~Mempool(options)

Mempool Represents a mempool.

Constructor

new Mempool(options)

Create a mempool.

Extends:
  • EventEmitter
Parameters:
Name Type Description
options Object
Source:

Extends

  • EventEmitter

Methods

(async, private) _addBlock(block, txs) → {Promise}

Notify the mempool that a new block has come without a lock.

Parameters:
Name Type Description
block ChainEntry
txs Array.<TX>
Source:
Returns:
Type
Promise

(async, private) _addTX(tx, idnullable) → {Promise}

Add a transaction to the mempool without a lock.

Parameters:
Name Type Attributes Description
tx TX
id Number <nullable>
Source:
Returns:
Type
Promise

(private) _countAncestors(entry, set, child, map) → {Number}

Traverse ancestors and count.

Parameters:
Name Type Description
entry MempoolEntry
set Object
child MempoolEntry
map function
Source:
Returns:
Type
Number

(private) _countDescendants(entry, set) → {Number}

Count the highest number of descendants a transaction may have.

Parameters:
Name Type Description
entry MempoolEntry
set Object
Source:
Returns:
Type
Number

(private) _getAncestors(entry, entries, set) → {Array.<MempoolEntry>}

Get all transaction ancestors.

Parameters:
Name Type Description
entry MempoolEntry
entries Array.<MempoolEntry>
set Object
Source:
Returns:
Type
Array.<MempoolEntry>

_getDescendants(entry, entries, set) → {Array.<MempoolEntry>}

Get all a transaction descendants.

Parameters:
Name Type Description
entry MempoolEntry
entries Array.<MempoolEntry>
set Object
Source:
Returns:
Type
Array.<MempoolEntry>

(async) _getSpentView(tx) → {Promise}

Get coin viewpoint

Parameters:
Name Type Description
tx TX
Source:
Returns:
  • Returns CoinView
Type
Promise

(async, private) _handleReorg() → {Promise}

Sanitize the mempool after a reorg.

Source:
Returns:
Type
Promise

(async, private) _removeBlock(block, txs) → {Promise}

Notify the mempool that a block has been disconnected without a lock.

Parameters:
Name Type Description
block ChainEntry
txs Array.<TX>
Source:
Returns:
Type
Promise

(async, private) _reset()

Reset the mempool without a lock.

Source:

(async) addBlock(block, txs) → {Promise}

Notify the mempool that a new block has come in (removes all transactions contained in the block from the mempool).

Parameters:
Name Type Description
block ChainEntry
txs Array.<TX>
Source:
Returns:
Type
Promise

(async) addEntry(entry, view) → {Promise}

Add a transaction to the mempool without performing any validation. Note that this method does not lock the mempool and may lend itself to race conditions if used unwisely. This function will also resolve orphans if possible (the resolved orphans will be validated).

Parameters:
Name Type Description
entry MempoolEntry
view CoinView
Source:
Returns:
Type
Promise

(async) addTX(tx, idnullable) → {Promise}

Add a transaction to the mempool. Note that this will lock the mempool until the transaction is fully processed.

Parameters:
Name Type Attributes Description
tx TX
id Number <nullable>
Source:
Returns:
Type
Promise

(async) close() → {Promise}

Close the chain, wait for the database to close.

Source:
Returns:
Type
Promise

countAncestors(entry) → {Number}

Count the highest number of ancestors a transaction may have.

Parameters:
Name Type Description
entry MempoolEntry
Source:
Returns:
Type
Number

countDescendants(entry) → {Number}

Count the highest number of descendants a transaction may have.

Parameters:
Name Type Description
entry MempoolEntry
Source:
Returns:
Type
Number

evictEntry(entry)

Remove a transaction from the mempool. Recursively remove its spenders.

Parameters:
Name Type Description
entry MempoolEntry
Source:

(private) exists(hash) → {Boolean}

Test the mempool to see if it contains a transaction or an orphan.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
Boolean

getAncestors(entry) → {Array.<MempoolEntry>}

Get all transaction ancestors.

Parameters:
Name Type Description
entry MempoolEntry
Source:
Returns:
Type
Array.<MempoolEntry>

getBalance() → {Amount}

Return the full balance of all unspents in the mempool (not very useful in practice, only used for testing).

Source:
Returns:
Type
Amount

getCoin(hash, index) → {Coin}

Retrieve a coin from the mempool (unspents only).

Parameters:
Name Type Description
hash Hash
index Number
Source:
Returns:
Type
Coin

(async) getCoinView(tx) → {Promise}

Get coin viewpoint (no lock).

Parameters:
Name Type Description
tx TX
Source:
Returns:
  • Returns CoinView.
Type
Promise

getDepends(tx) → {Array.<Hash>}

Find a unconfirmed transactions that this transaction depends on.

Parameters:
Name Type Description
tx TX
Source:
Returns:
Type
Array.<Hash>

getDescendants(entry) → {Array.<MempoolEntry>}

Get all a transaction descendants.

Parameters:
Name Type Description
entry MempoolEntry
Source:
Returns:
Type
Array.<MempoolEntry>

getEntry(hash) → {MempoolEntry}

Retrieve a transaction from the mempool.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
MempoolEntry

getHistory() → {Array.<TX>}

Retrieve all transactions from the mempool.

Source:
Returns:
Type
Array.<TX>

getMeta(hash) → {TXMeta}

Retrieve a transaction from the mempool.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
TXMeta

getMetaByAddress(addr, options) → {Array.<TXMeta>}

Find all transactions pertaining to a certain address.

Parameters:
Name Type Description
addr Address
options Object
Properties
Name Type Description
limit Number
reverse Number
after Buffer
Source:
Returns:
Type
Array.<TXMeta>

getOrphan(hash) → {TX}

Retrieve an orphan transaction.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
TX

getSize() → {Number}

Calculate the memory usage of the entire mempool.

Source:
See:
  • DynamicMemoryUsage()
Returns:

Usage in bytes.

Type
Number

getSnapshot() → {Array.<Hash>}

Get a snapshot of all transaction hashes in the mempool. Used for generating INV packets in response to MEMPOOL packets.

Source:
Returns:
Type
Array.<Hash>

getSpent(hash, index) → {MempoolEntry}

Get an output's spender entry.

Parameters:
Name Type Description
hash Hash
index Number
Source:
Returns:
Type
MempoolEntry

getSpentTX(hash, index) → {MempoolEntry}

Get an output's spender transaction.

Parameters:
Name Type Description
hash Hash
index Number
Source:
Returns:
Type
MempoolEntry

(async) getSpentView(tx) → {Promise}

Get coin viewpoint (lock). Note: this does not return historical view of coins from the indexers.

Parameters:
Name Type Description
tx TX
Source:
Returns:
  • Returns CoinView.
Type
Promise

getTX(hash) → {TX}

Retrieve a transaction from the mempool.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
TX

getTXByAddress(addr, options) → {Array.<TX>}

Find all transactions pertaining to a certain address.

Parameters:
Name Type Description
addr Address
options Object
Properties
Name Type Description
limit Number
reverse Number
after Buffer
Source:
Returns:
Type
Array.<TX>

(async) handleOrphans(parent) → {Promise}

Resolve orphans and attempt to add to mempool.

Parameters:
Name Type Description
parent TX
Source:
Returns:
  • Returns TX[].
Type
Promise

has(hash) → {Boolean}

Test the mempool to see if it contains a transaction or an orphan.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
Boolean

hasCoin(hash, index) → {boolean}

Check whether coin is still unspent.

Parameters:
Name Type Description
hash Hash
index Number
Source:
Returns:
Type
boolean

hasDepends(tx) → {Boolean}

Test whether a transaction has dependencies.

Parameters:
Name Type Description
tx TX
Source:
Returns:
Type
Boolean

hasEntry(hash) → {Boolean}

Test the mempool to see if it contains a transaction.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
Boolean

hasOrphan(hash) → {Boolean}

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
Boolean

hasReject(hash) → {Boolean}

Test the mempool to see if it contains a recent reject.

Parameters:
Name Type Description
hash Hash
Source:
Returns:
Type
Boolean

(private) indexEntry(entry, view)

Index an entry by address.

Parameters:
Name Type Description
entry MempoolEntry
view CoinView
Source:

(async, private) insertTX(tx, idnullable) → {Promise}

Add a transaction to the mempool without a lock.

Parameters:
Name Type Attributes Description
tx TX
id Number <nullable>
Source:
Returns:
Type
Promise

isDoubleSpend(tx) → {Promise}

Test all of a transactions outpoints to see if they are doublespends. Note that this will only test against the mempool spents, not the blockchain's. The blockchain spents are not checked against because the blockchain does not maintain a spent list. The transaction will be seen as an orphan rather than a double spend.

Parameters:
Name Type Description
tx TX
Source:
Returns:
  • Returns Boolean.
Type
Promise

isSpent(hash, index) → {Boolean}

Check to see if a coin has been spent. This differs from ChainDB#isSpent in that it actually maintains a map of spent coins, whereas ChainDB may return true for transaction outputs that never existed.

Parameters:
Name Type Description
hash Hash
index Number
Source:
Returns:
Type
Boolean

limitOrphans() → {Boolean}

Remove a random orphan transaction from the mempool.

Source:
Returns:
Type
Boolean

limitSize(added) → {Promise}

Ensure the size of the mempool stays below maxSize. Evicts entries by timestamp and cumulative fee rate.

Parameters:
Name Type Description
added MempoolEntry
Source:
Returns:
Type
Promise

maybeOrphan(tx, view, id)

Maybe store an orphaned transaction.

Parameters:
Name Type Description
tx TX
view CoinView
id Number
Source:

(async) open() → {Promise}

Open the chain, wait for the database to load.

Source:
Returns:
Type
Promise

prioritise(entry, pri, fee)

Prioritise transaction.

Parameters:
Name Type Description
entry MempoolEntry
pri Number
fee Amount
Source:

(async) removeBlock(block, txs) → {Promise}

Notify the mempool that a block has been disconnected from the main chain (reinserts transactions into the mempool).

Parameters:
Name Type Description
block ChainEntry
txs Array.<TX>
Source:
Returns:
Type
Promise

(private) removeDoubleSpends(tx)

Recursively remove double spenders of a mined transaction's outpoints.

Parameters:
Name Type Description
tx TX
Source:

removeEntry(entry)

Remove a transaction from the mempool. Generally only called when a new block is added to the main chain.

Parameters:
Name Type Description
entry MempoolEntry
Source:

removeOrphan(tx) → {Boolean}

Remove a transaction from the mempool.

Parameters:
Name Type Description
tx Hash
Source:
Returns:
Type
Boolean

(private) removeSpenders(entry)

Recursively remove spenders of a transaction.

Parameters:
Name Type Description
entry MempoolEntry
Source:

(async) reset() → {Promise}

Reset the mempool.

Source:
Returns:
Type
Promise

resolveOrphans(parent) → {Array.<Orphan>}

Potentially resolve any transactions that redeem the passed-in transaction. Deletes all orphan entries and returns orphan objects.

Parameters:
Name Type Description
parent TX
Source:
Returns:
Type
Array.<Orphan>

(private) trackEntry(entry, view)

Map a transaction to the mempool.

Parameters:
Name Type Description
entry MempoolEntry
view CoinView
Source:

(private) unindexEntry(entry)

Unindex an entry by address.

Parameters:
Name Type Description
entry MempoolEntry
Source:

(private) untrackEntry(entry)

Unmap a transaction from the mempool.

Parameters:
Name Type Description
entry MempoolEntry
Source:

updateAncestors(entry, map) → {Number}

Count the highest number of ancestors a transaction may have. Update descendant fees and size.

Parameters:
Name Type Description
entry MempoolEntry
map function
Source:
Returns:
Type
Number

(async) verify(tx, view) → {Promise}

Verify a transaction with mempool standards.

Parameters:
Name Type Description
tx TX
view CoinView
Source:
Returns:
Type
Promise

verifyFinal(tx, flags) → {Promise}

Check locktime on a transaction against the current tip.

Parameters:
Name Type Description
tx TX
flags LockFlags
Source:
Returns:
  • Returns Boolean.
Type
Promise

(async) verifyInputs(tx, view, flags) → {Promise}

Verify inputs for standard and mandatory flags on failure.

Parameters:
Name Type Description
tx TX
view CoinView
flags VerifyFlags
Source:
Returns:
Type
Promise

verifyLocks(tx, view, flags) → {Promise}

Check sequence locks on a transaction against the current tip.

Parameters:
Name Type Description
tx TX
view CoinView
flags LockFlags
Source:
Returns:
  • Returns Boolean.
Type
Promise

(async) verifyResult(tx, view, flags) → {Promise}

Verify inputs, return a boolean instead of an error based on success.

Parameters:
Name Type Description
tx TX
view CoinView
flags VerifyFlags
Source:
Returns:
Type
Promise