diff --git a/hathor/transaction/storage/cache_storage.py b/hathor/transaction/storage/cache_storage.py index 55f5be962..8a49b4938 100644 --- a/hathor/transaction/storage/cache_storage.py +++ b/hathor/transaction/storage/cache_storage.py @@ -21,6 +21,7 @@ from hathor.transaction import BaseTransaction from hathor.transaction.storage.migrations import MigrationState from hathor.transaction.storage.transaction_storage import BaseTransactionStorage +from hathor.transaction.storage.tx_allow_scope import TxAllowScope from hathor.util import Reactor @@ -69,6 +70,14 @@ def __init__(self, store: 'BaseTransactionStorage', reactor: Reactor, interval: # attribute the same weakref for both. super().__init__(indexes=indexes) self._tx_weakref = store._tx_weakref + # XXX: just to make sure this isn't being used anywhere, setters/getters should be used instead + del self._allow_scope + + def set_allow_scope(self, allow_scope: TxAllowScope) -> None: + self.store._allow_scope = allow_scope + + def get_allow_scope(self) -> TxAllowScope: + return self.store._allow_scope def set_capacity(self, capacity: int) -> None: """Change the max number of items in cache.""" diff --git a/hathor/transaction/storage/transaction_storage.py b/hathor/transaction/storage/transaction_storage.py index 10d105646..690a461bd 100644 --- a/hathor/transaction/storage/transaction_storage.py +++ b/hathor/transaction/storage/transaction_storage.py @@ -101,7 +101,7 @@ def __init__(self) -> None: self._weakref_lock: Lock = Lock() # Flag to allow/disallow partially validated vertices. - self.allow_scope: TxAllowScope = TxAllowScope.VALID + self._allow_scope: TxAllowScope = TxAllowScope.VALID # Cache for the best block tips # This cache is updated in the consensus algorithm. @@ -137,6 +137,16 @@ def __init__(self) -> None: raise ValueError(f'Duplicate migration name "{migration_name}"') migration_names.add(migration_name) + def set_allow_scope(self, allow_scope: TxAllowScope) -> None: + """Set the allow scope for the current storage. + + This method should not normally be used directly, use one of the `allow_*_scope` methods instead.""" + self._allow_scope = allow_scope + + def get_allow_scope(self) -> TxAllowScope: + """Get the current allow scope.""" + return self._allow_scope + @abstractmethod def reset_indexes(self) -> None: """Reset all the indexes, making sure that no persisted value is reused.""" @@ -353,7 +363,7 @@ def allow_partially_validated_context(self) -> AbstractContextManager[None]: The implementation will INCLUDE allowing partially valid transactions to the current allow scope. """ - new_allow_scope = self.allow_scope | TxAllowScope.PARTIAL + new_allow_scope = self.get_allow_scope() | TxAllowScope.PARTIAL return tx_allow_context(self, allow_scope=new_allow_scope) def allow_invalid_context(self) -> AbstractContextManager[None]: @@ -361,20 +371,20 @@ def allow_invalid_context(self) -> AbstractContextManager[None]: The implementation will INCLUDE allowing invalid transactions to the current allow scope. """ - new_allow_scope = self.allow_scope | TxAllowScope.INVALID + new_allow_scope = self.get_allow_scope() | TxAllowScope.INVALID return tx_allow_context(self, allow_scope=new_allow_scope) def is_only_valid_allowed(self) -> bool: """Whether only valid transactions are allowed to be returned/accepted by the storage, the default state.""" - return self.allow_scope is TxAllowScope.VALID + return self.get_allow_scope() is TxAllowScope.VALID def is_partially_validated_allowed(self) -> bool: """Whether partially validated transactions are allowed to be returned/accepted by the storage.""" - return TxAllowScope.PARTIAL in self.allow_scope + return TxAllowScope.PARTIAL in self.get_allow_scope() def is_invalid_allowed(self) -> bool: """Whether invalid transactions are allowed to be returned/accepted by the storage.""" - return TxAllowScope.INVALID in self.allow_scope + return TxAllowScope.INVALID in self.get_allow_scope() def _enable_weakref(self) -> None: """ Weakref should never be disabled unless you know exactly what you are doing. @@ -433,9 +443,9 @@ def _validate_partial_marker_consistency(self, tx_meta: TransactionMetadata) -> 'Inconsistent ValidationState and voided_by' def _validate_transaction_in_scope(self, tx: BaseTransaction) -> None: - if not self.allow_scope.is_allowed(tx): + if not self.get_allow_scope().is_allowed(tx): tx_meta = tx.get_metadata() - raise TransactionNotInAllowedScopeError(tx.hash_hex, self.allow_scope.name, tx_meta.validation.name) + raise TransactionNotInAllowedScopeError(tx.hash_hex, self.get_allow_scope().name, tx_meta.validation.name) @abstractmethod def remove_transaction(self, tx: BaseTransaction) -> None: @@ -567,7 +577,7 @@ def get_all_transactions(self) -> Iterator[BaseTransaction]: """Return all vertices (transactions and blocks) within the allowed scope. """ for tx in self._get_all_transactions(): - if self.allow_scope.is_allowed(tx): + if self.get_allow_scope().is_allowed(tx): yield tx @abstractmethod diff --git a/hathor/transaction/storage/tx_allow_scope.py b/hathor/transaction/storage/tx_allow_scope.py index f490abd0d..34031aee1 100644 --- a/hathor/transaction/storage/tx_allow_scope.py +++ b/hathor/transaction/storage/tx_allow_scope.py @@ -57,9 +57,9 @@ def tx_allow_context(tx_storage: 'TransactionStorage', *, allow_scope: TxAllowSc """This is used to wrap the storage with a temporary allow-scope that is reverted when the context exits""" from hathor.transaction.storage import TransactionStorage assert isinstance(tx_storage, TransactionStorage) - previous_allow_scope = tx_storage.allow_scope + previous_allow_scope = tx_storage.get_allow_scope() try: - tx_storage.allow_scope = allow_scope + tx_storage.set_allow_scope(allow_scope) yield finally: - tx_storage.allow_scope = previous_allow_scope + tx_storage.set_allow_scope(previous_allow_scope)