Skip to content

Commit

Permalink
test_lnpeer: test_reestablish_with_old_state
Browse files Browse the repository at this point in the history
  • Loading branch information
ecdsa committed Feb 12, 2020
1 parent 3377627 commit e3630d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions electrum/lnpeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,8 @@ def send_revoke_and_ack(self, chan: Channel):
def on_commitment_signed(self, payload):
channel_id = payload['channel_id']
chan = self.channels[channel_id]
if chan.peer_state == peer_states.BAD:
return
# make sure there were changes to the ctx, otherwise the remote peer is misbehaving
next_htlcs, latest_htlcs = chan.hm.get_htlcs_in_next_ctx(LOCAL), chan.hm.get_htlcs_in_latest_ctx(LOCAL)
self.logger.info(f'on_commitment_signed. chan {chan.short_channel_id}. ctn: {chan.get_next_ctn(LOCAL)}. '
Expand Down Expand Up @@ -1376,6 +1378,8 @@ async def fail_htlc(self, chan: Channel, htlc_id: int, onion_packet: OnionPacket
def on_revoke_and_ack(self, payload):
channel_id = payload["channel_id"]
chan = self.channels[channel_id]
if chan.peer_state == peer_states.BAD:
return
self.logger.info(f'on_revoke_and_ack. chan {chan.short_channel_id}. ctn: {chan.get_oldest_unrevoked_ctn(REMOTE)}')
rev = RevokeAndAck(payload["per_commitment_secret"], payload["next_per_commitment_point"])
chan.receive_revocation(rev)
Expand Down
30 changes: 27 additions & 3 deletions electrum/tests/test_lnpeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ def save_db(self):

class MockLNWallet:
def __init__(self, remote_keypair, local_keypair, chan, tx_queue):
self.chan = chan
self.remote_keypair = remote_keypair
self.node_keypair = local_keypair
self.network = MockNetwork(tx_queue)
self.channels = {self.chan.channel_id: self.chan}
self.channels = {chan.channel_id: chan}
self.payments = {}
self.logs = defaultdict(list)
self.wallet = MockWallet()
Expand Down Expand Up @@ -175,7 +174,6 @@ def setUpClass(cls):
def setUp(self):
super().setUp()
self.asyncio_loop, self._stop_loop, self._loop_thread = create_and_start_event_loop()
self.alice_channel, self.bob_channel = create_test_channels()

def tearDown(self):
super().tearDown()
Expand Down Expand Up @@ -220,6 +218,7 @@ def prepare_invoice(w2 # receiver
return lnencode(lnaddr, w2.node_keypair.privkey)

def test_reestablish(self):
self.alice_channel, self.bob_channel = create_test_channels()
p1, p2, w1, w2, _q1, _q2 = self.prepare_peers()
async def reestablish():
await asyncio.gather(
Expand All @@ -234,7 +233,31 @@ async def f():
with self.assertRaises(concurrent.futures.CancelledError):
run(f())

def test_reestablish_with_old_state(self):
self.alice_channel, self.bob_channel = create_test_channels()
self.alice_channel_0, self.bob_channel_0 = create_test_channels() # these are identical
p1, p2, w1, w2, _q1, _q2 = self.prepare_peers()
pay_req = self.prepare_invoice(w2)
async def reestablish():
result = await LNWallet._pay(w1, pay_req)
self.assertEqual(result, True)
w1.channels = {self.alice_channel_0.channel_id: self.alice_channel_0}
await asyncio.gather(
p1.reestablish_channel(self.alice_channel_0),
p2.reestablish_channel(self.bob_channel))
self.assertEqual(self.alice_channel_0.peer_state, peer_states.BAD)
self.assertEqual(self.bob_channel._state, channel_states.FORCE_CLOSING)
# wait so that pending messages are processed
#await asyncio.sleep(1)
gath.cancel()
gath = asyncio.gather(reestablish(), p1._message_loop(), p2._message_loop())
async def f():
await gath
with self.assertRaises(concurrent.futures.CancelledError):
run(f())

def test_payment(self):
self.alice_channel, self.bob_channel = create_test_channels()
p1, p2, w1, w2, _q1, _q2 = self.prepare_peers()
pay_req = self.prepare_invoice(w2)
async def pay():
Expand All @@ -248,6 +271,7 @@ async def f():
run(f())

def test_channel_usage_after_closing(self):
self.alice_channel, self.bob_channel = create_test_channels()
p1, p2, w1, w2, q1, q2 = self.prepare_peers()
pay_req = self.prepare_invoice(w2)

Expand Down

0 comments on commit e3630d8

Please sign in to comment.