Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions hathor/transaction/storage/cache_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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."""
Expand Down
28 changes: 19 additions & 9 deletions hathor/transaction/storage/transaction_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -353,28 +363,28 @@ 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]:
"""This method is used to temporarily make the storage allow invalid transactions.

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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions hathor/transaction/storage/tx_allow_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)