Skip to content

Commit

Permalink
refactor(verification): remove VertexVerifier from BlockVerifier
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco committed Nov 16, 2023
1 parent b076a15 commit cbdf80b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 53 deletions.
8 changes: 2 additions & 6 deletions hathor/verification/block_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,19 @@
TransactionDataError,
WeightError,
)
from hathor.verification.vertex_verifier import VertexVerifier


class BlockVerifier:
__slots__ = ('_settings', '_vertex_verifier', '_daa', '_feature_service')
__slots__ = ('_settings', '_daa', '_feature_service')

def __init__(
self,
*,
settings: HathorSettings,
vertex_verifier: VertexVerifier,
daa: DifficultyAdjustmentAlgorithm,
feature_service: FeatureService | None = None
) -> None:
self._settings = settings
self._vertex_verifier = vertex_verifier
self._daa = daa
self._feature_service = feature_service

Expand Down Expand Up @@ -74,8 +71,7 @@ def verify_no_inputs(self, block: Block) -> None:
if inputs:
raise BlockWithInputs('number of inputs {}'.format(len(inputs)))

def verify_outputs(self, block: Block) -> None:
self._vertex_verifier.verify_outputs(block)
def verify_output_token_indexes(self, block: Block) -> None:
for output in block.outputs:
if output.get_token_index() > 0:
raise BlockWithTokensError('in output: {}'.format(output.to_human_readable()))
Expand Down
3 changes: 2 additions & 1 deletion hathor/verification/verification_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ def _verify_without_storage_block(self, block: Block) -> None:
"""
self.verifiers.vertex.verify_pow(block)
self.verifiers.block.verify_no_inputs(block)
self.verifiers.block.verify_outputs(block)
self.verifiers.vertex.verify_outputs(block)
self.verifiers.block.verify_output_token_indexes(block)
self.verifiers.block.verify_data(block)
self.verifiers.vertex.verify_sigops_output(block)

Expand Down
7 changes: 1 addition & 6 deletions hathor/verification/vertex_verifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ def create(
"""
Create a VertexVerifiers instance using a custom vertex_verifier.
"""
block_verifier = BlockVerifier(
settings=settings,
vertex_verifier=vertex_verifier,
daa=daa,
feature_service=feature_service
)
block_verifier = BlockVerifier(settings=settings, daa=daa, feature_service=feature_service)
merge_mined_block_verifier = MergeMinedBlockVerifier()
tx_verifier = TransactionVerifier(settings=settings, vertex_verifier=vertex_verifier, daa=daa)
token_creation_tx_verifier = TokenCreationTransactionVerifier(settings=settings)
Expand Down
6 changes: 3 additions & 3 deletions tests/tx/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_verify_must_signal_when_feature_activation_is_disabled(is_signaling_man
settings.FEATURE_ACTIVATION.enable_usage = False
feature_service = Mock(spec_set=FeatureService)
feature_service.is_signaling_mandatory_features = Mock(return_value=is_signaling_mandatory_features)
verifier = BlockVerifier(settings=settings, feature_service=feature_service, daa=Mock(), vertex_verifier=Mock())
verifier = BlockVerifier(settings=settings, feature_service=feature_service, daa=Mock())
block = Block()

verifier.verify_mandatory_signaling(block)
Expand All @@ -160,7 +160,7 @@ def test_verify_must_signal() -> None:
feature_service.is_signaling_mandatory_features = Mock(
return_value=BlockIsMissingSignal(feature=Feature.NOP_FEATURE_1)
)
verifier = BlockVerifier(settings=settings, feature_service=feature_service, daa=Mock(), vertex_verifier=Mock())
verifier = BlockVerifier(settings=settings, feature_service=feature_service, daa=Mock())
block = Block()

with pytest.raises(BlockMustSignalError) as e:
Expand All @@ -174,7 +174,7 @@ def test_verify_must_not_signal() -> None:
settings.FEATURE_ACTIVATION.enable_usage = True
feature_service = Mock(spec_set=FeatureService)
feature_service.is_signaling_mandatory_features = Mock(return_value=BlockIsSignaling())
verifier = BlockVerifier(settings=settings, feature_service=feature_service, daa=Mock(), vertex_verifier=Mock())
verifier = BlockVerifier(settings=settings, feature_service=feature_service, daa=Mock())
block = Block()

verifier.verify_mandatory_signaling(block)
2 changes: 1 addition & 1 deletion tests/tx/test_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def test_block_outputs(self):
storage=self.tx_storage)

with self.assertRaises(TooManyOutputs):
self._verifiers.block.verify_outputs(block)
self._verifiers.vertex.verify_outputs(block)

def test_tx_number_parents(self):
genesis_block = self.genesis_blocks[0]
Expand Down
72 changes: 36 additions & 36 deletions tests/tx/test_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,45 +123,45 @@ def test_block_verify_basic(self) -> None:
def test_block_verify_without_storage(self) -> None:
block = self._get_valid_block()

vertex_verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)

verify_pow_wrapped = Mock(wraps=self.verifiers.vertex.verify_pow)
verify_no_inputs_wrapped = Mock(wraps=self.verifiers.block.verify_no_inputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.block.verify_outputs)
verify_output_token_indexes_wrapped = Mock(wraps=self.verifiers.block.verify_output_token_indexes)
verify_number_of_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_number_of_outputs)
verify_data_wrapped = Mock(wraps=self.verifiers.block.verify_data)
verify_sigops_output_wrapped = Mock(wraps=self.verifiers.vertex.verify_sigops_output)

with (
patch.object(VertexVerifier, 'verify_outputs', vertex_verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_pow', verify_pow_wrapped),
patch.object(BlockVerifier, 'verify_no_inputs', verify_no_inputs_wrapped),
patch.object(BlockVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(BlockVerifier, 'verify_output_token_indexes', verify_output_token_indexes_wrapped),
patch.object(VertexVerifier, 'verify_number_of_outputs', verify_number_of_outputs_wrapped),
patch.object(BlockVerifier, 'verify_data', verify_data_wrapped),
patch.object(VertexVerifier, 'verify_sigops_output', verify_sigops_output_wrapped),
):
self.manager.verification_service.verify_without_storage(block)

# Vertex methods
vertex_verify_outputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()

# Block methods
verify_pow_wrapped.assert_called_once()
verify_no_inputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()
verify_output_token_indexes_wrapped.assert_called_once()
verify_number_of_outputs_wrapped.assert_called_once()
verify_data_wrapped.assert_called_once()
verify_sigops_output_wrapped.assert_called_once()

def test_block_verify(self) -> None:
block = self._get_valid_block()

vertex_verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)

verify_pow_wrapped = Mock(wraps=self.verifiers.vertex.verify_pow)
verify_no_inputs_wrapped = Mock(wraps=self.verifiers.block.verify_no_inputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.block.verify_outputs)
verify_output_token_indexes_wrapped = Mock(wraps=self.verifiers.block.verify_output_token_indexes)
verify_number_of_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_number_of_outputs)
verify_data_wrapped = Mock(wraps=self.verifiers.block.verify_data)
verify_sigops_output_wrapped = Mock(wraps=self.verifiers.vertex.verify_sigops_output)
Expand All @@ -170,10 +170,10 @@ def test_block_verify(self) -> None:
verify_mandatory_signaling_wrapped = Mock(wraps=self.verifiers.block.verify_mandatory_signaling)

with (
patch.object(VertexVerifier, 'verify_outputs', vertex_verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_pow', verify_pow_wrapped),
patch.object(BlockVerifier, 'verify_no_inputs', verify_no_inputs_wrapped),
patch.object(BlockVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(BlockVerifier, 'verify_output_token_indexes', verify_output_token_indexes_wrapped),
patch.object(VertexVerifier, 'verify_number_of_outputs', verify_number_of_outputs_wrapped),
patch.object(BlockVerifier, 'verify_data', verify_data_wrapped),
patch.object(VertexVerifier, 'verify_sigops_output', verify_sigops_output_wrapped),
Expand All @@ -184,12 +184,12 @@ def test_block_verify(self) -> None:
self.manager.verification_service.verify(block)

# Vertex methods
vertex_verify_outputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()

# Block methods
verify_pow_wrapped.assert_called_once()
verify_no_inputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()
verify_output_token_indexes_wrapped.assert_called_once()
verify_number_of_outputs_wrapped.assert_called_once()
verify_data_wrapped.assert_called_once()
verify_sigops_output_wrapped.assert_called_once()
Expand Down Expand Up @@ -240,11 +240,11 @@ def test_block_validate_basic(self) -> None:
def test_block_validate_full(self) -> None:
block = self._get_valid_block()

vertex_verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)

verify_pow_wrapped = Mock(wraps=self.verifiers.vertex.verify_pow)
verify_no_inputs_wrapped = Mock(wraps=self.verifiers.block.verify_no_inputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.block.verify_outputs)
verify_output_token_indexes_wrapped = Mock(wraps=self.verifiers.block.verify_output_token_indexes)
verify_number_of_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_number_of_outputs)
verify_data_wrapped = Mock(wraps=self.verifiers.block.verify_data)
verify_sigops_output_wrapped = Mock(wraps=self.verifiers.vertex.verify_sigops_output)
Expand All @@ -255,10 +255,10 @@ def test_block_validate_full(self) -> None:
verify_mandatory_signaling_wrapped = Mock(wraps=self.verifiers.block.verify_mandatory_signaling)

with (
patch.object(VertexVerifier, 'verify_outputs', vertex_verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_pow', verify_pow_wrapped),
patch.object(BlockVerifier, 'verify_no_inputs', verify_no_inputs_wrapped),
patch.object(BlockVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(BlockVerifier, 'verify_output_token_indexes', verify_output_token_indexes_wrapped),
patch.object(VertexVerifier, 'verify_number_of_outputs', verify_number_of_outputs_wrapped),
patch.object(BlockVerifier, 'verify_data', verify_data_wrapped),
patch.object(VertexVerifier, 'verify_sigops_output', verify_sigops_output_wrapped),
Expand All @@ -271,12 +271,12 @@ def test_block_validate_full(self) -> None:
self.manager.verification_service.validate_full(block)

# Vertex methods
vertex_verify_outputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()

# Block methods
verify_pow_wrapped.assert_called_once()
verify_no_inputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()
verify_output_token_indexes_wrapped.assert_called_once()
verify_number_of_outputs_wrapped.assert_called_once()
verify_data_wrapped.assert_called_once()
verify_sigops_output_wrapped.assert_called_once()
Expand Down Expand Up @@ -305,22 +305,22 @@ def test_merge_mined_block_verify_basic(self) -> None:
def test_merge_mined_block_verify_without_storage(self) -> None:
block = self._get_valid_merge_mined_block()

vertex_verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)

verify_pow_wrapped = Mock(wraps=self.verifiers.vertex.verify_pow)
verify_no_inputs_wrapped = Mock(wraps=self.verifiers.block.verify_no_inputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.block.verify_outputs)
verify_output_token_indexes_wrapped = Mock(wraps=self.verifiers.block.verify_output_token_indexes)
verify_number_of_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_number_of_outputs)
verify_data_wrapped = Mock(wraps=self.verifiers.block.verify_data)
verify_sigops_output_wrapped = Mock(wraps=self.verifiers.vertex.verify_sigops_output)

verify_aux_pow_wrapped = Mock(wraps=self.verifiers.merge_mined_block.verify_aux_pow)

with (
patch.object(VertexVerifier, 'verify_outputs', vertex_verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_pow', verify_pow_wrapped),
patch.object(BlockVerifier, 'verify_no_inputs', verify_no_inputs_wrapped),
patch.object(BlockVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(BlockVerifier, 'verify_output_token_indexes', verify_output_token_indexes_wrapped),
patch.object(VertexVerifier, 'verify_number_of_outputs', verify_number_of_outputs_wrapped),
patch.object(BlockVerifier, 'verify_data', verify_data_wrapped),
patch.object(VertexVerifier, 'verify_sigops_output', verify_sigops_output_wrapped),
Expand All @@ -329,12 +329,12 @@ def test_merge_mined_block_verify_without_storage(self) -> None:
self.manager.verification_service.verify_without_storage(block)

# Vertex methods
vertex_verify_outputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()

# Block methods
verify_pow_wrapped.assert_called_once()
verify_no_inputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()
verify_output_token_indexes_wrapped.assert_called_once()
verify_number_of_outputs_wrapped.assert_called_once()
verify_data_wrapped.assert_called_once()
verify_sigops_output_wrapped.assert_called_once()
Expand All @@ -345,11 +345,11 @@ def test_merge_mined_block_verify_without_storage(self) -> None:
def test_merge_mined_block_verify(self) -> None:
block = self._get_valid_merge_mined_block()

vertex_verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)

verify_pow_wrapped = Mock(wraps=self.verifiers.vertex.verify_pow)
verify_no_inputs_wrapped = Mock(wraps=self.verifiers.block.verify_no_inputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.block.verify_outputs)
verify_output_token_indexes_wrapped = Mock(wraps=self.verifiers.block.verify_output_token_indexes)
verify_number_of_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_number_of_outputs)
verify_data_wrapped = Mock(wraps=self.verifiers.block.verify_data)
verify_sigops_output_wrapped = Mock(wraps=self.verifiers.vertex.verify_sigops_output)
Expand All @@ -360,10 +360,10 @@ def test_merge_mined_block_verify(self) -> None:
verify_aux_pow_wrapped = Mock(wraps=self.verifiers.merge_mined_block.verify_aux_pow)

with (
patch.object(VertexVerifier, 'verify_outputs', vertex_verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_pow', verify_pow_wrapped),
patch.object(BlockVerifier, 'verify_no_inputs', verify_no_inputs_wrapped),
patch.object(BlockVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(BlockVerifier, 'verify_output_token_indexes', verify_output_token_indexes_wrapped),
patch.object(VertexVerifier, 'verify_number_of_outputs', verify_number_of_outputs_wrapped),
patch.object(BlockVerifier, 'verify_data', verify_data_wrapped),
patch.object(VertexVerifier, 'verify_sigops_output', verify_sigops_output_wrapped),
Expand All @@ -375,12 +375,12 @@ def test_merge_mined_block_verify(self) -> None:
self.manager.verification_service.verify(block)

# Vertex methods
vertex_verify_outputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()

# Block methods
verify_pow_wrapped.assert_called_once()
verify_no_inputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()
verify_output_token_indexes_wrapped.assert_called_once()
verify_number_of_outputs_wrapped.assert_called_once()
verify_data_wrapped.assert_called_once()
verify_sigops_output_wrapped.assert_called_once()
Expand Down Expand Up @@ -434,11 +434,11 @@ def test_merge_mined_block_validate_basic(self) -> None:
def test_merge_mined_block_validate_full(self) -> None:
block = self._get_valid_merge_mined_block()

vertex_verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_outputs)

verify_pow_wrapped = Mock(wraps=self.verifiers.vertex.verify_pow)
verify_no_inputs_wrapped = Mock(wraps=self.verifiers.block.verify_no_inputs)
verify_outputs_wrapped = Mock(wraps=self.verifiers.block.verify_outputs)
verify_output_token_indexes_wrapped = Mock(wraps=self.verifiers.block.verify_output_token_indexes)
verify_number_of_outputs_wrapped = Mock(wraps=self.verifiers.vertex.verify_number_of_outputs)
verify_data_wrapped = Mock(wraps=self.verifiers.block.verify_data)
verify_sigops_output_wrapped = Mock(wraps=self.verifiers.vertex.verify_sigops_output)
Expand All @@ -451,10 +451,10 @@ def test_merge_mined_block_validate_full(self) -> None:
verify_aux_pow_wrapped = Mock(wraps=self.verifiers.merge_mined_block.verify_aux_pow)

with (
patch.object(VertexVerifier, 'verify_outputs', vertex_verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(VertexVerifier, 'verify_pow', verify_pow_wrapped),
patch.object(BlockVerifier, 'verify_no_inputs', verify_no_inputs_wrapped),
patch.object(BlockVerifier, 'verify_outputs', verify_outputs_wrapped),
patch.object(BlockVerifier, 'verify_output_token_indexes', verify_output_token_indexes_wrapped),
patch.object(VertexVerifier, 'verify_number_of_outputs', verify_number_of_outputs_wrapped),
patch.object(BlockVerifier, 'verify_data', verify_data_wrapped),
patch.object(VertexVerifier, 'verify_sigops_output', verify_sigops_output_wrapped),
Expand All @@ -468,12 +468,12 @@ def test_merge_mined_block_validate_full(self) -> None:
self.manager.verification_service.validate_full(block)

# Vertex methods
vertex_verify_outputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()

# Block methods
verify_pow_wrapped.assert_called_once()
verify_no_inputs_wrapped.assert_called_once()
verify_outputs_wrapped.assert_called_once()
verify_output_token_indexes_wrapped.assert_called_once()
verify_number_of_outputs_wrapped.assert_called_once()
verify_data_wrapped.assert_called_once()
verify_sigops_output_wrapped.assert_called_once()
Expand Down

0 comments on commit cbdf80b

Please sign in to comment.