Skip to content

refactor(mypy): add stricter rules to more test modules [part III/VI] #972

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

Merged
merged 1 commit into from
Mar 20, 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: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ module = [
"tests.feature_activation.*",
# "tests.p2p.*",
"tests.pubsub.*",
# "tests.simulation.*",
"tests.simulation.*",
]
disallow_untyped_defs = true

Expand Down
2 changes: 2 additions & 0 deletions tests/consensus/test_consensus2.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def test_two_conflicts_intertwined_once(self) -> None:

self.graphviz = GraphvizVisualizer(manager1.tx_storage, include_verifications=True, include_funds=True)

assert manager1.wallet is not None
address = manager1.wallet.get_unused_address(mark_as_used=False)
value = 10
initial = gen_new_tx(manager1, address, value)
Expand Down Expand Up @@ -110,6 +111,7 @@ def test_two_conflicts_intertwined_multiple_times(self) -> None:

self.graphviz = GraphvizVisualizer(manager1.tx_storage, include_verifications=True, include_funds=True)

assert manager1.wallet is not None
address = manager1.wallet.get_unused_address(mark_as_used=False)
value = 10
initial = gen_new_tx(manager1, address, value)
Expand Down
2 changes: 1 addition & 1 deletion tests/consensus/test_soft_voided.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def _run_test(
simulator.run(300)

manager2 = self.create_peer(soft_voided_tx_ids=soft_voided_tx_ids, simulator=simulator)
manager2.soft_voided_tx_ids = soft_voided_tx_ids

graphviz = GraphvizVisualizer(manager2.tx_storage, include_verifications=True, include_funds=True)

Expand Down Expand Up @@ -83,6 +82,7 @@ def _run_test(
metaD1 = txD1.get_metadata()
self.assertEqual({txA.hash, txD1.hash}, metaD1.voided_by)

assert manager2.wallet is not None
address = manager2.wallet.get_unused_address(mark_as_used=False)
value = 1
txC = gen_new_tx(manager2, address, value)
Expand Down
1 change: 1 addition & 0 deletions tests/consensus/test_soft_voided2.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def _run_test(self, simulator: Simulator, soft_voided_tx_ids: set[VertexId]) ->

self.graphviz = GraphvizVisualizer(manager1.tx_storage, include_verifications=True, include_funds=True)

assert manager1.wallet is not None
address = manager1.wallet.get_unused_address(mark_as_used=False)
value = 10
initial = gen_new_tx(manager1, address, value)
Expand Down
6 changes: 3 additions & 3 deletions tests/consensus/test_soft_voided3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from hathor.simulator import FakeConnection, RandomTransactionGenerator, Simulator
from hathor.simulator.trigger import StopAfterNTransactions
from hathor.simulator.utils import gen_new_tx
from hathor.transaction import Transaction
from hathor.transaction import BaseTransaction
from hathor.types import VertexId
from tests import unittest
from tests.simulation.base import SimulatorTestCase
Expand All @@ -14,7 +14,7 @@
class BaseSoftVoidedTestCase(SimulatorTestCase):
seed_config = 5988775361793628169

def assertNoParentsAreSoftVoided(self, tx: Transaction) -> None:
def assertNoParentsAreSoftVoided(self, tx: BaseTransaction) -> None:
assert tx.storage is not None
for h in tx.parents:
tx2 = tx.storage.get_transaction(h)
Expand All @@ -39,7 +39,6 @@ def _run_test(
simulator.run(300)

manager2 = self.create_peer(soft_voided_tx_ids=soft_voided_tx_ids, simulator=simulator)
manager2.soft_voided_tx_ids = soft_voided_tx_ids

graphviz = GraphvizVisualizer(manager2.tx_storage, include_verifications=True, include_funds=True)

Expand Down Expand Up @@ -92,6 +91,7 @@ def _run_test(
graphviz.labels[blk1.hash] = 'blk1'

simulator.run(10)
assert manager2.wallet is not None
address = manager2.wallet.get_unused_address(mark_as_used=True)
txC = gen_new_tx(manager2, address, 6400)
if txD1.hash not in txC.parents:
Expand Down
4 changes: 3 additions & 1 deletion tests/consensus/test_soft_voided4.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from hathor.simulator import FakeConnection, RandomTransactionGenerator, Simulator
from hathor.simulator.trigger import StopAfterNTransactions
from hathor.simulator.utils import gen_new_double_spending
from hathor.transaction import Transaction
from hathor.types import VertexId
from tests import unittest
from tests.simulation.base import SimulatorTestCase
Expand Down Expand Up @@ -31,7 +32,6 @@ def _run_test(
gen_tx1.stop()

manager2 = self.create_peer(soft_voided_tx_ids=set(soft_voided_tx_ids), simulator=simulator)
manager2.soft_voided_tx_ids = set(soft_voided_tx_ids)

self.graphviz = GraphvizVisualizer(manager2.tx_storage, include_verifications=True, include_funds=True)

Expand Down Expand Up @@ -69,9 +69,11 @@ def _run_test(
self.graphviz.labels[txB_hash] = 'txB'

txB = manager2.tx_storage.get_transaction(txB_hash)
assert isinstance(txB, Transaction)

# Get the tx confirmed by the soft voided that will be voided
tx_base = manager2.tx_storage.get_transaction(txB.parents[0])
assert isinstance(tx_base, Transaction)
txC = gen_new_double_spending(manager2, use_same_parents=False, tx=tx_base)
self.graphviz.labels[tx_base.hash] = 'tx_base'
txC.weight = 30
Expand Down
1 change: 1 addition & 0 deletions tests/event/event_simulation_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def _create_artifacts(self) -> None:
self.settings = artifacts.settings

event_ws_factory = self.manager._event_manager._event_ws_factory
assert event_ws_factory is not None
event_ws_factory.openHandshakeTimeout = 0

self.protocol = event_ws_factory.buildProtocol(addr=Mock())
Expand Down
5 changes: 5 additions & 0 deletions tests/event/test_event_simulation_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class BaseEventSimulationScenariosTest(BaseEventSimulationTester):

def test_only_load(self) -> None:
stream_id = self.manager._event_manager._stream_id
assert stream_id is not None
Scenario.ONLY_LOAD.simulate(self.simulator, self.manager)
self._start_stream()

Expand All @@ -66,6 +67,7 @@ def test_only_load(self) -> None:

def test_single_chain_one_block(self) -> None:
stream_id = self.manager._event_manager._stream_id
assert stream_id is not None
Scenario.SINGLE_CHAIN_ONE_BLOCK.simulate(self.simulator, self.manager)
self._start_stream()

Expand Down Expand Up @@ -94,6 +96,7 @@ def test_single_chain_one_block(self) -> None:

def test_single_chain_blocks_and_transactions(self) -> None:
stream_id = self.manager._event_manager._stream_id
assert stream_id is not None
Scenario.SINGLE_CHAIN_BLOCKS_AND_TRANSACTIONS.simulate(self.simulator, self.manager)
self._start_stream()

Expand Down Expand Up @@ -159,6 +162,7 @@ def test_single_chain_blocks_and_transactions(self) -> None:

def test_reorg(self) -> None:
stream_id = self.manager._event_manager._stream_id
assert stream_id is not None
Scenario.REORG.simulate(self.simulator, self.manager)
self._start_stream()

Expand Down Expand Up @@ -207,6 +211,7 @@ def test_reorg(self) -> None:

def test_unvoided_transaction(self) -> None:
stream_id = self.manager._event_manager._stream_id
assert stream_id is not None
Scenario.UNVOIDED_TRANSACTION.simulate(self.simulator, self.manager)
self._start_stream()

Expand Down
3 changes: 2 additions & 1 deletion tests/feature_activation/test_feature_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from hathor.feature_activation.settings import Settings as FeatureSettings
from hathor.simulator import FakeConnection
from hathor.transaction.exceptions import BlockMustSignalError
from hathor.util import not_none
from tests import unittest
from tests.resources.base_resource import StubSite
from tests.simulation.base import SimulatorTestCase
Expand Down Expand Up @@ -615,7 +616,7 @@ def test_feature_from_existing_storage(self) -> None:
calculate_new_state_mock.reset_mock()

manager1.stop()
artifacts1.rocksdb_storage.close()
not_none(artifacts1.rocksdb_storage).close()

# new builder is created with the same storage from the previous manager
builder2 = self.get_simulator_builder_from_dir(rocksdb_dir).set_settings(settings)
Expand Down
2 changes: 2 additions & 0 deletions tests/p2p/test_sync_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
StopWhenTrue,
Trigger,
)
from hathor.transaction.storage import TransactionRocksDBStorage
from hathor.transaction.storage.traversal import DFSWalk
from tests.simulation.base import SimulatorTestCase
from tests.utils import HAS_ROCKSDB
Expand Down Expand Up @@ -89,6 +90,7 @@ def _run_restart_test(self, *, full_verification: bool, use_tx_storage_cache: bo
conn12.disconnect(Failure(Exception('testing')))
self.simulator.remove_connection(conn12)
manager2.stop()
assert isinstance(manager2.tx_storage, TransactionRocksDBStorage)
manager2.tx_storage._rocksdb_storage.close()
del manager2

Expand Down
14 changes: 11 additions & 3 deletions tests/simulation/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Optional

from hathor.manager import HathorManager
from hathor.simulator import Simulator
from hathor.types import VertexId
from tests import unittest


Expand All @@ -9,7 +11,7 @@ class SimulatorTestCase(unittest.TestCase):

seed_config: Optional[int] = None

def setUp(self):
def setUp(self) -> None:
super().setUp()

self.simulator = Simulator(self.seed_config)
Expand All @@ -19,11 +21,17 @@ def setUp(self):
print('Simulation seed config:', self.simulator.seed)
print('-'*30)

def tearDown(self):
def tearDown(self) -> None:
self.simulator.stop()
super().tearDown()

def create_peer(self, enable_sync_v1=None, enable_sync_v2=None, soft_voided_tx_ids=None, simulator=None):
def create_peer( # type: ignore[override]
self,
enable_sync_v1: bool | None = None,
enable_sync_v2: bool | None = None,
soft_voided_tx_ids: set[VertexId] = set(),
simulator: Simulator | None = None
) -> HathorManager:
if enable_sync_v1 is None:
assert hasattr(self, '_enable_sync_v1'), ('`_enable_sync_v1` has no default by design, either set one on '
'the test class or pass `enable_sync_v1` by argument')
Expand Down
23 changes: 12 additions & 11 deletions tests/simulation/test_simulator.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import pytest

from hathor.manager import HathorManager
from hathor.simulator import FakeConnection
from hathor.simulator.trigger import All as AllTriggers, StopWhenSynced
from hathor.simulator.trigger import All as AllTriggers, StopWhenSynced, Trigger
from hathor.verification.vertex_verifier import VertexVerifier
from tests import unittest
from tests.simulation.base import SimulatorTestCase


class BaseRandomSimulatorTestCase(SimulatorTestCase):
def test_verify_pow(self):
def test_verify_pow(self) -> None:
manager1 = self.create_peer()
# just get one of the genesis, we don't really need to create any transaction
tx = next(iter(manager1.tx_storage.get_all_genesis()))
# optional argument must be valid, it just has to not raise any exception, there's no assert for that
VertexVerifier(settings=self._settings, daa=manager1.daa).verify_pow(tx, override_weight=0.)

def test_one_node(self):
def test_one_node(self) -> None:
manager1 = self.create_peer()

miner1 = self.simulator.create_miner(manager1, hashpower=100e6)
Expand All @@ -29,7 +30,7 @@ def test_one_node(self):
# FIXME: the setup above produces 0 new blocks and transactions
# self.assertGreater(manager1.tx_storage.get_vertices_count(), 3)

def test_two_nodes(self):
def test_two_nodes(self) -> None:
manager1 = self.create_peer()
manager2 = self.create_peer()

Expand Down Expand Up @@ -63,10 +64,10 @@ def test_two_nodes(self):
self.assertTrue(conn12.is_connected)
self.assertTipsEqual(manager1, manager2)

def test_many_miners_since_beginning(self):
nodes = []
def test_many_miners_since_beginning(self) -> None:
nodes: list[HathorManager] = []
miners = []
stop_triggers = []
stop_triggers: list[Trigger] = []

for hashpower in [10e6, 5e6, 1e6, 1e6, 1e6]:
manager = self.create_peer()
Expand Down Expand Up @@ -96,11 +97,11 @@ def test_many_miners_since_beginning(self):
self.assertTipsEqual(nodes[0], node)

@pytest.mark.flaky(max_runs=5, min_passes=1)
def test_new_syncing_peer(self):
def test_new_syncing_peer(self) -> None:
nodes = []
miners = []
tx_generators = []
stop_triggers = []
stop_triggers: list[Trigger] = []

manager = self.create_peer()
nodes.append(manager)
Expand Down Expand Up @@ -162,15 +163,15 @@ class SyncV2RandomSimulatorTestCase(unittest.SyncV2Params, BaseRandomSimulatorTe
class SyncBridgeRandomSimulatorTestCase(unittest.SyncBridgeParams, SyncV2RandomSimulatorTestCase):
__test__ = True

def test_compare_mempool_implementations(self):
def test_compare_mempool_implementations(self) -> None:
manager1 = self.create_peer()
manager2 = self.create_peer()

# XXX: make sure we have both indexes
tx_storage = manager1.tx_storage
assert tx_storage.indexes is not None
assert tx_storage.indexes.mempool_tips is not None
assert manager1.tx_storage.indexes.tx_tips is not None
assert manager1.tx_storage.indexes and manager1.tx_storage.indexes.tx_tips is not None
mempool_tips = tx_storage.indexes.mempool_tips

miner1 = self.simulator.create_miner(manager1, hashpower=10e6)
Expand Down
20 changes: 14 additions & 6 deletions tests/simulation/test_simulator_itself.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest

from hathor.manager import HathorManager
from hathor.p2p.peer_id import PeerId
from hathor.simulator import FakeConnection, Simulator
from tests import unittest

Expand All @@ -11,7 +13,7 @@ class BaseSimulatorSelfTestCase(unittest.TestCase):

__test__ = False

def setUp(self):
def setUp(self) -> None:
super().setUp()

seed = None
Expand All @@ -29,14 +31,20 @@ def setUp(self):
print('Simulation seed config:', self.simulator1.seed)
print('-' * 30)

def tearDown(self):
def tearDown(self) -> None:
super().tearDown()

self.simulator1.stop()
self.simulator2.stop()
self.simulator3.stop()

def create_simulator_peer(self, simulator, peer_id_pool, enable_sync_v1=None, enable_sync_v2=None):
def create_simulator_peer(
self,
simulator: Simulator,
peer_id_pool: list[PeerId],
enable_sync_v1: bool | None = None,
enable_sync_v2: bool | None = None
) -> HathorManager:
if enable_sync_v1 is None:
assert hasattr(self, '_enable_sync_v1'), ('`_enable_sync_v1` has no default by design, either set one on '
'the test class or pass `enable_sync_v1` by argument')
Expand All @@ -54,7 +62,7 @@ def create_simulator_peer(self, simulator, peer_id_pool, enable_sync_v1=None, en

return simulator.create_peer(builder)

def _simulate_run(self, run_i, simulator):
def _simulate_run(self, run_i: int, simulator: Simulator) -> list[HathorManager]:
# XXX: the following was adapted from test_new_syncing_peer, it doesn't matter too much, but has good coverage
# of different behaviors that can be affected by non-determinism on the fullnode implementation

Expand Down Expand Up @@ -110,7 +118,7 @@ def _simulate_run(self, run_i, simulator):

# XXX: marked as flaky because of a known random issue
@pytest.mark.flaky(max_runs=3, min_passes=1)
def test_determinism_full_runs(self):
def test_determinism_full_runs(self) -> None:
# sanity assert as to not mess up with it on the setup
self.assertEqual(self.simulator1.seed, self.simulator2.seed)
self.assertEqual(self.simulator1.seed, self.simulator3.seed)
Expand All @@ -128,7 +136,7 @@ def test_determinism_full_runs(self):

# XXX: marked as flaky because of a known random issue
@pytest.mark.flaky(max_runs=3, min_passes=1)
def test_determinism_interleaved(self):
def test_determinism_interleaved(self) -> None:
# sanity assert as to not mess up with it on the setup
self.assertEqual(self.simulator1.seed, self.simulator2.seed)

Expand Down
Loading