Skip to content

Commit e774d52

Browse files
committed
review changes
1 parent e2f823e commit e774d52

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

hathor/transaction/base_transaction.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ def serialize_output(tx: BaseTransaction, tx_out: TxOutput) -> dict[str, Any]:
825825

826826
return ret
827827

828-
def clone(self, *, include_metadata: bool = True) -> 'BaseTransaction':
828+
def clone(self, *, include_metadata: bool = True, include_storage: bool = True) -> 'BaseTransaction':
829829
"""Return exact copy without sharing memory, including metadata if loaded.
830830
831831
:return: Transaction or Block copy
@@ -834,7 +834,8 @@ def clone(self, *, include_metadata: bool = True) -> 'BaseTransaction':
834834
if hasattr(self, '_metadata') and include_metadata:
835835
assert self._metadata is not None # FIXME: is this actually true or do we have to check if not None
836836
new_tx._metadata = self._metadata.clone()
837-
new_tx.storage = self.storage
837+
if include_storage:
838+
new_tx.storage = self.storage
838839
return new_tx
839840

840841
@abstractmethod

hathor/transaction/storage/simple_memory_storage.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

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
15+
from hathor.transaction import Block, Transaction
16+
from hathor.transaction.base_transaction import BaseTransaction
2017
from hathor.transaction.storage import TransactionStorage
2118
from hathor.transaction.storage.exceptions import TransactionDoesNotExist
2219
from hathor.types import VertexId
2320

2421

25-
@dataclass(frozen=True, slots=True)
26-
class _SimpleMemoryRecord:
27-
vertex_bytes: bytes
28-
vertex_metadata: dict[str, Any]
29-
30-
3122
class SimpleMemoryStorage:
3223
"""
3324
Instances of this class simply facilitate storing some data in memory, specifically for pre-fetched verification
@@ -36,33 +27,30 @@ class SimpleMemoryStorage:
3627
__slots__ = ('_blocks', '_transactions',)
3728

3829
def __init__(self) -> None:
39-
self._blocks: dict[VertexId, _SimpleMemoryRecord] = {}
40-
self._transactions: dict[VertexId, _SimpleMemoryRecord] = {}
30+
self._blocks: dict[VertexId, BaseTransaction] = {}
31+
self._transactions: dict[VertexId, BaseTransaction] = {}
4132

4233
@property
43-
def _vertices(self) -> dict[VertexId, _SimpleMemoryRecord]:
34+
def _vertices(self) -> dict[VertexId, BaseTransaction]:
4435
"""Blocks and Transactions together."""
4536
return {**self._blocks, **self._transactions}
4637

4738
def get_block(self, block_id: VertexId) -> Block:
4839
"""Return a block from the storage, throw if it's not found."""
49-
block = self._get_record(self._blocks, block_id)
40+
block = self._get_vertex(self._blocks, block_id)
5041
assert isinstance(block, Block)
5142
return block
5243

5344
def get_transaction(self, tx_id: VertexId) -> Transaction:
5445
"""Return a transaction from the storage, throw if it's not found."""
55-
tx = self._get_record(self._transactions, tx_id)
46+
tx = self._get_vertex(self._transactions, tx_id)
5647
assert isinstance(tx, Transaction)
5748
return tx
5849

5950
@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
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):
6654
return vertex
6755

6856
raise TransactionDoesNotExist(f'Vertex "{vertex_id.hex()}" does not exist in this SimpleMemoryStorage.')
@@ -90,16 +78,14 @@ def add_vertex_from_storage(self, storage: TransactionStorage, vertex_id: Vertex
9078
return
9179

9280
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)
81+
clone = vertex.clone(include_metadata=True, include_storage=False)
9682

9783
if isinstance(vertex, Block):
98-
self._blocks[vertex_id] = record
84+
self._blocks[vertex_id] = clone
9985
return
10086

10187
if isinstance(vertex, Transaction):
102-
self._transactions[vertex_id] = record
88+
self._transactions[vertex_id] = clone
10389
return
10490

10591
raise NotImplementedError

0 commit comments

Comments
 (0)