eth-brownie / brownie

A Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine.
https://eth-brownie.readthedocs.io
MIT License
2.65k stars 552 forks source link

Decrease in Transaction Creation Speed Over Time in Fork #1706

Open Akbar30Bill opened 1 year ago

Akbar30Bill commented 1 year ago

Environment information

What was wrong?

I use Brownie to create and execute transactions on a custom Anvil fork. Note that I am not using Brownie's development network cmd to run this fork; instead, I have forked the network separately and added the network using brownie networks add but generally, this case is also valid for other forking methods that I have tried such as ganache.

As I create more transactions, I've observed that the speed of transaction creation gradually decreases. After running my script for a while, hardware resource usage (CPU, RAM, Disk) significantly increases, and the CPU hits 100% usage (one entire core). This leads me to suspect that Brownie might have a bottleneck.

To Reproduce:

  1. Fork the network (anvil --base-fee 0 --fork-url <YOUR_FORK_URL> --port 42069 --auto-impersonate --compute-units-per-second 8000 --no-rate-limit)
  2. Add the network to brownie (brownie networks add Polygon anvil_polygon_mainnet_fork host='http://127.0.0.1:42069' chainid=137)
  3. Run the Brownie script to create transactions (brownie run scripts/bulktx.py --network anvil_polygon_mainnet_fork)
  4. Observe the decrease in transaction creation speed and increase in hardware resource usage over time.

Expected Behavior:

The transaction creation speed should remain consistent regardless of the number of transactions created without significant hardware resource increase.

Actual Behavior:

The transaction creation speed decreases over time, and hardware usage significantly increases, eventually hitting 100% CPU usage.

BulkTx script:

from brownie import accounts, web3
from web3.middleware import construct_simple_cache_middleware
from time import time

def main():
    web3.middleware_onion.add(construct_simple_cache_middleware(cache_class=dict))

    acc = accounts.at('<some public address that has balance in the forked network>', force=True)
    print(acc: {acc}')

    start_time, idx = time(), 0
    while idx := idx+1:
        tx = acc.transfer(acc.address, 1000000000000000)
        print(f'{idx}: {(time() - start_time)/idx}')

How it started: Screenshot 2023-08-28 at 16 55 18 Screenshot 2023-08-28 at 16 58 08 How it's going: Screenshot 2023-08-28 at 17 27 04 Screenshot 2023-08-28 at 17 26 07

Note: the speed in the screen shots shows the AVG time it takes to make a transaction and does not mean actual TPS speed

I am genuinely curious about why transaction creation slows down over time and would greatly appreciate any insights or solutions.

I appreciate your help!