/
test_graphs.py
407 lines (359 loc) · 12.7 KB
/
test_graphs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import random
import time
from copy import deepcopy
from typing import List
import pytest
from eth_utils import to_canonical_address, to_checksum_address
from tests.pathfinding.utils import SimpleReachabilityContainer
from pathfinding_service.constants import DIVERSITY_PEN_DEFAULT
from pathfinding_service.model import ChannelView, TokenNetwork
from pathfinding_service.model.channel import Channel
from raiden.network.transport.matrix import AddressReachability
from raiden.utils.typing import (
Address,
BlockTimeout,
ChannelID,
FeeAmount,
PaymentAmount,
ProportionalFeeAmount,
TokenAmount,
TokenNetworkAddress,
)
def test_edge_weight(addresses):
# pylint: disable=assigning-non-slot
channel_id = ChannelID(1)
participant1 = addresses[0]
participant2 = addresses[1]
capacity = TokenAmount(int(20 * 1e18))
capacity_partner = TokenAmount(int(10 * 1e18))
settle_timeout = BlockTimeout(15)
channel = Channel(
token_network_address=TokenNetworkAddress(bytes([1])),
channel_id=channel_id,
participant1=participant1,
participant2=participant2,
capacity1=capacity,
capacity2=capacity_partner,
settle_timeout=settle_timeout,
)
view, view_partner = channel.views
amount = PaymentAmount(int(1e18)) # one RDN
# no penalty
assert (
TokenNetwork.edge_weight(
visited=dict(), view=view, view_from_partner=view_partner, amount=amount, fee_penalty=0
)
== 1
)
# channel already used in a previous route
assert (
TokenNetwork.edge_weight(
visited={channel_id: 2},
view=view,
view_from_partner=view_partner,
amount=amount,
fee_penalty=0,
)
== 3
)
# absolute fee
view.fee_schedule_sender.flat = FeeAmount(int(0.03e18))
assert (
TokenNetwork.edge_weight(
visited=dict(),
view=view,
view_from_partner=view_partner,
amount=amount,
fee_penalty=100,
)
== 4
)
# relative fee
view.fee_schedule_sender.flat = FeeAmount(0)
view.fee_schedule_sender.proportional = ProportionalFeeAmount(int(0.01e6))
assert (
TokenNetwork.edge_weight(
visited=dict(),
view=view,
view_from_partner=view_partner,
amount=amount,
fee_penalty=100,
)
== 2
)
# partner has not enough capacity for refund (no_refund_weight) -> edge weight +1
view_partner.capacity = TokenAmount(0)
assert (
TokenNetwork.edge_weight(
visited=dict(),
view=view,
view_from_partner=view_partner,
amount=amount,
fee_penalty=100,
)
== 3
)
@pytest.mark.usefixtures("populate_token_network_case_1")
def test_routing_simple(
token_network_model: TokenNetwork,
reachability_state: SimpleReachabilityContainer,
addresses: List[Address],
):
hex_addrs = [to_checksum_address(addr) for addr in addresses]
view01: ChannelView = token_network_model.G[addresses[0]][addresses[1]]["view"]
view10: ChannelView = token_network_model.G[addresses[1]][addresses[0]]["view"]
assert view01.fee_schedule_sender.flat == 0
assert view01.capacity == 90
assert view10.capacity == 60
# 0->2->3 is the shortest path, but has no capacity, so 0->1->4->3 is used
paths = token_network_model.get_paths(
source=addresses[0],
target=addresses[3],
value=PaymentAmount(10),
max_paths=1,
reachability_state=reachability_state,
)
assert len(paths) == 1
assert paths[0].to_dict() == {
"path": [hex_addrs[0], hex_addrs[1], hex_addrs[4], hex_addrs[3]],
"estimated_fee": 0,
}
# Not connected.
no_paths = token_network_model.get_paths(
source=addresses[0],
target=addresses[5],
value=PaymentAmount(10),
max_paths=1,
reachability_state=reachability_state,
)
assert [] == no_paths
@pytest.mark.usefixtures("populate_token_network_case_1")
def test_capacity_check(
token_network_model: TokenNetwork,
reachability_state: SimpleReachabilityContainer,
addresses: List[Address],
):
""" Test that the mediation fees are included in the capacity check """
# First get a path without mediation fees. This must return the shortest path: 4->1->0
paths = token_network_model.get_paths(
source=addresses[4],
target=addresses[0],
value=PaymentAmount(35),
max_paths=1,
reachability_state=reachability_state,
)
index_paths = [addresses_to_indexes(p.nodes, addresses) for p in paths]
assert index_paths == [[4, 1, 0]]
# New let's add mediation fees to the channel 0->1.
model_with_fees = deepcopy(token_network_model)
model_with_fees.G[addresses[1]][addresses[0]]["view"].fee_schedule_sender.flat = 1
# The transfer from 4->1 must now include 1 Token for the mediation fee
# which will be payed for the 1->0 channel in addition to the payment
# value of 35. But 35 + 1 exceeds the capacity for channel 4->1, which is
# 35. So we should now get the next best route instead.
paths = model_with_fees.get_paths(
source=addresses[4],
target=addresses[0],
value=PaymentAmount(35),
max_paths=1,
reachability_state=reachability_state,
fee_penalty=0,
)
index_paths = [addresses_to_indexes(p.nodes, addresses) for p in paths]
assert index_paths == [[4, 1, 2, 0]]
@pytest.mark.usefixtures("populate_token_network_case_1")
def test_routing_result_order(
token_network_model: TokenNetwork,
reachability_state: SimpleReachabilityContainer,
addresses: List[Address],
):
hex_addrs = [to_checksum_address(addr) for addr in addresses]
paths = token_network_model.get_paths(
source=addresses[0],
target=addresses[2],
value=PaymentAmount(10),
max_paths=5,
reachability_state=reachability_state,
)
# 5 paths requested, but only 1 is available
assert len(paths) == 1
assert paths[0].to_dict() == {
"path": [hex_addrs[0], hex_addrs[1], hex_addrs[2]],
"estimated_fee": 0,
}
def addresses_to_indexes(path, addresses):
index_of_address = {a: i for i, a in enumerate(addresses)}
return [index_of_address[a] for a in path]
def get_paths( # pylint: disable=too-many-arguments
token_network_model: TokenNetwork,
reachability_state: SimpleReachabilityContainer,
addresses: List[Address],
source_index: int = 0,
target_index: int = 8,
value: PaymentAmount = PaymentAmount(10),
max_paths: int = 5,
diversity_penalty: float = DIVERSITY_PEN_DEFAULT,
) -> List:
paths = token_network_model.get_paths(
diversity_penalty=diversity_penalty,
source=addresses[source_index],
target=addresses[target_index],
value=value,
max_paths=max_paths,
reachability_state=reachability_state,
)
index_paths = [addresses_to_indexes(p.nodes, addresses) for p in paths]
return index_paths
@pytest.mark.usefixtures("populate_token_network_case_3")
def test_diversity_penalty(
token_network_model: TokenNetwork,
reachability_state: SimpleReachabilityContainer,
addresses: List[Address],
):
""" Check changes in routing when increasing diversity penalty """
assert get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
diversity_penalty=0.1,
) == [[0, 7, 8], [0, 7, 6, 8], [0, 7, 9, 10, 8], [0, 7, 6, 5, 8], [0, 1, 2, 3, 4, 8]]
assert get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
diversity_penalty=1.1,
) == [[0, 7, 8], [0, 7, 6, 8], [0, 1, 2, 3, 4, 8], [0, 7, 9, 10, 8], [0, 7, 6, 5, 8]]
assert get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
diversity_penalty=10,
) == [[0, 7, 8], [0, 1, 2, 3, 4, 8], [0, 7, 6, 8], [0, 7, 9, 10, 8], [0, 7, 6, 5, 8]]
@pytest.mark.usefixtures("populate_token_network_case_3")
def test_reachability_initiator(
token_network_model: TokenNetwork,
reachability_state: SimpleReachabilityContainer,
addresses: List[Address],
):
assert get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
) == [[0, 7, 8], [0, 1, 2, 3, 4, 8], [0, 7, 6, 8], [0, 7, 9, 10, 8], [0, 7, 6, 5, 8]]
reachability_state.reachabilities[addresses[0]] = AddressReachability.UNREACHABLE
assert (
get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
)
== []
)
reachability_state.reachabilities[addresses[0]] = AddressReachability.UNKNOWN
assert (
get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
)
== []
)
@pytest.mark.usefixtures("populate_token_network_case_3")
def test_reachability_mediator(
token_network_model: TokenNetwork,
reachability_state: SimpleReachabilityContainer,
addresses: List[Address],
):
assert get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
) == [[0, 7, 8], [0, 1, 2, 3, 4, 8], [0, 7, 6, 8], [0, 7, 9, 10, 8], [0, 7, 6, 5, 8]]
reachability_state.reachabilities[addresses[7]] = AddressReachability.UNREACHABLE
assert get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
) == [[0, 1, 2, 3, 4, 8]]
reachability_state.reachabilities[addresses[1]] = AddressReachability.UNKNOWN
assert (
get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
)
== []
)
@pytest.mark.usefixtures("populate_token_network_case_3")
def test_reachability_target(
token_network_model: TokenNetwork,
reachability_state: SimpleReachabilityContainer,
addresses: List[Address],
):
assert get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
) == [[0, 7, 8], [0, 1, 2, 3, 4, 8], [0, 7, 6, 8], [0, 7, 9, 10, 8], [0, 7, 6, 5, 8]]
reachability_state.reachabilities[addresses[8]] = AddressReachability.UNREACHABLE
assert (
get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
)
== []
)
reachability_state.reachabilities[addresses[8]] = AddressReachability.UNKNOWN
assert (
get_paths(
token_network_model=token_network_model,
reachability_state=reachability_state,
addresses=addresses,
)
== []
)
@pytest.mark.skip("Just run it locally for now")
@pytest.mark.usefixtures("populate_token_network_random")
def test_routing_benchmark(token_network_model: TokenNetwork): # pylint: disable=too-many-locals
value = PaymentAmount(100)
G = token_network_model.G
addresses_to_reachabilities = SimpleReachabilityContainer(
{
node: random.choice(
(
AddressReachability.REACHABLE,
AddressReachability.UNKNOWN,
AddressReachability.UNREACHABLE,
)
)
for node in G.nodes
}
)
times = []
start = time.time()
for _ in range(100):
tic = time.time()
source, target = random.sample(G.nodes, 2)
paths = token_network_model.get_paths(
source=source,
target=target,
value=value,
max_paths=5,
reachability_state=addresses_to_reachabilities,
)
toc = time.time()
times.append(toc - tic)
end = time.time()
for path_object in paths:
path = path_object.nodes
fees = path_object.estimated_fee
for node1, node2 in zip(path[:-1], path[1:]):
view: ChannelView = G[to_canonical_address(node1)][to_canonical_address(node2)]["view"]
print("capacity = ", view.capacity)
print("fee sum = ", fees)
print("Paths: ", paths)
print("Mean runtime: ", sum(times) / len(times))
print("Min runtime: ", min(times))
print("Max runtime: ", max(times))
print("Total runtime: ", end - start)