Skip to content

Commit

Permalink
Merge #35: formatting code and fixed flaky8 errors
Browse files Browse the repository at this point in the history
fc73e6e fmt: upgrade black to fix a bug inside the last version (Vincenzo Palazzo)
8f1e290 introduce black in the ci (Vincenzo Palazzo)
d5e216a formatting code and fixed flaky8 errors (Vincenzo Palazzo)

Pull request description:

  Signed-off-by: Vincenzo Palazzo <[email protected]>

Top commit has no ACKs.

Tree-SHA512: 55440f9b562b1cc8c4bdef13d85aa24b97c0b4df9abc9a73f1a7e1fec687ff0e6472c1516a957d8260fec7048a012a7239f3a48f7abd5133718dab10cd8c2cf2
  • Loading branch information
vincenzopalazzo committed Feb 8, 2022
2 parents 9547eeb + fc73e6e commit 8530319
Show file tree
Hide file tree
Showing 38 changed files with 6,649 additions and 4,976 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
extend-ignore = E203
8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install flake8 pytest black
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install -e .
- name: Lint with flake8
- name: Code style check
run: |
pip install black
black . --check --diff
- name: flake8 check
run: |
flake8 --ignore=E501,E731,W503
- name: Test with pytest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tags
**/github-merge.py
.venv
**.egg-info
.idea
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ check-pytest-found:
check: check-pytest-found
$(PYTEST) $(PYTEST_ARGS) $(TEST_DIR)

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

check-flake8:
flake8 --ignore=E501,E731,W503
Expand All @@ -29,5 +29,11 @@ check-quotes/%: %

check-quotes: $(PYTHONFILES:%=check-quotes/%)

check-fmt:
black --check .

fmt:
black .

TAGS:
etags `find . -name '*.py'`
57 changes: 53 additions & 4 deletions lnprototest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,66 @@
"""
from .errors import EventError, SpecFileError
from .event import Event, Connect, Disconnect, Msg, RawMsg, ExpectMsg, MustNotMsg, Block, ExpectTx, FundChannel, InitRbf, Invoice, AddHtlc, CheckEq, ExpectError, ResolvableInt, ResolvableStr, Resolvable, ResolvableBool, msat, negotiated, DualFundAccept, Wait
from .event import (
Event,
Connect,
Disconnect,
Msg,
RawMsg,
ExpectMsg,
MustNotMsg,
Block,
ExpectTx,
FundChannel,
InitRbf,
Invoice,
AddHtlc,
CheckEq,
ExpectError,
ResolvableInt,
ResolvableStr,
Resolvable,
ResolvableBool,
msat,
negotiated,
DualFundAccept,
Wait,
)
from .structure import Sequence, OneOf, AnyOrder, TryAll
from .runner import Runner, Conn, 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
from .runner import (
Runner,
Conn,
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,
)
from .dummyrunner import DummyRunner
from .namespace import peer_message_namespace, namespace, assign_namespace, make_namespace
from .namespace import (
peer_message_namespace,
namespace,
assign_namespace,
make_namespace,
)
from .bitfield import bitfield, has_bit, bitfield_len
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 .funding import AcceptFunding, CreateFunding, CreateDualFunding, Funding, AddInput, AddOutput, FinalizeFunding, AddWitnesses
from .funding import (
AcceptFunding,
CreateFunding,
CreateDualFunding,
Funding,
AddInput,
AddOutput,
FinalizeFunding,
AddWitnesses,
)
from .proposals import dual_fund_csv, channel_type_csv

__all__ = [
Expand Down
5 changes: 1 addition & 4 deletions lnprototest/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from .backend import Backend
from .bitcoind import Bitcoind

__all__ = [
"Backend",
"Bitcoind"
]
__all__ = ["Backend", "Bitcoind"]
55 changes: 31 additions & 24 deletions lnprototest/backend/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import subprocess
import logging

from typing import Any, Callable
from ephemeral_port_reserve import reserve
from bitcoin.rpc import RawProxy
from .backend import Backend
Expand All @@ -22,26 +23,28 @@ class BitcoinProxy:
throwaway connections. This is easier than to reach into the RPC
library to close, reopen and reauth upon failure.
"""
def __init__(self, btc_conf_file, *args, **kwargs):

def __init__(self, btc_conf_file: str, *args: Any, **kwargs: Any):
self.btc_conf_file = btc_conf_file

def __getattr__(self, name):
if name.startswith('__') and name.endswith('__'):
def __getattr__(self, name: str) -> Callable:
if name.startswith("__") and name.endswith("__"):
# Python internal stuff
raise AttributeError

def f(*args):
def f(*args: Any) -> Callable:
self.__proxy = RawProxy(btc_conf_file=self.btc_conf_file)

logging.debug("Calling {name} with arguments {args}".format(
name=name,
args=args
))
logging.debug(
"Calling {name} with arguments {args}".format(name=name, args=args)
)
res = self.__proxy._call(name, *args)
logging.debug("Result for {name} call: {res}".format(
name=name,
res=res,
))
logging.debug(
"Result for {name} call: {res}".format(
name=name,
res=res,
)
)
return res

# Make debuggers show <function bitcoin.rpc.name> rather than <function
Expand All @@ -52,24 +55,26 @@ def f(*args):

class Bitcoind(Backend):
"""Starts regtest bitcoind on an ephemeral port, and returns the RPC proxy"""

def __init__(self, basedir: str):
self.bitcoin_dir = os.path.join(basedir, "bitcoind")
if not os.path.exists(self.bitcoin_dir):
os.makedirs(self.bitcoin_dir)
self.bitcoin_conf = os.path.join(self.bitcoin_dir, 'bitcoin.conf')
self.bitcoin_conf = os.path.join(self.bitcoin_dir, "bitcoin.conf")
self.cmd_line = [
'bitcoind',
'-datadir={}'.format(self.bitcoin_dir),
'-server',
'-regtest',
'-logtimestamps',
'-nolisten']
"bitcoind",
"-datadir={}".format(self.bitcoin_dir),
"-server",
"-regtest",
"-logtimestamps",
"-nolisten",
]
self.port = reserve()
self.btc_version = None
print("Port is {}, dir is {}".format(self.port, self.bitcoin_dir))
# For after 0.16.1 (eg. 3f398d7a17f136cd4a67998406ca41a124ae2966), this
# needs its own [regtest] section.
with open(self.bitcoin_conf, 'w') as f:
with open(self.bitcoin_conf, "w") as f:
f.write("regtest=1\n")
f.write("rpcuser=rpcuser\n")
f.write("rpcpassword=rpcpass\n")
Expand All @@ -89,7 +94,7 @@ def version_compatibility(self) -> None:
# Sanity check
raise ValueError("bitcoind not initialized")

self.btc_version = self.rpc.getnetworkinfo()['version']
self.btc_version = self.rpc.getnetworkinfo()["version"]
assert self.btc_version is not None
logging.info("Bitcoin Core version {}".format(self.btc_version))
if self.btc_version >= 210000:
Expand All @@ -102,14 +107,16 @@ def start(self) -> None:
assert self.proc.stdout

# Wait for it to startup.
while b'Done loading' not in self.proc.stdout.readline():
while b"Done loading" not in self.proc.stdout.readline():
pass

self.version_compatibility()
# Block #1.
# Privkey the coinbase spends to:
# cUB4V7VCk6mX32981TWviQVLkj3pa2zBcXrjMZ9QwaZB5Kojhp59
self.rpc.submitblock('0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f84591a56720aabc8023cecf71801c5e0f9d049d0c550ab42412ad12a67d89f3a3dbb6c60ffff7f200400000001020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0200f2052a0100000016001419f5016f07fe815f611df3a2a0802dbd74e634c40000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000')
self.rpc.submitblock(
"0000002006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f84591a56720aabc8023cecf71801c5e0f9d049d0c550ab42412ad12a67d89f3a3dbb6c60ffff7f200400000001020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0200f2052a0100000016001419f5016f07fe815f611df3a2a0802dbd74e634c40000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000"
)
self.rpc.generatetoaddress(100, self.rpc.getnewaddress())

def stop(self) -> None:
Expand All @@ -119,5 +126,5 @@ def restart(self) -> None:
# Only restart if we have to.
if self.rpc.getblockcount() != 101 or self.rpc.getrawmempool() != []:
self.stop()
shutil.rmtree(os.path.join(self.bitcoin_dir, 'regtest'))
shutil.rmtree(os.path.join(self.bitcoin_dir, "regtest"))
self.start()
2 changes: 1 addition & 1 deletion lnprototest/bitfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ def bitfield(*args: int) -> str:
bytelen = (max(args) + 8) // 8
bfield = bytearray(bytelen)
for bitnum in args:
bfield[bytelen - 1 - bitnum // 8] |= (1 << (bitnum % 8))
bfield[bytelen - 1 - bitnum // 8] |= 1 << (bitnum % 8)
return bfield.hex()
4 changes: 1 addition & 3 deletions lnprototest/clightning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@

from .clightning import Runner

__all__ = [
"Runner"
]
__all__ = ["Runner"]
Loading

0 comments on commit 8530319

Please sign in to comment.