|
| 1 | +# Copyright 2026 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 structlog import get_logger |
| 16 | + |
| 17 | +from hathor import Blueprint |
| 18 | +from hathor.conf.settings import HathorSettings |
| 19 | +from hathor.feature_activation.feature_service import FeatureService |
| 20 | +from hathor.nanocontracts.catalog import NCBlueprintCatalog |
| 21 | +from hathor.transaction.storage import TransactionStorage |
| 22 | +from hathor.transaction.storage.exceptions import TransactionDoesNotExist |
| 23 | +from hathorlib.nanocontracts.types import BlueprintId |
| 24 | +from hathor.nanocontracts import OnChainBlueprint |
| 25 | +from hathor.nanocontracts.exception import ( |
| 26 | + BlueprintDoesNotExist, |
| 27 | + OCBBlueprintNotConfirmed, |
| 28 | + OCBInvalidBlueprintVertexType, |
| 29 | +) |
| 30 | + |
| 31 | +logger = get_logger() |
| 32 | + |
| 33 | + |
| 34 | +class BlueprintService: |
| 35 | + __slots__ = ('log', 'settings', 'nc_catalog', 'tx_storage', 'feature_service') |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + *, |
| 40 | + settings: HathorSettings, |
| 41 | + tx_storage: TransactionStorage, |
| 42 | + feature_service: FeatureService, |
| 43 | + ) -> None: |
| 44 | + self.log = logger.new() |
| 45 | + self.settings = settings |
| 46 | + self.nc_catalog = NCBlueprintCatalog() |
| 47 | + self.tx_storage = tx_storage |
| 48 | + self.feature_service = feature_service |
| 49 | + |
| 50 | + if settings.ENABLE_NANO_CONTRACTS: |
| 51 | + blueprints = NCBlueprintCatalog.generate_blueprints_from_settings(settings) |
| 52 | + self.register_blueprints(blueprints) |
| 53 | + |
| 54 | + def get_on_chain_blueprint(self, blueprint_id: BlueprintId) -> OnChainBlueprint: |
| 55 | + """Return an on-chain blueprint transaction.""" |
| 56 | + try: |
| 57 | + blueprint_tx = self.tx_storage.get_transaction(blueprint_id) |
| 58 | + except TransactionDoesNotExist: |
| 59 | + self.log.debug('no transaction with the given id found', blueprint_id=blueprint_id.hex()) |
| 60 | + raise BlueprintDoesNotExist(blueprint_id.hex()) |
| 61 | + if not isinstance(blueprint_tx, OnChainBlueprint): |
| 62 | + raise OCBInvalidBlueprintVertexType(blueprint_id.hex()) |
| 63 | + tx_meta = blueprint_tx.get_metadata() |
| 64 | + if tx_meta.voided_by or not tx_meta.first_block: |
| 65 | + raise OCBBlueprintNotConfirmed(blueprint_id.hex()) |
| 66 | + # XXX: maybe use N blocks confirmation, like reward-locks |
| 67 | + return blueprint_tx |
| 68 | + |
| 69 | + def get_blueprint_class(self, blueprint_id: BlueprintId) -> type[Blueprint]: |
| 70 | + """Returns the blueprint class associated with the given blueprint_id. |
| 71 | +
|
| 72 | + The blueprint class could be in the catalog (first search), or it could be the tx_id of an on-chain blueprint. |
| 73 | + """ |
| 74 | + from hathor.nanocontracts import OnChainBlueprint |
| 75 | + blueprint = self._get_blueprint(blueprint_id) |
| 76 | + if isinstance(blueprint, OnChainBlueprint): |
| 77 | + return blueprint.get_blueprint_class() |
| 78 | + else: |
| 79 | + return blueprint |
| 80 | + |
| 81 | + def get_blueprint_source(self, blueprint_id: BlueprintId) -> str: |
| 82 | + """Returns the source code associated with the given blueprint_id. |
| 83 | +
|
| 84 | + The blueprint class could be in the catalog (first search), or it could be the tx_id of an on-chain blueprint. |
| 85 | + """ |
| 86 | + import inspect |
| 87 | + |
| 88 | + from hathor.nanocontracts import OnChainBlueprint |
| 89 | + |
| 90 | + blueprint = self._get_blueprint(blueprint_id) |
| 91 | + if isinstance(blueprint, OnChainBlueprint): |
| 92 | + return self.get_on_chain_blueprint(blueprint_id).code.text |
| 93 | + else: |
| 94 | + module = inspect.getmodule(blueprint) |
| 95 | + assert module is not None |
| 96 | + return inspect.getsource(module) |
| 97 | + |
| 98 | + def _get_blueprint(self, blueprint_id: BlueprintId) -> type[Blueprint] | OnChainBlueprint: |
| 99 | + if blueprint_class := self.nc_catalog.get_blueprint_class(blueprint_id): |
| 100 | + return blueprint_class |
| 101 | + |
| 102 | + self.log.debug( |
| 103 | + 'blueprint_id not in the catalog, looking for on-chain blueprint', |
| 104 | + blueprint_id=blueprint_id.hex() |
| 105 | + ) |
| 106 | + return self.get_on_chain_blueprint(blueprint_id) |
| 107 | + |
| 108 | + def register_blueprint(self, blueprint_id: bytes, blueprint: type[Blueprint], *, strict: bool = False) -> None: |
| 109 | + self.nc_catalog.register_blueprints({blueprint_id: blueprint}, strict=strict) |
| 110 | + |
| 111 | + def register_blueprints(self, blueprints: dict[bytes, type[Blueprint]], *, strict: bool = False) -> None: |
| 112 | + self.nc_catalog.register_blueprints(blueprints, strict=strict) |
0 commit comments