12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
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
15
+ from hathor .transaction import Block , Transaction
16
+ from hathor .transaction .base_transaction import BaseTransaction
20
17
from hathor .transaction .storage import TransactionStorage
21
18
from hathor .transaction .storage .exceptions import TransactionDoesNotExist
22
19
from hathor .types import VertexId
23
20
24
21
25
- @dataclass (frozen = True , slots = True )
26
- class _SimpleMemoryRecord :
27
- vertex_bytes : bytes
28
- vertex_metadata : dict [str , Any ]
29
-
30
-
31
22
class SimpleMemoryStorage :
32
23
"""
33
24
Instances of this class simply facilitate storing some data in memory, specifically for pre-fetched verification
@@ -36,33 +27,30 @@ class SimpleMemoryStorage:
36
27
__slots__ = ('_blocks' , '_transactions' ,)
37
28
38
29
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 ] = {}
41
32
42
33
@property
43
- def _vertices (self ) -> dict [VertexId , _SimpleMemoryRecord ]:
34
+ def _vertices (self ) -> dict [VertexId , BaseTransaction ]:
44
35
"""Blocks and Transactions together."""
45
36
return {** self ._blocks , ** self ._transactions }
46
37
47
38
def get_block (self , block_id : VertexId ) -> Block :
48
39
"""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 )
50
41
assert isinstance (block , Block )
51
42
return block
52
43
53
44
def get_transaction (self , tx_id : VertexId ) -> Transaction :
54
45
"""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 )
56
47
assert isinstance (tx , Transaction )
57
48
return tx
58
49
59
50
@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 ):
66
54
return vertex
67
55
68
56
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
90
78
return
91
79
92
80
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 )
96
82
97
83
if isinstance (vertex , Block ):
98
- self ._blocks [vertex_id ] = record
84
+ self ._blocks [vertex_id ] = clone
99
85
return
100
86
101
87
if isinstance (vertex , Transaction ):
102
- self ._transactions [vertex_id ] = record
88
+ self ._transactions [vertex_id ] = clone
103
89
return
104
90
105
91
raise NotImplementedError
0 commit comments