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

[WIP] lnprototest refactoring #95

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ POSSIBLE_PYTEST_NAMES=pytest-3 pytest3 pytest
PYTEST := $(shell for p in $(POSSIBLE_PYTEST_NAMES); do if type $$p > /dev/null; then echo $$p; break; fi done)
TEST_DIR=tests

default: check-source check check-quotes
# We disable the default target for the moment because we
# need to fix pytest
# default: check-source check check-quotes
default: fmt check

check-pytest-found:
@if [ -z "$(PYTEST)" ]; then echo "Cannot find any pytest: $(POSSIBLE_PYTEST_NAMES)" >&2; exit 1; fi
Expand Down
74 changes: 1 addition & 73 deletions lnprototest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from .runner import (
Runner,
Conn,
RunnerConn,
remote_revocation_basepoint,
remote_payment_basepoint,
remote_delayed_payment_basepoint,
Expand All @@ -54,7 +55,6 @@
remote_funding_pubkey,
remote_funding_privkey,
)
from .dummyrunner import DummyRunner
from .namespace import (
peer_message_namespace,
namespace,
Expand Down Expand Up @@ -84,75 +84,3 @@
AddWitnesses,
)
from .proposals import dual_fund_csv, channel_type_csv

__all__ = [
"EventError",
"SpecFileError",
"Resolvable",
"ResolvableInt",
"ResolvableStr",
"ResolvableBool",
"Event",
"Connect",
"Disconnect",
"DualFundAccept",
"CreateDualFunding",
"AddInput",
"AddOutput",
"FinalizeFunding",
"AddWitnesses",
"Msg",
"RawMsg",
"ExpectMsg",
"Block",
"ExpectTx",
"FundChannel",
"InitRbf",
"Invoice",
"AddHtlc",
"ExpectError",
"Sequence",
"OneOf",
"AnyOrder",
"TryAll",
"CheckEq",
"MustNotMsg",
"SigType",
"Sig",
"DummyRunner",
"Runner",
"Conn",
"KeySet",
"peer_message_namespace",
"namespace",
"assign_namespace",
"make_namespace",
"bitfield",
"has_bit",
"bitfield_len",
"msat",
"negotiated",
"remote_revocation_basepoint",
"remote_payment_basepoint",
"remote_delayed_payment_basepoint",
"remote_htlc_basepoint",
"remote_per_commitment_point",
"remote_per_commitment_secret",
"remote_funding_pubkey",
"remote_funding_privkey",
"Commit",
"HTLC",
"UpdateCommit",
"Side",
"AcceptFunding",
"CreateFunding",
"Funding",
"regtest_hash",
"privkey_expand",
"Wait",
"dual_fund_csv",
"channel_type_csv",
"wait_for",
"CloseChannel",
"ExpectDisconnect",
]
61 changes: 25 additions & 36 deletions lnprototest/clightning/clightning.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/python3
# This script exercises the core-lightning implementation

# Released by Rusty Russell under CC0:
Expand Down Expand Up @@ -27,6 +26,7 @@
SpecFileError,
KeySet,
Conn,
RunnerConn,
namespace,
MustNotMsg,
)
Expand All @@ -37,23 +37,6 @@
LIGHTNING_SRC = os.path.join(os.getcwd(), os.getenv("LIGHTNING_SRC", "../lightning/"))


class CLightningConn(lnprototest.Conn):
def __init__(self, connprivkey: str, port: int):
super().__init__(connprivkey)
# FIXME: pyln.proto.wire should just use coincurve PrivateKey!
self.connection = pyln.proto.wire.connect(
pyln.proto.wire.PrivateKey(bytes.fromhex(self.connprivkey.to_hex())),
# FIXME: Ask node for pubkey
pyln.proto.wire.PublicKey(
bytes.fromhex(
"0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
)
),
"127.0.0.1",
port,
)


class Runner(lnprototest.Runner):
def __init__(self, config: Any):
super().__init__(config)
Expand Down Expand Up @@ -207,7 +190,7 @@ def stop(self, print_logs: bool = False, also_bitcoind: bool = True) -> None:
self.shutdown(also_bitcoind=also_bitcoind)
self.running = False
for c in self.conns.values():
cast(CLightningConn, c).connection.connection.close()
c.connection.connection.close()
if print_logs:
log_path = f"{self.lightning_dir}/regtest/log"
with open(log_path) as log:
Expand All @@ -228,8 +211,14 @@ def restart(self) -> None:
self.bitcoind.restart()
self.start(also_bitcoind=False)

def connect(self, _: Event, connprivkey: str) -> None:
self.add_conn(CLightningConn(connprivkey, self.lightning_port))
def connect(self, _: Event, connprivkey: str) -> RunnerConn:
conn = RunnerConn(
connprivkey,
"0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
self.lightning_port,
)
self.add_conn(conn)
return conn

def getblockheight(self) -> int:
return self.bitcoind.rpc.getblockcount()
Expand All @@ -245,15 +234,13 @@ def add_blocks(self, event: Event, txs: List[str], n: int) -> None:

wait_for(lambda: self.rpc.getinfo()["blockheight"] == self.getblockheight())

def recv(self, event: Event, conn: Conn, outbuf: bytes) -> None:
def recv(self, event: Event, conn: RunnerConn, outbuf: bytes) -> None:
try:
cast(CLightningConn, conn).connection.send_message(outbuf)
conn.connection.send_message(outbuf)
except BrokenPipeError:
# This happens when they've sent an error and closed; try
# reading it to figure out what went wrong.
fut = self.executor.submit(
cast(CLightningConn, conn).connection.read_message
)
fut = self.executor.submit(conn.connection.read_message)
try:
msg = fut.result(1)
except futures.TimeoutError:
Expand All @@ -268,7 +255,7 @@ def recv(self, event: Event, conn: Conn, outbuf: bytes) -> None:
def fundchannel(
self,
event: Event,
conn: Conn,
conn: RunnerConn,
amount: int,
feerate: int = 253,
expect_fail: bool = False,
Expand All @@ -292,7 +279,7 @@ def fundchannel(

def _fundchannel(
runner: Runner,
conn: Conn,
conn: RunnerConn,
amount: int,
feerate: int,
expect_fail: bool = False,
Expand Down Expand Up @@ -365,7 +352,7 @@ def kill_fundchannel(self) -> None:
def init_rbf(
self,
event: Event,
conn: Conn,
conn: RunnerConn,
channel_id: str,
amount: int,
utxo_txid: str,
Expand Down Expand Up @@ -429,7 +416,9 @@ def accept_add_fund(self, event: Event) -> None:
},
)

def addhtlc(self, event: Event, conn: Conn, amount: int, preimage: str) -> None:
def addhtlc(
self, event: Event, conn: RunnerConn, amount: int, preimage: str
) -> None:
payhash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
routestep = {
"msatoshi": amount,
Expand All @@ -442,9 +431,9 @@ def addhtlc(self, event: Event, conn: Conn, amount: int, preimage: str) -> None:
self.rpc.sendpay([routestep], payhash)

def get_output_message(
self, conn: Conn, event: Event, timeout: int = TIMEOUT
self, conn: RunnerConn, event: Event, timeout: int = TIMEOUT
) -> Optional[bytes]:
fut = self.executor.submit(cast(CLightningConn, conn).connection.read_message)
fut = self.executor.submit(conn.connection.read_message)
try:
return fut.result(timeout)
except futures.TimeoutError as ex:
Expand All @@ -454,7 +443,7 @@ def get_output_message(
logging.error(f"{ex}")
return None

def check_error(self, event: Event, conn: Conn) -> Optional[str]:
def check_error(self, event: Event, conn: RunnerConn) -> Optional[str]:
# We get errors in form of err msgs, always.
super().check_error(event, conn)
msg = self.get_output_message(conn, event)
Expand All @@ -465,13 +454,13 @@ def check_error(self, event: Event, conn: Conn) -> Optional[str]:
def check_final_error(
self,
event: Event,
conn: Conn,
conn: RunnerConn,
expected: bool,
must_not_events: List[MustNotMsg],
) -> None:
if not expected:
# Inject raw packet to ensure it hangs up *after* processing all previous ones.
cast(CLightningConn, conn).connection.connection.send(bytes(18))
conn.connection.connection.send(bytes(18))

while True:
binmsg = self.get_output_message(conn, event)
Expand All @@ -488,7 +477,7 @@ def check_final_error(
if msgtype == namespace().get_msgtype("error").number:
raise EventError(event, "Got error msg: {}".format(binmsg.hex()))

cast(CLightningConn, conn).connection.connection.close()
conn.connection.connection.close()

def expect_tx(self, event: Event, txid: str) -> None:
# Ah bitcoin endianness...
Expand Down
Loading
Loading