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 10, 2023
1 parent 738e2cb commit ee1d52e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 50 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
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 ee1d52e

Please sign in to comment.