Skip to content

Commit

Permalink
bolt2: add multiple shutdown with different script
Browse files Browse the repository at this point in the history
Changelog-Added: bolt2: add multiple `shutdown` with different script

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 26, 2022
1 parent 4b15032 commit 31f7da7
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 45 deletions.
12 changes: 9 additions & 3 deletions lnprototest/utils/bitcoin_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import hashlib
from enum import Enum

from bitcoin.core import CScript, Hash160
from bitcoin.core.script import OP_0, OP_CHECKSIG
from bitcoin.core import Hash160, x
from bitcoin.core.script import OP_0, OP_CHECKSIG, CScript
from bitcoin.wallet import CBitcoinSecret


Expand Down Expand Up @@ -40,11 +40,17 @@ def blockchain_hash() -> str:
@staticmethod
def build_valid_script(
script_type: ScriptType = ScriptType.VALID_CLOSE_SCRIPT,
word: str = "lnprototest",
) -> str:
"""Build a valid bitcoin script and hide the primitive of the library"""
h = hashlib.sha256(b"correct horse battery staple").digest()
secret_str = f"correct horse battery staple {word}"
h = hashlib.sha256(secret_str.encode("ascii")).digest()
seckey = CBitcoinSecret.from_secret_bytes(h)
if script_type is ScriptType.VALID_CLOSE_SCRIPT:
return CScript([OP_0, Hash160(seckey.pub)]).hex()
elif script_type is ScriptType.INVALID_CLOSE_SCRIPT:
return CScript([seckey.pub, OP_CHECKSIG]).hex()

@staticmethod
def build_script(hex: str) -> CScript:
return CScript(x(hex))
128 changes: 86 additions & 42 deletions tests/test_bolt2-01-close_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
ExpectMsg,
Msg,
Runner,
TryAll,
MustNotMsg,
)
from lnprototest.namespace import peer_message_namespace
Expand Down Expand Up @@ -69,24 +68,27 @@ def test_close_channel_shutdown_msg_normal_case_received_side(
channel_idx = channel_id()

script = BitcoinUtils.build_valid_script()

test = [
# runner sent shutdown message to the ln implementation
# BOLT 2:
# - MUST NOT send an `update_add_htlc` after a shutdown.
TryAll(
[
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script,
),
MustNotMsg("update_add_htlc"),
# TODO: must be -> ExpectMsg("shutdown", channel_id=channel_idx, scriptpubkey=script),
# why the script is different
ExpectMsg("shutdown", channel_id=channel_idx),
],
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script,
),
MustNotMsg("update_add_htlc"),
# TODO: must be -> ExpectMsg("shutdown", channel_id=channel_idx, scriptpubkey=script),
# why the script is different
ExpectMsg("shutdown", channel_id=channel_idx),
# TODO: including in bitcoin function the possibility to sign this values
# Msg(
# "closing_signed",
# channel_id=channel_idx,
# fee_satoshis='100',
# signature="0000",
# ),
# ExpectMsg("closing_signed")
]
run_runner(runner, merge_events_sequences(pre=pre_events, post=test))

Expand Down Expand Up @@ -119,18 +121,14 @@ def test_close_channel_shutdown_msg_wrong_script_pubkey_received_side(

test = [
# runner sent shutdown message to the ln implementation
TryAll(
[
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script,
),
MustNotMsg("shutdown"),
MustNotMsg("add_htlc"),
# FIXME: add support for warning messages in pyln package?
],
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script,
),
MustNotMsg("shutdown"),
MustNotMsg("add_htlc"),
# FIXME: add support for warning messages in pyln package?
]
run_runner(runner, merge_events_sequences(pre=pre_events, post=test))

Expand Down Expand Up @@ -165,23 +163,69 @@ def test_close_channel_allow_multiple_shutdown_msg_receive_side(

test = [
# runner sent shutdown message to the ln implementation
TryAll(
[
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script,
),
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script,
),
MustNotMsg("update_add_htlc"),
# TODO: must be -> ExpectMsg("shutdown", channel_id=channel_idx, scriptpubkey=script),
# why the script is different
ExpectMsg("shutdown", channel_id=channel_idx),
],
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script,
),
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script,
),
MustNotMsg("update_add_htlc"),
# TODO: must be -> ExpectMsg("shutdown", channel_id=channel_idx, scriptpubkey=script),
# why the script is different
ExpectMsg("shutdown", channel_id=channel_idx),
]
run_runner(runner, merge_events_sequences(pre=pre_events, post=test))


def test_close_channel_allow_multiple_shutdown_msg_with_diff_script_receive_side(
runner: Runner, namespaceoverride: Any
) -> None:
"""
Close operation from the receiver point of view, where the receiver will
receive multiple shutdown msg.
FIXME: this need to be allowed by the spec!
________________________________
| runner -> shutdown -> ln-node |
| runner -> shutdown -> ln-node |
| runner <- shutdown <- ln-node |
--------------------------------
"""
namespaceoverride(peer_message_namespace())
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()

script_one = BitcoinUtils.build_valid_script()
script_two = BitcoinUtils.build_valid_script(word="doubled")
assert script_one != script_two

test = [
# runner sent shutdown message to the ln implementation
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script_one,
),
Msg(
"shutdown",
channel_id=channel_idx,
scriptpubkey=script_two,
),
MustNotMsg("update_add_htlc"),
# TODO: must be -> ExpectMsg("shutdown", channel_id=channel_idx, scriptpubkey=script_two),
# why the script is different
ExpectMsg("shutdown", channel_id=channel_idx),
]
run_runner(runner, merge_events_sequences(pre=pre_events, post=test))

0 comments on commit 31f7da7

Please sign in to comment.