From 1a5de7bbc6f628789ddff2d4f087a15dbafca30a Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Wed, 30 Mar 2022 23:33:40 +0200 Subject: [PATCH] bolt2: introduce the coop-close channel tests Signed-off-by: Vincenzo Palazzo --- tests/test_bolt2-01-close_channel.py | 102 +++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/test_bolt2-01-close_channel.py diff --git a/tests/test_bolt2-01-close_channel.py b/tests/test_bolt2-01-close_channel.py new file mode 100644 index 0000000..8669c23 --- /dev/null +++ b/tests/test_bolt2-01-close_channel.py @@ -0,0 +1,102 @@ +#! /usr/bin/env python3 +""" +testing bolt2 closing channel operation described in the lightning network speck +https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#channel-close + +The overview of what we test in this integration testing is described by the following +figure. + + +-------+ +-------+ + | |--(1)----- shutdown ------->| | + | |<-(2)----- shutdown --------| | + | | | | + | | | | + | A | ... | B | + | | | | + | |--(3)-- closing_signed F1--->| | + | |<-(4)-- closing_signed F2----| | + | | ... | | + | |--(?)-- closing_signed Fn--->| | + | |<-(?)-- closing_signed Fn----| | + +-------+ +-------+ + + author: https://github.com/vincenzopalazzo +""" +import hashlib + +from bitcoin.core import CScript, Hash160 +from bitcoin.core.script import OP_0 +from bitcoin.wallet import CBitcoinSecret +from lnprototest import ( + ExpectMsg, + Msg, + Runner, + TryAll, + OneOf, +) +from helpers import run_runner, merge_events_sequences, tx_spendable +from lnprototest.stash import channel_id +from spec_helper import open_and_announce_channel_helper, connect_to_node_helper + + +def test_close_channel_shutdown_msg_normal_case(runner: Runner) -> None: + """Close the channel with the other peer, and check if the + shutdown message works in the expected way.""" + + # test preconditions. + # the option that the helper method feel for us + test_opts = {} + pre_events_conn = connect_to_node_helper( + runner, tx_spendable=tx_spendable, conn_privkey="03" + ) + pre_events = open_and_announce_channel_helper( + runner, conn_privkey="03", opts=test_opts + ) + # merge the two events + pre_events = merge_events_sequences(pre_events_conn, pre_events) + channel_idx = channel_id() + + # TODO: refactoring this + h = hashlib.sha256(b"correct horse battery staple").digest() + seckey = CBitcoinSecret.from_secret_bytes(h) + # Create an address from that private key. + script = CScript([OP_0, Hash160(seckey.pub)]).hex() + + test = [ + # runner sent shutdown message to the ln implementation + TryAll( + [ + Msg( + "shutdown", + channel_id=channel_idx, + scriptpubkey=script, + ), + # FIXME: when this have sense? when there is not update? + # from c-lightning code we update the channel state + # so I'm assuming that this will trigger a new channel_update + # from gossip + OneOf( + # channel_update to change the state of the channel from + # NORMAL -> SHUTDOWN + [ + ExpectMsg("channel_update"), + ExpectMsg("shutdown", channel_id=channel_idx), + ], + # channel_update to change the state of the connection from + # NORMAL -> SHUTDOWN -> ONCHAIN + [ + ExpectMsg("channel_update"), + ExpectMsg("channel_update"), + ExpectMsg("shutdown", channel_id=channel_idx), + ], + # No channel_update received; this means that there is now pending update? + [ExpectMsg("shutdown", channel_id=channel_idx)], + ), + # FIXME: We should not be able to ping the node right? + ], + # ln implementation sent the shutdown message + # TODO: to be implemented. + [], + ), + ] + run_runner(runner, merge_events_sequences(pre=pre_events, post=test))