@@ -168,7 +168,7 @@ def __init__(self,
168
168
self .outputs = outputs or []
169
169
self .parents = parents or []
170
170
self .storage = storage
171
- self .hash = hash # Stored as bytes.
171
+ self ._hash : VertexId | None = hash # Stored as bytes.
172
172
173
173
@classproperty
174
174
def log (cls ):
@@ -253,7 +253,7 @@ def __eq__(self, other: object) -> bool:
253
253
"""
254
254
if not isinstance (other , BaseTransaction ):
255
255
return NotImplemented
256
- if self .hash and other .hash :
256
+ if self ._hash and other ._hash :
257
257
return self .hash == other .hash
258
258
return False
259
259
@@ -265,7 +265,6 @@ def __bytes__(self) -> bytes:
265
265
return self .get_struct ()
266
266
267
267
def __hash__ (self ) -> int :
268
- assert self .hash is not None
269
268
return hash (self .hash )
270
269
271
270
@abstractmethod
@@ -276,10 +275,19 @@ def calculate_height(self) -> int:
276
275
def calculate_min_height (self ) -> int :
277
276
raise NotImplementedError
278
277
278
+ @property
279
+ def hash (self ) -> VertexId :
280
+ assert self ._hash is not None , 'Vertex hash must be initialized.'
281
+ return self ._hash
282
+
283
+ @hash .setter
284
+ def hash (self , value : VertexId ) -> None :
285
+ self ._hash = value
286
+
279
287
@property
280
288
def hash_hex (self ) -> str :
281
289
"""Return the current stored hash in hex string format"""
282
- if self .hash is not None :
290
+ if self ._hash is not None :
283
291
return self .hash .hex ()
284
292
else :
285
293
return ''
@@ -332,7 +340,7 @@ def is_genesis(self) -> bool:
332
340
333
341
:rtype: bool
334
342
"""
335
- if self .hash is None :
343
+ if self ._hash is None :
336
344
return False
337
345
from hathor .transaction .genesis import is_genesis
338
346
return is_genesis (self .hash , settings = self ._settings )
@@ -451,7 +459,7 @@ def can_validate_full(self) -> bool:
451
459
""" Check if this transaction is ready to be fully validated, either all deps are full-valid or one is invalid.
452
460
"""
453
461
assert self .storage is not None
454
- assert self .hash is not None
462
+ assert self ._hash is not None
455
463
if self .is_genesis :
456
464
return True
457
465
deps = self .get_all_dependencies ()
@@ -608,7 +616,6 @@ def get_metadata(self, *, force_reload: bool = False, use_storage: bool = True)
608
616
else :
609
617
metadata = getattr (self , '_metadata' , None )
610
618
if not metadata and use_storage and self .storage :
611
- assert self .hash is not None
612
619
metadata = self .storage .get_metadata (self .hash )
613
620
self ._metadata = metadata
614
621
if not metadata :
@@ -619,15 +626,15 @@ def get_metadata(self, *, force_reload: bool = False, use_storage: bool = True)
619
626
score = self .weight if self .is_genesis else 0
620
627
621
628
metadata = TransactionMetadata (
622
- hash = self .hash ,
629
+ hash = self ._hash ,
623
630
accumulated_weight = self .weight ,
624
631
height = height ,
625
632
score = score ,
626
633
min_height = 0 ,
627
634
)
628
635
self ._metadata = metadata
629
636
if not metadata .hash :
630
- metadata .hash = self .hash
637
+ metadata .hash = self ._hash
631
638
metadata ._tx_ref = weakref .ref (self )
632
639
return metadata
633
640
@@ -638,7 +645,7 @@ def reset_metadata(self) -> None:
638
645
from hathor .transaction .transaction_metadata import ValidationState
639
646
assert self .storage is not None
640
647
score = self .weight if self .is_genesis else 0
641
- self ._metadata = TransactionMetadata (hash = self .hash ,
648
+ self ._metadata = TransactionMetadata (hash = self ._hash ,
642
649
score = score ,
643
650
accumulated_weight = self .weight )
644
651
if self .is_genesis :
@@ -724,7 +731,7 @@ def _update_reward_lock_metadata(self) -> None:
724
731
725
732
def _update_parents_children_metadata (self ) -> None :
726
733
"""Update the txs/block parent's children metadata."""
727
- assert self .hash is not None
734
+ assert self ._hash is not None
728
735
assert self .storage is not None
729
736
730
737
for parent in self .get_parents (existing_only = True ):
@@ -792,7 +799,6 @@ def to_json(self, decode_script: bool = False, include_metadata: bool = False) -
792
799
return data
793
800
794
801
def to_json_extended (self ) -> dict [str , Any ]:
795
- assert self .hash is not None
796
802
assert self .storage is not None
797
803
798
804
def serialize_output (tx : BaseTransaction , tx_out : TxOutput ) -> dict [str , Any ]:
@@ -824,7 +830,6 @@ def serialize_output(tx: BaseTransaction, tx_out: TxOutput) -> dict[str, Any]:
824
830
tx2 = self .storage .get_transaction (tx_in .tx_id )
825
831
tx2_out = tx2 .outputs [tx_in .index ]
826
832
output = serialize_output (tx2 , tx2_out )
827
- assert tx2 .hash is not None
828
833
output ['tx_id' ] = tx2 .hash_hex
829
834
output ['index' ] = tx_in .index
830
835
ret ['inputs' ].append (output )
0 commit comments