Skip to content

CHIA-1808 Speedup requesting removals by using a cache #18895

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion chia/full_node/coin_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ class CoinStore:

db_wrapper: DBWrapper2
coins_added_at_height_cache: LRUCache[uint32, list[CoinRecord]]
coins_removed_at_height_cache: LRUCache[uint32, list[CoinRecord]]

@classmethod
async def create(cls, db_wrapper: DBWrapper2) -> CoinStore:
if db_wrapper.db_version != 2:
raise RuntimeError(f"CoinStore does not support database schema v{db_wrapper.db_version}")
self = CoinStore(db_wrapper, LRUCache(100))
self = CoinStore(db_wrapper, LRUCache(100), LRUCache(100))

async with self.db_wrapper.writer_maybe_transaction() as conn:
log.info("DB: Creating coin store tables and indexes.")
Expand Down Expand Up @@ -199,6 +200,11 @@ async def get_coins_removed_at_height(self, height: uint32) -> list[CoinRecord]:
# Special case to avoid querying all unspent coins (spent_index=0)
if height == 0:
return []

removed_coins_records = self.coins_removed_at_height_cache.get(height)
if removed_coins_records is not None:
return removed_coins_records

async with self.db_wrapper.reader_no_transaction() as conn:
async with conn.execute(
"SELECT confirmed_index, spent_index, coinbase, puzzle_hash, "
Expand All @@ -211,6 +217,7 @@ async def get_coins_removed_at_height(self, height: uint32) -> list[CoinRecord]:
coin = self.row_to_coin(row)
coin_record = CoinRecord(coin, row[0], row[1], row[2], row[6])
coins.append(coin_record)
self.coins_removed_at_height_cache.put(height, coins)
return coins

async def get_all_coins(self, include_spent_coins: bool) -> list[CoinRecord]:
Expand Down Expand Up @@ -567,6 +574,7 @@ async def rollback_to_block(self, block_index: int) -> list[CoinRecord]:

await conn.execute("UPDATE coin_record SET spent_index=0 WHERE spent_index>?", (block_index,))
self.coins_added_at_height_cache = LRUCache(self.coins_added_at_height_cache.capacity)
self.coins_removed_at_height_cache = LRUCache(self.coins_removed_at_height_cache.capacity)
return list(coin_changes.values())

# Store CoinRecord in DB
Expand Down
Loading