Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable stashing of expected messages #85

Merged
merged 5 commits into from Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/build.yml
Expand Up @@ -29,9 +29,6 @@ jobs:
- name: Code style check
run: |
black . --check --diff
- name: flake8 check
run: |
make check-flake8
- name: Test with pytest
run: |
pytest tests
5 changes: 1 addition & 4 deletions Makefile
Expand Up @@ -13,10 +13,7 @@ check-pytest-found:
check: check-pytest-found
$(PYTEST) $(PYTEST_ARGS) $(TEST_DIR)

check-source: check-fmt check-flake8 check-mypy check-internal-tests

check-flake8:
flake8 --ignore=E501,E731,W503 --exclude=venv\/,build\/
check-source: check-fmt check-mypy check-internal-tests

check-mypy:
mypy --ignore-missing-imports --disallow-untyped-defs --disallow-incomplete-defs $(PYTHONFILES)
Expand Down
4 changes: 3 additions & 1 deletion lnprototest/__init__.py
Expand Up @@ -11,6 +11,7 @@

"""
from .errors import EventError, SpecFileError

from .event import (
Event,
Connect,
Expand Down Expand Up @@ -38,7 +39,9 @@
CloseChannel,
ExpectDisconnect,
)

from .structure import Sequence, OneOf, AnyOrder, TryAll

from .runner import (
Runner,
Conn,
Expand All @@ -64,7 +67,6 @@
from .commit_tx import Commit, HTLC, UpdateCommit
from .utils import (
Side,
regtest_hash,
privkey_expand,
wait_for,
LightningUtils,
Expand Down
4 changes: 2 additions & 2 deletions lnprototest/clightning/clightning.py
Expand Up @@ -33,7 +33,7 @@
from lnprototest import wait_for
from typing import Dict, Any, Callable, List, Optional, cast

TIMEOUT = int(os.getenv("TIMEOUT", "30"))
TIMEOUT = int(os.getenv("TIMEOUT", "60"))
LIGHTNING_SRC = os.path.join(os.getcwd(), os.getenv("LIGHTNING_SRC", "../lightning/"))


Expand Down Expand Up @@ -175,7 +175,7 @@ def node_ready(rpc: pyln.client.LightningRpc) -> bool:
logging.debug(f"waiting for core-lightning: Exception received {ex}")
return False

wait_for(lambda: node_ready(self.rpc))
wait_for(lambda: node_ready(self.rpc), timeout=TIMEOUT)
logging.debug("Waited for core-lightning")

# Make sure that we see any funds that come to our wallet
Expand Down
2 changes: 1 addition & 1 deletion lnprototest/dummyrunner.py
Expand Up @@ -49,7 +49,7 @@ def has_option(self, optname: str) -> Optional[str]:
def start(self) -> None:
self.blockheight = 102

def stop(self) -> None:
def stop(self, print_logs: bool = False) -> None:
pass

def restart(self) -> None:
Expand Down
7 changes: 6 additions & 1 deletion lnprototest/event.py
Expand Up @@ -7,13 +7,17 @@
import struct
import time
import json

from typing import Optional, Dict, Union, Callable, Any, List, TYPE_CHECKING, overload

from pyln.proto.message import Message

from .errors import SpecFileError, EventError
from .namespace import namespace
from .utils import check_hex
from .signature import Sig
from .bitfield import has_bit
from .utils import check_hex

from bitcoin.core import CTransaction

if TYPE_CHECKING:
Expand Down Expand Up @@ -346,6 +350,7 @@ def action(self, runner: "Runner") -> bool:
# Might be completely unknown to namespace.
try:
msg = Message.read(namespace(), io.BytesIO(binmsg))
runner.add_stash(msg.messagetype.name, msg)
except ValueError as ve:
raise EventError(
self, "Runner gave bad msg {}: {}".format(binmsg.hex(), ve)
Expand Down
31 changes: 18 additions & 13 deletions lnprototest/funding.py
@@ -1,15 +1,21 @@
# Support for funding txs.
import coincurve
import io
import logging

from typing import Tuple, Any, Optional, Union, Callable, Dict, List
from .utils import Side, privkey_expand, regtest_hash

from hashlib import sha256
from pyln.proto.message import Message

from .utils.bitcoin_utils import BitcoinUtils
from .utils import Side, privkey_expand
from .event import Event, ResolvableInt, ResolvableStr
from .namespace import namespace
from .runner import Runner
from .signature import Sig
from pyln.proto.message import Message
from hashlib import sha256
import coincurve
import io
import logging

import bitcoin.core.script as script
from bitcoin.core import (
COutPoint,
CScript,
Expand All @@ -22,7 +28,6 @@
Hash160,
CTransaction,
)
import bitcoin.core.script as script
from bitcoin.wallet import P2WPKHBitcoinAddress

ResolvableFunding = Union["Funding", Callable[["Runner", "Event", str], "Funding"]]
Expand All @@ -43,7 +48,7 @@ def __init__(
local_funding_privkey: str,
remote_node_privkey: str,
remote_funding_privkey: str,
chain_hash: str = regtest_hash,
chain_hash: str = BitcoinUtils.blockchain_hash(),
locktime: int = 0,
):
self.chain_hash = chain_hash
Expand Down Expand Up @@ -137,7 +142,7 @@ def start(
remote_funding_privkey: str,
funding_sats: int,
locktime: int,
chain_hash: str = regtest_hash,
chain_hash: str = BitcoinUtils.blockchain_hash(),
) -> "Funding":
# Create dummy one to start: we will fill in txid at the end
return Funding(
Expand Down Expand Up @@ -305,7 +310,7 @@ def from_utxo(
local_funding_privkey: str,
remote_node_privkey: str,
remote_funding_privkey: str,
chain_hash: str = regtest_hash,
chain_hash: str = BitcoinUtils.blockchain_hash(),
) -> Tuple["Funding", str]:
"""Make a funding transaction by spending this utxo using privkey: return Funding, tx."""

Expand Down Expand Up @@ -654,7 +659,7 @@ def __init__(
local_funding_privkey: ResolvableStr,
remote_node_privkey: ResolvableStr,
remote_funding_privkey: ResolvableStr,
chain_hash: str = regtest_hash,
chain_hash: str = BitcoinUtils.blockchain_hash(),
):
super().__init__()
self.funding_txid = funding_txid
Expand Down Expand Up @@ -702,7 +707,7 @@ def __init__(
local_funding_privkey: ResolvableStr,
remote_node_privkey: ResolvableStr,
remote_funding_privkey: ResolvableStr,
chain_hash: str = regtest_hash,
chain_hash: str = BitcoinUtils.blockchain_hash(),
):
super().__init__()
self.txid_in = txid_in
Expand Down Expand Up @@ -754,7 +759,7 @@ def __init__(
local_funding_privkey: str,
remote_node_privkey: str,
remote_funding_privkey: ResolvableStr,
chain_hash: str = regtest_hash,
chain_hash: str = BitcoinUtils.blockchain_hash(),
):
super().__init__()

Expand Down
4 changes: 3 additions & 1 deletion lnprototest/keyset.py
Expand Up @@ -2,7 +2,6 @@
# FIXME: clean this up for use as pyln.proto.tx
import coincurve
import hashlib
from .utils import privkey_expand, check_hex


class KeySet(object):
Expand All @@ -14,6 +13,9 @@ def __init__(
delayed_payment_base_secret: str,
shachain_seed: str,
):

from .utils import privkey_expand, check_hex

self.revocation_base_secret = privkey_expand(revocation_base_secret)
self.payment_base_secret = privkey_expand(payment_base_secret)
self.htlc_base_secret = privkey_expand(htlc_base_secret)
Expand Down
23 changes: 1 addition & 22 deletions lnprototest/stash/__init__.py
Expand Up @@ -28,26 +28,5 @@
htlc_sigs_to_recv,
locking_script,
witnesses,
stash_field_from_event,
)


__all__ = [
"commitsig_to_send",
"commitsig_to_recv",
"htlc_sigs_to_send",
"htlc_sigs_to_recv",
"channel_id",
"channel_announcement",
"channel_update",
"get_member",
"rcvd",
"sent",
"funding_amount",
"funding_pubkey",
"funding_tx",
"funding_txid",
"funding",
"funding_close_tx",
"locking_script",
"witnesses",
]
11 changes: 11 additions & 0 deletions lnprototest/stash/stash.py
Expand Up @@ -273,3 +273,14 @@ def _funding_close_tx(runner: Runner, event: Event, field: str) -> str:
return runner.get_stash(event, "Funding").close_tx()

return _funding_close_tx


def stash_field_from_event(stash_key: str) -> Callable[[Runner, Event, str], str]:
"""Generic stash function to get the information back from a previous event"""

def _stash_field_from_event(runner: Runner, event: Event, field: str) -> str:
if runner._is_dummy():
return "0"
return runner.get_stash(event, stash_key).fields[field]

return _stash_field_from_event
29 changes: 26 additions & 3 deletions lnprototest/utils/__init__.py
@@ -1,3 +1,26 @@
from .ln_spec_utils import LightningUtils
from .utils import Side, regtest_hash, privkey_expand, wait_for, check_hex
from .bitcoin_utils import ScriptType, BitcoinUtils
from .ln_spec_utils import (
LightningUtils,
connect_to_node_helper,
open_and_announce_channel_helper,
)
from .utils import (
Side,
privkey_expand,
wait_for,
check_hex,
gen_random_keyset,
run_runner,
pubkey_of,
check_hex,
privkey_for_index,
merge_events_sequences,
)
from .bitcoin_utils import (
ScriptType,
BitcoinUtils,
utxo,
utxo_amount,
funding_amount_for_utxo,
tx_spendable,
tx_out_for_index,
)