Skip to content

Commit e37306f

Browse files
committed
using runner_features and stash_field_from_event inside tests
runner_features provides the features required by an implementation to Lnprototest. stash_field_from_event allows retrieval of information from the message that was just decoded within the `ExpectMsg` event. Using these methods will make Lnprototest more generalised and compatible with other lightning implementations.
1 parent c9e78d9 commit e37306f

8 files changed

+257
-72
lines changed

tests/test_bolt1-01-init.py

Lines changed: 135 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ def test_echo_init(runner: Runner, namespaceoverride: Any) -> None:
8888
test = [
8989
Connect(connprivkey="03"),
9090
ExpectMsg("init"),
91-
Msg("init", globalfeatures="", features=""),
91+
Msg(
92+
"init",
93+
globalfeatures=runner.runner_features(globals=True),
94+
features=runner.runner_features(),
95+
),
9296
# optionally disconnect that first one
9397
Connect(connprivkey="02"),
9498
# You should always handle us echoing your own features back!
@@ -105,7 +109,11 @@ def test_echo_init_after_disconnect(runner: Runner, namespaceoverride: Any) -> N
105109
test = [
106110
Connect(connprivkey="03"),
107111
ExpectMsg("init"),
108-
Msg("init", globalfeatures="", features=""),
112+
Msg(
113+
"init",
114+
globalfeatures=runner.runner_features(globals=True),
115+
features=runner.runner_features(),
116+
),
109117
# optionally disconnect that first one
110118
Disconnect(),
111119
Connect(connprivkey="02"),
@@ -123,7 +131,13 @@ def test_init_check_received_msg(runner: Runner, namespaceoverride: Any) -> None
123131
sequences = [
124132
Connect(connprivkey="03"),
125133
ExpectMsg("init"),
126-
Msg("init", globalfeatures="", features=""),
134+
Msg(
135+
"init",
136+
globalfeatures=runner.runner_features(globals=True),
137+
features=runner.runner_features(),
138+
),
139+
# optionally disconnect that first one
140+
TryAll([], Disconnect()),
127141
Connect(connprivkey="02"),
128142
# Even if we don't send anything, it should send init.
129143
ExpectMsg("init", if_match=no_gf13),
@@ -137,13 +151,25 @@ def test_init_invalid_globalfeatures(runner: Runner, namespaceoverride: Any) ->
137151
sequences = [
138152
Connect(connprivkey="03"),
139153
ExpectMsg("init"),
140-
Msg("init", globalfeatures="", features=""),
154+
Msg(
155+
"init",
156+
globalfeatures=runner.runner_features(globals=True),
157+
features=runner.runner_features(),
158+
),
159+
# optionally disconnect that first one
160+
TryAll([], Disconnect()),
141161
Connect(connprivkey="02"),
142162
ExpectMsg("init", if_match=no_gf13),
143163
# BOLT #1:
144164
# The sending node:...
145165
# - SHOULD NOT set features greater than 13 in `globalfeatures`.
146-
Msg("init", globalfeatures=bitfield(99), features=""),
166+
Msg(
167+
"init",
168+
globalfeatures=runner.runner_features(
169+
globals=True, additional_features=[99]
170+
),
171+
features=runner.runner_features(),
172+
),
147173
]
148174
run_runner(runner, sequences)
149175

@@ -154,14 +180,24 @@ def test_init_is_first_msg(runner: Runner, namespaceoverride: Any) -> None:
154180
sequences = [
155181
Connect(connprivkey="03"),
156182
ExpectMsg("init"),
157-
Msg("init", globalfeatures="", features=""),
183+
Msg(
184+
"init",
185+
globalfeatures=runner.runner_features(globals=True),
186+
features=runner.runner_features(),
187+
),
188+
# optionally disconnect that first one
189+
TryAll([], Disconnect()),
158190
Connect(connprivkey="02"),
159191
# Minimal possible init message.
160192
# BOLT #1:
161193
# The sending node:
162194
# - MUST send `init` as the first Lightning message for any connection.
163195
ExpectMsg("init"),
164-
Msg("init", globalfeatures="", features=""),
196+
Msg(
197+
"init",
198+
globalfeatures=runner.runner_features(globals=True),
199+
features=runner.runner_features(),
200+
),
165201
]
166202
run_runner(runner, sequences)
167203

@@ -172,15 +208,25 @@ def test_init_check_free_featurebits(runner: Runner, namespaceoverride: Any) ->
172208
sequences = [
173209
Connect(connprivkey="03"),
174210
ExpectMsg("init"),
175-
Msg("init", globalfeatures="", features=""),
211+
Msg(
212+
"init",
213+
globalfeatures=runner.runner_features(globals=True),
214+
features=runner.runner_features(),
215+
),
216+
# optionally disconnect that first one
217+
TryAll([], Disconnect()),
176218
Connect(connprivkey="02"),
177219
ExpectMsg("init", if_match=functools.partial(no_feature, [98, 99])),
178220
# BOLT #1:
179221
# The receiving node:...
180222
# - upon receiving unknown _odd_ feature bits that are non-zero:
181223
# - MUST ignore the bit.
182224
# init msg with unknown odd local bit (99): no error
183-
Msg("init", globalfeatures="", features=bitfield(99)),
225+
Msg(
226+
"init",
227+
globalfeatures=runner.runner_features(globals=True),
228+
features=runner.runner_features(additional_features=[99]),
229+
),
184230
]
185231
run_runner(runner, sequences)
186232

@@ -193,14 +239,24 @@ def test_init_fail_connection_if_receive_an_even_unknown_featurebits(
193239
sequences = [
194240
Connect(connprivkey="03"),
195241
ExpectMsg("init"),
196-
Msg("init", globalfeatures="", features=""),
242+
Msg(
243+
"init",
244+
globalfeatures=runner.runner_features(globals=True),
245+
features=runner.runner_features(),
246+
),
247+
# optionally disconnect that first one
248+
TryAll([], Disconnect()),
197249
Connect(connprivkey="02"),
198250
# BOLT #1:
199251
# The receiving node: ...
200252
# - upon receiving unknown _even_ feature bits that are non-zero:
201253
# - MUST fail the connection.
202254
ExpectMsg("init"),
203-
Msg("init", globalfeatures="", features=bitfield(98)),
255+
Msg(
256+
"init",
257+
globalfeatures=runner.runner_features(globals=True),
258+
features=runner.runner_features(additional_features=[98]),
259+
),
204260
ExpectDisconnect(),
205261
]
206262
run_runner(runner, sequences)
@@ -214,11 +270,23 @@ def test_init_fail_connection_if_receive_an_even_unknown_globalfeaturebits(
214270
sequences = [
215271
Connect(connprivkey="03"),
216272
ExpectMsg("init"),
217-
Msg("init", globalfeatures="", features=""),
273+
Msg(
274+
"init",
275+
globalfeatures=runner.runner_features(globals=True),
276+
features=runner.runner_features(),
277+
),
278+
# optionally disconnect that first one
279+
TryAll([], Disconnect()),
218280
Connect(connprivkey="02"),
219281
# init msg with unknown even global bit (98): you will error
220282
ExpectMsg("init"),
221-
Msg("init", globalfeatures=bitfield(98), features=""),
283+
Msg(
284+
"init",
285+
globalfeatures=runner.runner_features(
286+
globals=True, additional_features=[98]
287+
),
288+
features=runner.runner_features(),
289+
),
222290
ExpectDisconnect(),
223291
]
224292
run_runner(runner, sequences)
@@ -232,14 +300,24 @@ def test_init_fail_ask_for_option_data_loss_protect(
232300
sequences = [
233301
Connect(connprivkey="03"),
234302
ExpectMsg("init"),
235-
Msg("init", globalfeatures="", features=""),
303+
Msg(
304+
"init",
305+
globalfeatures=runner.runner_features(globals=True),
306+
features=runner.runner_features(),
307+
),
308+
# optionally disconnect that first one
309+
TryAll([], Disconnect()),
236310
Connect(connprivkey="02"),
237311
# If you don't support `option_data_loss_protect`, you will be ok if
238312
# we ask for it.
239313
Sequence(
240314
[
241315
ExpectMsg("init", if_match=functools.partial(no_feature, [0, 1])),
242-
Msg("init", globalfeatures="", features=bitfield(1)),
316+
Msg(
317+
"init",
318+
globalfeatures=runner.runner_features(globals=True),
319+
features=runner.runner_features(additional_features=[1]),
320+
),
243321
],
244322
enable=not runner.has_option("option_data_loss_protect"),
245323
),
@@ -255,7 +333,13 @@ def test_init_advertize_option_data_loss_protect(
255333
sequences = [
256334
Connect(connprivkey="03"),
257335
ExpectMsg("init"),
258-
Msg("init", globalfeatures="", features=""),
336+
Msg(
337+
"init",
338+
globalfeatures=runner.runner_features(globals=True),
339+
features=runner.runner_features(),
340+
),
341+
# optionally disconnect that first one
342+
TryAll([], Disconnect()),
259343
Connect(connprivkey="02"),
260344
# If you support `option_data_loss_protect`, you will advertize it odd.
261345
Sequence(
@@ -274,7 +358,13 @@ def test_init_required_option_data_loss_protect(
274358
sequences = [
275359
Connect(connprivkey="03"),
276360
ExpectMsg("init"),
277-
Msg("init", globalfeatures="", features=""),
361+
Msg(
362+
"init",
363+
globalfeatures=runner.runner_features(globals=True),
364+
features=runner.runner_features(),
365+
),
366+
# optionally disconnect that first one
367+
TryAll([], Disconnect()),
278368
Connect(connprivkey="02"),
279369
# If you require `option_data_loss_protect`, you will advertize it even.
280370
Sequence(
@@ -293,7 +383,13 @@ def test_init_reject_option_data_loss_protect_if_not_supported(
293383
sequences = [
294384
Connect(connprivkey="03"),
295385
ExpectMsg("init"),
296-
Msg("init", globalfeatures="", features=""),
386+
Msg(
387+
"init",
388+
globalfeatures=runner.runner_features(globals=True),
389+
features=runner.runner_features(),
390+
),
391+
# optionally disconnect that first one
392+
TryAll([], Disconnect()),
297393
Connect(connprivkey="02"),
298394
# If you don't support `option_anchor_outputs`, you will error if
299395
# we require it.
@@ -317,7 +413,13 @@ def test_init_advertize_option_anchor_outputs(
317413
sequences = [
318414
Connect(connprivkey="03"),
319415
ExpectMsg("init"),
320-
Msg("init", globalfeatures="", features=""),
416+
Msg(
417+
"init",
418+
globalfeatures=runner.runner_features(globals=True),
419+
features=runner.runner_features(),
420+
),
421+
# optionally disconnect that first one
422+
TryAll([], Disconnect()),
321423
Connect(connprivkey="02"),
322424
# If you support `option_anchor_outputs`, you will advertize it odd.
323425
Sequence(
@@ -336,7 +438,13 @@ def test_init_required_option_anchor_outputs(
336438
sequences = [
337439
Connect(connprivkey="03"),
338440
ExpectMsg("init"),
339-
Msg("init", globalfeatures="", features=""),
441+
Msg(
442+
"init",
443+
globalfeatures=runner.runner_features(globals=True),
444+
features=runner.runner_features(),
445+
),
446+
# optionally disconnect that first one
447+
TryAll([], Disconnect()),
340448
Connect(connprivkey="02"),
341449
# If you require `option_anchor_outputs`, you will advertize it even.
342450
Sequence(
@@ -355,7 +463,13 @@ def test_init_advertize_option_static_remotekey(
355463
sequences = [
356464
Connect(connprivkey="03"),
357465
ExpectMsg("init"),
358-
Msg("init", globalfeatures="", features=""),
466+
Msg(
467+
"init",
468+
globalfeatures=runner.runner_features(globals=True),
469+
features=runner.runner_features(),
470+
),
471+
# optionally disconnect that first one
472+
TryAll([], Disconnect()),
359473
Connect(connprivkey="02"),
360474
# BOLT-a12da24dd0102c170365124782b46d9710950ac1 #9:
361475
# | Bits | Name | ... | Dependencies

tests/test_bolt1-02-unknown-messages.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ def test_unknowns(runner: Runner, namespaceoverride: Any) -> None:
1616
test = [
1717
Connect(connprivkey="03"),
1818
ExpectMsg("init"),
19-
Msg("init", globalfeatures="", features=""),
19+
Msg(
20+
"init",
21+
globalfeatures=runner.runner_features(globals=True),
22+
features=runner.runner_features(),
23+
),
2024
# BOLT #1:
2125
# A receiving node:
2226
# - upon receiving a message of _odd_, unknown type:
@@ -32,7 +36,11 @@ def test_unknowns_even_message(runner: Runner, namespaceoverride: Any) -> None:
3236
test = [
3337
Connect(connprivkey="03"),
3438
ExpectMsg("init"),
35-
Msg("init", globalfeatures="", features=""),
39+
Msg(
40+
"init",
41+
globalfeatures=runner.runner_features(globals=True),
42+
features=runner.runner_features(),
43+
),
3644
# BOLT #1:
3745
# A receiving node:...
3846
# - upon receiving a message of _even_, unknown type:

tests/test_bolt2-10-add-htlc.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
funding,
3939
htlc_sigs_to_send,
4040
htlc_sigs_to_recv,
41+
stash_field_from_event,
4142
)
4243
from lnprototest.utils import pubkey_of
4344
from lnprototest.utils.bitcoin_utils import (
@@ -94,16 +95,10 @@ def test_htlc_add(runner: Runner) -> None:
9495
Block(blockheight=102, txs=[tx_spendable]),
9596
Connect(connprivkey="02"),
9697
ExpectMsg("init"),
97-
TryAll(
98-
Msg("init", globalfeatures="", features=bitfield(data_loss_protect)),
99-
Msg("init", globalfeatures="", features=bitfield(static_remotekey)),
100-
Msg(
101-
"init",
102-
globalfeatures="",
103-
features=bitfield(static_remotekey, anchor_outputs),
104-
),
105-
# And nothing.
106-
Msg("init", globalfeatures="", features=""),
98+
Msg(
99+
"init",
100+
globalfeatures=runner.runner_features(globals=True),
101+
features=runner.runner_features(),
107102
),
108103
Msg(
109104
"open_channel",
@@ -135,7 +130,7 @@ def test_htlc_add(runner: Runner) -> None:
135130
delayed_payment_basepoint=remote_delayed_payment_basepoint(),
136131
htlc_basepoint=remote_htlc_basepoint(),
137132
first_per_commitment_point=remote_per_commitment_point(0),
138-
minimum_depth=3,
133+
minimum_depth=stash_field_from_event("accept_channel", dummy_val=3),
139134
channel_reserve_satoshis=9998,
140135
),
141136
# Create and stash Funding object and FundingTx
@@ -171,7 +166,13 @@ def test_htlc_add(runner: Runner) -> None:
171166
"funding_signed", channel_id=channel_id(), signature=commitsig_to_recv()
172167
),
173168
# Mine it and get it deep enough to confirm channel.
174-
Block(blockheight=103, number=3, txs=[funding_tx()]),
169+
Block(
170+
blockheight=103,
171+
number=stash_field_from_event(
172+
"accept_channel", field_name="minimum_depth", dummy_val=3
173+
),
174+
txs=[funding_tx()],
175+
),
175176
ExpectMsg(
176177
"channel_ready",
177178
channel_id=channel_id(),

0 commit comments

Comments
 (0)