Skip to content

Commit

Permalink
fix unconfirmed tx race (Chia-Network#2266)
Browse files Browse the repository at this point in the history
* add tx lock

* init lock

* call with async
  • Loading branch information
almogdepaz authored Apr 21, 2021
1 parent 8c24306 commit 6af8681
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
17 changes: 9 additions & 8 deletions chia/rpc/wallet_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,9 @@ async def send_transaction(self, request):
fee = uint64(request["fee"])
else:
fee = uint64(0)
tx: TransactionRecord = await wallet.generate_signed_transaction(amount, puzzle_hash, fee)

await wallet.push_transaction(tx)
async with self.service.wallet_state_manager.tx_lock:
tx: TransactionRecord = await wallet.generate_signed_transaction(amount, puzzle_hash, fee)
await wallet.push_transaction(tx)

# Transaction may not have been included in the mempool yet. Use get_transaction to check.
return {
Expand Down Expand Up @@ -594,9 +594,9 @@ async def cc_spend(self, request):
fee = uint64(request["fee"])
else:
fee = uint64(0)

tx: TransactionRecord = await wallet.generate_signed_transaction([amount], [puzzle_hash], fee)
await wallet.wallet_state_manager.add_pending_transaction(tx)
async with self.service.wallet_state_manager.tx_lock:
tx: TransactionRecord = await wallet.generate_signed_transaction([amount], [puzzle_hash], fee)
await wallet.push_transaction(tx)

return {
"transaction": tx,
Expand Down Expand Up @@ -873,8 +873,9 @@ async def send_clawback_transaction(self, request):
wallet: RLWallet = self.service.wallet_state_manager.wallets[wallet_id]

fee = int(request["fee"])
tx = await wallet.clawback_rl_coin_transaction(fee)
await wallet.push_transaction(tx)
async with self.service.wallet_state_manager.tx_lock:
tx = await wallet.clawback_rl_coin_transaction(fee)
await wallet.push_transaction(tx)

# Transaction may not have been included in the mempool yet. Use get_transaction to check.
return {
Expand Down
3 changes: 3 additions & 0 deletions chia/wallet/wallet_state_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class WalletStateManager:
# Makes sure only one asyncio thread is changing the blockchain state at one time
lock: asyncio.Lock

tx_lock: asyncio.Lock

log: logging.Logger

# TODO Don't allow user to send tx until wallet is synced
Expand Down Expand Up @@ -119,6 +121,7 @@ async def create(
else:
self.log = logging.getLogger(__name__)
self.lock = asyncio.Lock()
self.tx_lock = asyncio.Lock()

self.log.debug(f"Starting in db path: {db_path}")
self.db_connection = await aiosqlite.connect(db_path)
Expand Down

0 comments on commit 6af8681

Please sign in to comment.