Skip to content

Commit 94b9075

Browse files
committed
refactor(verification): move block-only verification methods
1 parent 3365e4b commit 94b9075

File tree

5 files changed

+119
-135
lines changed

5 files changed

+119
-135
lines changed

hathor/transaction/block.py

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,12 @@
1818
from struct import pack
1919
from typing import TYPE_CHECKING, Any, Optional
2020

21-
from hathor import daa
2221
from hathor.checkpoint import Checkpoint
2322
from hathor.feature_activation.feature import Feature
2423
from hathor.feature_activation.model.feature_state import FeatureState
2524
from hathor.profiler import get_cpu_profiler
2625
from hathor.transaction import BaseTransaction, TxOutput, TxVersion
27-
from hathor.transaction.exceptions import (
28-
BlockWithInputs,
29-
BlockWithTokensError,
30-
CheckpointError,
31-
InvalidBlockReward,
32-
RewardLocked,
33-
TransactionDataError,
34-
WeightError,
35-
)
26+
from hathor.transaction.exceptions import BlockWithTokensError, CheckpointError
3627
from hathor.transaction.util import VerboseCallback, int_to_bytes, unpack, unpack_len
3728
from hathor.util import not_none
3829
from hathor.utils.int import get_bit_list
@@ -337,55 +328,12 @@ def verify_checkpoint(self, checkpoints: list[Checkpoint]) -> None:
337328
# TODO: check whether self is a parent of any checkpoint-valid block, this is left for a future PR
338329
pass
339330

340-
def verify_weight(self) -> None:
341-
"""Validate minimum block difficulty."""
342-
block_weight = daa.calculate_block_difficulty(self)
343-
if self.weight < block_weight - self._settings.WEIGHT_TOL:
344-
raise WeightError(f'Invalid new block {self.hash_hex}: weight ({self.weight}) is '
345-
f'smaller than the minimum weight ({block_weight})')
346-
347-
def verify_height(self) -> None:
348-
"""Validate that the block height is enough to confirm all transactions being confirmed."""
349-
meta = self.get_metadata()
350-
assert meta.height is not None
351-
assert meta.min_height is not None
352-
if meta.height < meta.min_height:
353-
raise RewardLocked(f'Block needs {meta.min_height} height but has {meta.height}')
354-
355-
def verify_reward(self) -> None:
356-
"""Validate reward amount."""
357-
parent_block = self.get_block_parent()
358-
tokens_issued_per_block = daa.get_tokens_issued_per_block(parent_block.get_height() + 1)
359-
if self.sum_outputs != tokens_issued_per_block:
360-
raise InvalidBlockReward(
361-
f'Invalid number of issued tokens tag=invalid_issued_tokens tx.hash={self.hash_hex} '
362-
f'issued={self.sum_outputs} allowed={tokens_issued_per_block}'
363-
)
364-
365-
def verify_no_inputs(self) -> None:
366-
inputs = getattr(self, 'inputs', None)
367-
if inputs:
368-
raise BlockWithInputs('number of inputs {}'.format(len(inputs)))
369-
370331
def verify_outputs(self) -> None:
371332
super().verify_outputs()
372333
for output in self.outputs:
373334
if output.get_token_index() > 0:
374335
raise BlockWithTokensError('in output: {}'.format(output.to_human_readable()))
375336

376-
def verify_data(self) -> None:
377-
if len(self.data) > self._settings.BLOCK_DATA_MAX_SIZE:
378-
raise TransactionDataError('block data has {} bytes'.format(len(self.data)))
379-
380-
def verify_without_storage(self) -> None:
381-
""" Run all verifications that do not need a storage.
382-
"""
383-
self.verify_pow()
384-
self.verify_no_inputs()
385-
self.verify_outputs()
386-
self.verify_data()
387-
self.verify_sigops_output()
388-
389337
def get_base_hash(self) -> bytes:
390338
from hathor.merged_mining.bitcoin import sha256d_hash
391339
return sha256d_hash(self.get_header_without_nonce())

hathor/transaction/merge_mined_block.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,3 @@ def to_json(self, decode_script: bool = False, include_metadata: bool = False) -
7474
del json['nonce']
7575
json['aux_pow'] = bytes(self.aux_pow).hex() if self.aux_pow else None
7676
return json
77-
78-
def verify_without_storage(self) -> None:
79-
self.verify_aux_pow()
80-
super().verify_without_storage()
81-
82-
def verify_aux_pow(self) -> None:
83-
""" Verify auxiliary proof-of-work (for merged mining).
84-
"""
85-
assert self.aux_pow is not None
86-
self.aux_pow.verify(self.get_base_hash())

hathor/verification/block_verifier.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from hathor import daa
1516
from hathor.profiler import get_cpu_profiler
1617
from hathor.transaction import BaseTransaction, Block
18+
from hathor.transaction.exceptions import (
19+
BlockWithInputs,
20+
InvalidBlockReward,
21+
RewardLocked,
22+
TransactionDataError,
23+
WeightError,
24+
)
1725
from hathor.verification.vertex_verifier import VertexVerifier
1826

1927
cpu = get_cpu_profiler()
@@ -52,25 +60,45 @@ def verify(self, block: Block) -> None:
5260
def verify_without_storage(self, block: Block) -> None:
5361
""" Run all verifications that do not need a storage.
5462
"""
55-
block.verify_without_storage()
63+
self.verify_pow(block)
64+
self.verify_no_inputs(block)
65+
self.verify_outputs(block)
66+
self.verify_data(block)
67+
self.verify_sigops_output(block)
5668

5769
def verify_height(self, block: Block) -> None:
5870
"""Validate that the block height is enough to confirm all transactions being confirmed."""
59-
block.verify_height()
71+
meta = block.get_metadata()
72+
assert meta.height is not None
73+
assert meta.min_height is not None
74+
if meta.height < meta.min_height:
75+
raise RewardLocked(f'Block needs {meta.min_height} height but has {meta.height}')
6076

6177
def verify_weight(self, block: Block) -> None:
6278
"""Validate minimum block difficulty."""
63-
block.verify_weight()
79+
min_block_weight = daa.calculate_block_difficulty(block)
80+
if block.weight < min_block_weight - self._settings.WEIGHT_TOL:
81+
raise WeightError(f'Invalid new block {block.hash_hex}: weight ({block.weight}) is '
82+
f'smaller than the minimum weight ({min_block_weight})')
6483

6584
def verify_reward(self, block: Block) -> None:
6685
"""Validate reward amount."""
67-
block.verify_reward()
86+
parent_block = block.get_block_parent()
87+
tokens_issued_per_block = daa.get_tokens_issued_per_block(parent_block.get_height() + 1)
88+
if block.sum_outputs != tokens_issued_per_block:
89+
raise InvalidBlockReward(
90+
f'Invalid number of issued tokens tag=invalid_issued_tokens tx.hash={block.hash_hex} '
91+
f'issued={block.sum_outputs} allowed={tokens_issued_per_block}'
92+
)
6893

6994
def verify_no_inputs(self, block: Block) -> None:
70-
block.verify_no_inputs()
95+
inputs = getattr(block, 'inputs', None)
96+
if inputs:
97+
raise BlockWithInputs('number of inputs {}'.format(len(inputs)))
7198

7299
def verify_outputs(self, block: BaseTransaction) -> None:
73100
block.verify_outputs()
74101

75102
def verify_data(self, block: Block) -> None:
76-
block.verify_data()
103+
if len(block.data) > self._settings.BLOCK_DATA_MAX_SIZE:
104+
raise TransactionDataError('block data has {} bytes'.format(len(block.data)))

hathor/verification/merge_mined_block_verifier.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from hathor.transaction import MergeMinedBlock
15+
from hathor.transaction import Block, MergeMinedBlock
1616
from hathor.verification.block_verifier import BlockVerifier
1717

1818

1919
class MergeMinedBlockVerifier(BlockVerifier):
2020
__slots__ = ()
2121

22+
def verify_without_storage(self, block: Block) -> None:
23+
assert isinstance(block, MergeMinedBlock)
24+
self.verify_aux_pow(block)
25+
super().verify_without_storage(block)
26+
2227
def verify_aux_pow(self, block: MergeMinedBlock) -> None:
2328
""" Verify auxiliary proof-of-work (for merged mining).
2429
"""
25-
block.verify_aux_pow()
30+
assert block.aux_pow is not None
31+
block.aux_pow.verify(block.get_base_hash())

0 commit comments

Comments
 (0)