Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove unnecessary asserts #968

Merged
merged 1 commit into from
Apr 18, 2024
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
2 changes: 0 additions & 2 deletions hathor/cli/db_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def iter_tx(self) -> Iterator['BaseTransaction']:
soft_voided_ids = set(settings.SOFT_VOIDED_TX_IDS)

for tx in self._iter_tx:
assert tx.hash is not None
# XXX: if we're skipping voided transactions, we have to be careful not to skip soft-voided ones
if self.skip_voided:
voided_by = tx.get_metadata().voided_by or set()
Expand All @@ -110,7 +109,6 @@ def run(self) -> None:
# nothing, and it's probably better to finish sooner than expected, rather than later than expected
total = self.tx_storage.get_vertices_count()
for tx in tx_progress(self.iter_tx(), log=self.log, total=total):
assert tx.hash is not None
tx_meta = tx.get_metadata()
if tx.is_block:
assert isinstance(tx, Block)
Expand Down
1 change: 0 additions & 1 deletion hathor/cli/db_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def _import_txs(self) -> Iterator['BaseTransaction']:
sys.exit(2)
tx = tx_or_block_from_bytes(tx_bytes)
assert tx is not None
assert tx.hash is not None
tx.storage = self.tx_storage
self.manager.on_new_tx(tx, quiet=True, fails_silently=False, skip_block_weight_verification=True)
yield tx
Expand Down
3 changes: 1 addition & 2 deletions hathor/cli/events_simulator/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def simulate_reorg(simulator: 'Simulator', manager: 'HathorManager') -> None:
def simulate_unvoided_transaction(simulator: 'Simulator', manager: 'HathorManager') -> None:
from hathor.conf.get_settings import get_global_settings
from hathor.simulator.utils import add_new_block, add_new_blocks, gen_new_tx
from hathor.util import not_none

settings = get_global_settings()
assert manager.wallet is not None
Expand Down Expand Up @@ -132,7 +131,7 @@ def simulate_unvoided_transaction(simulator: 'Simulator', manager: 'HathorManage
block.parents = [
block.parents[0],
settings.GENESIS_TX1_HASH,
not_none(tx2.hash),
tx2.hash,
]
block.update_hash()
assert manager.propagate_tx(block, fails_silently=False)
Expand Down
2 changes: 0 additions & 2 deletions hathor/cli/mining.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def execute(args: Namespace) -> None:

block_bytes = base64.b64decode(data['block_bytes'])
block = Block.create_from_struct(block_bytes)
assert block.hash is not None
assert isinstance(block, Block)
print('Mining block with weight {}'.format(block.weight))

Expand All @@ -130,7 +129,6 @@ def execute(args: Namespace) -> None:

block = q_out.get()
block.update_hash()
assert block.hash is not None
print('[{}] New block found: {} (nonce={}, weight={})'.format(datetime.datetime.now(), block.hash.hex(),
block.nonce, block.weight))

Expand Down
2 changes: 0 additions & 2 deletions hathor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ def _on_new_tx(self, key: HathorEvents, args: EventArguments) -> None:

async def submit(self, block: Block) -> Optional[BlockTemplate]:
if await self.submit(block):
assert block.hash is not None
return self.manager.make_block_template(block.hash)
else:
return None
Expand Down Expand Up @@ -397,7 +396,6 @@ def create_tx_from_dict(data: dict[str, Any], update_hash: bool = False,
tx = cls(**data)
if update_hash:
tx.update_hash()
assert tx.hash is not None
if hash_bytes:
assert tx.hash == hash_bytes, f'Hashes differ: {tx.hash!r} != {hash_bytes!r}'
if metadata:
Expand Down
12 changes: 3 additions & 9 deletions hathor/consensus/block_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from hathor.conf.get_settings import get_global_settings
from hathor.profiler import get_cpu_profiler
from hathor.transaction import BaseTransaction, Block, Transaction, sum_weights
from hathor.util import classproperty, not_none
from hathor.util import classproperty

if TYPE_CHECKING:
from hathor.consensus.context import ConsensusAlgorithmContext
Expand Down Expand Up @@ -107,7 +107,6 @@ def update_voided_info(self, block: Block) -> None:
return

assert block.storage is not None
assert block.hash is not None

storage = block.storage
assert storage.indexes is not None
Expand Down Expand Up @@ -216,8 +215,8 @@ def update_voided_info(self, block: Block) -> None:
if common_block not in heads:
self.context.mark_as_reorg(common_block)
else:
best_block_tips = [not_none(blk.hash) for blk in heads]
best_block_tips.append(not_none(block.hash))
best_block_tips = [blk.hash for blk in heads]
best_block_tips.append(block.hash)
storage.update_best_block_tips_cache(best_block_tips)
if not meta.voided_by:
self.context.mark_as_reorg(common_block)
Expand All @@ -231,7 +230,6 @@ def union_voided_by_from_parents(self, block: Block) -> set[bytes]:
"""
voided_by: set[bytes] = set()
for parent in block.get_parents():
assert parent.hash is not None
parent_meta = parent.get_metadata()
voided_by2 = parent_meta.voided_by
if voided_by2:
Expand Down Expand Up @@ -370,7 +368,6 @@ def add_voided_by(self, block: Block, voided_hash: Optional[bytes] = None) -> bo
the block's own hash.
"""
assert block.storage is not None
assert block.hash is not None

storage = block.storage

Expand Down Expand Up @@ -401,7 +398,6 @@ def remove_voided_by(self, block: Block, voided_hash: Optional[bytes] = None) ->
the block's own hash.
"""
assert block.storage is not None
assert block.hash is not None

storage = block.storage

Expand Down Expand Up @@ -454,7 +450,6 @@ def _score_block_dfs(self, block: BaseTransaction, used: set[bytes],
""" Internal method to run a DFS. It is used by `calculate_score()`.
"""
assert block.storage is not None
assert block.hash is not None
assert block.is_block

storage = block.storage
Expand All @@ -475,7 +470,6 @@ def _score_block_dfs(self, block: BaseTransaction, used: set[bytes],
from hathor.transaction.storage.traversal import BFSTimestampWalk
bfs = BFSTimestampWalk(storage, is_dag_verifications=True, is_left_to_right=False)
for tx in bfs.run(parent, skip_root=False):
assert tx.hash is not None
assert not tx.is_block

if tx.hash in used:
Expand Down
10 changes: 0 additions & 10 deletions hathor/consensus/transaction_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def mark_inputs_as_used(self, tx: Transaction) -> None:
def mark_input_as_used(self, tx: Transaction, txin: TxInput) -> None:
""" Mark a given input as used
"""
assert tx.hash is not None
assert tx.storage is not None

spent_tx = tx.storage.get_transaction(txin.tx_id)
Expand Down Expand Up @@ -117,7 +116,6 @@ def check_twins(self, tx: Transaction, transactions: Iterable[BaseTransaction])

:param transactions: list of transactions to be checked if they are twins with self
"""
assert tx.hash is not None
assert tx.storage is not None

# Getting tx metadata to save the new twins
Expand All @@ -128,7 +126,6 @@ def check_twins(self, tx: Transaction, transactions: Iterable[BaseTransaction])
sorted_outputs = sorted(tx.outputs, key=lambda x: (x.script, x.value))

for candidate in transactions:
assert candidate.hash is not None

# If quantity of inputs is different, it's not a twin.
if len(candidate.inputs) != len(tx.inputs):
Expand Down Expand Up @@ -170,7 +167,6 @@ def check_twins(self, tx: Transaction, transactions: Iterable[BaseTransaction])
def update_voided_info(self, tx: Transaction) -> None:
""" This method should be called only once when the transactions is added to the DAG.
"""
assert tx.hash is not None
assert tx.storage is not None

voided_by: set[bytes] = set()
Expand Down Expand Up @@ -267,7 +263,6 @@ def check_conflicts(self, tx: Transaction) -> None:
The verification is made for each input, and `self` is only marked as winner if it
wins in all its inputs.
"""
assert tx.hash is not None
assert tx.storage is not None
self.log.debug('tx.check_conflicts', tx=tx.hash_hex)

Expand Down Expand Up @@ -326,7 +321,6 @@ def mark_as_winner(self, tx: Transaction) -> None:
""" Mark a transaction as winner when it has a conflict and its aggregated weight
is the greatest one.
"""
assert tx.hash is not None
self.log.debug('tx.mark_as_winner', tx=tx.hash_hex)
meta = tx.get_metadata()
assert bool(meta.conflict_with) # FIXME: this looks like a runtime guarantee, MUST NOT be an assert
Expand All @@ -341,7 +335,6 @@ def remove_voided_by(self, tx: Transaction, voided_hash: bytes) -> bool:
"""
from hathor.transaction.storage.traversal import BFSTimestampWalk

assert tx.hash is not None
assert tx.storage is not None

meta = tx.get_metadata()
Expand Down Expand Up @@ -382,7 +375,6 @@ def mark_as_voided(self, tx: Transaction) -> None:
""" Mark a transaction as voided when it has a conflict and its aggregated weight
is NOT the greatest one.
"""
assert tx.hash is not None
self.log.debug('tx.mark_as_voided', tx=tx.hash_hex)
meta = tx.get_metadata()
assert bool(meta.conflict_with)
Expand All @@ -395,7 +387,6 @@ def add_voided_by(self, tx: Transaction, voided_hash: bytes) -> bool:
""" Add a hash from `meta.voided_by` and its descendants (both from verification DAG
and funds tree).
"""
assert tx.hash is not None
assert tx.storage is not None

meta = tx.get_metadata()
Expand All @@ -415,7 +406,6 @@ def add_voided_by(self, tx: Transaction, voided_hash: bytes) -> bool:
check_list: list[Transaction] = []
for tx2 in bfs.run(tx, skip_root=False):
assert tx2.storage is not None
assert tx2.hash is not None
meta2 = tx2.get_metadata()

if tx2.is_block:
Expand Down
4 changes: 0 additions & 4 deletions hathor/graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def __init__(self, storage: TransactionStorage, include_funds: bool = False,
def get_node_label(self, tx: BaseTransaction) -> str:
""" Return the node's label for tx.
"""
assert tx.hash is not None
if tx.hash in self.labels:
parts = [self.labels[tx.hash]]
else:
Expand All @@ -79,7 +78,6 @@ def get_node_label(self, tx: BaseTransaction) -> str:
def get_node_attrs(self, tx: BaseTransaction) -> dict[str, str]:
""" Return node's attributes.
"""
assert tx.hash is not None
node_attrs = {'label': self.get_node_label(tx)}

if tx.is_block:
Expand Down Expand Up @@ -151,7 +149,6 @@ def dot(self, format: str = 'pdf') -> Digraph:
if self.only_blocks and not tx.is_block:
continue

assert tx.hash is not None
name = tx.hash.hex()

node_attrs = self.get_node_attrs(tx)
Expand Down Expand Up @@ -204,7 +201,6 @@ def tx_neighborhood(self, tx: BaseTransaction, format: str = 'pdf',

while to_visit:
level, tx = to_visit.pop()
assert tx.hash is not None
assert tx.storage is not None
name = tx.hash.hex()
node_attrs = self.get_node_attrs(tx)
Expand Down
4 changes: 1 addition & 3 deletions hathor/indexes/height_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from hathor.indexes.scope import Scope
from hathor.transaction import BaseTransaction, Block
from hathor.types import VertexId
from hathor.util import not_none

SCOPE = Scope(
include_blocks=True,
Expand Down Expand Up @@ -66,7 +65,6 @@ def init_loop_step(self, tx: BaseTransaction) -> None:
if tx.is_genesis:
return
assert isinstance(tx, Block)
assert tx.hash is not None
if tx.get_metadata().voided_by:
return
self.add_new(tx.get_height(), tx.hash, tx.timestamp)
Expand Down Expand Up @@ -118,7 +116,7 @@ def update_new_chain(self, height: int, block: Block) -> None:
add_to_index: list[_AddToIndexItem] = []
while self.get(block_height) != side_chain_block.hash:
add_to_index.append(
_AddToIndexItem(block_height, not_none(side_chain_block.hash), side_chain_block.timestamp)
_AddToIndexItem(block_height, side_chain_block.hash, side_chain_block.timestamp)
)

side_chain_block = side_chain_block.get_block_parent()
Expand Down
1 change: 0 additions & 1 deletion hathor/indexes/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ def del_tx(self, tx: BaseTransaction, *, remove_all: bool = False, relax_assert:
:param tx: Transaction to be deleted
"""
assert tx.storage is not None
assert tx.hash is not None

if remove_all:
# We delete from indexes in two cases: (i) mark tx as voided, and (ii) remove tx.
Expand Down
1 change: 0 additions & 1 deletion hathor/indexes/memory_timestamp_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def force_clear(self) -> None:
self._index = SortedKeyList(key=lambda x: (x.timestamp, x.hash))

def add_tx(self, tx: BaseTransaction) -> bool:
assert tx.hash is not None
# It is safe to use the in operator because it is O(log(n)).
# http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html#sortedcontainers.SortedList.__contains__
element = TransactionIndexElement(tx.timestamp, tx.hash)
Expand Down
3 changes: 0 additions & 3 deletions hathor/indexes/memory_tips_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def add_tx(self, tx: BaseTransaction) -> bool:

:param tx: Transaction to be added
"""
assert tx.hash is not None
assert tx.storage is not None
if tx.hash in self.tx_last_interval:
return False
Expand Down Expand Up @@ -110,7 +109,6 @@ def add_tx(self, tx: BaseTransaction) -> bool:
def del_tx(self, tx: BaseTransaction, *, relax_assert: bool = False) -> None:
""" Remove a transaction from the index.
"""
assert tx.hash is not None
assert tx.storage is not None

interval = self.tx_last_interval.pop(tx.hash, None)
Expand All @@ -134,7 +132,6 @@ def update_tx(self, tx: BaseTransaction, *, relax_assert: bool = False) -> None:
""" Update a tx according to its children.
"""
assert tx.storage is not None
assert tx.hash is not None

meta = tx.get_metadata()
if meta.voided_by:
Expand Down
5 changes: 0 additions & 5 deletions hathor/indexes/memory_tokens_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def force_clear(self) -> None:
def _add_to_index(self, tx: BaseTransaction, index: int) -> None:
""" Add tx to mint/melt indexes and total amount
"""
assert tx.hash is not None

tx_output = tx.outputs[index]
token_uid = tx.get_token_uid(tx_output.get_token_index())
Expand All @@ -98,7 +97,6 @@ def _add_to_index(self, tx: BaseTransaction, index: int) -> None:
def _remove_from_index(self, tx: BaseTransaction, index: int) -> None:
""" Remove tx from mint/melt indexes and total amount
"""
assert tx.hash is not None

tx_output = tx.outputs[index]
token_uid = tx.get_token_uid(tx_output.get_token_index())
Expand All @@ -125,7 +123,6 @@ def add_tx(self, tx: BaseTransaction) -> None:
if tx.version == TxVersion.TOKEN_CREATION_TRANSACTION:
from hathor.transaction.token_creation_tx import TokenCreationTransaction
tx = cast(TokenCreationTransaction, tx)
assert tx.hash is not None
status = self._tokens[tx.hash]
status._name = tx.token_name
status._symbol = tx.token_symbol
Expand All @@ -137,7 +134,6 @@ def add_tx(self, tx: BaseTransaction) -> None:
transactions = self._tokens[token_uid]._transactions
# It is safe to use the in operator because it is O(log(n)).
# http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html#sortedcontainers.SortedList.__contains__
assert tx.hash is not None
element = TransactionIndexElement(tx.timestamp, tx.hash)
if element in transactions:
return
Expand All @@ -162,7 +158,6 @@ def del_tx(self, tx: BaseTransaction) -> None:

# if it's a TokenCreationTransaction, remove it from index
if tx.version == TxVersion.TOKEN_CREATION_TRANSACTION:
assert tx.hash is not None
del self._tokens[tx.hash]

def iter_all_tokens(self) -> Iterator[tuple[bytes, TokenIndexInfo]]:
Expand Down
5 changes: 1 addition & 4 deletions hathor/indexes/memory_tx_group_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from hathor.indexes.tx_group_index import TxGroupIndex
from hathor.transaction import BaseTransaction
from hathor.util import not_none

logger = get_logger()

Expand All @@ -40,21 +39,19 @@ def force_clear(self) -> None:
self.index = defaultdict(set)

def _add_tx(self, key: KT, tx: BaseTransaction) -> None:
self.index[key].add((tx.timestamp, not_none(tx.hash)))
self.index[key].add((tx.timestamp, tx.hash))

@abstractmethod
def _extract_keys(self, tx: BaseTransaction) -> Iterable[KT]:
"""Extract the keys related to a given tx. The transaction will be added to all extracted keys."""
raise NotImplementedError

def add_tx(self, tx: BaseTransaction) -> None:
assert tx.hash is not None

for key in self._extract_keys(tx):
self._add_tx(key, tx)

def remove_tx(self, tx: BaseTransaction) -> None:
assert tx.hash is not None

for key in self._extract_keys(tx):
self.index[key].discard((tx.timestamp, tx.hash))
Expand Down
Loading
Loading