Skip to content

Commit 5318232

Browse files
committed
refactor(mypy): add stricter rules to p2p tests
1 parent 21bdd94 commit 5318232

26 files changed

+349
-313
lines changed

hathor/builder/builder.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
TransactionRocksDBStorage,
4747
TransactionStorage,
4848
)
49+
from hathor.transaction.storage.transaction_storage import BaseTransactionStorage
4950
from hathor.util import Random, get_environment_info, not_none
5051
from hathor.verification.verification_service import VerificationService
5152
from hathor.verification.vertex_verifiers import VertexVerifiers
@@ -131,7 +132,7 @@ def __init__(self) -> None:
131132
self._tx_storage_cache_capacity: Optional[int] = None
132133

133134
self._indexes_manager: Optional[IndexesManager] = None
134-
self._tx_storage: Optional[TransactionStorage] = None
135+
self._tx_storage: Optional[BaseTransactionStorage] = None
135136
self._event_storage: Optional[EventStorage] = None
136137

137138
self._reactor: Optional[Reactor] = None
@@ -393,7 +394,7 @@ def _get_or_create_indexes_manager(self) -> IndexesManager:
393394

394395
return self._indexes_manager
395396

396-
def _get_or_create_tx_storage(self) -> TransactionStorage:
397+
def _get_or_create_tx_storage(self) -> BaseTransactionStorage:
397398
indexes = self._get_or_create_indexes_manager()
398399

399400
if self._tx_storage is not None:
@@ -616,7 +617,7 @@ def enable_event_queue(self) -> 'Builder':
616617
self._enable_event_queue = True
617618
return self
618619

619-
def set_tx_storage(self, tx_storage: TransactionStorage) -> 'Builder':
620+
def set_tx_storage(self, tx_storage: BaseTransactionStorage) -> 'Builder':
620621
self.check_if_can_modify()
621622
self._tx_storage = tx_storage
622623
return self

hathor/manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
from hathor.stratum import StratumFactory
5858
from hathor.transaction import BaseTransaction, Block, MergeMinedBlock, Transaction, TxVersion, sum_weights
5959
from hathor.transaction.exceptions import TxValidationError
60-
from hathor.transaction.storage import TransactionStorage
6160
from hathor.transaction.storage.exceptions import TransactionDoesNotExist
61+
from hathor.transaction.storage.transaction_storage import BaseTransactionStorage
6262
from hathor.transaction.storage.tx_allow_scope import TxAllowScope
6363
from hathor.types import Address, VertexId
6464
from hathor.util import EnvironmentInfo, LogDuration, Random, calculate_min_significant_weight, not_none
@@ -97,7 +97,7 @@ def __init__(self,
9797
consensus_algorithm: ConsensusAlgorithm,
9898
daa: DifficultyAdjustmentAlgorithm,
9999
peer_id: PeerId,
100-
tx_storage: TransactionStorage,
100+
tx_storage: BaseTransactionStorage,
101101
p2p_manager: ConnectionsManager,
102102
event_manager: EventManager,
103103
feature_service: FeatureService,

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module = [
162162
"tests.event.*",
163163
"tests.execution_manager.*",
164164
"tests.feature_activation.*",
165-
# "tests.p2p.*",
165+
"tests.p2p.*",
166166
"tests.pubsub.*",
167167
"tests.simulation.*",
168168
]

tests/p2p/netfilter/test_factory.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest.mock import Mock
2+
13
from twisted.internet.address import IPv4Address
24

35
from hathor.p2p.netfilter import get_table
@@ -10,7 +12,7 @@
1012

1113

1214
class NetfilterFactoryTest(unittest.TestCase):
13-
def test_factory(self):
15+
def test_factory(self) -> None:
1416
pre_conn = get_table('filter').get_chain('pre_conn')
1517

1618
match = NetfilterMatchIPAddress('192.168.0.1/32')
@@ -20,7 +22,7 @@ def test_factory(self):
2022
builder = TestBuilder()
2123
artifacts = builder.build()
2224
wrapped_factory = artifacts.p2p_manager.server_factory
23-
factory = NetfilterFactory(connections=None, wrappedFactory=wrapped_factory)
25+
factory = NetfilterFactory(connections=Mock(), wrappedFactory=wrapped_factory)
2426

2527
ret = factory.buildProtocol(IPv4Address('TCP', '192.168.0.1', 1234))
2628
self.assertIsNone(ret)

tests/p2p/netfilter/test_match.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def match(self, context: 'NetfilterContext') -> bool:
2222

2323

2424
class NetfilterMatchTest(unittest.TestCase):
25-
def test_match_all(self):
25+
def test_match_all(self) -> None:
2626
matcher = NetfilterMatchAll()
2727
context = NetfilterContext()
2828
self.assertTrue(matcher.match(context))
@@ -31,7 +31,7 @@ def test_match_all(self):
3131
json = matcher.to_json()
3232
self.assertEqual(json['type'], 'NetfilterMatchAll')
3333

34-
def test_never_match(self):
34+
def test_never_match(self) -> None:
3535
matcher = NetfilterNeverMatch()
3636
context = NetfilterContext()
3737
self.assertFalse(matcher.match(context))
@@ -40,14 +40,14 @@ def test_never_match(self):
4040
json = matcher.to_json()
4141
self.assertEqual(json['type'], 'NetfilterNeverMatch')
4242

43-
def test_match_and_success(self):
43+
def test_match_and_success(self) -> None:
4444
m1 = NetfilterMatchAll()
4545
m2 = NetfilterMatchAll()
4646
matcher = NetfilterMatchAnd(m1, m2)
4747
context = NetfilterContext()
4848
self.assertTrue(matcher.match(context))
4949

50-
def test_match_and_fail_01(self):
50+
def test_match_and_fail_01(self) -> None:
5151
m1 = NetfilterNeverMatch()
5252
m2 = NetfilterMatchAll()
5353
matcher = NetfilterMatchAnd(m1, m2)
@@ -60,28 +60,28 @@ def test_match_and_fail_01(self):
6060
self.assertEqual(json['match_params']['a']['type'], 'NetfilterNeverMatch')
6161
self.assertEqual(json['match_params']['b']['type'], 'NetfilterMatchAll')
6262

63-
def test_match_and_fail_10(self):
63+
def test_match_and_fail_10(self) -> None:
6464
m1 = NetfilterMatchAll()
6565
m2 = NetfilterNeverMatch()
6666
matcher = NetfilterMatchAnd(m1, m2)
6767
context = NetfilterContext()
6868
self.assertFalse(matcher.match(context))
6969

70-
def test_match_and_fail_00(self):
70+
def test_match_and_fail_00(self) -> None:
7171
m1 = NetfilterNeverMatch()
7272
m2 = NetfilterNeverMatch()
7373
matcher = NetfilterMatchAnd(m1, m2)
7474
context = NetfilterContext()
7575
self.assertFalse(matcher.match(context))
7676

77-
def test_match_or_success_11(self):
77+
def test_match_or_success_11(self) -> None:
7878
m1 = NetfilterMatchAll()
7979
m2 = NetfilterMatchAll()
8080
matcher = NetfilterMatchOr(m1, m2)
8181
context = NetfilterContext()
8282
self.assertTrue(matcher.match(context))
8383

84-
def test_match_or_success_10(self):
84+
def test_match_or_success_10(self) -> None:
8585
m1 = NetfilterMatchAll()
8686
m2 = NetfilterNeverMatch()
8787
matcher = NetfilterMatchOr(m1, m2)
@@ -94,21 +94,21 @@ def test_match_or_success_10(self):
9494
self.assertEqual(json['match_params']['a']['type'], 'NetfilterMatchAll')
9595
self.assertEqual(json['match_params']['b']['type'], 'NetfilterNeverMatch')
9696

97-
def test_match_or_success_01(self):
97+
def test_match_or_success_01(self) -> None:
9898
m1 = NetfilterNeverMatch()
9999
m2 = NetfilterMatchAll()
100100
matcher = NetfilterMatchOr(m1, m2)
101101
context = NetfilterContext()
102102
self.assertTrue(matcher.match(context))
103103

104-
def test_match_or_fail_00(self):
104+
def test_match_or_fail_00(self) -> None:
105105
m1 = NetfilterNeverMatch()
106106
m2 = NetfilterNeverMatch()
107107
matcher = NetfilterMatchOr(m1, m2)
108108
context = NetfilterContext()
109109
self.assertFalse(matcher.match(context))
110110

111-
def test_match_ip_address_empty_context(self):
111+
def test_match_ip_address_empty_context(self) -> None:
112112
matcher = NetfilterMatchIPAddress('192.168.0.0/24')
113113
context = NetfilterContext()
114114
self.assertFalse(matcher.match(context))
@@ -118,7 +118,7 @@ def test_match_ip_address_empty_context(self):
118118
self.assertEqual(json['type'], 'NetfilterMatchIPAddress')
119119
self.assertEqual(json['match_params']['host'], '192.168.0.0/24')
120120

121-
def test_match_ip_address_ipv4_net(self):
121+
def test_match_ip_address_ipv4_net(self) -> None:
122122
matcher = NetfilterMatchIPAddress('192.168.0.0/24')
123123
context = NetfilterContext(addr=IPv4Address('TCP', '192.168.0.10', 1234))
124124
self.assertTrue(matcher.match(context))
@@ -129,7 +129,7 @@ def test_match_ip_address_ipv4_net(self):
129129
context = NetfilterContext(addr=IPv4Address('TCP', '', 1234))
130130
self.assertFalse(matcher.match(context))
131131

132-
def test_match_ip_address_ipv4_ip(self):
132+
def test_match_ip_address_ipv4_ip(self) -> None:
133133
matcher = NetfilterMatchIPAddress('192.168.0.1/32')
134134
context = NetfilterContext(addr=IPv4Address('TCP', '192.168.0.1', 1234))
135135
self.assertTrue(matcher.match(context))
@@ -138,24 +138,24 @@ def test_match_ip_address_ipv4_ip(self):
138138
context = NetfilterContext(addr=IPv4Address('TCP', '', 1234))
139139
self.assertFalse(matcher.match(context))
140140

141-
def test_match_ip_address_ipv4_hostname(self):
141+
def test_match_ip_address_ipv4_hostname(self) -> None:
142142
matcher = NetfilterMatchIPAddress('192.168.0.1/32')
143-
context = NetfilterContext(addr=HostnameAddress('hathor.network', 80))
143+
context = NetfilterContext(addr=HostnameAddress(b'hathor.network', 80))
144144
self.assertFalse(matcher.match(context))
145145

146-
def test_match_ip_address_ipv4_unix(self):
146+
def test_match_ip_address_ipv4_unix(self) -> None:
147147
matcher = NetfilterMatchIPAddress('192.168.0.1/32')
148148
context = NetfilterContext(addr=UNIXAddress('/unix.sock'))
149149
self.assertFalse(matcher.match(context))
150150

151-
def test_match_ip_address_ipv4_ipv6(self):
151+
def test_match_ip_address_ipv4_ipv6(self) -> None:
152152
matcher = NetfilterMatchIPAddress('192.168.0.1/32')
153153
context = NetfilterContext(addr=IPv6Address('TCP', '2001:db8::', 80))
154154
self.assertFalse(matcher.match(context))
155155
context = NetfilterContext(addr=IPv6Address('TCP', '', 80))
156156
self.assertFalse(matcher.match(context))
157157

158-
def test_match_ip_address_ipv6_net(self):
158+
def test_match_ip_address_ipv6_net(self) -> None:
159159
matcher = NetfilterMatchIPAddress('2001:0db8:0:f101::/64')
160160
context = NetfilterContext(addr=IPv6Address('TCP', '2001:db8::8a2e:370:7334', 1234))
161161
self.assertFalse(matcher.match(context))
@@ -167,7 +167,7 @@ def test_match_ip_address_ipv6_net(self):
167167
self.assertEqual(json['type'], 'NetfilterMatchIPAddress')
168168
self.assertEqual(json['match_params']['host'], str(ip_network('2001:0db8:0:f101::/64')))
169169

170-
def test_match_ip_address_ipv6_ip(self):
170+
def test_match_ip_address_ipv6_ip(self) -> None:
171171
matcher = NetfilterMatchIPAddress('2001:0db8:0:f101::1/128')
172172
context = NetfilterContext(addr=IPv6Address('TCP', '2001:db8:0:f101::1', 1234))
173173
self.assertTrue(matcher.match(context))
@@ -176,22 +176,22 @@ def test_match_ip_address_ipv6_ip(self):
176176
context = NetfilterContext(addr=IPv6Address('TCP', '2001:db8:0:f101:2::7334', 1234))
177177
self.assertFalse(matcher.match(context))
178178

179-
def test_match_ip_address_ipv6_hostname(self):
179+
def test_match_ip_address_ipv6_hostname(self) -> None:
180180
matcher = NetfilterMatchIPAddress('2001:0db8:0:f101::1/128')
181-
context = NetfilterContext(addr=HostnameAddress('hathor.network', 80))
181+
context = NetfilterContext(addr=HostnameAddress(b'hathor.network', 80))
182182
self.assertFalse(matcher.match(context))
183183

184-
def test_match_ip_address_ipv6_unix(self):
184+
def test_match_ip_address_ipv6_unix(self) -> None:
185185
matcher = NetfilterMatchIPAddress('2001:0db8:0:f101::1/128')
186186
context = NetfilterContext(addr=UNIXAddress('/unix.sock'))
187187
self.assertFalse(matcher.match(context))
188188

189-
def test_match_ip_address_ipv6_ipv4(self):
189+
def test_match_ip_address_ipv6_ipv4(self) -> None:
190190
matcher = NetfilterMatchIPAddress('2001:0db8:0:f101::1/128')
191191
context = NetfilterContext(addr=IPv4Address('TCP', '192.168.0.1', 1234))
192192
self.assertFalse(matcher.match(context))
193193

194-
def test_match_peer_id_empty_context(self):
194+
def test_match_peer_id_empty_context(self) -> None:
195195
matcher = NetfilterMatchPeerId('123')
196196
context = NetfilterContext()
197197
self.assertFalse(matcher.match(context))
@@ -200,7 +200,7 @@ def test_match_peer_id_empty_context(self):
200200
class BaseNetfilterMatchTest(unittest.TestCase):
201201
__test__ = False
202202

203-
def test_match_peer_id(self):
203+
def test_match_peer_id(self) -> None:
204204
network = 'testnet'
205205
peer_id1 = PeerId()
206206
peer_id2 = PeerId()

tests/p2p/netfilter/test_match_remote.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class NetfilterMatchRemoteTest(unittest.TestCase):
9-
def test_match_ip(self):
9+
def test_match_ip(self) -> None:
1010
matcher = NetfilterMatchIPAddressRemoteURL('test', self.clock, 'http://localhost:8080')
1111
context = NetfilterContext(addr=IPv4Address('TCP', '192.168.0.1', 1234))
1212
self.assertFalse(matcher.match(context))

tests/p2p/netfilter/test_tables.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77

88
class NetfilterTableTest(unittest.TestCase):
9-
def test_default_table_filter(self):
9+
def test_default_table_filter(self) -> None:
1010
tb_filter = get_table('filter')
1111
tb_filter.get_chain('pre_conn')
1212
tb_filter.get_chain('post_hello')
1313
tb_filter.get_chain('post_peerid')
1414

15-
def test_default_table_not_exists(self):
15+
def test_default_table_not_exists(self) -> None:
1616
with self.assertRaises(KeyError):
1717
get_table('do-not-exists')
1818

19-
def test_add_get_chain(self):
19+
def test_add_get_chain(self) -> None:
2020
mytable = NetfilterTable('mytable')
2121
mychain = NetfilterChain('mychain', NetfilterAccept())
2222
mytable.add_chain(mychain)

tests/p2p/netfilter/test_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
class NetfilterUtilsTest(unittest.TestCase):
7-
def test_peer_id_blacklist(self):
7+
def test_peer_id_blacklist(self) -> None:
88
post_peerid = get_table('filter').get_chain('post_peerid')
99

1010
# Chain starts empty

tests/p2p/test_capabilities.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from hathor.p2p.states import ReadyState
12
from hathor.p2p.sync_v1.agent import NodeSyncTimestamp
23
from hathor.p2p.sync_v2.agent import NodeBlockSync
34
from hathor.simulator import FakeConnection
45
from tests import unittest
56

67

78
class SyncV1HathorCapabilitiesTestCase(unittest.SyncV1Params, unittest.TestCase):
8-
def test_capabilities(self):
9+
def test_capabilities(self) -> None:
910
network = 'testnet'
1011
manager1 = self.create_peer(network, capabilities=[self._settings.CAPABILITY_WHITELIST])
1112
manager2 = self.create_peer(network, capabilities=[])
@@ -18,6 +19,8 @@ def test_capabilities(self):
1819
self.clock.advance(0.1)
1920

2021
# Even if we don't have the capability we must connect because the whitelist url conf is None
22+
assert isinstance(conn._proto1.state, ReadyState)
23+
assert isinstance(conn._proto2.state, ReadyState)
2124
self.assertEqual(conn._proto1.state.state_name, 'READY')
2225
self.assertEqual(conn._proto2.state.state_name, 'READY')
2326
self.assertIsInstance(conn._proto1.state.sync_agent, NodeSyncTimestamp)
@@ -33,14 +36,16 @@ def test_capabilities(self):
3336
conn2.run_one_step(debug=True)
3437
self.clock.advance(0.1)
3538

39+
assert isinstance(conn2._proto1.state, ReadyState)
40+
assert isinstance(conn2._proto2.state, ReadyState)
3641
self.assertEqual(conn2._proto1.state.state_name, 'READY')
3742
self.assertEqual(conn2._proto2.state.state_name, 'READY')
3843
self.assertIsInstance(conn2._proto1.state.sync_agent, NodeSyncTimestamp)
3944
self.assertIsInstance(conn2._proto2.state.sync_agent, NodeSyncTimestamp)
4045

4146

4247
class SyncV2HathorCapabilitiesTestCase(unittest.SyncV2Params, unittest.TestCase):
43-
def test_capabilities(self):
48+
def test_capabilities(self) -> None:
4449
network = 'testnet'
4550
manager1 = self.create_peer(network, capabilities=[self._settings.CAPABILITY_WHITELIST,
4651
self._settings.CAPABILITY_SYNC_VERSION])
@@ -54,6 +59,8 @@ def test_capabilities(self):
5459
self.clock.advance(0.1)
5560

5661
# Even if we don't have the capability we must connect because the whitelist url conf is None
62+
assert isinstance(conn._proto1.state, ReadyState)
63+
assert isinstance(conn._proto2.state, ReadyState)
5764
self.assertEqual(conn._proto1.state.state_name, 'READY')
5865
self.assertEqual(conn._proto2.state.state_name, 'READY')
5966
self.assertIsInstance(conn._proto1.state.sync_agent, NodeBlockSync)
@@ -71,6 +78,8 @@ def test_capabilities(self):
7178
conn2.run_one_step(debug=True)
7279
self.clock.advance(0.1)
7380

81+
assert isinstance(conn2._proto1.state, ReadyState)
82+
assert isinstance(conn2._proto2.state, ReadyState)
7483
self.assertEqual(conn2._proto1.state.state_name, 'READY')
7584
self.assertEqual(conn2._proto2.state.state_name, 'READY')
7685
self.assertIsInstance(conn2._proto1.state.sync_agent, NodeBlockSync)

tests/p2p/test_connections.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class ConnectionsTest(unittest.TestCase):
1010
@pytest.mark.skipif(sys.platform == 'win32', reason='run_server is very finicky on Windows')
11-
def test_connections(self):
11+
def test_connections(self) -> None:
1212
process = run_server()
1313
process2 = run_server(listen=8006, status=8086, bootstrap='tcp://127.0.0.1:8005')
1414
process3 = run_server(listen=8007, status=8087, bootstrap='tcp://127.0.0.1:8005')
@@ -17,7 +17,7 @@ def test_connections(self):
1717
process2.terminate()
1818
process3.terminate()
1919

20-
def test_manager_connections(self):
20+
def test_manager_connections(self) -> None:
2121
manager = self.create_peer('testnet', enable_sync_v1=True, enable_sync_v2=False)
2222

2323
endpoint = 'tcp://127.0.0.1:8005'

0 commit comments

Comments
 (0)