Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Witness hash storage tests, with repeated block
Browse files Browse the repository at this point in the history
Repeated persistence with the same block should not consume history
slots. I tested the test by patching into line 59 of
trinity/protocol/wit/db.py:
+ if True or block_hash not in recent_blocks_with_witnesses:

The test fails with that change, as expected.

Also, explicitly check that DB eviction works after a block repeat is
persisted.
  • Loading branch information
carver committed Nov 27, 2020
1 parent 2569092 commit 653f264
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/core/p2p-proto/test_wit_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,50 @@ async def test_witness_for_recent_blocks():
assert len(wit_db._get_recent_blocks_with_witnesses()) == wit_db._max_witness_history


@pytest.mark.asyncio
async def test_witness_history_on_repeat_blocks():
"""
Repeated blocks should not consume more slots in the limited history of block witnesses
"""
wit_db = AsyncWitnessDB(MemoryDB())
hash1 = Hash32Factory()
hash1_witnesses = tuple(Hash32Factory.create_batch(5))
await wit_db.coro_persist_witness_hashes(hash1, hash1_witnesses)

hash2 = Hash32Factory()
await wit_db.coro_persist_witness_hashes(hash2, tuple(Hash32Factory.create_batch(5)))

# *almost* push the first witness out of history
for _ in range(wit_db._max_witness_history - 2):
await wit_db.coro_persist_witness_hashes(Hash32Factory(), Hash32Factory.create_batch(2))

# It should still be there...
loaded_hashes = await wit_db.coro_get_witness_hashes(hash1)
assert set(loaded_hashes) == set(hash1_witnesses)

# Add one more new witness, for an existing block
await wit_db.coro_persist_witness_hashes(hash2, Hash32Factory.create_batch(2))

# That new witness should *not* consume a block slot in history, so the first hash's
# witness should still be available.
loaded_hashes = await wit_db.coro_get_witness_hashes(hash1)
assert set(loaded_hashes) == set(hash1_witnesses)


@pytest.mark.asyncio
async def test_witness_eviction_on_repeat_blocks():
"""
After witnesses are persisted twice for the same block, make sure that eviction
does not cause any failures.
"""
wit_db = AsyncWitnessDB(MemoryDB())
hash_ = Hash32Factory()
await wit_db.coro_persist_witness_hashes(hash_, Hash32Factory.create_batch(2))
await wit_db.coro_persist_witness_hashes(hash_, Hash32Factory.create_batch(2))
for _ in range(wit_db._max_witness_history):
await wit_db.coro_persist_witness_hashes(Hash32Factory(), Hash32Factory.create_batch(2))


@pytest.mark.asyncio
async def test_witness_union():
wit_db = AsyncWitnessDB(MemoryDB())
Expand Down

0 comments on commit 653f264

Please sign in to comment.