Skip to content

Commit

Permalink
using runner_features and stash_field_from_event inside tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Psycho-Pirate authored and vincenzopalazzo committed Dec 21, 2024
1 parent c9e78d9 commit ed8a06d
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 72 deletions.
156 changes: 135 additions & 21 deletions tests/test_bolt1-01-init.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ def test_echo_init(runner: Runner, namespaceoverride: Any) -> None:
test = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
Connect(connprivkey="02"),
# You should always handle us echoing your own features back!
Expand All @@ -105,7 +109,11 @@ def test_echo_init_after_disconnect(runner: Runner, namespaceoverride: Any) -> N
test = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
Disconnect(),
Connect(connprivkey="02"),
Expand All @@ -123,7 +131,13 @@ def test_init_check_received_msg(runner: Runner, namespaceoverride: Any) -> None
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# Even if we don't send anything, it should send init.
ExpectMsg("init", if_match=no_gf13),
Expand All @@ -137,13 +151,25 @@ def test_init_invalid_globalfeatures(runner: Runner, namespaceoverride: Any) ->
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
ExpectMsg("init", if_match=no_gf13),
# BOLT #1:
# The sending node:...
# - SHOULD NOT set features greater than 13 in `globalfeatures`.
Msg("init", globalfeatures=bitfield(99), features=""),
Msg(
"init",
globalfeatures=runner.runner_features(
globals=True, additional_features=[99]
),
features=runner.runner_features(),
),
]
run_runner(runner, sequences)

Expand All @@ -154,14 +180,24 @@ def test_init_is_first_msg(runner: Runner, namespaceoverride: Any) -> None:
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# Minimal possible init message.
# BOLT #1:
# The sending node:
# - MUST send `init` as the first Lightning message for any connection.
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
]
run_runner(runner, sequences)

Expand All @@ -172,15 +208,25 @@ def test_init_check_free_featurebits(runner: Runner, namespaceoverride: Any) ->
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
ExpectMsg("init", if_match=functools.partial(no_feature, [98, 99])),
# BOLT #1:
# The receiving node:...
# - upon receiving unknown _odd_ feature bits that are non-zero:
# - MUST ignore the bit.
# init msg with unknown odd local bit (99): no error
Msg("init", globalfeatures="", features=bitfield(99)),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(additional_features=[99]),
),
]
run_runner(runner, sequences)

Expand All @@ -193,14 +239,24 @@ def test_init_fail_connection_if_receive_an_even_unknown_featurebits(
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# BOLT #1:
# The receiving node: ...
# - upon receiving unknown _even_ feature bits that are non-zero:
# - MUST fail the connection.
ExpectMsg("init"),
Msg("init", globalfeatures="", features=bitfield(98)),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(additional_features=[98]),
),
ExpectDisconnect(),
]
run_runner(runner, sequences)
Expand All @@ -214,11 +270,23 @@ def test_init_fail_connection_if_receive_an_even_unknown_globalfeaturebits(
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# init msg with unknown even global bit (98): you will error
ExpectMsg("init"),
Msg("init", globalfeatures=bitfield(98), features=""),
Msg(
"init",
globalfeatures=runner.runner_features(
globals=True, additional_features=[98]
),
features=runner.runner_features(),
),
ExpectDisconnect(),
]
run_runner(runner, sequences)
Expand All @@ -232,14 +300,24 @@ def test_init_fail_ask_for_option_data_loss_protect(
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# If you don't support `option_data_loss_protect`, you will be ok if
# we ask for it.
Sequence(
[
ExpectMsg("init", if_match=functools.partial(no_feature, [0, 1])),
Msg("init", globalfeatures="", features=bitfield(1)),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(additional_features=[1]),
),
],
enable=not runner.has_option("option_data_loss_protect"),
),
Expand All @@ -255,7 +333,13 @@ def test_init_advertize_option_data_loss_protect(
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# If you support `option_data_loss_protect`, you will advertize it odd.
Sequence(
Expand All @@ -274,7 +358,13 @@ def test_init_required_option_data_loss_protect(
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# If you require `option_data_loss_protect`, you will advertize it even.
Sequence(
Expand All @@ -293,7 +383,13 @@ def test_init_reject_option_data_loss_protect_if_not_supported(
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# If you don't support `option_anchor_outputs`, you will error if
# we require it.
Expand All @@ -317,7 +413,13 @@ def test_init_advertize_option_anchor_outputs(
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# If you support `option_anchor_outputs`, you will advertize it odd.
Sequence(
Expand All @@ -336,7 +438,13 @@ def test_init_required_option_anchor_outputs(
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# If you require `option_anchor_outputs`, you will advertize it even.
Sequence(
Expand All @@ -355,7 +463,13 @@ def test_init_advertize_option_static_remotekey(
sequences = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# optionally disconnect that first one
TryAll([], Disconnect()),
Connect(connprivkey="02"),
# BOLT-a12da24dd0102c170365124782b46d9710950ac1 #9:
# | Bits | Name | ... | Dependencies
Expand Down
12 changes: 10 additions & 2 deletions tests/test_bolt1-02-unknown-messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def test_unknowns(runner: Runner, namespaceoverride: Any) -> None:
test = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# BOLT #1:
# A receiving node:
# - upon receiving a message of _odd_, unknown type:
Expand All @@ -32,7 +36,11 @@ def test_unknowns_even_message(runner: Runner, namespaceoverride: Any) -> None:
test = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
# BOLT #1:
# A receiving node:...
# - upon receiving a message of _even_, unknown type:
Expand Down
25 changes: 13 additions & 12 deletions tests/test_bolt2-10-add-htlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
funding,
htlc_sigs_to_send,
htlc_sigs_to_recv,
stash_field_from_event,
)
from lnprototest.utils import pubkey_of
from lnprototest.utils.bitcoin_utils import (
Expand Down Expand Up @@ -94,16 +95,10 @@ def test_htlc_add(runner: Runner) -> None:
Block(blockheight=102, txs=[tx_spendable]),
Connect(connprivkey="02"),
ExpectMsg("init"),
TryAll(
Msg("init", globalfeatures="", features=bitfield(data_loss_protect)),
Msg("init", globalfeatures="", features=bitfield(static_remotekey)),
Msg(
"init",
globalfeatures="",
features=bitfield(static_remotekey, anchor_outputs),
),
# And nothing.
Msg("init", globalfeatures="", features=""),
Msg(
"init",
globalfeatures=runner.runner_features(globals=True),
features=runner.runner_features(),
),
Msg(
"open_channel",
Expand Down Expand Up @@ -135,7 +130,7 @@ def test_htlc_add(runner: Runner) -> None:
delayed_payment_basepoint=remote_delayed_payment_basepoint(),
htlc_basepoint=remote_htlc_basepoint(),
first_per_commitment_point=remote_per_commitment_point(0),
minimum_depth=3,
minimum_depth=stash_field_from_event("accept_channel", dummy_val=3),
channel_reserve_satoshis=9998,
),
# Create and stash Funding object and FundingTx
Expand Down Expand Up @@ -171,7 +166,13 @@ def test_htlc_add(runner: Runner) -> None:
"funding_signed", channel_id=channel_id(), signature=commitsig_to_recv()
),
# Mine it and get it deep enough to confirm channel.
Block(blockheight=103, number=3, txs=[funding_tx()]),
Block(
blockheight=103,
number=stash_field_from_event(
"accept_channel", field_name="minimum_depth", dummy_val=3
),
txs=[funding_tx()],
),
ExpectMsg(
"channel_ready",
channel_id=channel_id(),
Expand Down
Loading

0 comments on commit ed8a06d

Please sign in to comment.