- Demonstrates how to monitor new Ethereum transactions in the mempool using a Python script (web3.py + concurrency).
- Explains why transaction propagation matters for network performance and how to approximate how quickly transactions spread across different nodes.
- Shows how to tweak your endpoint usage (same vs. different) to measure local or network-wide propagation times more accurately.
- Concludes that regular mempool propagation testing helps reveal bottlenecks and fortifies blockchain reliability.
Main article
With the adoption of Ethereum and other blockchains that use the same kind of technology (we call these EVM blockchains), it’s important to make sure that the network is stable, safe, and efficient. One big thing developers and the people who manage the network need to keep an eye on is how transactions move around in the mempool. In this article, we’re going to show you how to test how blocks move in the mempool for EVM nodes using Chainstack. We’ll talk about why these tests are so important, the tools and tricks you’ll need, and some common problems and slowdowns that can happen when blocks are moving around.Why test transaction propagation?
The mempool is a temporary storage space where unconfirmed transactions wait to be included in a block. In a decentralized system, such as Ethereum, multiple nodes maintain separate mempools, but they constantly communicate with each other to sync up on the latest state of pending transactions. Propagation in the mempool predominantly affects two major factors: latency and network efficiency. Poor propagation can lead to increased transaction confirmation times and the risk of chain reorganizations or forks. Thus, testing propagation ensures the network performs optimally and maintains consensus.Learn more about the mempool by reading A developer’s guide to transactions in Ethereum mempool.
Roll your own propagation test
If you want to see how transactions and blocks are moving in a blockchain network, tools like web3.js or web3.py are your friends. They let you write your own scripts to understand better how things like the network setup, network conditions, and the number of transactions can make a difference. You can run these tests on your private networks or public ones like Goerli and Sepolia to mimic what happens in the real world. Pair this with network monitoring tools; you’ll get a clear image of your blockchain’s performance and where things might be slowing down. This can help you adjust your setup and keep things moving smoothly.We’ll be using the web3.py library to interact with the Ethereum network.
Prerequisites
- Python 3.6 or higher
- A Sepolia node with Chainstack
-
web3.py library. Install it with:
Getting started
Let’s start by importing the libraries we need and connecting to an Ethereum node usingWeb3 HTTPProvider.
Add your Sepolia node URL instead of
YOUR_CHAINSTACK_ENDPOINT.Making responses look nice
Next, we’ll create a function to make the transaction details we get from the Ethereum network look pretty. This function will also wait for the transaction receipt to update the block hash and block number when the transaction gets validated.Monitoring the mempool
Now, let’s whip up a two functions to check if there are any transactions from or to the address you’re watching in the mempool:You’ll notice we’re using
ThreadPoolExecutor to check pending transactions simultaneously, making everything quicker.Keeping an eye on transactions
With those helper functions ready, we can put together the main function to keep an eye on the mempool for new transactions involving the address you’re interested in:pretty_print_transaction(). After that, the main function will stop, but you can tweak the loop if you want it to keep watching for new transactions.
The full code
Here you can find the entire code for the monitoring tool:Running the test
To conduct this test, you’ll first need to execute the Python script. Then, initiate a transaction. For simplicity, we’ll use MetaMask in this scenario. Your objective determines whether you use the same or a different endpoint in comparison to your script. If you wish to measure the speed at which a transaction reaches the mempool of your own node, stick with the same endpoint as used in the script. However, if your goal is to determine the time taken for the transaction to propagate across other nodes in the Ethereum network, opt for a different endpoint. This will provide a more accurate depiction of transaction propagation times across the network.Learn how to add your Chainstack endpoint to MetaMask by reading Fault-tolerant transactions with MetaMask and Chainstack nodes.
- Start the script.
- Send a transaction using MetaMask.