Skip to content

Commit 6e3e325

Browse files
committed
refactor(verification): remove VertexVerifier inheritance
1 parent bad9939 commit 6e3e325

File tree

13 files changed

+385
-251
lines changed

13 files changed

+385
-251
lines changed

hathor/builder/cli_builder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
from hathor.pubsub import PubSubManager
3838
from hathor.stratum import StratumFactory
3939
from hathor.util import Random, Reactor, not_none
40-
from hathor.verification.verification_service import VerificationService, VertexVerifiers
40+
from hathor.verification.verification_service import VerificationService
41+
from hathor.verification.vertex_verifiers import VertexVerifiers
4142
from hathor.wallet import BaseWallet, HDWallet, Wallet
4243

4344
logger = get_logger()

hathor/simulator/patches.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,16 @@
1818

1919
from hathor.mining.cpu_mining_service import CpuMiningService
2020
from hathor.transaction import BaseTransaction
21-
from hathor.verification.block_verifier import BlockVerifier
22-
from hathor.verification.merge_mined_block_verifier import MergeMinedBlockVerifier
23-
from hathor.verification.token_creation_transaction_verifier import TokenCreationTransactionVerifier
24-
from hathor.verification.transaction_verifier import TransactionVerifier
21+
from hathor.verification.vertex_verifier import VertexVerifier
2522

2623
logger = get_logger()
2724

2825

29-
def _verify_pow(vertex: BaseTransaction) -> None:
30-
assert vertex.hash is not None
31-
logger.new().debug('Skipping VertexVerifier.verify_pow() for simulator')
32-
33-
34-
class SimulatorBlockVerifier(BlockVerifier):
35-
@classmethod
36-
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
37-
_verify_pow(vertex)
38-
39-
40-
class SimulatorMergeMinedBlockVerifier(MergeMinedBlockVerifier):
41-
@classmethod
42-
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
43-
_verify_pow(vertex)
44-
45-
46-
class SimulatorTransactionVerifier(TransactionVerifier):
47-
@classmethod
48-
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
49-
_verify_pow(vertex)
50-
51-
52-
class SimulatorTokenCreationTransactionVerifier(TokenCreationTransactionVerifier):
26+
class SimulatorVertexVerifier(VertexVerifier):
5327
@classmethod
5428
def verify_pow(cls, vertex: BaseTransaction, *, override_weight: Optional[float] = None) -> None:
55-
_verify_pow(vertex)
29+
assert vertex.hash is not None
30+
logger.new().debug('Skipping VertexVerifier.verify_pow() for simulator')
5631

5732

5833
class SimulatorCpuMiningService(CpuMiningService):

hathor/simulator/simulator.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@
2929
from hathor.p2p.peer_id import PeerId
3030
from hathor.simulator.clock import HeapClock, MemoryReactorHeapClock
3131
from hathor.simulator.miner.geometric_miner import GeometricMiner
32-
from hathor.simulator.patches import (
33-
SimulatorBlockVerifier,
34-
SimulatorCpuMiningService,
35-
SimulatorMergeMinedBlockVerifier,
36-
SimulatorTokenCreationTransactionVerifier,
37-
SimulatorTransactionVerifier,
38-
)
32+
from hathor.simulator.patches import SimulatorCpuMiningService, SimulatorVertexVerifier
3933
from hathor.simulator.tx_generator import RandomTransactionGenerator
4034
from hathor.util import Random
41-
from hathor.verification.verification_service import VertexVerifiers
35+
from hathor.verification.vertex_verifiers import VertexVerifiers
4236
from hathor.wallet import HDWallet
4337

4438
if TYPE_CHECKING:
@@ -257,9 +251,9 @@ def _build_vertex_verifiers(
257251
"""
258252
A custom VertexVerifiers builder to be used by the simulator.
259253
"""
260-
return VertexVerifiers(
261-
block=SimulatorBlockVerifier(settings=settings, daa=daa, feature_service=feature_service),
262-
merge_mined_block=SimulatorMergeMinedBlockVerifier(),
263-
tx=SimulatorTransactionVerifier(settings=settings, daa=daa),
264-
token_creation_tx=SimulatorTokenCreationTransactionVerifier(settings=settings),
254+
return VertexVerifiers.create(
255+
settings=settings,
256+
vertex_verifier=SimulatorVertexVerifier(settings=settings, daa=daa),
257+
daa=daa,
258+
feature_service=feature_service,
265259
)

hathor/transaction/resources/create_tx.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from hathor.cli.openapi_files.register import register_resource
1919
from hathor.crypto.util import decode_address
2020
from hathor.exception import InvalidNewTransaction
21+
from hathor.manager import HathorManager
2122
from hathor.transaction import Transaction, TxInput, TxOutput
2223
from hathor.transaction.scripts import create_output_script
2324
from hathor.util import api_catch_exceptions, json_dumpb, json_loadb
@@ -49,7 +50,7 @@ class CreateTxResource(Resource):
4950
"""
5051
isLeaf = True
5152

52-
def __init__(self, manager):
53+
def __init__(self, manager: HathorManager) -> None:
5354
# Important to have the manager so we can know the tx_storage
5455
self.manager = manager
5556

@@ -107,15 +108,16 @@ def render_POST(self, request):
107108
def _verify_unsigned_skip_pow(self, tx: Transaction) -> None:
108109
""" Same as .verify but skipping pow and signature verification."""
109110
assert type(tx) is Transaction
110-
verifier = self.manager.verification_service.verifiers.tx
111-
verifier.verify_number_of_inputs(tx)
112-
verifier.verify_number_of_outputs(tx)
113-
verifier.verify_outputs(tx)
114-
verifier.verify_sigops_output(tx)
115-
verifier.verify_sigops_input(tx)
116-
verifier.verify_inputs(tx, skip_script=True) # need to run verify_inputs first to check if all inputs exist
117-
verifier.verify_parents(tx)
118-
verifier.verify_sum(tx)
111+
verifiers = self.manager.verification_service.verifiers
112+
verifiers.tx.verify_number_of_inputs(tx)
113+
verifiers.vertex.verify_number_of_outputs(tx)
114+
verifiers.tx.verify_outputs(tx)
115+
verifiers.vertex.verify_sigops_output(tx)
116+
verifiers.tx.verify_sigops_input(tx)
117+
# need to run verify_inputs first to check if all inputs exist
118+
verifiers.tx.verify_inputs(tx, skip_script=True)
119+
verifiers.vertex.verify_parents(tx)
120+
verifiers.tx.verify_sum(tx)
119121

120122

121123
CreateTxResource.openapi = {

hathor/verification/block_verifier.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from hathor.conf.settings import HathorSettings
1616
from hathor.daa import DifficultyAdjustmentAlgorithm
1717
from hathor.feature_activation.feature_service import BlockIsMissingSignal, BlockIsSignaling, FeatureService
18-
from hathor.transaction import BaseTransaction, Block
18+
from hathor.transaction import Block
1919
from hathor.transaction.exceptions import (
2020
BlockMustSignalError,
2121
BlockWithInputs,
@@ -28,17 +28,20 @@
2828
from hathor.verification.vertex_verifier import VertexVerifier
2929

3030

31-
class BlockVerifier(VertexVerifier):
32-
__slots__ = ('_feature_service', )
31+
class BlockVerifier:
32+
__slots__ = ('_settings', '_vertex_verifier', '_daa', '_feature_service')
3333

3434
def __init__(
3535
self,
3636
*,
3737
settings: HathorSettings,
38+
vertex_verifier: VertexVerifier,
3839
daa: DifficultyAdjustmentAlgorithm,
3940
feature_service: FeatureService | None = None
4041
) -> None:
41-
super().__init__(settings=settings, daa=daa)
42+
self._settings = settings
43+
self._vertex_verifier = vertex_verifier
44+
self._daa = daa
4245
self._feature_service = feature_service
4346

4447
def verify_height(self, block: Block) -> None:
@@ -71,9 +74,8 @@ def verify_no_inputs(self, block: Block) -> None:
7174
if inputs:
7275
raise BlockWithInputs('number of inputs {}'.format(len(inputs)))
7376

74-
def verify_outputs(self, block: BaseTransaction) -> None:
75-
assert isinstance(block, Block)
76-
super().verify_outputs(block)
77+
def verify_outputs(self, block: Block) -> None:
78+
self._vertex_verifier.verify_outputs(block)
7779
for output in block.outputs:
7880
if output.get_token_index() > 0:
7981
raise BlockWithTokensError('in output: {}'.format(output.to_human_readable()))

hathor/verification/transaction_verifier.py

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

15+
from hathor.conf.settings import HathorSettings
16+
from hathor.daa import DifficultyAdjustmentAlgorithm
1517
from hathor.profiler import get_cpu_profiler
1618
from hathor.transaction import BaseTransaction, Transaction, TxInput
1719
from hathor.transaction.exceptions import (
@@ -39,8 +41,19 @@
3941
cpu = get_cpu_profiler()
4042

4143

42-
class TransactionVerifier(VertexVerifier):
43-
__slots__ = ()
44+
class TransactionVerifier:
45+
__slots__ = ('_settings', '_vertex_verifier', '_daa')
46+
47+
def __init__(
48+
self,
49+
*,
50+
settings: HathorSettings,
51+
vertex_verifier: VertexVerifier,
52+
daa: DifficultyAdjustmentAlgorithm,
53+
) -> None:
54+
self._settings = settings
55+
self._vertex_verifier = vertex_verifier
56+
self._daa = daa
4457

4558
def verify_parents_basic(self, tx: Transaction) -> None:
4659
"""Verify number and non-duplicity of parents."""
@@ -164,13 +177,12 @@ def verify_number_of_inputs(self, tx: Transaction) -> None:
164177
if not tx.is_genesis:
165178
raise NoInputError('Transaction must have at least one input')
166179

167-
def verify_outputs(self, tx: BaseTransaction) -> None:
180+
def verify_outputs(self, tx: Transaction) -> None:
168181
"""Verify outputs reference an existing token uid in the tokens list
169182
170183
:raises InvalidToken: output references non existent token uid
171184
"""
172-
assert isinstance(tx, Transaction)
173-
super().verify_outputs(tx)
185+
self._vertex_verifier.verify_outputs(tx)
174186
for output in tx.outputs:
175187
# check index is valid
176188
if output.get_token_index() > len(tx.tokens):

hathor/verification/verification_service.py

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

15-
from typing import NamedTuple
16-
1715
from typing_extensions import assert_never
1816

19-
from hathor.conf.settings import HathorSettings
20-
from hathor.daa import DifficultyAdjustmentAlgorithm
21-
from hathor.feature_activation.feature_service import FeatureService
2217
from hathor.profiler import get_cpu_profiler
2318
from hathor.transaction import BaseTransaction, Block, MergeMinedBlock, Transaction, TxVersion
2419
from hathor.transaction.token_creation_tx import TokenCreationTransaction
2520
from hathor.transaction.validation_state import ValidationState
26-
from hathor.verification.block_verifier import BlockVerifier
27-
from hathor.verification.merge_mined_block_verifier import MergeMinedBlockVerifier
28-
from hathor.verification.token_creation_transaction_verifier import TokenCreationTransactionVerifier
29-
from hathor.verification.transaction_verifier import TransactionVerifier
21+
from hathor.verification.vertex_verifiers import VertexVerifiers
3022

3123
cpu = get_cpu_profiler()
3224

3325

34-
class VertexVerifiers(NamedTuple):
35-
"""A group of verifier instances, one for each vertex type."""
36-
block: BlockVerifier
37-
merge_mined_block: MergeMinedBlockVerifier
38-
tx: TransactionVerifier
39-
token_creation_tx: TokenCreationTransactionVerifier
40-
41-
@classmethod
42-
def create_defaults(
43-
cls,
44-
*,
45-
settings: HathorSettings,
46-
daa: DifficultyAdjustmentAlgorithm,
47-
feature_service: FeatureService | None = None,
48-
) -> 'VertexVerifiers':
49-
"""
50-
Create a VertexVerifiers instance using the default verifier for each vertex type,
51-
from all required dependencies.
52-
"""
53-
return VertexVerifiers(
54-
block=BlockVerifier(settings=settings, daa=daa, feature_service=feature_service),
55-
merge_mined_block=MergeMinedBlockVerifier(),
56-
tx=TransactionVerifier(settings=settings, daa=daa),
57-
token_creation_tx=TokenCreationTransactionVerifier(settings=settings),
58-
)
59-
60-
6126
class VerificationService:
6227
__slots__ = ('verifiers', )
6328

@@ -192,7 +157,7 @@ def _verify_block(self, block: Block) -> None:
192157
self.verify_without_storage(block)
193158

194159
# (1) and (4)
195-
self.verifiers.block.verify_parents(block)
160+
self.verifiers.vertex.verify_parents(block)
196161

197162
self.verifiers.block.verify_height(block)
198163

@@ -220,7 +185,7 @@ def _verify_tx(self, tx: Transaction, *, reject_locked_reward: bool) -> None:
220185
self.verify_without_storage(tx)
221186
self.verifiers.tx.verify_sigops_input(tx)
222187
self.verifiers.tx.verify_inputs(tx) # need to run verify_inputs first to check if all inputs exist
223-
self.verifiers.tx.verify_parents(tx)
188+
self.verifiers.vertex.verify_parents(tx)
224189
self.verifiers.tx.verify_sum(tx)
225190
if reject_locked_reward:
226191
self.verifiers.tx.verify_reward_locked(tx)
@@ -255,11 +220,11 @@ def verify_without_storage(self, vertex: BaseTransaction) -> None:
255220
def _verify_without_storage_block(self, block: Block) -> None:
256221
""" Run all verifications that do not need a storage.
257222
"""
258-
self.verifiers.block.verify_pow(block)
223+
self.verifiers.vertex.verify_pow(block)
259224
self.verifiers.block.verify_no_inputs(block)
260225
self.verifiers.block.verify_outputs(block)
261226
self.verifiers.block.verify_data(block)
262-
self.verifiers.block.verify_sigops_output(block)
227+
self.verifiers.vertex.verify_sigops_output(block)
263228

264229
def _verify_without_storage_merge_mined_block(self, block: MergeMinedBlock) -> None:
265230
self.verifiers.merge_mined_block.verify_aux_pow(block)
@@ -268,10 +233,10 @@ def _verify_without_storage_merge_mined_block(self, block: MergeMinedBlock) -> N
268233
def _verify_without_storage_tx(self, tx: Transaction) -> None:
269234
""" Run all verifications that do not need a storage.
270235
"""
271-
self.verifiers.tx.verify_pow(tx)
236+
self.verifiers.vertex.verify_pow(tx)
272237
self.verifiers.tx.verify_number_of_inputs(tx)
273238
self.verifiers.tx.verify_outputs(tx)
274-
self.verifiers.tx.verify_sigops_output(tx)
239+
self.verifiers.vertex.verify_sigops_output(tx)
275240

276241
def _verify_without_storage_token_creation_tx(self, tx: TokenCreationTransaction) -> None:
277242
self._verify_without_storage_tx(tx)

0 commit comments

Comments
 (0)