|
14 | 14 |
|
15 | 15 | from typing import NamedTuple
|
16 | 16 |
|
| 17 | +from typing_extensions import assert_never |
| 18 | + |
17 | 19 | from hathor.conf.settings import HathorSettings
|
18 | 20 | from hathor.daa import DifficultyAdjustmentAlgorithm
|
19 | 21 | from hathor.feature_activation.feature_service import FeatureService
|
20 | 22 | from hathor.transaction import BaseTransaction, Block, MergeMinedBlock, Transaction, TxVersion
|
21 |
| -from hathor.transaction.exceptions import TxValidationError |
22 | 23 | from hathor.transaction.token_creation_tx import TokenCreationTransaction
|
23 | 24 | from hathor.transaction.validation_state import ValidationState
|
24 | 25 | from hathor.verification.block_verifier import BlockVerifier
|
@@ -107,76 +108,64 @@ def verify_basic(self, vertex: BaseTransaction, *, skip_block_weight_verificatio
|
107 | 108 | """Basic verifications (the ones without access to dependencies: parents+inputs). Raises on error.
|
108 | 109 |
|
109 | 110 | Used by `self.validate_basic`. Should not modify the validation state."""
|
| 111 | + # We assert with type() instead of isinstance() because each subclass has a specific branch. |
110 | 112 | match vertex.version:
|
111 | 113 | case TxVersion.REGULAR_BLOCK:
|
112 |
| - assert isinstance(vertex, Block) |
| 114 | + assert type(vertex) is Block |
113 | 115 | self.verifiers.block.verify_basic(
|
114 | 116 | vertex,
|
115 | 117 | skip_block_weight_verification=skip_block_weight_verification
|
116 | 118 | )
|
117 | 119 | case TxVersion.MERGE_MINED_BLOCK:
|
118 |
| - assert isinstance(vertex, MergeMinedBlock) |
| 120 | + assert type(vertex) is MergeMinedBlock |
119 | 121 | self.verifiers.merge_mined_block.verify_basic(
|
120 | 122 | vertex,
|
121 | 123 | skip_block_weight_verification=skip_block_weight_verification
|
122 | 124 | )
|
123 | 125 | case TxVersion.REGULAR_TRANSACTION:
|
124 |
| - assert isinstance(vertex, Transaction) |
| 126 | + assert type(vertex) is Transaction |
125 | 127 | self.verifiers.tx.verify_basic(vertex)
|
126 | 128 | case TxVersion.TOKEN_CREATION_TRANSACTION:
|
127 |
| - assert isinstance(vertex, TokenCreationTransaction) |
| 129 | + assert type(vertex) is TokenCreationTransaction |
128 | 130 | self.verifiers.token_creation_tx.verify_basic(vertex)
|
129 | 131 | case _:
|
130 |
| - raise NotImplementedError |
| 132 | + assert_never(vertex.version) |
131 | 133 |
|
132 | 134 | def verify(self, vertex: BaseTransaction, *, reject_locked_reward: bool = True) -> None:
|
133 | 135 | """Run all verifications. Raises on error.
|
134 | 136 |
|
135 | 137 | Used by `self.validate_full`. Should not modify the validation state."""
|
| 138 | + # We assert with type() instead of isinstance() because each subclass has a specific branch. |
136 | 139 | match vertex.version:
|
137 | 140 | case TxVersion.REGULAR_BLOCK:
|
138 |
| - assert isinstance(vertex, Block) |
| 141 | + assert type(vertex) is Block |
139 | 142 | self.verifiers.block.verify(vertex)
|
140 | 143 | case TxVersion.MERGE_MINED_BLOCK:
|
141 |
| - assert isinstance(vertex, MergeMinedBlock) |
| 144 | + assert type(vertex) is MergeMinedBlock |
142 | 145 | self.verifiers.merge_mined_block.verify(vertex)
|
143 | 146 | case TxVersion.REGULAR_TRANSACTION:
|
144 |
| - assert isinstance(vertex, Transaction) |
| 147 | + assert type(vertex) is Transaction |
145 | 148 | self.verifiers.tx.verify(vertex, reject_locked_reward=reject_locked_reward)
|
146 | 149 | case TxVersion.TOKEN_CREATION_TRANSACTION:
|
147 |
| - assert isinstance(vertex, TokenCreationTransaction) |
| 150 | + assert type(vertex) is TokenCreationTransaction |
148 | 151 | self.verifiers.token_creation_tx.verify(vertex, reject_locked_reward=reject_locked_reward)
|
149 | 152 | case _:
|
150 |
| - raise NotImplementedError |
| 153 | + assert_never(vertex.version) |
151 | 154 |
|
152 | 155 | def verify_without_storage(self, vertex: BaseTransaction) -> None:
|
| 156 | + # We assert with type() instead of isinstance() because each subclass has a specific branch. |
153 | 157 | match vertex.version:
|
154 | 158 | case TxVersion.REGULAR_BLOCK:
|
155 |
| - assert isinstance(vertex, Block) |
| 159 | + assert type(vertex) is Block |
156 | 160 | self.verifiers.block.verify_without_storage(vertex)
|
157 | 161 | case TxVersion.MERGE_MINED_BLOCK:
|
158 |
| - assert isinstance(vertex, MergeMinedBlock) |
| 162 | + assert type(vertex) is MergeMinedBlock |
159 | 163 | self.verifiers.merge_mined_block.verify_without_storage(vertex)
|
160 | 164 | case TxVersion.REGULAR_TRANSACTION:
|
161 |
| - assert isinstance(vertex, Transaction) |
| 165 | + assert type(vertex) is Transaction |
162 | 166 | self.verifiers.tx.verify_without_storage(vertex)
|
163 | 167 | case TxVersion.TOKEN_CREATION_TRANSACTION:
|
164 |
| - assert isinstance(vertex, TokenCreationTransaction) |
| 168 | + assert type(vertex) is TokenCreationTransaction |
165 | 169 | self.verifiers.token_creation_tx.verify_without_storage(vertex)
|
166 | 170 | case _:
|
167 |
| - raise NotImplementedError |
168 |
| - |
169 |
| - def validate_vertex_error(self, vertex: BaseTransaction) -> tuple[bool, str]: |
170 |
| - """ Verify if tx is valid and return success and possible error message |
171 |
| -
|
172 |
| - :return: Success if tx is valid and possible error message, if not |
173 |
| - :rtype: tuple[bool, str] |
174 |
| - """ |
175 |
| - success = True |
176 |
| - message = '' |
177 |
| - try: |
178 |
| - self.verify(vertex) |
179 |
| - except TxValidationError as e: |
180 |
| - success = False |
181 |
| - message = str(e) |
182 |
| - return success, message |
| 171 | + assert_never(vertex.version) |
0 commit comments