-
Notifications
You must be signed in to change notification settings - Fork 0
/
hop_test.py
61 lines (55 loc) · 2.76 KB
/
hop_test.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
from hop import Hop
from channel import Channel
from enumtypes import FeeType
from direction import Direction
from htlc import Htlc
import logging
logger = logging.getLogger(__name__)
def test_hop_create():
hop = Hop()
ch = Channel(capacity=1000, cid="cid0")
ch.enable_direction_with_num_slots(Direction.Alph, num_slots=2)
hop.add_channel(ch)
assert(ch.is_enabled_in_direction(Direction.Alph))
assert(not ch.is_enabled_in_direction(Direction.NonAlph))
ch.set_fee_in_direction(Direction.Alph, FeeType.SUCCESS, base_fee=1, fee_rate=0.01)
assert ch.in_direction(Direction.Alph).requires_fee_for_body(FeeType.SUCCESS, 100) == 2
ch_1 = Channel(capacity=2000, cid="cid1", num_slots_per_direction=2)
hop.add_channel(ch_1)
assert(hop.has_channel("cid0"))
assert(hop.has_channel("cid1"))
assert hop.get_channel("no_such_cid") is None
ch_1.set_fee_in_direction(Direction.Alph, FeeType.SUCCESS, base_fee=2, fee_rate=0.02)
assert(len(hop.get_all_channels()) == 2)
chs_can_forward_1500 = hop.get_channels_with_condition(
condition=lambda ch: ch.really_can_forward_in_direction_at_time(Direction.Alph, time=0, amount=1500))
assert(len(chs_can_forward_1500) == 1)
def ch_in_dir_requires_total_fee_for_body(body):
ch_in_dir = ch.in_direction(Direction.Alph)
requires_fee_upfront = ch_in_dir.requires_fee_for_body(FeeType.UPFRONT, body)
requires_fee_success = ch_in_dir.requires_fee_for_body(FeeType.SUCCESS, body)
return requires_fee_upfront + requires_fee_success
chs_by_fee_for_100 = hop.get_channels_with_condition(
sorting_function=lambda ch: ch_in_dir_requires_total_fee_for_body(body=100))
assert len(chs_by_fee_for_100) == 2
assert chs_by_fee_for_100[0].capacity == 1000
def test_is_jammed():
hop = Hop()
ch = Channel(capacity=1000, cid="cid0")
ch.enable_direction_with_num_slots(Direction.Alph, num_slots=2)
hop.add_channel(ch)
assert(not hop.cannot_forward(Direction.Alph, time=0))
assert(hop.get_total_num_slots_occupied_in_direction(Direction.Alph) == 0)
htlc_01 = Htlc(payment_id="pid1", success_fee=0, desired_result=True)
htlc_02 = Htlc(payment_id="pid2", success_fee=0, desired_result=True)
ch_in_dir_alph = ch.in_direction(Direction.Alph)
ch_in_dir_alph.push_htlc(resolution_time=1, in_flight_htlc=htlc_01)
ch_in_dir_alph.push_htlc(resolution_time=1, in_flight_htlc=htlc_02)
assert hop.cannot_forward(Direction.Alph, time=0)
assert hop.get_total_num_slots_occupied_in_direction(Direction.Alph) == 2
assert hop.cannot_forward(Direction.NonAlph, time=0)
assert hop.get_total_num_slots_occupied_in_direction(Direction.NonAlph) == 0
# checking jammed status does NOT pop HTLCs!
assert not hop.cannot_forward(Direction.Alph, time=1)
assert hop.get_total_num_slots_occupied_in_direction(Direction.Alph) == 2
assert hop.get_jammed_status(Direction.Alph, time=0) == (True, 2)