-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_bolt12_prism.py
374 lines (319 loc) · 11.9 KB
/
test_bolt12_prism.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
import os
import time
import pytest
from pyln.client import RpcError
from pyln.testing.fixtures import * # noqa: F401,F403
from pyln.testing.utils import sync_blockheight, wait_for
plugin_path = os.path.join(os.path.dirname(__file__), "./bolt12-prism.py")
plugin_opt = {"plugin": plugin_path}
# spin up a network
def test_basic_test(node_factory):
# Start five lightning nodes
l1, l2, l3, l4, l5 = node_factory.get_nodes(5)
# Connect the nodes in a prism layout.
l1.rpc.connect(l2.info["id"], "localhost", l2.port) # Alice -> Bob
l2.rpc.connect(l3.info["id"], "localhost", l3.port) # Bob -> Carol
l2.rpc.connect(l4.info["id"], "localhost", l4.port) # Bob -> Dave
l2.rpc.connect(l5.info["id"], "localhost", l5.port) # Bob -> Erin
# Check the list of peers to confirm connection
# Alice -> Bob
assert len(l1.rpc.listpeers()["peers"]) == 1
assert l1.rpc.listpeers()["peers"][0]["id"] == l2.info["id"]
# Bob -> all others
assert len(l2.rpc.listpeers()["peers"]) == 4
# TODO we don't really know which index an id will be in; just search for existence?
# try:
assert l2.rpc.plugin_start(plugin_path), "Failed to start plugin"
list_plugin = l2.rpc.plugin_list()
our_plugin = [
plugin
for plugin in list_plugin["plugins"]
if "bolt12-prism" in plugin["name"]
]
assert len(our_plugin) > 0
assert l2.rpc.prism_list()
assert l2.rpc.prism_listbindings()
assert l2.rpc.plugin_stop(plugin_path)
def test_general_prism(node_factory, bitcoind):
l1, l2, l3, l4, l5 = node_factory.get_nodes(
5, opts={"experimental-offers": None}
)
nodes = [l1, l2, l3, l4, l5]
# fund nodes, create channels, l2 is the prism
#
# -> l3
# l1 -> l2 -> l4
# -> l5
#
l1.fundwallet(10_000_000)
l2.fundwallet(10_000_000)
l1.rpc.fundchannel(l2.info["id"] + "@localhost:" + str(l2.port), 1_000_000)
l2.rpc.fundchannel(l3.info["id"] + "@localhost:" + str(l3.port), 1_000_000)
bitcoind.generate_block(1)
sync_blockheight(bitcoind, nodes)
l2.rpc.fundchannel(l4.info["id"] + "@localhost:" + str(l4.port), 1_000_000)
bitcoind.generate_block(1)
sync_blockheight(bitcoind, nodes)
l2.rpc.fundchannel(l5.info["id"] + "@localhost:" + str(l5.port), 1_000_000)
bitcoind.generate_block(6)
sync_blockheight(bitcoind, nodes)
l2.rpc.plugin_start(plugin_path)
l3_offer = l3.rpc.offer("any", "Lead-Singer")
l4_offer = l4.rpc.offer("any", "Drummer")
l5_offer = l5.rpc.offer("any", "Guitarist")
members_json = [
{
"description": "Lead-Singer",
"destination": l3_offer["bolt12"],
"split": 1.0,
},
{
"description": "Drummer",
"destination": l4_offer["bolt12"],
"split": 1,
},
{
"description": "Guitarist",
"destination": l5_offer["bolt12"],
"split": 1.0,
},
]
description = "prism1"
prism = l2.rpc.call(
"prism-create", {"members": members_json, "description": description}
)
prism1_id = prism["prism_id"]
prism_ids = [prism["prism_id"] for prism in l2.rpc.call("prism-list")["prisms"]]
assert prism1_id in prism_ids
l2.rpc.call("prism-pay", {"prism_id": prism1_id, "amount_msat": 1_000_000})
wait_for(
lambda: l3.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 300_000
)
wait_for(
lambda: l4.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 300_000
)
wait_for(
lambda: l5.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 300_000
)
l2_offer = l2.rpc.offer("any", "Prism")
l2.rpc.call(
"prism-addbinding",
{"offer_id": l2_offer["offer_id"], "prism_id": prism1_id},
)
binding = l2.rpc.call("prism-listbindings")["bolt12_prism_bindings"][0]
assert binding["offer_id"] == l2_offer["offer_id"]
assert binding["prism_id"] == prism1_id
invoice = l1.rpc.fetchinvoice(l2_offer["bolt12"], 1_000_000)
l1.rpc.pay(invoice["invoice"])
wait_for(
lambda: l3.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 600_000
)
wait_for(
lambda: l4.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 600_000
)
wait_for(
lambda: l5.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 600_000
)
l2.rpc.call("prism-deletebinding", {"offer_id": l2_offer["offer_id"]})
assert len(l2.rpc.call("prism-listbindings")["bolt12_prism_bindings"]) == 0
l2.rpc.call("prism-delete", {"prism_id": prism1_id})
assert prism1_id not in [prism["prism_id"] for prism in l2.rpc.call("prism-list")["prisms"]]
def test_splits(node_factory, bitcoind):
l1, l2, l3, l4 = node_factory.get_nodes(
4, opts={"experimental-offers": None}
)
nodes = [l1, l2, l3, l4]
# fund nodes, create channels, l1 is the prism
#
# -> l2
# l1 -> l3
# -> l4
#
l1.fundwallet(10_000_000)
l1.rpc.fundchannel(l2.info["id"] + "@localhost:" + str(l2.port), 1_000_000)
bitcoind.generate_block(1)
sync_blockheight(bitcoind, nodes)
l1.rpc.fundchannel(l3.info["id"] + "@localhost:" + str(l3.port), 1_000_000)
bitcoind.generate_block(1)
sync_blockheight(bitcoind, nodes)
l1.rpc.fundchannel(l4.info["id"] + "@localhost:" + str(l4.port), 1_000_000)
bitcoind.generate_block(6)
sync_blockheight(bitcoind, nodes)
l1.rpc.plugin_start(plugin_path)
l2_offer = l2.rpc.offer("any", "CEO")
l3_offer = l3.rpc.offer("any", "CTO")
l4_offer = l4.rpc.offer("any", "Janitor")
prism1_description = "prism1"
members_json = [
{
"description": "CEO",
"destination": l2_offer["bolt12"],
"split": 5,
},
{
"description": "CTO",
"destination": l3_offer["bolt12"],
"split": 3.0,
},
{
"description": "Janitor",
"destination": l4_offer["bolt12"],
"split": 1.0,
},
]
prism = l1.rpc.call(
"prism-create", {"members": members_json, "description": prism1_description}
)
prism1_id = prism["prism_id"]
l1.rpc.call("prism-pay", {"prism_id": prism1_id, "amount_msat": 1_000_000})
wait_for(
lambda: l2.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 555_000
)
wait_for(
lambda: l3.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 333_000
)
wait_for(
lambda: l4.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 111_000
)
def test_payment_threshold(node_factory, bitcoind):
l1, l2, l3, l4, l5 = node_factory.get_nodes(
5, opts={"experimental-offers": None}
)
nodes = [l1, l2, l3, l4, l5]
# fund nodes, create channels, l2 is the prism
#
# -> l3
# l1 -> l2 -> l4
# -> l5
#
l1.fundwallet(10_000_000)
l2.fundwallet(10_000_000)
l1.rpc.fundchannel(l2.info["id"] + "@localhost:" + str(l2.port), 1_000_000)
l2.rpc.fundchannel(l3.info["id"] + "@localhost:" + str(l3.port), 1_000_000)
bitcoind.generate_block(1)
sync_blockheight(bitcoind, nodes)
l2.rpc.fundchannel(l4.info["id"] + "@localhost:" + str(l4.port), 1_000_000)
bitcoind.generate_block(1)
sync_blockheight(bitcoind, nodes)
l2.rpc.fundchannel(l5.info["id"] + "@localhost:" + str(l5.port), 1_000_000)
bitcoind.generate_block(6)
sync_blockheight(bitcoind, nodes)
l2.rpc.plugin_start(plugin_path)
l3_offer = l3.rpc.offer("any", "Lead-Singer")
l4_offer = l4.rpc.offer("any", "Drummer")
l5_offer = l5.rpc.offer("any", "Guitarist")
members_json = [
{
"description": "Lead-Singer",
"destination": l3_offer["bolt12"],
"split": 1.0,
"payout_threshold_msat": 500000,
},
{
"description": "Drummer",
"destination": l4_offer["bolt12"],
"split": 1.0,
},
{
"description": "Guitarist",
"destination": l5_offer["bolt12"],
"split": 1,
},
]
prism1_description = "prism1"
prism = l2.rpc.call(
"prism-create", {"members": members_json, "description": prism1_description}
)
prism1_id = prism["prism_id"]
l2_offer = l2.rpc.offer("any", "Prism")
l2.rpc.call(
"prism-addbinding",
{"offer_id": l2_offer["offer_id"], "prism_id": prism1_id},
)
invoice = l1.rpc.fetchinvoice(l2_offer["bolt12"], 1_000_000)
l1.rpc.pay(invoice["invoice"])
wait_for(
lambda: l4.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 300_000
)
wait_for(
lambda: l5.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 300_000
)
# hold, CI can be slow
time.sleep(5)
assert l3.rpc.listpeerchannels()["channels"][0]["to_us_msat"] == 0
invoice = l1.rpc.fetchinvoice(l2_offer["bolt12"], 1_000_000)
l1.rpc.pay(invoice["invoice"])
wait_for(
lambda: l3.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 600_000
)
wait_for(
lambda: l4.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 600_000
)
wait_for(
lambda: l5.rpc.listpeerchannels()["channels"][0]["to_us_msat"] > 600_000
)
def test_update_outlay(node_factory, bitcoind):
l1, l2, l3, l4, l5 = node_factory.get_nodes(
5, opts={"experimental-offers": None}
)
nodes = [l1, l2, l3, l4, l5]
# fund nodes, create channels, l2 is the prism
#
# -> l3
# l1 -> l2 -> l4
# -> l5
#
l1.fundwallet(10_000_000)
l2.fundwallet(10_000_000)
l1.rpc.fundchannel(l2.info["id"] + "@localhost:" + str(l2.port), 1_000_000)
l2.rpc.fundchannel(l3.info["id"] + "@localhost:" + str(l3.port), 1_000_000)
bitcoind.generate_block(1)
sync_blockheight(bitcoind, nodes)
l2.rpc.fundchannel(l4.info["id"] + "@localhost:" + str(l4.port), 1_000_000)
bitcoind.generate_block(1)
sync_blockheight(bitcoind, nodes)
l2.rpc.fundchannel(l5.info["id"] + "@localhost:" + str(l5.port), 1_000_000)
bitcoind.generate_block(6)
sync_blockheight(bitcoind, nodes)
l2.rpc.plugin_start(plugin_path)
l3_offer = l3.rpc.offer("any", "Lead-Singer")
l4_offer = l4.rpc.offer("any", "Drummer")
l5_offer = l5.rpc.offer("any", "Guitarist")
members_json = [
{
"description": "Lead-Singer",
"destination": l3_offer["bolt12"],
"split": 1,
"payout_threshold_msat": 500000,
},
{
"description": "Drummer",
"destination": l4_offer["bolt12"],
"split": 1.0,
},
{
"description": "Guitarist",
"destination": l5_offer["bolt12"],
"split": 1.0,
},
]
prism1_description = "prism1"
prism = l2.rpc.call(
"prism-create", {"members": members_json, "description": prism1_description}
)
prism1_id = prism["prism_id"]
# generate offer and bind to it
l2_offer = l2.rpc.offer("any", "Prism")
binding = l2.rpc.call(
"prism-addbinding",
{"offer_id": l2_offer["offer_id"], "prism_id": prism1_id},
)
lead_singer_id = [member["member_id"] for member in binding["prism_members"] if member["description"] == "Lead-Singer"][0]
assert lead_singer_id is not None, "Lead-Singer not found in prism"
new_outlay_msat = 1000000
# just updates a single member's outlay
updated_binding = l2.rpc.call("prism-setoutlay", {"offer_id": l2_offer["offer_id"], "member_id": lead_singer_id, "new_outlay_msat": new_outlay_msat})["bolt12_prism_bindings"]
print(f"updated_binding: {updated_binding}")
assert updated_binding["member_outlays"][0]["outlay_msat"] == new_outlay_msat, "Outlay not updated correctly"
sanity_check = l2.rpc.call("prism-listbindings", {"offer_id": l2_offer["offer_id"]})["bolt12_prism_bindings"]
assert sanity_check["member_outlays"][0]["outlay_msat"] == new_outlay_msat, "Outlay not updated in DB correctly"