Skip to content

Instantly share code, notes, and snippets.

@ismaelsadeeq
Last active September 21, 2023 10:24
Show Gist options
  • Save ismaelsadeeq/ddf6a1f990fe6cf15f3557ddda333c7f to your computer and use it in GitHub Desktop.
Save ismaelsadeeq/ddf6a1f990fe6cf15f3557ddda333c7f to your computer and use it in GitHub Desktop.

Another attempt at #23074

1. Update TxStatsInfo to have the following additional fields:

  • ancestors: std::set of uint256
  • descendants: std::set of uint256
  • nSize: CAmount
  • txFee: uint64_t

2. When we receive a new transaction notification

If the transaction does not have any parents:

  • Create a new mapMemPoolTxs entry with 0 ancestors and 0 descendants.
  • Record fee statistics using the tx ancestorScore (which will basically be the same as the tx fee rate).

If the transaction has parent(s) (Use the m_has_no_mempool_parents boolean to decides that):

  • Create a new mapMemPoolTxs entry for the transaction.
  • Get the input txIds.
  • For each input (parent of the transaction), find their iterator in mapMemPoolTxs.
  • If the iterator exist:
    • Add the transaction as a descendant of the each parents.
    • Add the each parents as the ancestor of the transaction.
    • Add the transaction as a descendant to any of the parents' ancestors if they exist.
    • Add each of the parents ancestors as the transaction ancestors if they exist.
    • Assign the nSize fee and txFee.
  • Descendants are set to 0.

NB: Ancestors of the new transaction are the transaction's parents' ancestors plus the parents themselves.

  • Record the transaction in the fee stats using the transaction's ancestorScore.

3. When we Receive a notification that a Transaction is removed from mempool for any reason other than block

Get the mapMemPoolTxs entry of the transaction.

  • Remove the transaction from the fee stats (using the transaction's ancestorScore) and count it as a failure.
  • For all the ancestors of the transaction:
    • Remove the transaction from their list of descendants.
  • For all the descendants of the transaction:
    • Remove each descendant from the fee stats (using the ancestorScore) without counting as a failure.
    • Clear them from mapMemPoolTxs.

4. When Receiving a Notification of Transactions Removed After a Block Is Connected

Iterate through the vector of the transactions from the end so that children come first before ancestors.

Get the mapMemPoolTxs entry of the transaction.

If the transaction has descendant(s):

  • For all descendant(s) of the transaction:
    • Remove the transaction from their ancestor list.
    • Update the fee stats of each descendant to track the new ancestorScore.

If the transaction has ancestor(s) and its a CPFP[0]:

Record the fee stats of the transaction as a success with the ancestorScore.

For all the transaction's ancestors:

  • Remove them from the fee stats without counting as a failure. (We should continue here, as the CPFP is what we will consider for our fee estimate. The child transaction incentivizes the inclusion of all the ancestors, so there's no need to account them in fee estimation. Recording the child transaction as a success should be sufficient).
  • For all the descendants of each ancestor:
    • Remove the ancestor from the descendant's ancestor list.
    • Update the fee stats to be tracking the new ancestorScore of the descendant.

If the transaction has ancestor(s) and its not CPFP[0]:

  • Update the fee stats of the transaction to it’s actual fee rate not ancestorScore.

  • Record the fee stats of the transaction as a success with the transaction actual fee rate.

  • For all the transaction's ancestors:

    • Remove the transaction as a descendant

Note: AncestorScore will be calculated as CFeerate(sum of ancestors' txFee and the transaction's txFee, sum of the ancestors' nSize and the transaction's nSize).

[0]- We detect whether a transaction is a CPFP or not by checking if it's ancestor score is greater than its individual fee rate, then it sponsor all its ancestors to be confirmed in the new block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment