| 1 |
import random |
| 2 |
|
| 3 |
import electrum.mpp_split as mpp_split |
| 4 |
from electrum.lnutil import NoPathFound |
| 5 |
|
| 6 |
from . import ElectrumTestCase |
| 7 |
|
| 8 |
PART_PENALTY = mpp_split.PART_PENALTY |
| 9 |
|
| 10 |
|
| 11 |
class TestMppSplit(ElectrumTestCase): |
| 12 |
def setUp(self): |
| 13 |
super().setUp() |
| 14 |
|
| 15 |
random.seed(0) |
| 16 |
|
| 17 |
self.channels_with_funds = { |
| 18 |
(b"0", b"0"): (1_000_000_000, 3), |
| 19 |
(b"1", b"1"): (500_000_000, 2), |
| 20 |
(b"2", b"0"): (302_000_000, 2), |
| 21 |
(b"3", b"2"): (101_000_000, 1), |
| 22 |
} |
| 23 |
|
| 24 |
def tearDown(self): |
| 25 |
super().tearDown() |
| 26 |
|
| 27 |
mpp_split.PART_PENALTY = PART_PENALTY |
| 28 |
|
| 29 |
def test_suggest_splits(self): |
| 30 |
with self.subTest(msg="do a payment with the maximal amount spendable over a single channel"): |
| 31 |
splits = mpp_split.suggest_splits(1_000_000_000, self.channels_with_funds, exclude_single_part_payments=True) |
| 32 |
self.assertEqual({ |
| 33 |
(b"0", b"0"): [671_020_676], |
| 34 |
(b"1", b"1"): [328_979_324]}, |
| 35 |
splits[0].config |
| 36 |
) |
| 37 |
|
| 38 |
with self.subTest(msg="payment amount that does not require to be split"): |
| 39 |
splits = mpp_split.suggest_splits(50_000_000, self.channels_with_funds, exclude_single_part_payments=False) |
| 40 |
self.assertEqual({(b"0", b"0"): [50_000_000]}, splits[0].config) |
| 41 |
self.assertEqual({(b"1", b"1"): [50_000_000]}, splits[1].config) |
| 42 |
self.assertEqual({(b"2", b"0"): [50_000_000]}, splits[2].config) |
| 43 |
self.assertEqual({(b"3", b"2"): [50_000_000]}, splits[3].config) |
| 44 |
self.assertEqual(2, splits[4].config.number_parts()) |
| 45 |
|
| 46 |
with self.subTest(msg="do a payment with a larger amount than what is supported by a single channel"): |
| 47 |
splits = mpp_split.suggest_splits(1_100_000_000, self.channels_with_funds, exclude_single_part_payments=False) |
| 48 |
self.assertEqual(2, splits[0].config.number_parts()) |
| 49 |
|
| 50 |
with self.subTest(msg="do a payment with the maximal amount spendable over all channels"): |
| 51 |
splits = mpp_split.suggest_splits( |
| 52 |
sum([x[0] for x in self.channels_with_funds.values()]), self.channels_with_funds, exclude_single_part_payments=True) |
| 53 |
self.assertEqual({ |
| 54 |
(b"0", b"0"): [1_000_000_000], |
| 55 |
(b"1", b"1"): [500_000_000], |
| 56 |
(b"2", b"0"): [302_000_000], |
| 57 |
(b"3", b"2"): [101_000_000]}, |
| 58 |
splits[0].config |
| 59 |
) |
| 60 |
|
| 61 |
with self.subTest(msg="do a payment with the amount supported by all channels"): |
| 62 |
splits = mpp_split.suggest_splits(101_000_000, self.channels_with_funds, exclude_single_part_payments=False) |
| 63 |
for split in splits[:3]: |
| 64 |
self.assertEqual(1, split.config.number_nonzero_channels()) |
| 65 |
|
| 66 |
|
| 67 |
self.assertEqual(2, splits[4].config.number_parts()) |
| 68 |
|
| 69 |
with self.subTest(msg="no htlc slots available"): |
| 70 |
channels = self.channels_with_funds.copy() |
| 71 |
|
| 72 |
for chan, (amount, _slots) in channels.items(): |
| 73 |
channels[chan] = (amount, 0) |
| 74 |
with self.assertRaises(NoPathFound): |
| 75 |
mpp_split.suggest_splits(20_000_000, channels, exclude_single_part_payments=False) |
| 76 |
|
| 77 |
with self.subTest(msg="only one channel can add htlcs"): |
| 78 |
channels = self.channels_with_funds.copy() |
| 79 |
|
| 80 |
for chan, (amount, _slots) in channels.items(): |
| 81 |
if chan != (b"0", b"0"): |
| 82 |
channels[chan] = (amount, 0) |
| 83 |
splits = mpp_split.suggest_splits(1_000_000_000, channels, exclude_single_part_payments=True) |
| 84 |
for split in splits: |
| 85 |
|
| 86 |
self.assertEqual(sum(split.config[(b"0", b"0")]), 1_000_000_000) |
| 87 |
|
| 88 |
with self.subTest(msg="test exclude single channel splits"): |
| 89 |
splits = mpp_split.suggest_splits(1_000_000_000, self.channels_with_funds, exclude_single_channel_splits=True) |
| 90 |
for split in splits: |
| 91 |
for channel_split in split.config.values(): |
| 92 |
assert len(channel_split) <= 1, split |
| 93 |
|
| 94 |
def test_send_to_single_node(self): |
| 95 |
splits = mpp_split.suggest_splits(1_000_000_000, self.channels_with_funds, exclude_single_part_payments=False, exclude_multinode_payments=True) |
| 96 |
for split in splits: |
| 97 |
assert split.config.number_nonzero_nodes() == 1 |
| 98 |
|
| 99 |
def test_saturation(self): |
| 100 |
"""Split configurations which spend the full amount in a channel should be avoided.""" |
| 101 |
channels_with_funds = { |
| 102 |
(b"0", b"0"): (159_799_733_076, 1), |
| 103 |
(b"1", b"1"): (499_986_152_000, 1) |
| 104 |
} |
| 105 |
splits = mpp_split.suggest_splits(600_000_000_000, channels_with_funds, exclude_single_part_payments=True) |
| 106 |
|
| 107 |
uses_full_amount = False |
| 108 |
for c, a in splits[0].config.items(): |
| 109 |
if a == channels_with_funds[c]: |
| 110 |
uses_full_amount |= True |
| 111 |
|
| 112 |
self.assertFalse(uses_full_amount) |
| 113 |
|
| 114 |
def test_payment_below_min_part_size(self): |
| 115 |
amount = mpp_split.MIN_PART_SIZE_MSAT // 2 |
| 116 |
splits = mpp_split.suggest_splits(amount, self.channels_with_funds, exclude_single_part_payments=False) |
| 117 |
|
| 118 |
|
| 119 |
self.assertEqual(4, len(splits)) |
| 120 |
|
| 121 |
def test_suggest_part_penalty(self): |
| 122 |
"""Test is mainly for documentation purposes. |
| 123 |
Decreasing the part penalty from 1.0 towards 0.0 leads to an increase |
| 124 |
in the number of parts a payment is split. A configuration which has |
| 125 |
about equally distributed amounts will result.""" |
| 126 |
with self.subTest(msg="split payments with intermediate part penalty"): |
| 127 |
mpp_split.PART_PENALTY = 1.0 |
| 128 |
splits = mpp_split.suggest_splits(1_100_000_000, self.channels_with_funds) |
| 129 |
self.assertEqual(2, splits[0].config.number_parts()) |
| 130 |
|
| 131 |
with self.subTest(msg="split payments with intermediate part penalty"): |
| 132 |
mpp_split.PART_PENALTY = 0.3 |
| 133 |
splits = mpp_split.suggest_splits(1_100_000_000, self.channels_with_funds) |
| 134 |
self.assertEqual(4, splits[0].config.number_parts()) |
| 135 |
|
| 136 |
with self.subTest(msg="split payments with no part penalty"): |
| 137 |
mpp_split.PART_PENALTY = 0.0 |
| 138 |
splits = mpp_split.suggest_splits(1_100_000_000, self.channels_with_funds) |
| 139 |
self.assertEqual(5, splits[0].config.number_parts()) |
| 140 |
|
| 141 |
def test_suggest_splits_single_channel(self): |
| 142 |
channels_with_funds = { |
| 143 |
(b"0", b"0"): (1_000_000_000, 3), |
| 144 |
} |
| 145 |
with self.subTest(msg="do a payment with the maximal amount spendable on a single channel"): |
| 146 |
splits = mpp_split.suggest_splits(1_000_000_000, channels_with_funds, exclude_single_part_payments=False) |
| 147 |
self.assertEqual(1, len(splits[0].config[(b"0", b"0")])) |
| 148 |
self.assertEqual({(b"0", b"0"): [1_000_000_000]}, splits[0].config) |
| 149 |
|
| 150 |
with self.subTest(msg="test sending an amount greater than what we have available"): |
| 151 |
self.assertRaises(NoPathFound, mpp_split.suggest_splits, *(1_100_000_000, channels_with_funds)) |
| 152 |
|
| 153 |
with self.subTest(msg="test sending a large amount over a single channel in chunks"): |
| 154 |
mpp_split.PART_PENALTY = 0.5 |
| 155 |
splits = mpp_split.suggest_splits(1_000_000_000, channels_with_funds, exclude_single_part_payments=False) |
| 156 |
self.assertEqual(2, len(splits[0].config[(b"0", b"0")])) |
| 157 |
|
| 158 |
with self.subTest(msg="test sending a large amount over a single channel in chunks"): |
| 159 |
mpp_split.PART_PENALTY = 0.3 |
| 160 |
splits = mpp_split.suggest_splits(1_000_000_000, channels_with_funds, exclude_single_part_payments=False) |
| 161 |
self.assertEqual(3, len(splits[0].config[(b"0", b"0")])) |
| 162 |
with self.subTest(msg="exclude all single channel splits"): |
| 163 |
mpp_split.PART_PENALTY = 0.3 |
| 164 |
splits = mpp_split.suggest_splits(1_000_000_000, channels_with_funds, exclude_single_channel_splits=True) |
| 165 |
self.assertEqual(1, len(splits[0].config[(b"0", b"0")])) |