Skip to content

Commit

Permalink
more improvements 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jansegre committed Jun 1, 2023
1 parent f5d9663 commit 94718d2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions hathor/mining/block_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def generate_mining_block(self, rng: Random, merge_mined: bool = False, address:
if include_metadata:
block._metadata = TransactionMetadata(height=self.height, score=self.score)
block.get_metadata(use_storage=False)
block._update_height_metadata()
return block

def get_random_parents(self, rng: Random) -> Tuple[bytes, bytes, bytes]:
Expand Down
16 changes: 13 additions & 3 deletions hathor/transaction/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from hathor.transaction.exceptions import (
BlockWithInputs,
BlockWithTokensError,
CheckpointError,
InvalidBlockReward,
RewardLocked,
TransactionDataError,
Expand Down Expand Up @@ -285,9 +286,18 @@ def verify_checkpoint(self, checkpoints: List[Checkpoint]) -> None:
assert self.hash is not None
assert self.storage is not None
meta = self.get_metadata()
# XXX: it's fine to use `in` with NamedTuples
if Checkpoint(meta.get_height(soft=True), self.hash) in checkpoints:
return
height = meta.get_height(soft=True)
# find checkpoint with our height:
checkpoint: Optional[Checkpoint] = None
for cp in checkpoints:
if cp.height == height:
checkpoint = cp
break
if checkpoint is not None and checkpoint.hash != self.hash:
raise CheckpointError(f'Invalid new block {self.hash_hex}: checkpoint hash does not match')
else:
# TODO: check whether self is a parent of any checkpoint-valid block, this is left for a future PR
pass

def verify_weight(self) -> None:
"""Validate minimum block difficulty."""
Expand Down
2 changes: 1 addition & 1 deletion tests/tx/test_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_checkpoint_validation(self):
manager2 = self.create_peer('testnet', checkpoints=[Checkpoint(block2_height, block2.hash)])
del block2._metadata
block2.storage = manager2.tx_storage
block2.set_height(block2_height)
block2.set_soft_height(block2_height)
# it should fail if we don't pass sync_checkpoints=True
with self.assertRaises(InvalidNewTransaction):
manager2.on_new_tx(block2, partial=True, fails_silently=False)
Expand Down

0 comments on commit 94718d2

Please sign in to comment.