Skip to content

Commit

Permalink
feat: Enable stashing of expected messages
Browse files Browse the repository at this point in the history
There are cases where we need to access the message that was
just decoded within the `ExpectMsg` event.

This commit adds a generic method to the stash module,
allowing retrieval of information from the message.

Furthermore, starting from this point, lnprototest will stash
all messages that are decoded using `ExpectMsg`. This enhancement
simplifies cross-compatibility efforts with other implementations.

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Jun 1, 2023
1 parent dea47c2 commit 251a9f7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lnprototest/dummyrunner.py
Expand Up @@ -49,7 +49,7 @@ def has_option(self, optname: str) -> Optional[str]:
def start(self) -> None:
self.blockheight = 102

def stop(self) -> None:
def stop(self, print_logs: bool = False) -> None:
pass

def restart(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions lnprototest/event.py
Expand Up @@ -346,6 +346,7 @@ def action(self, runner: "Runner") -> bool:
# Might be completely unknown to namespace.
try:
msg = Message.read(namespace(), io.BytesIO(binmsg))
runner.add_stash(msg.messagetype.name, msg)
except ValueError as ve:
raise EventError(
self, "Runner gave bad msg {}: {}".format(binmsg.hex(), ve)
Expand Down
23 changes: 1 addition & 22 deletions lnprototest/stash/__init__.py
Expand Up @@ -28,26 +28,5 @@
htlc_sigs_to_recv,
locking_script,
witnesses,
stash_field_from_event,
)


__all__ = [
"commitsig_to_send",
"commitsig_to_recv",
"htlc_sigs_to_send",
"htlc_sigs_to_recv",
"channel_id",
"channel_announcement",
"channel_update",
"get_member",
"rcvd",
"sent",
"funding_amount",
"funding_pubkey",
"funding_tx",
"funding_txid",
"funding",
"funding_close_tx",
"locking_script",
"witnesses",
]
11 changes: 11 additions & 0 deletions lnprototest/stash/stash.py
Expand Up @@ -273,3 +273,14 @@ def _funding_close_tx(runner: Runner, event: Event, field: str) -> str:
return runner.get_stash(event, "Funding").close_tx()

return _funding_close_tx


def stash_field_from_event(stash_key: str) -> Callable[[Runner, Event, str], str]:
"""Generic stash function to get the information back from a previous event"""

def _stash_field_from_event(runner: Runner, event: Event, field: str) -> str:
if runner._is_dummy():
return "0"
return runner.get_stash(event, stash_key).fields[field]

return _stash_field_from_event
5 changes: 3 additions & 2 deletions tests/test_bolt2-01-open_channel.py
Expand Up @@ -33,6 +33,7 @@
funding_txid,
funding_tx,
funding,
stash_field_from_event,
)
from helpers import (
utxo,
Expand Down Expand Up @@ -110,7 +111,7 @@ def test_open_channel_from_accepter_side(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"),
channel_reserve_satoshis=9998,
),
# Ignore unknown odd messages
Expand Down Expand Up @@ -203,7 +204,7 @@ def test_open_channel_opener_side(runner: Runner) -> None:
max_htlc_value_in_flight_msat=4294967295,
channel_reserve_satoshis=9998,
htlc_minimum_msat=0,
minimum_depth=3,
minimum_depth=stash_field_from_event("accept_channel"),
max_accepted_htlcs=483,
# We use 5, because c-lightning runner uses 6, so this is different.
to_self_delay=5,
Expand Down

0 comments on commit 251a9f7

Please sign in to comment.