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