Skip to content

Commit

Permalink
fix performance regression
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco committed Apr 12, 2024
1 parent 9e7caf3 commit f156d96
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
6 changes: 3 additions & 3 deletions hathor/verification/block_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
TransactionDataError,
WeightError,
)
from hathor.verification.verification_dependencies import BlockDependencies
from hathor.verification.verification_dependencies import BasicBlockDependencies, BlockDependencies


class BlockVerifier:
Expand All @@ -50,14 +50,14 @@ def verify_height(self, block: Block) -> None:
if meta.height < meta.min_height:
raise RewardLocked(f'Block needs {meta.min_height} height but has {meta.height}')

def verify_weight(self, block: Block, block_deps: BlockDependencies) -> None:
def verify_weight(self, block: Block, block_deps: BasicBlockDependencies) -> None:
"""Validate minimum block difficulty."""
min_block_weight = self._daa.calculate_block_difficulty(block, block_deps.storage)
if block.weight < min_block_weight - self._settings.WEIGHT_TOL:
raise WeightError(f'Invalid new block {block.hash_hex}: weight ({block.weight}) is '
f'smaller than the minimum weight ({min_block_weight})')

def verify_reward(self, block: Block, block_deps: BlockDependencies) -> None:
def verify_reward(self, block: Block, block_deps: BasicBlockDependencies) -> None:
"""Validate reward amount."""
parent_block = block_deps.storage.get_parent_block(block)
tokens_issued_per_block = self._daa.get_tokens_issued_per_block(parent_block.get_height() + 1)
Expand Down
23 changes: 19 additions & 4 deletions hathor/verification/verification_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,38 @@ class VertexDependencies:
storage: SimpleMemoryStorage


@dataclass(frozen=True, slots=True)
class BasicBlockDependencies(VertexDependencies):
"""A dataclass of dependencies necessary for basic block verification."""

@classmethod
def create(cls, block: Block, daa: DifficultyAdjustmentAlgorithm, *, skip_weight_verification: bool) -> Self:
"""Create a basic block dependencies instance."""
assert block.storage is not None
simple_storage = SimpleMemoryStorage()
daa_deps = [] if skip_weight_verification else daa.get_block_dependencies(block)
deps = block.parents + daa_deps

simple_storage.add_vertices_from_storage(block.storage, deps)

return cls(simple_storage)


@dataclass(frozen=True, slots=True)
class BlockDependencies(VertexDependencies):
"""A dataclass of dependencies necessary for block verification."""
signaling_state: BlockSignalingState
feature_info: dict[Feature, FeatureInfo]

@classmethod
def create(cls, block: Block, daa: DifficultyAdjustmentAlgorithm, feature_service: FeatureService) -> Self:
def create(cls, block: Block, feature_service: FeatureService) -> Self:
"""Create a block dependencies instance."""
assert block.storage is not None
signaling_state = feature_service.is_signaling_mandatory_features(block)
feature_info = feature_service.get_feature_info(block=block)
simple_storage = SimpleMemoryStorage()
daa_deps = daa.get_block_dependencies(block)
deps = block.parents + daa_deps

simple_storage.add_vertices_from_storage(block.storage, deps)
simple_storage.add_vertices_from_storage(block.storage, block.parents)
simple_storage.add_vertex(block) # we add the block itself so its metadata can be used as a dependency.

return cls(
Expand Down
22 changes: 15 additions & 7 deletions hathor/verification/verification_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
from hathor.transaction.transaction import TokenInfo
from hathor.transaction.validation_state import ValidationState
from hathor.types import TokenUid
from hathor.verification.verification_dependencies import BlockDependencies, TransactionDependencies
from hathor.verification.verification_dependencies import (
BasicBlockDependencies,
BlockDependencies,
TransactionDependencies,
)
from hathor.verification.vertex_verifiers import VertexVerifiers

cpu = get_cpu_profiler()
Expand Down Expand Up @@ -99,11 +103,15 @@ def verify_basic(self, vertex: BaseTransaction, *, skip_block_weight_verificatio
match vertex.version:
case TxVersion.REGULAR_BLOCK:
assert type(vertex) is Block
block_deps = BlockDependencies.create(vertex, self._daa, self._feature_service)
block_deps = BasicBlockDependencies.create(
vertex, self._daa, skip_weight_verification=skip_block_weight_verification
)
self._verify_basic_block(vertex, block_deps, skip_weight_verification=skip_block_weight_verification)
case TxVersion.MERGE_MINED_BLOCK:
assert type(vertex) is MergeMinedBlock
block_deps = BlockDependencies.create(vertex, self._daa, self._feature_service)
block_deps = BasicBlockDependencies.create(
vertex, self._daa, skip_weight_verification=skip_block_weight_verification
)
self._verify_basic_merge_mined_block(
vertex, block_deps, skip_weight_verification=skip_block_weight_verification
)
Expand All @@ -119,7 +127,7 @@ def verify_basic(self, vertex: BaseTransaction, *, skip_block_weight_verificatio
def _verify_basic_block(
self,
block: Block,
block_deps: BlockDependencies,
block_deps: BasicBlockDependencies,
*,
skip_weight_verification: bool
) -> None:
Expand All @@ -131,7 +139,7 @@ def _verify_basic_block(
def _verify_basic_merge_mined_block(
self,
block: MergeMinedBlock,
block_deps: BlockDependencies,
block_deps: BasicBlockDependencies,
*,
skip_weight_verification: bool
) -> None:
Expand Down Expand Up @@ -162,11 +170,11 @@ def verify(self, vertex: BaseTransaction, *, reject_locked_reward: bool = True)
match vertex.version:
case TxVersion.REGULAR_BLOCK:
assert type(vertex) is Block
block_deps = BlockDependencies.create(vertex, self._daa, self._feature_service)
block_deps = BlockDependencies.create(vertex, self._feature_service)
self._verify_block(vertex, block_deps)
case TxVersion.MERGE_MINED_BLOCK:
assert type(vertex) is MergeMinedBlock
block_deps = BlockDependencies.create(vertex, self._daa, self._feature_service)
block_deps = BlockDependencies.create(vertex, self._feature_service)
self._verify_merge_mined_block(vertex, block_deps)
case TxVersion.REGULAR_TRANSACTION:
assert type(vertex) is Transaction
Expand Down

0 comments on commit f156d96

Please sign in to comment.