Skip to content

Commit

Permalink
bolt2: introduce the coop-close channel tests
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Mar 31, 2022
1 parent 6629da3 commit 1a5de7b
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions tests/test_bolt2-01-close_channel.py
Original file line number Diff line number Diff line change
@@ -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 --------| |
| | | |
| | <complete all pending HTLCs> | |
| 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))

0 comments on commit 1a5de7b

Please sign in to comment.