|
| 1 | +# Copyright 2024 Hathor Labs |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from dataclasses import dataclass |
| 16 | +from typing import Generic, TypeAlias, TypeVar |
| 17 | + |
| 18 | +from typing_extensions import assert_never |
| 19 | + |
| 20 | +from hathor.daa import DifficultyAdjustmentAlgorithm |
| 21 | +from hathor.transaction import BaseTransaction, Block, MergeMinedBlock, TxVersion |
| 22 | +from hathor.transaction.storage import TransactionStorage |
| 23 | +from hathor.transaction.token_creation_tx import TokenCreationTransaction |
| 24 | +from hathor.transaction.transaction import Transaction |
| 25 | +from hathor.verification.verification_dependencies import ( |
| 26 | + BasicBlockDependencies, |
| 27 | + BlockDependencies, |
| 28 | + TransactionDependencies, |
| 29 | +) |
| 30 | + |
| 31 | +T = TypeVar('T', bound=BaseTransaction) |
| 32 | +BasicDepsT = TypeVar('BasicDepsT') |
| 33 | +DepsT = TypeVar('DepsT') |
| 34 | + |
| 35 | + |
| 36 | +@dataclass(frozen=True, slots=True) |
| 37 | +class _VerificationModel(Generic[T, BasicDepsT, DepsT]): |
| 38 | + """A simple dataclass that wraps a vertex and all dependencies necessary for its verification.""" |
| 39 | + vertex: T |
| 40 | + basic_deps: BasicDepsT |
| 41 | + deps: DepsT | None |
| 42 | + |
| 43 | + |
| 44 | +@dataclass(frozen=True, slots=True) |
| 45 | +class VerificationBlock(_VerificationModel[Block, BasicBlockDependencies, BlockDependencies]): |
| 46 | + """A simple dataclass that wraps a Block and all dependencies necessary for its verification.""" |
| 47 | + |
| 48 | + |
| 49 | +@dataclass(frozen=True, slots=True) |
| 50 | +class VerificationMergeMinedBlock(_VerificationModel[MergeMinedBlock, BasicBlockDependencies, BlockDependencies]): |
| 51 | + """A simple dataclass that wraps a MergeMinedBlock and all dependencies necessary for its verification.""" |
| 52 | + |
| 53 | + |
| 54 | +@dataclass(frozen=True, slots=True) |
| 55 | +class VerificationTransaction(_VerificationModel[Transaction, None, TransactionDependencies]): |
| 56 | + """A simple dataclass that wraps a Transaction and all dependencies necessary for its verification.""" |
| 57 | + |
| 58 | + |
| 59 | +@dataclass(frozen=True, slots=True) |
| 60 | +class VerificationTokenCreationTransaction( |
| 61 | + _VerificationModel[TokenCreationTransaction, None, TransactionDependencies] |
| 62 | +): |
| 63 | + """A simple dataclass that wraps a TokenCreationTransaction and all dependencies necessary for its verification.""" |
| 64 | + |
| 65 | + |
| 66 | +"""A type alias representing an union type for verification models for all vertex types.""" |
| 67 | +VerificationModel: TypeAlias = ( |
| 68 | + VerificationBlock | VerificationMergeMinedBlock | VerificationTransaction | VerificationTokenCreationTransaction |
| 69 | +) |
| 70 | + |
| 71 | + |
| 72 | +def get_verification_model_from_storage( |
| 73 | + vertex: BaseTransaction, |
| 74 | + storage: TransactionStorage, |
| 75 | + *, |
| 76 | + daa: DifficultyAdjustmentAlgorithm, |
| 77 | + skip_weight_verification: bool = False, |
| 78 | + only_basic: bool = False |
| 79 | +) -> VerificationModel: |
| 80 | + """Create a verification model instance for a vertex using dependencies from a storage.""" |
| 81 | + # We assert with type() instead of isinstance() because each subclass has a specific branch. |
| 82 | + match vertex.version: |
| 83 | + case TxVersion.REGULAR_BLOCK: |
| 84 | + assert type(vertex) is Block |
| 85 | + basic_deps, deps = _get_block_deps( |
| 86 | + vertex, |
| 87 | + storage=storage, |
| 88 | + daa=daa, |
| 89 | + skip_weight_verification=skip_weight_verification, |
| 90 | + only_basic=only_basic, |
| 91 | + ) |
| 92 | + return VerificationBlock( |
| 93 | + vertex=vertex, |
| 94 | + basic_deps=basic_deps, |
| 95 | + deps=deps, |
| 96 | + ) |
| 97 | + |
| 98 | + case TxVersion.MERGE_MINED_BLOCK: |
| 99 | + assert type(vertex) is MergeMinedBlock |
| 100 | + basic_deps, deps = _get_block_deps( |
| 101 | + vertex, |
| 102 | + storage=storage, |
| 103 | + daa=daa, |
| 104 | + skip_weight_verification=skip_weight_verification, |
| 105 | + only_basic=only_basic, |
| 106 | + ) |
| 107 | + return VerificationMergeMinedBlock( |
| 108 | + vertex=vertex, |
| 109 | + basic_deps=basic_deps, |
| 110 | + deps=deps, |
| 111 | + ) |
| 112 | + |
| 113 | + case TxVersion.REGULAR_TRANSACTION: |
| 114 | + assert type(vertex) is Transaction |
| 115 | + return VerificationTransaction( |
| 116 | + vertex=vertex, |
| 117 | + basic_deps=None, |
| 118 | + deps=_get_tx_deps(vertex, storage=storage, only_basic=only_basic), |
| 119 | + ) |
| 120 | + |
| 121 | + case TxVersion.TOKEN_CREATION_TRANSACTION: |
| 122 | + assert type(vertex) is TokenCreationTransaction |
| 123 | + return VerificationTokenCreationTransaction( |
| 124 | + vertex=vertex, |
| 125 | + basic_deps=None, |
| 126 | + deps=_get_tx_deps(vertex, storage=storage, only_basic=only_basic), |
| 127 | + ) |
| 128 | + |
| 129 | + case _: |
| 130 | + assert_never(vertex.version) |
| 131 | + |
| 132 | + |
| 133 | +def _get_block_deps( |
| 134 | + block: Block, |
| 135 | + *, |
| 136 | + storage: TransactionStorage, |
| 137 | + daa: DifficultyAdjustmentAlgorithm, |
| 138 | + skip_weight_verification: bool, |
| 139 | + only_basic: bool |
| 140 | +) -> tuple[BasicBlockDependencies, BlockDependencies | None]: |
| 141 | + """Create the necessary dependencies instances for a Block, using a storage.""" |
| 142 | + basic_deps = BasicBlockDependencies.create_from_storage( |
| 143 | + block, |
| 144 | + storage=storage, |
| 145 | + daa=daa, |
| 146 | + skip_weight_verification=skip_weight_verification, |
| 147 | + ) |
| 148 | + deps = None |
| 149 | + if not only_basic: |
| 150 | + deps = BlockDependencies.create_from_storage(block, storage=storage) |
| 151 | + |
| 152 | + return basic_deps, deps |
| 153 | + |
| 154 | + |
| 155 | +def _get_tx_deps(tx: Transaction, *, storage: TransactionStorage, only_basic: bool) -> TransactionDependencies | None: |
| 156 | + """Create the necessary dependencies instances for a Transaction, using a storage.""" |
| 157 | + deps = None |
| 158 | + if not only_basic: |
| 159 | + deps = TransactionDependencies.create_from_storage(tx, storage) |
| 160 | + |
| 161 | + return deps |
0 commit comments