What is a Mempool: Definition and Usage
In a blockchain transaction, a mempool, or memory pool, is a list of pending transactions waiting for validation from a node before it is committed to a block on the blockchain. This is similar to using 'Staging' environments in traditional software development to perform testing and quality control before releasing to production.
While traditional 'Staging' environments are centralized, each node has its own mempool to manage. Let's look at the transaction flow to understand the purpose of the mempool and how, by accessing it, we can learn important information:
Ethereum Transaction Flow
Ethereum transactions fall into 3 categories:
1) Simple Transfer - value is sent from one public address to another address. This could be a payment for an NFT or a service.
2) Deployment of a smart contract - whenever a smart contract is deployed on the blockchain, it creates a transaction.
3) Execution of a smart contract - if an address would like to send funds or data to a smart contract address.
You can monitor Ethereum transactions using an Alchemy's monitoring tool, and find the history of transactions for a given address by using tools like the Alchemy Transfers API
Every type of Ethereum transaction follows the same transaction lifecycle:
1) A user creates a transaction and signs using their private key.
2) A node receives the transaction and stores it in the mempool
3) While in the mempool, the node performs validation tests of the transaction. If the mempool pass the tests, the transaction status is moved to 'Pending'
4) The node broadcasts the pending transaction to all other nodes on the network to add the transaction to their own mempools.
5) A miner will then include the transactions with the highest gas prices once a new block is created. This will move the transaction to a 'Successful' state.
6) Transactions will receive confirmations after the node adds each new block after the one it is included in. This provides insurance that the transaction is permanently added to the blockchain, since the risk of block reorganizing decreases over time.
You can learn more about sending transactions and send your own here.
Purpose of a mempool
Since blockchains are immutable, it is important that transactions entered onto the blockchain are valid and without errors. The node hosting the mempool plays an important role in validating each transaction before the process is started to commit it to the blockchain.
For developers, it is important to understand the validation tests to understand why transactions might fail or be stuck in 'Pending' state:
1. Availability of Funds
The node validates whether the sender address has enough funds to cover the cost of both the funds being sent and the gas for completing the transaction. If this validation fails, the transaction will be removed from the mempool. It is important to use the correct parameters of sending and the destination address to ensure that this validation is passed.
2. Validity of Transaction Signatures
Each transaction needs to be signed by the sender's address. This is to identify that the sender is indeed the correct user. The node will validate that the signature is the correct one attached to the sender's address.
3. Correct Nonce Value
The nonce value is the number of transactions that have been sent from the sender address. Each time a sender transaction sends a new transaction, the nonce value is increased by 1. The nonce value is important because it forces each transaction from a given address to happen in the order in which they were sent.
When the transaction’s nonce values cannot be duplicated or skipped, this prevents the issue of double-spending or overspending available funds. The node validates that the correct nonce value is attached to the pending transaction. If this test fails, the transaction will not progress.
4. Gas Price
Depending on the mempool, there is a limit to how many transactions can be in the mempool at once. Geth, the popular Ethereum client, has a default limit of 4096 pending and 1024 queued transactions. When this limit is reached, the transactions with the lowest gas prices are removed from the pool to manage the space.
Access a mempool
A major advantage to the mempool structure is replacement transactions can overwrite pending transactions to correct, speed up, or cancel them altogether. You can find out how to speed up transactions so they are picked up by miners faster using Metamask.
Other than correcting pending transactions stuck in a mempool, accessing a mempool can be useful for other cases such as:
1. Estimating Gas Fees
Looking at the current pending transaction list and their connected fees can help avoid overpaying fees.
2. NFT Mining
By using a mempool scanner, users can get insight into pending transactions interacting with NFT contracts and outbid those transactions with higher gas fees.
3. Performance Insights
By using services like the Notify API, smart contract developers can get insights on smart contract activity from the mempool. Pending transactions can give information on bugs and vulnerabilities of a smart contract.
A mempool plays an important part in the function and health of a blockchain. Allowing nodes to validate transactions and miners to prioritize pending transactions according to gas fees is an essential part for the flow of transactions.
Developers can also use the data provided by a mempool to reduce costs and issues in their smart contracts. Mempools have been often described as "dark forests" of a blockchain but platforms like Alchemy Monitoring tools enable users to get more clarity in this critical step.