[Ethereum] What’s the difference between “pending” block and txpool

noncepending-transactionstransactionstxpool

Even though my parity node (private network) is not mining, it seems to have a pending block as well as a txpool.

After sending some transactions, the web3.eth.getBlock("pending") call returns information about the pending block including the pending transactions:

{ author: '0x819773969b2622a0a362fd137b088f90f88bd812',
  difficulty: '131072',
  extraData: '0x506172697479',
  gasLimit: 134086668,
  gasUsed: 3264884,
  hash: '0x084fd0478d7d7cea29ecb1a3734a3810933eddf26b1f18138711c93d83066759',
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  miner: '0x819773969b2622a0a362fd137b088f90f88bd812',
  number: 1,
  parentHash: '0xf5ef33749ed2fdfae1e6d1ce353d01d3d24cd58c37fd82f0afa78d71f206ffa0',
  receiptsRoot: '0x70e8edb579ffacaf92e02c9186fff660f9dfcf02e415d6d12f0d6523bcb67dfb',
  sealFields: [],
  sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
  size: 14575,
  stateRoot: '0xfd4a9996a00551085ba9c94c362ccbd7ae082683c428afac14de04b426a0015b',
  timestamp: 1520939275,
  totalDifficulty: '132096',
  transactions:
   [ '0xa6a6eb4ba46b798ca89c58770acaa6be030c5ea96bdf1e26f5a6e692625beb99',
     '0x77d18ff8c6304ec1f4e7e28605498eb2018e2d3089dd7e59b2124077f3b7956c',
     '0xddf1cfca112e3e4c653a8a14267f569015db6a1db9f0ba2144fec8f8cfdaebcc',
     '0x7cedab2409a862b922c19c260f137e94e625a84a08d17414ebc835ecca7c19a9',
     '0x7d2bb13e6c08c99f3e8cf07fce84842a46a979e547eb009e02b2ba659bcec296',
     '0x2be0a050cf877d1feb92924d4af313155bd65146fd24cb45cf08323370484c38',
     '0x02d55f8200a2176dea514a2481a7ddfa3e1fdf9e7514008a6030e4f0dc7dfe82',
     '0x88d40a05425cd2f5f56ab84ba1aca715b99fc3234fc11b5dfe48688f5de78139',
     '0xbd6f6e70aaf16916d34b56f3a96af1b344a44fc738a1da0eb0d55c94ffafcadd',
     '0xf966a75580009d506096396676fc5ea8f8da660fc265d7b83f954a6ee46fd061',
     '0x40509d3abaabc04367de8428f14d0763b798061180b04bb5a1833746d564e377',
     '0x41b50420117a8751b8d397a1f043e3767b29928fc0ecdc0d795fd0aa821df749',
     '0x172631800f2f4125e42feceb1db45d7ef0b0d0f205d09f089b2d877d310a99d8',
     '0x20629025b98574d7eb0744b252753cff4e65ff9070bb5d03c8540108e72027c4',
     '0xf7367abaa8c356e9057d36d8ce03e437546afb271b677f6aafdccb464867a5d8',
     '0xd5e6c9c04bd5b787612d90b9668d0be804e7ddc142186885f019654063fd37f8',

The web3.eth.getTransactionCount(<address>, "pending") call returns a transaction count of:

94

When making a curl request to make use of the parity-specific API endpoint parity_nextNonce which returns account transaction count based on the pending block as well as the txpool, it also returns the same number of transactions:

{"jsonrpc":"2.0","result":"0x5e","id":1}

I don't seem to have grasped the difference between the pending block and the txpool.

Initially I was under the impression that the txpool is where all the pending transactions are stored on a node (in memory) and that the pending block is only present when a node is mining (i.e. the block that's being solved for), but clearly that's not the case.

Could someone explain the difference between these two concepts in the context of pending transactions, the account nonce and pending transaction broadcasting?


Similar questions have been asked with no concrete answers:

getBlock('pending').transactions vs txpool.status.pending?

eth.pendingTransactions vs eth.getBlock('pending').transactions

Best Answer

It looks to me as though this is a bit of an optimisation/edge case that is causing some confusion - that the node assumes that the network that you are connected to is functioning normally i.e. that blocks will be mined by rational actors.

Initially I was under the impression that the txpool is where all the pending transactions are stored on a node

This is correct.

...that the pending block is only present when a node is mining (i.e. the block that's being solved for)

This, specifically, is the part where I think the node assumes that it's part of a network that is or will be functioning correctly in the future.


To help clarify, we can run through some scenarios:

  1. In the case above: where there are fewer transactions (TXs) than could be expected to fit into a block (based on the gas limit) then it is safe to assume that which ever node goes on to mine the next block will put all of the TXs from the mempool into the block (after all, there's an economic incentive for the miner to maximise their profit by including as many TXs as possible). So the node can make an intelligent guess at what the next block will look like.
  2. In the case where there are more TXs in the mempool than would be able to fit into the block, then it's more difficult for predict which TXs will be included in the block (as reordering/modifying the list of TXs impacts the mining of the block), but it's probably safe to assume that any miner will be aiming to include the TXs with the largest fees to maximise their profit.

Could someone explain the difference between these two concepts in the context of pending transactions, the account nonce and pending transaction broadcasting?

To summarise and hopefully answer these questions:

Valid TXs (either created on this node or received from a Peer) will be added to the node's, local mempool until they are included in a block (whether that block is mined by this node or another on the network). At which point the TX are removed from the mempool and included in the block.

Since we can expect that most of the nodes are rational and behaving correctly; it's possible to make an intelligent guess as to which TXs (from the mempool) will be included in the next block.

As an account makes TXs, the account's nonce will increase to enforce the ordering of TXs for that account. Even if the past TXs have not been included in a block, the account can continue to make further valid TXs as long as the nonce is incremented. They can appear in the mempool and could be expected to appear in the next block - depending on gas fees, num TXs in the mempool etc.

New TXs that are submitted on a node will be broadcast to all or a random subset of its peers (depending on how the node is coded). The new, pending block will only be broadcast to a node's peers when it is mined by a mining node.

Related Topic