Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: concurrent send_tx shares web3 instance #1562

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions testground/benchmark/benchmark/sendtx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time

Check failure on line 1 in testground/benchmark/benchmark/sendtx.py

View workflow job for this annotation

GitHub Actions / Lint python

./testground/benchmark/benchmark/sendtx.py:1:1: F401 'time' imported but unused
from concurrent.futures import ThreadPoolExecutor, as_completed

import web3
Expand Down Expand Up @@ -31,7 +31,9 @@
return accounts


def sendtx(w3: web3.Web3, acct: Account, tx_amount: int):
def sendtx(acct: Account, tx_amount: int):
w3 = web3.Web3(web3.providers.HTTPProvider("http://localhost:8545"))
yihuang marked this conversation as resolved.
Show resolved Hide resolved
chain_id = w3.eth.chain_id
yihuang marked this conversation as resolved.
Show resolved Hide resolved
initial_nonce = w3.eth.get_transaction_count(acct.address)
print(
"test begin, address:",
Expand All @@ -50,20 +52,10 @@
"nonce": nonce,
"gas": 21000,
"gasPrice": GAS_PRICE,
"chainId": chain_id,
}
try:
send_transaction(w3, tx, acct, wait=False)
except ValueError as e:
msg = str(e)
if "invalid nonce" in msg:
print("invalid nonce and retry", nonce)
time.sleep(1)
continue
if "tx already in mempool" not in msg:
raise

send_transaction(w3, tx, acct, wait=False)
nonce += 1

if nonce % 100 == 0:
print(f"{acct.address} sent {nonce} transactions")

Expand All @@ -83,7 +75,7 @@
genesis_account = export_eth_account(cli, "account", **kwargs)
accounts = fund_test_accounts(w3, genesis_account, num_accounts)
with ThreadPoolExecutor(max_workers=num_accounts) as executor:
futs = (executor.submit(sendtx, w3, acct, num_txs) for acct in accounts)
futs = (executor.submit(sendtx, acct, num_txs) for acct in accounts)
yihuang marked this conversation as resolved.
Show resolved Hide resolved
for fut in as_completed(futs):
try:
fut.result()
Expand Down
Loading