Skip to content

Commit

Permalink
lnprototest: refactoring the way to write a test
Browse files Browse the repository at this point in the history
Changelog-Fixed: pytest consider a exception an event that don't need to happens, and this keep alive all the subdeamon that lnprotetotest ran.We fixed with a try-catch-


Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Mar 24, 2022
1 parent f34aac0 commit 7f1ba78
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 277 deletions.
4 changes: 4 additions & 0 deletions lnprototest/funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ def from_utxo(
tx.wit = CTxWitness([CTxInWitness(CScriptWitness([sig, inkey_pub.format()]))])
return funding, tx.serialize().hex()

def script_pub_key(self) -> str:
"""Return the script pub key"""
return CScript([script.OP_0, sha256(self.redeemscript()).digest()]).hex()

def channel_id(self) -> str:
# BOLT #2: This message introduces the `channel_id` to identify the
# channel. It's derived from the funding transaction by combining the
Expand Down
9 changes: 0 additions & 9 deletions lnprototest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ def __init__(self, config: Any):
else:
self.logger.setLevel(logging.INFO)

def __enter__(self) -> "Runner":
"""Call the method when enter inside the class the first time"""
self.start()
return self

def __del__(self):
"""Call the method when the class is out of scope"""
self.stop()

def _is_dummy(self) -> bool:
"""The DummyRunner returns True here, as it can't do some things"""
return False
Expand Down
27 changes: 25 additions & 2 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bitcoin.core
import coincurve
from typing import Tuple
from lnprototest import privkey_expand, KeySet
from typing import Tuple, Union, Sequence, List
from lnprototest import privkey_expand, KeySet, Runner, Event

# Here are the keys to spend funds, derived from BIP32 seed
# `0000000000000000000000000000000000000000000000000000000000000001`:
Expand Down Expand Up @@ -145,3 +145,26 @@ def gen_random_keyset(counter: int = 20) -> KeySet:
delayed_payment_base_secret=f"{counter + 4}",
shachain_seed="00" * 32,
)


def run_runner(runner: Runner, test: Union[Sequence, List[Event], Event]) -> None:
"""
The pytest using the assertion as safe failure, and the exception it is only
an event that must not happen.
From design, lnprototest fails with an exception, and for this reason, if the
lnprototest throws an exception, we catch it, and we fail with an assent.
"""
try:
runner.run(test)
except Exception as ex:
runner.stop()
assert False, ex


def merge_events_sequences(
pre: Union[Sequence, List[Event], Event], post: Union[Sequence, List[Event], Event]
) -> Union[Sequence, List[Event], Event]:
"""Merge the two list in the pre-post order"""
pre.extend(post)
return pre
47 changes: 47 additions & 0 deletions tests/spec_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Spec helper is a collection of functions to help to speed up the
operation of interoperability testing.
It contains a method to generate the correct sequence of channel opening
and, and it feels a dictionary with all the propriety that needs to
be used after this sequence of steps.
author: https://github.com/vincenzopalazzo
"""
from typing import List, Union

from lnprototest import (
TryAll,
Connect,
Block,
ExpectMsg,
RawMsg,
Msg,
Runner,
Funding,
)
from helpers import (
utxo,
)


def open_and_announce_channel_helper(
runner: Runner, tx_spendable: str, opts: dict
) -> List[Union[Block, Connect, ExpectMsg, TryAll]]:
funding, funding_tx = Funding.from_utxo(
*utxo(0),
local_node_privkey="02",
local_funding_privkey="10",
remote_node_privkey="03",
remote_funding_privkey="20"
)
opts["funding"] = funding
opts["funding_tx"] = funding_tx
return [
Block(blockheight=102, txs=[tx_spendable]),
Connect(connprivkey="03"),
ExpectMsg("init"),
Msg("init", globalfeatures="", features=""),
Block(blockheight=103, number=6, txs=[funding_tx]),
RawMsg(funding.channel_announcement("103x1x0", "")),
]
Loading

0 comments on commit 7f1ba78

Please sign in to comment.