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:
- 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.
- 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.
const sendTransaction = async() => {
const transaction = await provider.sendTransaction(rawTransaction);
// wait() has the logic to return receipt once the transaction is mined
const receipt = await wait(transaction);
}
In this way you can write a wait in your code.
Best Answer
Your concept is bit wrong.
provider.on("pending", ...)
is called listener and it is should be called globaly. So it will listen forpending
transactions and when find one it would notify you by executing the function passed as second parameter.