Skip to content

events: throw an exception if the peer did not disconnect #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lnprototest/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,23 +566,28 @@ def action(self, runner: "Runner") -> bool:


class ExpectDisconnect(PerConnEvent):
"""This is considerer an hack, because the protocol
is not specifing what fail the connection means, so in
some case a node close the connection without any message
or in a local env the connection got close before sending
out the msg, so this event is the simplest way to work around
this current issue."""
"""
This is considered a hack because the protocol does not specify what failing the connection means.
In some cases, a node closes the connection without any message, or in a local environment, the
connection may close before sending out the message. This event is the simplest way to work around
this current issue.
"""

def __init__(self, connprivkey: Optional[str] = None):
super().__init__(connprivkey)

def action(self, runner: "Runner") -> bool:
super().action(runner)
if runner._is_dummy():
return True
msg = runner.check_error(self, self.find_conn(runner))
logging.info(f"expecting disconnection: `{msg}`")
# in this case of the dummy runner we return a `Dummy error`
# but in this case we wan receive an None value
# because the connected got close before
return msg is None or runner._is_dummy()
if msg is None:
return True
raise EventError(self, "Peer did not disconnect")


class CheckEq(Event):
Expand Down
20 changes: 16 additions & 4 deletions tests/test_bolt1-02-unknown-messages.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#! /usr/bin/env python3
# Init exchange, with unknown messages
#
import pyln.spec.bolt1

from typing import Any

from lnprototest import TryAll, Connect, ExpectMsg, Msg, RawMsg, Runner
from lnprototest.event import ExpectDisconnect
import pyln.spec.bolt1
from typing import Any
from lnprototest.utils import run_runner


def test_unknowns(runner: Runner, namespaceoverride: Any) -> None:
Expand All @@ -20,6 +22,17 @@ def test_unknowns(runner: Runner, namespaceoverride: Any) -> None:
# - upon receiving a message of _odd_, unknown type:
# - MUST ignore the received message.
RawMsg(bytes.fromhex("270F")),
]
run_runner(runner, test)


def test_unknowns_even_message(runner: Runner, namespaceoverride: Any) -> None:
# We override default namespace since we only need BOLT1
namespaceoverride(pyln.spec.bolt1.namespace)
test = [
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
# BOLT #1:
# A receiving node:...
# - upon receiving a message of _even_, unknown type:
Expand All @@ -28,5 +41,4 @@ def test_unknowns(runner: Runner, namespaceoverride: Any) -> None:
RawMsg(bytes.fromhex("2710")),
ExpectDisconnect(),
]

runner.run(test)
run_runner(runner, test)
Loading