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

coop-close channel integration testing #37

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[flake8]
max-line-length = 88
extend-ignore = E203
extend-ignore = E203
per-file-ignores = __init__.py:F401
2 changes: 2 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches: [master]
pull_request:
branches: [master]
schedule:
- cron: '30 1 1,15 * *'

jobs:
test:
Expand Down
3 changes: 1 addition & 2 deletions docker/Dockerfile.clightning
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ RUN pip3 install -U pip && \
RUN git config --global user.name "John Doe" && \
git config --global user.email [email protected] && \
git clone https://github.com/ElementsProject/lightning.git && \
cd lightning && \
git checkout v$CLIGHTNING_VERSION && \
cd lightning && \
poetry config virtualenvs.create false && \
poetry install && \
./configure --enable-developer && \
Expand Down
1 change: 1 addition & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /bin/bash
cd lnprototest || exit

for i in range{0..14};
do
if make check PYTEST_ARGS='--runner=lnprototest.clightning.Runner -n4 --dist=loadfile --log-cli-level=DEBUG'; then
Expand Down
10 changes: 9 additions & 1 deletion lnprototest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@
from .signature import SigType, Sig
from .keyset import KeySet
from .commit_tx import Commit, HTLC, UpdateCommit
from .utils import Side, regtest_hash, privkey_expand, wait_for
from .utils import (
Side,
regtest_hash,
privkey_expand,
wait_for,
LightningUtils,
ScriptType,
BitcoinUtils,
)
from .funding import (
AcceptFunding,
CreateFunding,
Expand Down
6 changes: 2 additions & 4 deletions lnprototest/event.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#! /usr/bin/python3
import logging
import traceback
from pyln.proto.message import Message
import collections
Expand Down Expand Up @@ -69,9 +70,6 @@ def resolve_args(
ret[field] = self.resolve_arg(field, runner, str_or_func)
return ret

def __repr__(self) -> str:
return self.name


class PerConnEvent(Event):
"""An event which takes a connprivkey arg"""
Expand Down Expand Up @@ -129,7 +127,7 @@ def matches(self, binmsg: bytes) -> bool:
name = msgtype.name
else:
name = str(msgnum)

logging.info(f"msg {name} != from what we are looking for {self.must_not}?")
return name == self.must_not

def action(self, runner: "Runner") -> bool:
Expand Down
3 changes: 3 additions & 0 deletions lnprototest/structure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#! /usr/bin/python3
import io
import logging

from .event import Event, ExpectMsg, ResolvableBool
from .errors import SpecFileError, EventError
from .namespace import namespace
Expand Down Expand Up @@ -44,6 +46,7 @@ def action(self, runner: "Runner", skip_first: bool = False) -> bool:
super().action(runner)
all_done = True
for e in self.events:
logging.debug(f"receiving event {e}")
if not e.enabled(runner):
continue
if skip_first:
Expand Down
3 changes: 3 additions & 0 deletions lnprototest/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .ln_spec_utils import LightningUtils
from .utils import Side, regtest_hash, privkey_expand, wait_for, check_hex
from .bitcoin_utils import ScriptType, BitcoinUtils
56 changes: 56 additions & 0 deletions lnprototest/utils/bitcoin_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Bitcoin utils is a collection of methods that helps to
work with bitcoin primitive.
"""
import hashlib
from enum import Enum

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


class ScriptType(Enum):
"""
Type of Script used in the Runner.

In particular, during the testing we need to have
two type of script, the valid one and the invalid one.
This is useful when is needed to send an invalid script.

FIXME: naming is too simple.
"""

VALID_CLOSE_SCRIPT = 1
INVALID_CLOSE_SCRIPT = 2


class BitcoinUtils:
"""Main implementation class of the lightning networks utils.

The implementation class contains only static methods that
apply the rules specified by the BIP."""

@staticmethod
def blockchain_hash() -> str:
"""Return the chain transaction hash.
That in this case is the regtest transaction hash."""
return "06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f"

@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"""
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))
38 changes: 38 additions & 0 deletions lnprototest/utils/ln_spec_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Lightning network Speck utils, is a collection of methods that helps to
work with some concept of lightning network RFC
"""


class LightningUtils:
"""
Main implementation class of the lightning networks utils.

The implementation class contains only static methods that
apply the rules specified in the lightning network RFC.
"""

@staticmethod
def derive_short_channel_id(block_height: int, tx_idx: int, tx_output) -> str:
"""
Derive the short channel id with the specified
parameters, and return the result as string.

RFC definition: https://github.com/lightning/bolts/blob/93909f67f6a48ee3f155a6224c182e612dd5f187/07-routing-gossip.md#definition-of-short_channel_id

The short_channel_id is the unique description of the funding transaction. It is constructed as follows:
- the most significant 3 bytes: indicating the block height
- the next 3 bytes: indicating the transaction index within the block
- the least significant 2 bytes: indicating the output index that pays to the channel.

e.g: a short_channel_id might be written as 539268x845x1, indicating a channel on the
output 1 of the transaction at index 845 of the block at height 539268.

block_height: str
Block height.
tx_idx: int
Transaction index inside the block.
tx_output: int
Output index inside the transaction.
"""
return f"{block_height}x{tx_idx}x{tx_output}"
1 change: 1 addition & 0 deletions lnprototest/utils.py → lnprototest/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from enum import IntEnum

# regtest chain hash (hash of regtest genesis block)
# deprecated, use instead the BitcoinUtils.get_blockhain_hash()
regtest_hash = "06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f"


Expand Down
Loading