test_lnpeer: factorize test_reestablish_replay_messages
What changed, and why it matters
This commit is a simple code cleanup in Electrum's test suite. It merges two nearly identical Lightning network reconnection tests into one shared helper method, removing duplicated code. No production code was changed, and nothing about the actual wallet or payment behavior is affected.
No action needed. This is a non-functional test refactor with no security relevance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors tests/test_lnpeer.py by extracting the common body of test_reestablish_replay_messages_rev_then_sig and test_reestablish_replay_messages_sig_then_rev into a new private helper _test_reestablish_replay_messages(rev_then_sig: bool). The two public test methods now only contain their docstrings and a one-line call to the helper. The logic, assertions, and scenario diagrams remain unchanged; only duplicated test scaffolding was removed.
Changed components
tests/test_lnpeer.pyInspect captured patch +30 / −60
diff --git a/tests/test_lnpeer.py b/tests/test_lnpeer.py
index ed73385..af0716a 100644
--- a/tests/test_lnpeer.py
+++ b/tests/test_lnpeer.py
@@ -728,23 +728,7 @@ class TestPeerDirect(TestPeer):
)
return htlc
- async def test_reestablish_replay_messages_rev_then_sig(self):
- """
- See https://github.com/lightning/bolts/pull/810#issue-728299277
-
- Rev then Sig
- A B
- <---add-----
- ----add---->
- <---sig-----
- ----rev----x
- ----sig----x
-
- A needs to retransmit:
- ----rev--> (note that 'add' can be first too)
- ----add-->
- ----sig-->
- """
+ async def _test_reestablish_replay_messages(self, rev_then_sig: bool):
alice_lnwallet, bob_lnwallet = self.prepare_lnwallets(self.GRAPH_DEFINITIONS['single_chan']).values()
chan_AB, chan_BA = create_test_channels(alice_lnwallet=alice_lnwallet, bob_lnwallet=bob_lnwallet)
# note: we don't start peer.htlc_switch() so that the fake htlcs are left alone.
@@ -758,8 +742,13 @@ class TestPeerDirect(TestPeer):
self._send_fake_htlc(p2, chan_BA)
self._send_fake_htlc(p1, chan_AB)
p2.transport.queue.put_nowait(asyncio.Event()) # break Bob's incoming pipe
- self.assertTrue(p2.maybe_send_commitment(chan_BA))
- await p1.received_commitsig_event.wait()
+ if rev_then_sig:
+ self.assertTrue(p2.maybe_send_commitment(chan_BA))
+ await p1.received_commitsig_event.wait()
+ else:
+ self.assertTrue(p1.maybe_send_commitment(chan_AB))
+ self.assertTrue(p2.maybe_send_commitment(chan_BA))
+ await p1.received_commitsig_event.wait()
await group.cancel_remaining()
# simulating disconnection. recreate transports.
self.logger.info("simulating disconnection. recreating transports.")
@@ -773,8 +762,8 @@ class TestPeerDirect(TestPeer):
async with OldTaskGroup() as group2:
await group2.spawn(p1.reestablish_channel(chan_AB))
await group2.spawn(p2.reestablish_channel(chan_BA))
- self.assertTrue(any(("alice->bob" in msg and
- "replaying a revoke_and_ack first" in msg) for msg in logs.output))
+ s = "replaying a revoke_and_ack " + ("first" if rev_then_sig else "last")
+ self.assertTrue(any(("alice->bob" in msg and s in msg) for msg in logs.output))
self.assertTrue(any(("alice->bob" in msg and
"replayed 2 unacked messages. ['update_add_htlc', 'commitment_signed']" in msg) for msg in logs.output))
self.assertEqual(chan_AB.peer_state, PeerState.GOOD)
@@ -784,6 +773,25 @@ class TestPeerDirect(TestPeer):
with self.assertRaises(SuccessfulTest):
await f()
+ async def test_reestablish_replay_messages_rev_then_sig(self):
+ """
+ See https://github.com/lightning/bolts/pull/810#issue-728299277
+
+ Rev then Sig
+ A B
+ <---add-----
+ ----add---->
+ <---sig-----
+ ----rev----x
+ ----sig----x
+
+ A needs to retransmit:
+ ----rev--> (note that 'add' can be first too)
+ ----add-->
+ ----sig-->
+ """
+ await self._test_reestablish_replay_messages(True)
+
async def test_reestablish_replay_messages_sig_then_rev(self):
"""
See https://github.com/lightning/bolts/pull/810#issue-728299277
@@ -801,45 +809,7 @@ class TestPeerDirect(TestPeer):
----sig-->
----rev-->
"""
- alice_lnwallet, bob_lnwallet = self.prepare_lnwallets(self.GRAPH_DEFINITIONS['single_chan']).values()
- chan_AB, chan_BA = create_test_channels(alice_lnwallet=alice_lnwallet, bob_lnwallet=bob_lnwallet)
- # note: we don't start peer.htlc_switch() so that the fake htlcs are left alone.
- async def f():
- p1, p2, w1, w2 = self.prepare_peers(chan_AB, chan_BA)
- async with OldTaskGroup() as group:
- await group.spawn(p1._message_loop())
- await group.spawn(p2._message_loop())
- await p1.initialized
- await p2.initialized
- self._send_fake_htlc(p2, chan_BA)
- self._send_fake_htlc(p1, chan_AB)
- p2.transport.queue.put_nowait(asyncio.Event()) # break Bob's incoming pipe
- self.assertTrue(p1.maybe_send_commitment(chan_AB))
- self.assertTrue(p2.maybe_send_commitment(chan_BA))
- await p1.received_commitsig_event.wait()
- await group.cancel_remaining()
- # simulating disconnection. recreate transports.
- self.logger.info("simulating disconnection. recreating transports.")
- p1, p2, w1, w2 = self.prepare_peers(chan_AB, chan_BA)
- for chan in (chan_AB, chan_BA):
- chan.peer_state = PeerState.DISCONNECTED
- async with OldTaskGroup() as group:
- await group.spawn(p1._message_loop())
- await group.spawn(p2._message_loop())
- with self.assertLogs('electrum', level='INFO') as logs:
- async with OldTaskGroup() as group2:
- await group2.spawn(p1.reestablish_channel(chan_AB))
- await group2.spawn(p2.reestablish_channel(chan_BA))
- self.assertTrue(any(("alice->bob" in msg and
- "replaying a revoke_and_ack last" in msg) for msg in logs.output))
- self.assertTrue(any(("alice->bob" in msg and
- "replayed 2 unacked messages. ['update_add_htlc', 'commitment_signed']" in msg) for msg in logs.output))
- self.assertEqual(chan_AB.peer_state, PeerState.GOOD)
- self.assertEqual(chan_BA.peer_state, PeerState.GOOD)
- await group.cancel_remaining()
- raise SuccessfulTest()
- with self.assertRaises(SuccessfulTest):
- await f()
+ await self._test_reestablish_replay_messages(False)
async def _test_simple_payment(
self,
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.