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 all 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
21 changes: 6 additions & 15 deletions testground/benchmark/benchmark/sendtx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
from concurrent.futures import ThreadPoolExecutor, as_completed

import web3
Expand Down Expand Up @@ -31,7 +30,9 @@ def fund_test_accounts(w3, from_account, num_accounts) -> [Account]:
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"))
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)
yihuang marked this conversation as resolved.
Show resolved Hide resolved
print(
"test begin, address:",
Expand All @@ -50,20 +51,10 @@ def sendtx(w3: web3.Web3, acct: Account, tx_amount: int):
"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 +74,7 @@ def generate_load(cli, num_accounts, num_txs, **kwargs):
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)
for fut in as_completed(futs):
yihuang marked this conversation as resolved.
Show resolved Hide resolved
try:
fut.result()
Expand Down
Loading