Skip to content

Commit

Permalink
feat(consensus): Change
Browse files Browse the repository at this point in the history
  • Loading branch information
msbrogli committed Jun 16, 2023
1 parent a651eb1 commit 8abec23
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
15 changes: 11 additions & 4 deletions hathor/consensus/block_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,20 @@ def update_voided_info(self, block: Block) -> None:
# Either we have a single best chain or all chains have already been voided.
assert len(valid_heads) <= 1, 'We must never have more than one valid head'

# Add voided_by to all heads.
common_block = self._find_first_parent_in_best_chain(block)
self.add_voided_by_to_multiple_chains(block, heads, common_block)

winner = False
if score >= best_score + settings.WEIGHT_TOL:
winner = True
else:
min_hash: bytes = min(not_none(blk.hash) for blk in heads)
if block.hash < min_hash:
winner = True

if winner:
# Add voided_by to all heads.
self.add_voided_by_to_multiple_chains(block, heads, common_block)

# We have a new winner candidate.
self.update_score_and_mark_as_the_best_chain_if_possible(block)
# As `update_score_and_mark_as_the_best_chain_if_possible` may affect `voided_by`,
Expand All @@ -217,8 +226,6 @@ def update_voided_info(self, block: Block) -> None:
self.context.mark_as_reorg(common_block)
else:
storage.update_best_block_tips_cache([not_none(blk.hash) for blk in heads])
if not meta.voided_by:
self.context.mark_as_reorg(common_block)

def union_voided_by_from_parents(self, block: Block) -> set[bytes]:
"""Return the union of the voided_by of block's parents.
Expand Down
19 changes: 13 additions & 6 deletions tests/tx/test_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,28 @@ def test_multiple_forks(self):
sidechain.append(fork_block2)

# Now, both chains have the same score.
for block in blocks:
if blocks[-1].hash < sidechain[-1].hash:
winning_chain = blocks
losing_chain = sidechain
else:
winning_chain = sidechain
losing_chain = blocks

for block in winning_chain:
meta = block.get_metadata(force_reload=True)
self.assertEqual(meta.voided_by, {block.hash})
self.assertIsNone(meta.voided_by)

for block in sidechain:
for block in losing_chain:
meta = block.get_metadata(force_reload=True)
self.assertEqual(meta.voided_by, {block.hash})

for tx in txs1:
meta = tx.get_metadata(force_reload=True)
self.assertEqual(meta.first_block, block_before_fork.hash)

for tx in txs2:
meta = tx.get_metadata(force_reload=True)
self.assertIsNone(meta.first_block)
# for tx in txs2:
# meta = tx.get_metadata(force_reload=True)
# self.assertIsNone(meta.first_block)

# Mine 1 block, starting another fork.
# This block belongs to case (vi).
Expand Down

0 comments on commit 8abec23

Please sign in to comment.