|
| 1 | +# Copyright 2023 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 hathor.transaction import Block, Transaction |
| 16 | +from hathor.transaction.base_transaction import BaseTransaction |
| 17 | +from hathor.transaction.storage import TransactionStorage |
| 18 | +from hathor.transaction.storage.exceptions import TransactionDoesNotExist |
| 19 | +from hathor.types import VertexId |
| 20 | + |
| 21 | + |
| 22 | +class SimpleMemoryStorage: |
| 23 | + """ |
| 24 | + Instances of this class simply facilitate storing some data in memory, specifically for pre-fetched verification |
| 25 | + dependencies. |
| 26 | + """ |
| 27 | + __slots__ = ('_blocks', '_transactions',) |
| 28 | + |
| 29 | + def __init__(self) -> None: |
| 30 | + self._blocks: dict[VertexId, BaseTransaction] = {} |
| 31 | + self._transactions: dict[VertexId, BaseTransaction] = {} |
| 32 | + |
| 33 | + @property |
| 34 | + def _vertices(self) -> dict[VertexId, BaseTransaction]: |
| 35 | + """Blocks and Transactions together.""" |
| 36 | + return {**self._blocks, **self._transactions} |
| 37 | + |
| 38 | + def get_block(self, block_id: VertexId) -> Block: |
| 39 | + """Return a block from the storage, throw if it's not found.""" |
| 40 | + block = self._get_vertex(self._blocks, block_id) |
| 41 | + assert isinstance(block, Block) |
| 42 | + return block |
| 43 | + |
| 44 | + def get_transaction(self, tx_id: VertexId) -> Transaction: |
| 45 | + """Return a transaction from the storage, throw if it's not found.""" |
| 46 | + tx = self._get_vertex(self._transactions, tx_id) |
| 47 | + assert isinstance(tx, Transaction) |
| 48 | + return tx |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def _get_vertex(storage: dict[VertexId, BaseTransaction], vertex_id: VertexId) -> BaseTransaction: |
| 52 | + """Return a vertex from a storage, throw if it's not found.""" |
| 53 | + if vertex := storage.get(vertex_id): |
| 54 | + return vertex |
| 55 | + |
| 56 | + raise TransactionDoesNotExist(f'Vertex "{vertex_id.hex()}" does not exist in this SimpleMemoryStorage.') |
| 57 | + |
| 58 | + def get_parent_block(self, block: Block) -> Block: |
| 59 | + """Get the parent block of a block.""" |
| 60 | + parent_hash = block.get_block_parent_hash() |
| 61 | + |
| 62 | + return self.get_block(parent_hash) |
| 63 | + |
| 64 | + def add_vertices_from_storage(self, storage: TransactionStorage, ids: list[VertexId]) -> None: |
| 65 | + """ |
| 66 | + Add multiple vertices to this storage. It automatically fetches data from the provided TransactionStorage |
| 67 | + and a list of ids. |
| 68 | + """ |
| 69 | + for vertex_id in ids: |
| 70 | + self.add_vertex_from_storage(storage, vertex_id) |
| 71 | + |
| 72 | + def add_vertex_from_storage(self, storage: TransactionStorage, vertex_id: VertexId) -> None: |
| 73 | + """ |
| 74 | + Add a vertex to this storage. It automatically fetches data from the provided TransactionStorage and a list |
| 75 | + of ids. |
| 76 | + """ |
| 77 | + if vertex_id in self._vertices: |
| 78 | + return |
| 79 | + |
| 80 | + vertex = storage.get_transaction(vertex_id) |
| 81 | + clone = vertex.clone(include_metadata=True, include_storage=False) |
| 82 | + |
| 83 | + if isinstance(vertex, Block): |
| 84 | + self._blocks[vertex_id] = clone |
| 85 | + return |
| 86 | + |
| 87 | + if isinstance(vertex, Transaction): |
| 88 | + self._transactions[vertex_id] = clone |
| 89 | + return |
| 90 | + |
| 91 | + raise NotImplementedError |
| 92 | + |
| 93 | + def get_vertex(self, vertex_id: VertexId) -> BaseTransaction: |
| 94 | + # TODO: Currently unused, will be implemented in a next PR. |
| 95 | + raise NotImplementedError |
| 96 | + |
| 97 | + def get_best_block_tips(self) -> list[VertexId]: |
| 98 | + # TODO: Currently unused, will be implemented in a next PR. |
| 99 | + raise NotImplementedError |
0 commit comments