Skip to content

Commit

Permalink
Speedup WalletTransactionStore's _get_new_tx_records_from_old (#17529)
Browse files Browse the repository at this point in the history
Speedup WalletTransactionStore's _get_new_tx_records_from_old.
  • Loading branch information
AmineKhaldi authored Feb 9, 2024
1 parent d207705 commit 8fcff28
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions chia/wallet/wallet_transaction_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,27 +426,42 @@ async def delete_unconfirmed_transactions(self, wallet_id: int):
).close()

async def _get_new_tx_records_from_old(self, old_records: List[TransactionRecordOld]) -> List[TransactionRecord]:
tx_id_to_valid_times: Dict[bytes, ConditionValidTimes] = {}
empty_valid_times = ConditionValidTimes()
async with self.db_wrapper.reader_no_transaction() as conn:
valid_times: Dict[bytes32, ConditionValidTimes] = {}
chunked_records: List[List[TransactionRecordOld]] = [
old_records[i : min(len(old_records), i + self.db_wrapper.host_parameter_limit)]
for i in range(0, len(old_records), self.db_wrapper.host_parameter_limit)
]
for records_chunk in chunked_records:
cursor = await conn.execute(
f"SELECT txid, valid_times from tx_times WHERE txid IN ({','.join('?' * len(records_chunk))})",
f"SELECT txid, valid_times from tx_times WHERE txid IN ({','.join('?' * len(records_chunk))})",
tuple(tx.name for tx in records_chunk),
)
valid_times = {
**valid_times,
**{bytes32(res[0]): ConditionValidTimes.from_bytes(res[1]) for res in await cursor.fetchall()},
}
for row in await cursor.fetchall():
tx_id_to_valid_times[row[0]] = ConditionValidTimes.from_bytes(row[1])
await cursor.close()
return [
TransactionRecord(
valid_times=valid_times[record.name] if record.name in valid_times else ConditionValidTimes(),
confirmed_at_height=record.confirmed_at_height,
created_at_time=record.created_at_time,
to_puzzle_hash=record.to_puzzle_hash,
amount=record.amount,
fee_amount=record.fee_amount,
confirmed=record.confirmed,
sent=record.sent,
spend_bundle=record.spend_bundle,
**{k: v for k, v in dataclasses.asdict(record).items() if k != "spend_bundle"},
additions=record.additions,
removals=record.removals,
wallet_id=record.wallet_id,
sent_to=record.sent_to,
trade_id=record.trade_id,
type=record.type,
name=record.name,
memos=record.memos,
valid_times=(
tx_id_to_valid_times[record.name] if record.name in tx_id_to_valid_times else empty_valid_times
),
)
for record in old_records
]

0 comments on commit 8fcff28

Please sign in to comment.