|
| 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 tx_or_block_from_bytes |
| 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, bytes] = {} |
| 31 | + self._transactions: dict[VertexId, bytes] = {} |
| 32 | + |
| 33 | + @property |
| 34 | + def _vertices(self) -> dict[VertexId, bytes]: |
| 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 | + if block_bytes := self._blocks.get(block_id): |
| 41 | + block = tx_or_block_from_bytes(block_bytes) |
| 42 | + assert isinstance(block, Block) |
| 43 | + return block |
| 44 | + |
| 45 | + raise TransactionDoesNotExist(f'Block "{block_id.hex()}" does not exist in this SimpleMemoryStorage.') |
| 46 | + |
| 47 | + def get_transaction(self, tx_id: VertexId) -> Transaction: |
| 48 | + """Return a transaction from the storage, throw if it's not found.""" |
| 49 | + if tx_bytes := self._transactions.get(tx_id): |
| 50 | + tx = tx_or_block_from_bytes(tx_bytes) |
| 51 | + assert isinstance(tx, Transaction) |
| 52 | + return tx |
| 53 | + |
| 54 | + raise TransactionDoesNotExist(f'Transaction "{tx_id.hex()}" does not exist in this SimpleMemoryStorage.') |
| 55 | + |
| 56 | + def get_parent_block(self, block: Block) -> Block: |
| 57 | + """Get the parent block of a block.""" |
| 58 | + parent_hash = block.get_block_parent_hash() |
| 59 | + |
| 60 | + return self.get_block(parent_hash) |
| 61 | + |
| 62 | + def add_vertices_from_storage(self, storage: TransactionStorage, ids: list[VertexId]) -> None: |
| 63 | + """ |
| 64 | + Add multiple vertices to this storage. It automatically fetches data from the provided TransactionStorage |
| 65 | + and a list of ids. |
| 66 | + """ |
| 67 | + for vertex_id in ids: |
| 68 | + self.add_vertex_from_storage(storage, vertex_id) |
| 69 | + |
| 70 | + def add_vertex_from_storage(self, storage: TransactionStorage, vertex_id: VertexId) -> None: |
| 71 | + """ |
| 72 | + Add a vertex to this storage. It automatically fetches data from the provided TransactionStorage and a list |
| 73 | + of ids. |
| 74 | + """ |
| 75 | + if vertex_id in self._vertices: |
| 76 | + return |
| 77 | + |
| 78 | + vertex = storage.get_transaction(vertex_id) |
| 79 | + vertex_bytes = vertex.get_struct() |
| 80 | + |
| 81 | + if isinstance(vertex, Block): |
| 82 | + self._blocks[vertex_id] = vertex_bytes |
| 83 | + return |
| 84 | + |
| 85 | + if isinstance(vertex, Transaction): |
| 86 | + self._transactions[vertex_id] = vertex_bytes |
| 87 | + return |
| 88 | + |
| 89 | + raise NotImplementedError |
0 commit comments