-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathln_sim.py
2210 lines (1739 loc) · 94.7 KB
/
ln_sim.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import networkx as nx
import numpy as np
from networkx.algorithms.shortest_paths.generic import all_shortest_paths
from networkx.algorithms.simple_paths import PathBuffer
from networkx.generators.random_graphs import _random_subset
import matplotlib.pyplot as plt
from math import inf, floor
from queue import PriorityQueue, Empty
from uuid import uuid4
from heapq import heappush, heappop
from itertools import count
import time
"""
Units of transfer are presumed to be satoshi (0.00000001 BTC) - this is the smallest unit
available on BTC - in reality, the LN supports millisatoshi for fee rounding purposes.
-- Hence, fees are allowed be in the order of 0.0001 sat.
"""
"""
/////////////
CONFIGURATION
/////////////
"""
DEBUG = False
NUM_NODES = 2000
NUM_TEST_NODES = 20
TOTAL_TO_SEND = 7000
AMP_RESEND = False # Retry flag for AMP payments
JIT_ROUTING = False
JIT_RESERVE = False
JIT_REPEAT_REBALANCE = True
JIT_FULL_KNOWLEDGE = False
FEE_BALANCING = False
FEE_BALANCING_CHECKPOINT = 5
FEE_BALANCING_UPDATE_POINT = 0.2
MAX_HOPS = 20
AMP_TIMEOUT = 60
MERCHANT_PROB = 0.67
LATENCY_OPTIONS = [0.1, 1, 10] # Sprites paper, in seconds
LATENCY_DISTRIBUTION = [0.925, 0.049, 0.026]
CTLV_OPTIONS = [9] # Default on LN for now
CTLV_DISTRIBUTION = [1]
CLTV_MULTIPLIER = 600 # BTC block time is ~10 minutes, so 600 seconds
DELAY_RESP_VAL = [0, 0.2, 0.4, 0.6, 0.8, 1] # Amount of timelock time node will wait before cancelling/claiming
DELAY_RESP_DISTRIBUTION = [1, 0, 0, 0, 0, 0]#0.9, 0.05, 0.01, 0.005, 0.005, 0.03]
MAX_PATH_ATTEMPTS = 30 # For AMP, if we try a subpayment on this many paths and fail, stop
ENCRYPTION_DELAYS = [0] # For now, setting these to 0 as latency should include this.
ENCRYPTION_DELAY_DISTRIBUTION = [1]
DECRYPTION_DELAYS = [0]
DECRYPTION_DELAY_DISTRIBUTION = [1]
# Consumers pay merchants - this is the chance for different role,
# i.e. merchant to pay or consumer to receive, [0, 0.5] - 0.5 means both as likely as each other
ROLE_BIAS = 0.0
HIGH_FUNDS_CHANCE = 0.2
# Terminal output colours
class bcolours:
HEADER = '\033[95m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
# Analytics
success_count = 0
fail_count = 0
amp_retry_count = 0
total_path_hops = 0
total_paths_found = 0
path_too_large_count = 0
# Packet handling
sim_time = 0.0
packet_queue = PriorityQueue()
"""
//////////////////
CORE FUNCTIONALITY
//////////////////
"""
class Packet:
"""Passes between nodes to send equity down a path, and release HTLCs back.
Index mapping is used to simulate onion routing.
Attributes:
path: (list<Node>) list of nodes on the path.
index_mapping: (dict) dictionary mapping Node ID's to indexes.
send_amnts: (list<float>) list of amnts to attempt to send per hop on onward direction.
type: (string) type of communication packet.
- pay: directly update channel balance between neighbouring nodes.
- pay_htlc: lock up funds to be sent in HTLC between neighbouring nodes.
- preimage: release HTLC, revealing preimage (R) to neighbouring node.
- cancel: cancel previously set up HTLC between neighbouring node.
- cancel_rest: same as cancel, but no HTLC between previous node was set up yet.
subpayment: (boolean / (UUID, int, int, float))
- (ID of containing AMP, index of subpayment, total num subpayments, timeout time),
- or False if not part of AMP.
jit_id: (UUID) if involved in JIT rebalancing, store the ID.
"""
def __init__(self, path, index_mapping, send_amnts, type="pay_htlc", subpayment=False, jit_id=False):
self._path = path
self._index_mapping = index_mapping
self._type = type
self._send_amnts = send_amnts
self._subpayment = subpayment
self._jit_id = jit_id
self._htlc_ids = []
self._timestamp = None
self._final_index = len(path) - 1
self._delayed = [False] * (len(path) - 1)
def get_path(self):
return self._path
def get_node(self, index):
return self._path[index]
def get_index(self, ID):
return self._index_mapping[ID]
def get_type(self):
return self._type
def set_type(self, type):
self._type = type
def get_total_fees(self):
return sum([(self._send_amnts[i] - self._send_amnts[-1]) for i in range(len(self._send_amnts))])
def get_amnt(self, index):
return self._send_amnts[index]
def get_subpayment(self):
return self._subpayment
def get_jit_id(self):
return self._jit_id
def get_timestamp(self):
return self._timestamp
def set_timestamp(self, timestamp):
self._timestamp = timestamp
def get_final_index(self):
return self._final_index
def add_htlc_id(self, id):
self._htlc_ids.append(id)
def get_htlc_id(self, index):
return self._htlc_ids[index]
def is_delayed(self, index):
return self._delayed[index]
def set_delayed(self, index):
self._delayed[index] = True
def __lt__(self, other):
return self.get_timestamp() < other.get_timestamp()
class Node:
"""Singular lightning node.
Attributes:
id: (int) ID of node.
merchant: (boolean) True if a merchant, False if consumer. (consumer more likely to spend)
spend_freq: (float) likelihood of selection to make next payment.
receive_freq: (float) likelihood of selection to receive next payment.
encryption_delay: (float) seconds it takes to encrypt a full packet.
decryption_delay: (float) seconds it takes to decrypt a layer of a packet.
"""
def __init__(self, id, merchant, spend_freq, receive_freq, encryption_delay, decryption_delay):
self._id = id
self._merchant = merchant
self._spend_freq = spend_freq
self._receive_freq = receive_freq
self._encryption_delay = encryption_delay
self._decryption_delay = decryption_delay
self._amp_out = {} # Outgoing AMPs still active
self._amp_in = {} # Awaiting incoming AMPs
self._htlc_out = {} # Maps other nodes to HTLCs
self._jit_out = {} # Maps JIT route IDs to payment packets (to be completed after if JIT successful)
self._jit_reserve = {} # Total number of equity reserved while rebalancing, mapped by out-going edges
def get_id(self):
return self._id
def is_merchant(self):
return self._merchant
def get_spend_freq(self):
return self._spend_freq
def get_receive_freq(self):
return self._receive_freq
def get_decryption_delay(self):
return self._decryption_delay
def get_jit_reserve(self, node=None):
if node is None:
return self._jit_reserve
return self._jit_reserve[node]
def get_htlc_out(self, node):
"""Get HTLCs locked up with a specific node."""
if node in self._htlc_out:
return self._htlc_out[node]
else:
return None
def update_htlc_out(self, node, id, amnt, ttl):
"""Neighbouring nodes can add new HTLC info when established on their channel.
Args:
node: (Node) the node at the other end of the channel.
id: (UUID) ID of HTLC between nodes.
amnt: (float) number of satoshi contained in the HTLC.
ttl: (float) real UTC time when the HTLC will be available to claim by self.
"""
if not node in self._htlc_out:
self._htlc_out[node] = {}
self._htlc_out[node][id] = (amnt, ttl)
def receive_packet(self, packet, debug=DEBUG):
"""Receive a packet from the main node communicator.
Args:
packet: (Packet) the packet to add to the Node's priority queue.
debug: (boolean) True to display debug print statements.
"""
global packet_queue
if debug: print(bcolours.OKGREEN + "%s is receiving packet from node communicator." % self + bcolours.ENDC)
packet_queue.put((packet, self))
def process_packet(self, G, node_comm, p, test_mode=False, amp_resend_enabled=AMP_RESEND, jit_enabled=JIT_ROUTING, debug=DEBUG):
"""Process next packet if any.
Args:
G: NetworkX graph in use.
debug: (boolean) True to display debug print statements.
node_comm: (NodeCommunicator) Main message communicator between nodes.
p: (Packet) Packet to process.
test_mode: (boolean) True if running from test function.
debug: (boolean) True to display debug print statements.
"""
# Analytics
global success_count
global fail_count
global amp_retry_count
index = p.get_index(self._id)
type = p.get_type()
amnt = p.get_amnt(index - 1)
subpayment = p.get_subpayment()
# -- ORDER --
# Equity moves when destination node receives message from source node.
# HTLC equity is claimed back by source node as they receive preimage, and then sent to next.
# HTLCs are fully cancelled when cancel message is received by destination node.
if type == "pay" or type == "pay_htlc":
src = p.get_node(index - 1)
jit_reserve = 0
if self in src.get_jit_reserve():
jit_reserve = src.get_jit_reserve(self)
# Assume: amnt > 0, check for available funds only
if G[src][self]["equity"] - jit_reserve >= amnt:
G[src][self]["equity"] -= amnt # To HTLC (if not direct) or other node (if direct)
if type == "pay":
if not subpayment:
success_count += 1
G[self][src]["equity"] += amnt
else:
id, subp_index, _, ttl = p.get_subpayment()
if not id in self._amp_in:
self._amp_in[id] = []
if sim_time <= ttl: # Within time
self._amp_in[id].append(p)
if debug: print(bcolours.OKGREEN + "Received partial payment at %s. [%d]" % (self, subpayment[1]) + bcolours.ENDC)
if len(self._amp_in[id]) == subpayment[2]: # All collected, release HTLCs
for s in self._amp_in[id]:
index = s.get_index(self._id)
dest = s.get_node(index - 1)
amnt = s.get_amnt(index - 1)
G[self][dest]["equity"] += amnt
# Even if pay and not pay_htlc - can still send preimage, won't change anything.
s.set_type("preimage")
s.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Sending preimage release message to node communicator from %s (-> %s). [%d]" % (self, dest, subpayment[1]))
node_comm.send_packet(self, dest, s)
del self._amp_in[id]
else: # Out of time, need to release subpayments back
for s in self._amp_in[id]:
index = s.get_index(self._id)
dest = s.get_node(index - 1)
s.set_type("cancel")
s.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Sending cancel message to node communicator from %s (-> %s). [%d]" % (self, dest, subpayment[1]))
node_comm.send_packet(self, dest, s)
del self._amp_in[id]
if debug: print("Sent %.4f from %s to %s." % (amnt, src, self) + (" [%d]" % subpayment[1] if subpayment else ""))
else:
id = uuid4()
ttl = sim_time + node_comm.get_ctlv_delta(src, self) * CLTV_MULTIPLIER
src.update_htlc_out(self, id, amnt, ttl)
p.add_htlc_id(id)
if debug: print("Sent (HTLC) %.4f from %s to %s." % (amnt, src, self) + (" [%d]" % subpayment[1] if subpayment else ""))
if index == p.get_final_index():
# Receiver - so release preimage back and claim funds if not AMP
if not subpayment:
G[self][src]["equity"] += amnt
dest = p.get_node(index - 1)
p.set_type("preimage")
# p.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Received payment - sending preimage release message to node communicator from %s (-> %s)." % (self, dest))
node_comm.send_packet(self, dest, p)
# Clear reverse
if JIT_RESERVE:
original_amnt = original_p.get_amnt(index)
self._jit_reserve[dest] = 0
jit_id = p.get_jit_id()
if jit_id: # Now (might be) enough funds are available after rebalance.
original_p = self._jit_out[jit_id]
del self._jit_out[jit_id]
index = original_p.get_index(self._id)
dest = original_p.get_node(index + 1)
amnt = original_p.get_amnt(index)
# But if another payment stole the funds while rebalancing, try to rebalance again
if JIT_REPEAT_REBALANCE and G[self][dest]["equity"] < amnt:
found_jit_route = False
if jit_enabled and not original_p.get_jit_id(): # Don't create rebalance loops
rebalance_delta = amnt - G[self][dest]["equity"]
cost, path = _jit_dijsktra_reverse(G, self, dest, rebalance_delta)
if len(path) == 4:
found_jit_route = True
jit_id = uuid4()
self._jit_out[jit_id] = original_p # @TODO: decryption delay here too
# Reserve equity, so another payment doesn't take it while rebalancing
if JIT_RESERVE:
if dest not in self._jit_reserve:
self._jit_reserve[dest] = 0
self._jit_reserve[dest] += G[self][dest]["equity"]
send_amnts = calc_path_fees(G, path, rebalance_delta)
index_mapping = {path[i].get_id(): i for i in range(len(path))}
index_mapping["src"] = self
if debug: print("Attempting to rebalancing funds by sending from %s to %s for JIT payment." % (self, path[1]))
jit_p = Packet(path, index_mapping, send_amnts, jit_id=jit_id)
jit_p.set_timestamp(sim_time)
# jit_p.set_timestamp(sim_time + self._encryption_delay + path[1].get_decryption_delay())
if debug: print("Sending %s message to node communicator from %s (-> %s)." % (type, self, path[1]) + (" [%d]" % subpayment[1] if subpayment else ""))
node_comm.send_packet(self, path[1], jit_p)
if not found_jit_route:
if debug: print(bcolours.FAIL + "Error: equity between %s and %s not available for transfer - reversing." % (self, dest) + (" [%d]" % subpayment[1] if subpayment else "") + bcolours.ENDC)
original_p.set_type("cancel")
dest = original_p.get_node(index - 1)
original_p.set_timestamp(sim_time + dest.get_decryption_delay())
node_comm.send_packet(self, dest, original_p)
else:
original_p.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Sending pay_htlc message to node communicator from %s (-> %s)." % (self, dest) + (" [%d]" % subpayment[1] if subpayment else ""))
node_comm.send_packet(self, dest, original_p)
else: # If AMP, we need to keep and store all the partial payments
id, subp_index, _, ttl = p.get_subpayment()
if not id in self._amp_in:
self._amp_in[id] = []
if sim_time <= ttl: # Within time
self._amp_in[id].append(p)
if debug: print(bcolours.OKGREEN + "Received partial payment at %s. [%d]" % (self, subpayment[1]) + bcolours.ENDC)
if len(self._amp_in[id]) == subpayment[2]: # All collected, release HTLCs
for s in self._amp_in[id]:
index = s.get_index(self._id)
dest = s.get_node(index - 1)
amnt = s.get_amnt(index - 1)
G[self][dest]["equity"] += amnt
s.set_type("preimage")
s.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Sending preimage release message to node communicator from %s (-> %s). [%d]" % (self, dest, subpayment[1]))
node_comm.send_packet(self, dest, s)
del self._amp_in[id]
else: # Out of time, need to release subpayments back
for s in self._amp_in[id]:
index = s.get_index(self._id)
dest = s.get_node(index - 1)
s.set_type("cancel")
s.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Sending cancel message to node communicator from %s (-> %s). [%d]" % (self, dest, subpayment[1]))
node_comm.send_packet(self, dest, s)
del self._amp_in[id]
else:
# Need to keep sending it on, but only if funds are available
dest = p.get_node(index + 1)
amnt = p.get_amnt(index)
jit_reserve = 0
if dest in self.get_jit_reserve():
jit_reserve = self.get_jit_reserve(dest)
if G[self][dest]["equity"] - jit_reserve >= amnt:
p.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Sending pay_htlc message to node communicator from %s (-> %s)." % (self, dest) + (" [%d]" % subpayment[1] if subpayment else ""))
node_comm.send_packet(self, dest, p)
G[self][dest]["fee_metrics"][1] += 1 # Sent payment on down this edge, record
else:
# If JIT routing is turned on, try to rebalance.
found_jit_route = False
if jit_enabled and not p.get_jit_id(): # Don't create rebalance loops
rebalance_delta = amnt - G[self][dest]["equity"]
cost, path = _jit_dijsktra_reverse(G, self, dest, rebalance_delta)
if len(path) == 4: # Proper cycle found
found_jit_route = True
jit_id = uuid4()
self._jit_out[jit_id] = p # @TODO: decryption delay here too
# Reserve equity, so another payment doesn't take it while rebalancing
if JIT_RESERVE:
if dest not in self._jit_reserve:
self._jit_reserve[dest] = 0
self._jit_reserve[dest] += G[self][dest]["equity"]
send_amnts = calc_path_fees(G, path, rebalance_delta)
index_mapping = {path[i].get_id(): i for i in range(len(path))}
index_mapping["src"] = self
if debug: print("Attempting to rebalancing funds by sending from %s to %s for JIT payment." % (self, path[1]))
jit_p = Packet(path, index_mapping, send_amnts, jit_id=jit_id)
jit_p.set_timestamp(sim_time + self._encryption_delay + path[1].get_decryption_delay())
if debug: print("Sending %s message to node communicator from %s (-> %s)." % (type, self, path[1]) + (" [%d]" % subpayment[1] if subpayment else ""))
node_comm.send_packet(self, path[1], jit_p)
if not found_jit_route:
if debug: print(bcolours.FAIL + "Error: equity between %s and %s not available for transfer - reversing." % (self, dest) + (" [%d]" % subpayment[1] if subpayment else "") + bcolours.ENDC)
p.set_type("cancel")
p.set_timestamp(sim_time + p.get_node(index - 1).get_decryption_delay())
node_comm.send_packet(self, p.get_node(index - 1), p)
else:
if debug: print(bcolours.FAIL + "Error: equity between %s and %s not available for transfer." % (src, self) + (" [%d]" % subpayment[1] if subpayment else "") + bcolours.ENDC)
dest = p.get_node(index - 1) # Propagate back CANCEL
p.set_type("cancel_rest") # Not regular cancel as no HTLC with prev. node.
p.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Sending cancellation message to node communicator from %s (-> %s)." % (self, dest) + (" [%d]" % subpayment[1] if subpayment else ""))
node_comm.send_packet(self, dest, p)
elif type == "preimage":
# Release HTLC entry
if p.get_index("src") == self:
del self._htlc_out[p.get_node(1)][p.get_htlc_id(0)]
else:
if p.get_htlc_id(index) in self._htlc_out[p.get_node(index + 1)]:
del self._htlc_out[p.get_node(index + 1)][p.get_htlc_id(index)]
if index != 0 and p.get_index("src") != self: # Keep releasing
dest = p.get_node(index - 1)
G[self][dest]["equity"] += amnt
if debug:
print("%s claimed back %.4f from payment from %s." % (self, amnt, dest) + (" [%d]" % subpayment[1] if subpayment else ""))
print("Sending preimage release message to node communicator from %s (-> %s)." % (self, dest) + (" [%d]" % subpayment[1] if subpayment else ""))
p.set_timestamp(sim_time + dest.get_decryption_delay())
node_comm.send_packet(self, dest, p)
else: # Sender, receipt come back
if not subpayment and not p.get_jit_id():
success_count += 1
if debug:
j = p.get_final_index()
print(bcolours.OKGREEN + "Payment [%s -> %s // %.4f] successfully completed." % (self, p.get_node(j), p.get_amnt(j-1)) + (" [%d]" % subpayment[1] if subpayment else "") + bcolours.ENDC)
if p.get_subpayment() and p.get_subpayment()[0] in self._amp_out:
# Then, receiver must have got all of AMP
del self._amp_out[p.get_subpayment()[0]]
success_count += 1
j = p.get_final_index()
if debug: print(bcolours.OKGREEN + "AMP from %s to %s [%.4s] fully completed." % (self, p.get_node(j), p.get_amnt(j-1)) + bcolours.ENDC)
else: # Cancel message
if p.get_index("src") == self:
index = 0
dest = p.get_node(index + 1)
amnt = p.get_amnt(index)
if not p.is_delayed(index):
cancel_chance = np.random.choice(DELAY_RESP_VAL, 1, p=DELAY_RESP_DISTRIBUTION)[0] if not test_mode else 0
# Some unresponsive or malicious nodes might delay the cancellation
if cancel_chance:
# Simulate this by requeuing the Packet with an updated timestamp to open
# This isn't entirely correct - just does period of delta - possibly much larger... @TODO
ttl = sim_time + node_comm.get_ctlv_delta(self, dest) * CLTV_MULTIPLIER * cancel_chance
p.set_timestamp(ttl)
p.set_delayed(index)
self._queue.put(p)
if debug: print("%s is waiting for %.4f of HTLC timeout before signing..." % (self, cancel_chance))
return
if p.get_type() == "cancel": # Not cancel_rest
if p.get_htlc_id(index) in self._htlc_out[dest]:
del self._htlc_out[dest][p.get_htlc_id(index)]
G[self][dest]["equity"] += amnt # Claim back
if debug: print("%s cancelled HTLC and claimed back %.4f from payment to %s." % (self, amnt, dest) + (" [%d]" % subpayment[1] if subpayment else ""))
else:
p.set_type("cancel")
if index == 0:
if debug:
j = p.get_final_index()
print(bcolours.FAIL + "Payment [%s -> %s // %.4f] failed and returned." % (self, p.get_node(j), p.get_amnt(j-1)) + (" [%d]" % subpayment[1] if subpayment else "") + bcolours.ENDC)
jit_id = p.get_jit_id()
if jit_id:
original_p = self._jit_out[jit_id]
del self._jit_out[jit_id]
original_index = original_p.get_index(self._id)
original_dest = original_p.get_node(original_index - 1)
# Clear reserve
if JIT_RESERVE:
original_out_amnt = original_p.get_amnt(original_index)
self._jit_reserve[original_p.get_node(original_index + 1)] = 0
original_p.set_type("cancel")
original_p.set_timestamp(sim_time + original_dest.get_decryption_delay())
if debug: print("Sending cancel message to node communicator from %s (-> %s)." % (self, original_dest))
node_comm.send_packet(self, original_dest, original_p)
if index != 0: # Send cancel message on
G[self][dest]["fee_metrics"][2] += 1 # Record fail coming back from payment relayed on
# Check metrics and adjust fee rate accordingly
if FEE_BALANCING and G[self][dest]["fee_metrics"][1] > FEE_BALANCING_CHECKPOINT:
fail_return = G[self][dest]["fee_metrics"][2] / G[self][dest]["fee_metrics"][1]
old = G[self][dest]["fee_metrics"][0]
if old is not None:
diff = fail_return - old
if diff < -FEE_BALANCING_UPDATE_POINT: # Too high, increase
# print("REDUCING")
fees = G[self][dest]["fees"]
if fees[0] > 0.0: fees[0] -= 0.1
if fees[1] > 0.0: fees[1] -= 0.0000001
elif diff > FEE_BALANCING_UPDATE_POINT:
# print("INCREASING")
fees = G[self][dest]["fees"]
fees[0] += 0.1
fees[1] += 0.0000001
G[self][dest]["fee_metrics"] = [fail_return, 0, 0] # Re-init
dest = p.get_node(index - 1)
p.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Sending cancellation message to node communicator from %s (-> %s)." % (self, dest) + (" [%d]" % subpayment[1] if subpayment else ""))
node_comm.send_packet(self, dest, p)
elif subpayment: # Partial payment of AMP failed
# subpayment in form [ID, index, total num, ttl]
id, i, k, ttl = subpayment
if id in self._amp_out: # Still on-going
j = p.get_final_index()
if amp_resend_enabled and sim_time < ttl and self._amp_out[id][2]:
# Still within time limit - so try again!
# In Rusty Russell podcast - c-lightning is 60 seconds.
self._amp_out[id][0][i].append(p.get_path())
new_subp = (id, i, k)
if debug: print(bcolours.WARNING + "Resending... [%d]" % i + bcolours.ENDC)
self._init_payment(G, p.get_node(j), p.get_amnt(j-1), node_comm, new_subp, test_mode=test_mode, debug=debug)
amp_retry_count += 1
else:
fail_count += 1
del self._amp_out[id]
# Clear stored packets
p.get_node(j).clear_old_amps(G, node_comm)
if debug:
j = p.get_final_index()
print(bcolours.FAIL + "Partial payment [%d] of failed AMP from %s to %s [%.4s] returned - not resending." % (i, self, p.get_node(j), p.get_amnt(j-1)) + bcolours.ENDC)
else:
if debug:
j = p.get_final_index()
print(bcolours.FAIL + "Partial payment [%d] of failed AMP from %s to %s [%.4s] returned - not resending." % (i, self, p.get_node(j), p.get_amnt(j-1)) + bcolours.ENDC)
else: # Non-subpayment failed
if not p.get_jit_id(): fail_count += 1
def clear_old_amps(self, G, node_comm, force=False, debug=DEBUG):
"""Check for timed out AMP payments with partial subpayments that need
to be released back to the sender.
Args:
G: NetworkX graph in use.
node_comm: (NodeCommunicator) main communicator to send packets to.
force: (boolean) clear all partially received payments regardless of timeout - for testing.
"""
to_delete = []
for id in self._amp_in:
if force or (len(self._amp_in[id]) and self._amp_in[id][0].get_subpayment()[3] - sim_time < 0):
if debug: print(bcolours.WARNING + "Cancelling partial subpayments from %s for AMP ID %s" % (self, id) + bcolours.ENDC)
for p in self._amp_in[id]:
index = p.get_index(self._id)
dest = p.get_node(index - 1)
amnt = p.get_amnt(index - 1)
p.set_type("cancel")
p.set_timestamp(sim_time + dest.get_decryption_delay())
if debug: print("Sending cancel message to node communicator from %s (-> %s)." % (self, dest))
node_comm.send_packet(self, dest, p)
to_delete.append(id)
for id in to_delete:
del self._amp_in[id]
def send_direct(self, G, dest, amnt, node_comm, debug=DEBUG):
"""Send funds over a direct payment channel to neighbouring node.
Args:
G: NetworkX graph in use.
dest: (Node) destination node to attempt to find best path to.
amnt: (float) number of satoshi the resultant path must support.
node_comm: (NodeCommunicator) main communicator to send packets to.
debug: (boolean) True to display debug print statements.
Returns:
True if payment packet sent out successfully,
False otherwise.
"""
if G.has_edge(self, dest):
path = [self, dest]
index_mapping = {path[i].get_id(): i for i in range(len(path))}
index_mapping["src"] = self
p = Packet(path, index_mapping, [amnt], "pay", False)
p.set_timestamp(sim_time + dest.get_decryption_delay())
node_comm.send_packet(self, dest, p)
if debug: print("Sending pay message to node communicator from %s (-> %s)." % (self, dest))
return True
else:
if debug: print(bcolours.FAIL + "Error: no direct payment channel between %s and %s." % (self, dest) + bcolours.ENDC)
return False
def _find_path(self, G, dest, amnt, failed_paths, k=False):
"""Attempt to find the next shortest path from self to dest within graph G, that is not in failed_paths.
Adapted from NetworkX shorest_simple_paths src code.
Args:
G: NetworkX graph in use.
dest: (Node) destination node to attempt to find best path to.
amnt: (float) number of satoshi the resultant path must support.
failed_paths: (list) previously yielded routes to skip.
k: (int) number of new paths to find.
Returns:
a generator that produces a list of possible paths, from best to worst,
or False if no paths exist.
Raises:
NetworkXError: if self or dest are not in the input graph.
"""
if self not in G:
raise nx.NodeNotFound("Src [%s] not in graph" % self)
if dest not in G:
raise nx.NodeNotFound("Dest [%s] not in graph" % dest)
def length_func(path):
send_amnts = calc_path_fees(G, path, amnt)
return send_amnts[0] - amnt
shortest_path_func = _dijkstra_reverse
listA = [] # Previously yielded paths
listB = PathBuffer()
num_to_find = len(failed_paths) if not k else k - 1
# Find one new path per func call, up until a global maximum.
while len(listA) <= num_to_find and len(listA) < MAX_PATH_ATTEMPTS:
avoid_edges = []
to_avoid = listA if k else failed_paths
for p in to_avoid:
for i in range(len(p) - 1):
avoid_edges.append((p[i], p[i+1]))
attempt = shortest_path_func(G, self, dest, amnt, avoid_edges)
if attempt:
length, path = attempt
listB.push(length, path)
if listB:
path = listB.pop()
yield path
listA.append(path)
else:
return False
def _init_payment(self, G, dest, amnt, node_comm, subpayment=False, routes=[], route_index=0, test_mode=False, debug=DEBUG):
"""Initialise a regular single payment from this node to destination node of amnt.
Args:
G: NetworkX graph in use.
dest: (Node) destination node to attempt to send money to.
amnt: (float) number of satoshi to send.
node_comm: (NodeCommunicator) main communicator to send packets to.
subpayment: (boolean / (UUID, int, int))
- (ID of subpayment group, index of subpayment, total num subpayments),
- or False if not AMP.
routes: (List<List<Node>>) preset routes to try.
route_index: (int) index for which route in routes to try for this subpayment.
test_mode: (boolean) True if running from test function.
debug: (boolean) True to display debug print statements.
Returns:
True if payment packet sent out successfully,
False otherwise.
"""
# Analytics
global fail_count
global total_path_hops
global total_paths_found
global path_too_large_count
if subpayment:
if len(routes) > 0:
paths = [routes[route_index]]
# For now, set all failed_routes for future as routes
id, i, n = subpayment
self._amp_out[id][0][i] = routes
else: # Won't go in here for my experiments
id, i, n = subpayment
failed_routes = self._amp_out[id][0][i]
paths = [p for p in self._find_path(G, dest, amnt, failed_routes)]
else:
# Only try best path once for regular payments
paths = [_slack_based_reverse(G, self, dest, amnt)]
# For my experiments with AMP, clause won't be entered
if len(paths) == 0 or paths[0] is False:
if debug: print(bcolours.FAIL + "Error: no possible routes available." + (" [%d]" % subpayment[1] if subpayment else "") + bcolours.ENDC)
if subpayment:
self._amp_out[id][2] = False
if debug: print(bcolours.FAIL + "AMP from %s to %s [%.4s] failed." % (self, dest, amnt) + bcolours.ENDC)
else:
fail_count += 1
return False
path = paths[0] if subpayment else paths[0][1]
total_path_hops += len(path)
total_paths_found += 1
if len(path) - 1 > MAX_HOPS:
path_too_large_count += 1
if debug: print(bcolours.FAIL + "Error: path exceeds max-hop distance. (%d)" % (len(path) - 1) + (" [%d]" % subpayment[1] if subpayment else "") + bcolours.ENDC)
if subpayment:
self._amp_out[id][0][i].append(path)
if not AMP_RESEND:
if self._amp_out[id][2]:
self._amp_out[id][2] = False
fail_count += 1
else:
# Need to re-send here instead of returning False, not in experiments so ignore for now
pass
else:
fail_count += 1
return False
send_amnts = calc_path_fees(G, path, amnt)
index_mapping = {path[i].get_id(): i for i in range(len(path))}
index_mapping["src"] = self
type = "pay_htlc" if len(path) > 2 else "pay"
if subpayment: ttl = self._amp_out[subpayment[0]][1]
p = Packet(path, index_mapping, send_amnts, type,
(subpayment[0], subpayment[1], subpayment[2], ttl) if subpayment else False)
p.set_timestamp(sim_time + self._encryption_delay + dest.get_decryption_delay())
if debug: print("Sending %s message to node communicator from %s (-> %s)." % (type, self, path[1]) + (" [%d]" % subpayment[1] if subpayment else ""))
node_comm.send_packet(self, path[1], p)
return True
def init_payment(self, G, dest, amnt, node_comm, k=1, test_mode=False, debug=DEBUG):
"""Initialse a payment from this node to destination node of amnt.
May be split by into k different packets [ AMP ].
Args:
G: NetworkX graph in use.
dest: (Node) destination node to attempt to send money to.
amnt: (float) number of satoshi to send.
node_comm: (NodeCommunicator) main communicator to send packets to.
k: (int) AMP if >1 - number of ways to split payment. (1 by default)
test_mode: (boolean) True if running from test function.
debug: (boolean) True to display debug print statements.
"""
global fail_count
if k == 1:
self._init_payment(G, dest, amnt, node_comm, False, test_mode=test_mode, debug=debug)
else: # AMP
# Of form, [subpayment index, failed routes]
subp_statuses = [[] for i in range(k)]
id = uuid4()
# After timeout, attempt at AMP cancelled
ttl = sim_time + AMP_TIMEOUT
self._amp_out[id] = [subp_statuses, ttl, True] # True here means hasn't failed yet
routes = [p for p in self._find_path(G, dest, amnt / k, [], k=k)]
if k > 1 and len(routes) != k: # AMP, didn't find any route
fail_count += 1
else:
# Send off each subpayment - first attempt.
for i in range(k):
self._init_payment(G, dest, amnt / k, node_comm, (id, i, k), routes=routes, route_index=i, test_mode=test_mode, debug=debug)
def get_total_equity(self, G):
"""Returns the total equity held by a node (not locked up in HTLCs). """
out_edges = G.out_edges(self)
total = 0
for out_edge in out_edges:
total += G[self][out_edge[1]]["equity"]
return total
def get_largest_outgoing_equity(self, G):
"""Returns the largest equity held by the node in a single payment channel. """
out_edges = G.out_edges(self)
largest = 0
for out_edge in out_edges:
if G[self][out_edge[1]]["equity"] > largest:
largest = G[self][out_edge[1]]["equity"]
return largest
def __str__(self):
return "Node %d" % self._id
class NodeCommunicator:
"""Handles communication between nodes.
Messages are synced to simulator time and network latency is applied by using future timestamps.
Edge latencies between nodes are initialsed here.
CTLV expiracy deltas (BOLT 7) per directed edge initialised here. (unit: number of BTC blocks)
Attributes:
nodes: (list<Node>) set of nodes to handle communication between.
edge_pairs: (set<tuple<networkX.Graph.Edge>>) payment channels: corresponding edges grouped.
test_mode: (boolean) True if testing - remove randomised latencies.
debug: (boolean) True to display debug print statements.
"""
def __init__(self, nodes, edge_pairs, test_mode=False, debug=DEBUG):
self._nodes = nodes
self._edge_pairs = edge_pairs
self._debug = debug
self._latencies = {}
self._ctlv_deltas = {}
for edge_pair in edge_pairs:
latency = np.random.choice(LATENCY_OPTIONS, 1, p=LATENCY_DISTRIBUTION)
ctlv_expiracy_delta = np.random.choice(CTLV_OPTIONS, 1, p=CTLV_DISTRIBUTION)
# Both directions for ease of access in send_packet
self._latencies[edge_pair] = latency[0] if not test_mode else 0
self._latencies[edge_pair[::-1]] = latency[0] if not test_mode else 0
self._ctlv_deltas[edge_pair] = ctlv_expiracy_delta[0]
self._ctlv_deltas[edge_pair[::-1]] = ctlv_expiracy_delta[0]
def set_latency(self, a, b, latency):
"""Update the latency between two nodes. (assumes edge exists) """
self._latencies[(a, b)] = latency
self._latencies[(b, a)] = latency
def get_ctlv_delta(self, a, b):
return self._ctlv_deltas[(a, b)]
def send_packet(self, src, dest, packet):
"""Processes and sends a packet from src to dest. """
if self._debug: print(bcolours.HEADER + "[NODE-COMM] Relaying message between %s and %s." % (src, dest) + bcolours.ENDC)
try:
packet.set_timestamp(packet.get_timestamp() + self._latencies[(src, dest)])
except KeyError:
print("ERROR - packet sending to self... [%s/%s/%s] [Len: %d]" % (src, dest, packet.get_type(), len(packet.get_path())))
return
dest.receive_packet(packet, self._debug)
def floor_msat(satoshi):
"""Floor to nearest millisatoshi. (lowest possible unit in LN) """
return np.round(satoshi - 0.5 * 10**(-4), 4)
def calc_path_fees(G, path, amnt):
"""Calculate the compound path fees required for a given path.
Note: compounding as amnt of equity moving per node is different!
Args:
G: NetworkX graph in use.
path: (list<Node>) path for which fees are to be calculated.
amnt: (float) number of satoshi to send to final Node in path.
Returns:
a list of satoshi to send at each hop.
"""
hop_amnts = [amnt]
for i in range(len(path)-2):
fees = G[path[i]][path[i+1]]["fees"]
fee_this_hop = floor_msat(fees[0] + fees[1] * hop_amnts[-1]) # Fees always floored to nearest msat
hop_amnts.append(fee_this_hop + hop_amnts[-1])
return hop_amnts[::-1]
def calc_g_unbalance(G, pairs):
"""Calculate the unbalance of equity between payment channels.
Higher equity channels take proportionally higher effect on the resultant ratio.
Args:
G: NetworkX graph in use.
pairs: represents payment channels - pairs of nodes (but only one direction so no duplicates)
Returns:
a float in range from 0 (completely balanced) to 1 (completely unbalanced).
"""
total_diff = 0
total_equity = 0
for pair in pairs:
u, v = pair[0], pair[1]
total_diff += abs(G[u][v]["equity"] - G[v][u]["equity"])