Skip to content

Commit

Permalink
CHIA-1849 Evict related BLS cache entries on new peak (#18912)
Browse files Browse the repository at this point in the history
Evict related BLS cache entries on new peak.
  • Loading branch information
AmineKhaldi authored Nov 22, 2024
1 parent 24e3644 commit cdd3ed2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
29 changes: 29 additions & 0 deletions chia/_tests/core/full_node/test_full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2439,3 +2439,32 @@ def check_nodes_in_sync2():

print(f"reorg1 timing: {reorg1_timing:0.2f}s")
print(f"reorg2 timing: {reorg2_timing:0.2f}s")


@pytest.mark.anyio
@pytest.mark.limit_consensus_modes(allowed=[ConsensusMode.HARD_FORK_2_0], reason="save time")
async def test_eviction_from_bls_cache(one_node_one_block: tuple[FullNodeSimulator, ChiaServer, BlockTools]) -> None:
"""
This test covers the case where adding a block to the blockchain evicts
all its pk msg pairs from the BLS cache.
"""
full_node_1, _, bt = one_node_one_block
blocks = bt.get_consecutive_blocks(
3, guarantee_transaction_block=True, farmer_reward_puzzle_hash=bt.pool_ph, pool_reward_puzzle_hash=bt.pool_ph
)
for block in blocks:
await full_node_1.full_node.add_block(block)
wt = bt.get_pool_wallet_tool()
reward_coins = blocks[-1].get_included_reward_coins()
# Setup a test block with two pk msg pairs
tx1 = wt.generate_signed_transaction(uint64(42), wt.get_new_puzzlehash(), reward_coins[0])
tx2 = wt.generate_signed_transaction(uint64(1337), wt.get_new_puzzlehash(), reward_coins[1])
tx = SpendBundle.aggregate([tx1, tx2])
await full_node_1.full_node.add_transaction(tx, tx.name(), None, test=True)
assert len(full_node_1.full_node._bls_cache.items()) == 2
blocks = bt.get_consecutive_blocks(
1, block_list_input=blocks, guarantee_transaction_block=True, transaction_data=tx
)
# Farming a block with this tx evicts those pk msg pairs from the BLS cache
await full_node_1.full_node.add_block(blocks[-1], None, full_node_1.full_node._bls_cache)
assert len(full_node_1.full_node._bls_cache.items()) == 0
7 changes: 7 additions & 0 deletions chia/full_node/full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
from chia.util.augmented_chain import AugmentedBlockchain
from chia.util.bech32m import encode_puzzle_hash
from chia.util.check_fork_next_block import check_fork_next_block
from chia.util.condition_tools import pkm_pairs
from chia.util.config import process_config_start_method
from chia.util.db_synchronous import db_synchronous_on
from chia.util.db_version import lookup_db_version, set_db_version_async
Expand Down Expand Up @@ -2125,6 +2126,12 @@ async def add_block(
raise RuntimeError("Expected block to be added, received disconnected block.")
return None
elif added == AddBlockResult.NEW_PEAK:
# Evict any related BLS cache entries as we no longer need them
if bls_cache is not None and pre_validation_result.conds is not None:
pairs_pks, pairs_msgs = pkm_pairs(
pre_validation_result.conds, self.constants.AGG_SIG_ME_ADDITIONAL_DATA
)
bls_cache.evict(pairs_pks, pairs_msgs)
# Only propagate blocks which extend the blockchain (becomes one of the heads)
assert state_change_summary is not None
post_process_time = time.monotonic()
Expand Down

0 comments on commit cdd3ed2

Please sign in to comment.