Browse Source

Make Python code conform to the Python PEP 8 style guide

ppa-0.6.1
practicalswift 7 years ago
committed by Christian Decker
parent
commit
ae7d857c44
  1. 9
      tests/benchmark.py
  2. 52
      tests/test_lightningd.py
  3. 11
      tests/utils.py
  4. 20
      tools/generate-wire.py

9
tests/benchmark.py

@ -1,16 +1,13 @@
from concurrent.futures import ThreadPoolExecutor
from lightning import LightningRpc
from test_lightningd import NodeFactory
import logging import logging
import pytest import pytest
import random import random
import utils import utils
from concurrent import futures from concurrent import futures
from test_lightningd import NodeFactory
from time import time from time import time
from tqdm import tqdm from tqdm import tqdm
num_workers = 480 num_workers = 480
num_payments = 10000 num_payments = 10000
@ -21,6 +18,7 @@ def executor():
yield ex yield ex
ex.shutdown(wait=False) ex.shutdown(wait=False)
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def bitcoind(): def bitcoind():
bitcoind = utils.BitcoinD(rpcport=28332) bitcoind = utils.BitcoinD(rpcport=28332)
@ -42,12 +40,14 @@ def bitcoind():
bitcoind.proc.kill() bitcoind.proc.kill()
bitcoind.proc.wait() bitcoind.proc.wait()
@pytest.fixture @pytest.fixture
def node_factory(request, bitcoind, executor): def node_factory(request, bitcoind, executor):
nf = NodeFactory(request.node.name, bitcoind, executor) nf = NodeFactory(request.node.name, bitcoind, executor)
yield nf yield nf
nf.killall() nf.killall()
def test_single_hop(node_factory, executor): def test_single_hop(node_factory, executor):
l1 = node_factory.get_node() l1 = node_factory.get_node()
l2 = node_factory.get_node() l2 = node_factory.get_node()
@ -74,6 +74,7 @@ def test_single_hop(node_factory, executor):
diff = time() - start_time diff = time() - start_time
print("Done. %d payments performed in %f seconds (%f payments per second)" % (num_payments, diff, num_payments / diff)) print("Done. %d payments performed in %f seconds (%f payments per second)" % (num_payments, diff, num_payments / diff))
def test_single_payment(node_factory, benchmark): def test_single_payment(node_factory, benchmark):
l1 = node_factory.get_node() l1 = node_factory.get_node()
l2 = node_factory.get_node() l2 = node_factory.get_node()

52
tests/test_lightningd.py

@ -1,7 +1,5 @@
from concurrent import futures from concurrent import futures
from decimal import Decimal from decimal import Decimal
from hashlib import sha256
from lightning import LightningRpc
import copy import copy
import json import json
@ -19,7 +17,9 @@ import tempfile
import threading import threading
import time import time
import unittest import unittest
import utils import utils
from lightning import LightningRpc
bitcoind = None bitcoind = None
TEST_DIR = tempfile.mkdtemp(prefix='lightning-') TEST_DIR = tempfile.mkdtemp(prefix='lightning-')
@ -33,9 +33,11 @@ if TEST_DEBUG:
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
logging.info("Tests running in '%s'", TEST_DIR) logging.info("Tests running in '%s'", TEST_DIR)
def to_json(arg): def to_json(arg):
return json.loads(json.dumps(arg)) return json.loads(json.dumps(arg))
def setupBitcoind(directory): def setupBitcoind(directory):
global bitcoind global bitcoind
bitcoind = utils.BitcoinD(bitcoin_dir=directory, rpcport=28332) bitcoind = utils.BitcoinD(bitcoin_dir=directory, rpcport=28332)
@ -332,8 +334,8 @@ class LightningDTests(BaseLightningDTests):
bitcoind.generate_block(5) bitcoind.generate_block(5)
# Could happen in any order... # Could happen in any order...
l1.daemon.wait_for_logs(['Received channel_update for channel {}\\(0\\)'.format(c) l1.daemon.wait_for_logs(['Received channel_update for channel {}\\(0\\)'.format(c)
for c in channel_ids] for c in channel_ids] +
+ ['Received channel_update for channel {}\\(1\\)'.format(c) ['Received channel_update for channel {}\\(1\\)'.format(c)
for c in channel_ids]) for c in channel_ids])
@unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1") @unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1")
@ -957,7 +959,7 @@ class LightningDTests(BaseLightningDTests):
c = self.fund_channel(lsrc, ldst, 10000000) c = self.fund_channel(lsrc, ldst, 10000000)
self.wait_for_routes(lpayer, [c]) self.wait_for_routes(lpayer, [c])
## Setup # Setup
# Construct lightningd # Construct lightningd
l1 = self.node_factory.get_node() l1 = self.node_factory.get_node()
l2 = self.node_factory.get_node() l2 = self.node_factory.get_node()
@ -975,7 +977,7 @@ class LightningDTests(BaseLightningDTests):
fund_from_to_payer(l4, l1, l1) fund_from_to_payer(l4, l1, l1)
fund_from_to_payer(l4, l2, l1) fund_from_to_payer(l4, l2, l1)
## Test # Test
inv = l4.rpc.invoice(1234567, 'inv', 'for testing')['bolt11'] inv = l4.rpc.invoice(1234567, 'inv', 'for testing')['bolt11']
l1.rpc.pay(inv) l1.rpc.pay(inv)
@ -1075,8 +1077,7 @@ class LightningDTests(BaseLightningDTests):
# Default feerate = 15000/7500/1000 # Default feerate = 15000/7500/1000
# It will start at the second number, accepting anything above the first. # It will start at the second number, accepting anything above the first.
feerates=[ [ 20000, 15000, 7400 ], feerates = [[20000, 15000, 7400], [8000, 1001, 100]]
[ 8000, 1001, 100 ] ]
amounts = [0, 545999, 546000] amounts = [0, 545999, 546000]
num_peers = len(feerates) * len(amounts) num_peers = len(feerates) * len(amounts)
self.give_funds(l1, (10**6) * num_peers + 10000 * num_peers) self.give_funds(l1, (10**6) * num_peers + 10000 * num_peers)
@ -1102,8 +1103,8 @@ class LightningDTests(BaseLightningDTests):
bitcoind.generate_block(6) bitcoind.generate_block(6)
# Now wait for them all to hit normal state, do payments # Now wait for them all to hit normal state, do payments
l1.daemon.wait_for_logs(['update for channel .* now ACTIVE'] * num_peers l1.daemon.wait_for_logs(['update for channel .* now ACTIVE'] * num_peers +
+ ['to CHANNELD_NORMAL'] * num_peers) ['to CHANNELD_NORMAL'] * num_peers)
for p in peers: for p in peers:
if p.amount != 0: if p.amount != 0:
self.pay(l1, p, 100000000) self.pay(l1, p, 100000000)
@ -1726,13 +1727,13 @@ class LightningDTests(BaseLightningDTests):
# Outgoing should be active, but not public. # Outgoing should be active, but not public.
channels = l1.rpc.listchannels()['channels'] channels = l1.rpc.listchannels()['channels']
assert len(channels) == 1 assert len(channels) == 1
assert channels[0]['active'] == True assert channels[0]['active'] is True
assert channels[0]['public'] == False assert channels[0]['public'] is False
channels = l2.rpc.listchannels()['channels'] channels = l2.rpc.listchannels()['channels']
assert len(channels) == 1 assert len(channels) == 1
assert channels[0]['active'] == True assert channels[0]['active'] is True
assert channels[0]['public'] == False assert channels[0]['public'] is False
# Now proceed to funding-depth and do a full gossip round # Now proceed to funding-depth and do a full gossip round
l1.bitcoin.generate_block(5) l1.bitcoin.generate_block(5)
@ -1764,9 +1765,9 @@ class LightningDTests(BaseLightningDTests):
if DEVELOPER: if DEVELOPER:
assert n1['alias'] == 'JUNIORBEAM' assert n1['alias'] == 'JUNIORBEAM'
assert n1['color'] == '0266e4' assert n1['color'] == '0266e4'
if not 'alias' in n2: if 'alias' not in n2:
assert not 'color' in n2 assert 'color' not in n2
assert not 'addresses' in n2 assert 'addresses' not in n2
else: else:
assert n2['alias'] == 'SILENTARTIST' assert n2['alias'] == 'SILENTARTIST'
assert n2['color'] == '022d22' assert n2['color'] == '022d22'
@ -2359,7 +2360,7 @@ class LightningDTests(BaseLightningDTests):
for d in disconnects: for d in disconnects:
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port']) l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
self.assertRaises(ValueError, l1.rpc.fundchannel, l2.info['id'], 20000) self.assertRaises(ValueError, l1.rpc.fundchannel, l2.info['id'], 20000)
assert l1.rpc.getpeer(l2.info['id']) == None assert l1.rpc.getpeer(l2.info['id']) is None
# This one will succeed. # This one will succeed.
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port']) l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
@ -2383,7 +2384,7 @@ class LightningDTests(BaseLightningDTests):
for d in disconnects: for d in disconnects:
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port']) l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
self.assertRaises(ValueError, l1.rpc.fundchannel, l2.info['id'], 20000) self.assertRaises(ValueError, l1.rpc.fundchannel, l2.info['id'], 20000)
assert l1.rpc.getpeer(l2.info['id']) == None assert l1.rpc.getpeer(l2.info['id']) is None
# This one will succeed. # This one will succeed.
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port']) l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
@ -2407,7 +2408,7 @@ class LightningDTests(BaseLightningDTests):
self.assertRaises(ValueError, l1.rpc.fundchannel, l2.info['id'], 20000) self.assertRaises(ValueError, l1.rpc.fundchannel, l2.info['id'], 20000)
# Fundee remembers, funder doesn't. # Fundee remembers, funder doesn't.
assert l1.rpc.getpeer(l2.info['id']) == None assert l1.rpc.getpeer(l2.info['id']) is None
assert l2.rpc.getpeer(l1.info['id'])['id'] == l1.info['id'] assert l2.rpc.getpeer(l1.info['id'])['id'] == l1.info['id']
@unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1") @unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1")
@ -2447,7 +2448,7 @@ class LightningDTests(BaseLightningDTests):
# l2 closes on l1, l1 forgets. # l2 closes on l1, l1 forgets.
self.assertRaises(ValueError, l1.rpc.fundchannel, l2.info['id'], 20000) self.assertRaises(ValueError, l1.rpc.fundchannel, l2.info['id'], 20000)
assert l1.rpc.getpeer(l2.info['id']) == None assert l1.rpc.getpeer(l2.info['id']) is None
# Reconnect. # Reconnect.
l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port']) l1.rpc.connect(l2.info['id'], 'localhost', l2.info['port'])
@ -2788,7 +2789,6 @@ class LightningDTests(BaseLightningDTests):
self.assertRaises(ValueError, l1.rpc.withdraw, waddr, 'all') self.assertRaises(ValueError, l1.rpc.withdraw, waddr, 'all')
l1.daemon.wait_for_log('Cannot afford fee') l1.daemon.wait_for_log('Cannot afford fee')
def test_funding_change(self): def test_funding_change(self):
"""Add some funds, fund a channel, and make sure we remember the change """Add some funds, fund a channel, and make sure we remember the change
""" """
@ -2962,7 +2962,7 @@ class LightningDTests(BaseLightningDTests):
print(" ".join(l2.daemon.cmd_line + ['--dev-debugger=channeld'])) print(" ".join(l2.daemon.cmd_line + ['--dev-debugger=channeld']))
# Wait for l1 to notice # Wait for l1 to notice
wait_for(lambda: not 'connected' in l1.rpc.listpeers()['peers'][0]['channels'][0]) wait_for(lambda: 'connected' not in l1.rpc.listpeers()['peers'][0]['channels'][0])
# Now restart l2 and it should reload peers/channels from the DB # Now restart l2 and it should reload peers/channels from the DB
l2.daemon.start() l2.daemon.start()
@ -3194,7 +3194,6 @@ class LightningDTests(BaseLightningDTests):
self.assertRaises(ValueError, l2.rpc.waitanyinvoice, 'non-number') self.assertRaises(ValueError, l2.rpc.waitanyinvoice, 'non-number')
def test_waitanyinvoice_reversed(self): def test_waitanyinvoice_reversed(self):
"""Test waiting for invoices, where they are paid in reverse order """Test waiting for invoices, where they are paid in reverse order
to when they are created. to when they are created.
@ -3311,7 +3310,6 @@ class LightningDTests(BaseLightningDTests):
l1.daemon.wait_for_log(' to CLOSINGD_COMPLETE') l1.daemon.wait_for_log(' to CLOSINGD_COMPLETE')
l2.daemon.wait_for_log(' to CLOSINGD_COMPLETE') l2.daemon.wait_for_log(' to CLOSINGD_COMPLETE')
@unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1") @unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1")
def test_fee_limits(self): def test_fee_limits(self):
# FIXME: Test case where opening denied. # FIXME: Test case where opening denied.
@ -3491,10 +3489,10 @@ class LightningDTests(BaseLightningDTests):
assert configs['bitcoin-datadir'] == bitcoind.bitcoin_dir assert configs['bitcoin-datadir'] == bitcoind.bitcoin_dir
assert configs['lightning-dir'] == l1.daemon.lightning_dir assert configs['lightning-dir'] == l1.daemon.lightning_dir
assert configs['port'] == l1.info['port'] assert configs['port'] == l1.info['port']
assert configs['allow-deprecated-apis'] == False assert configs['allow-deprecated-apis'] is False
assert configs['override-fee-rates'] == '15000/7500/1000' assert configs['override-fee-rates'] == '15000/7500/1000'
assert configs['network'] == 'regtest' assert configs['network'] == 'regtest'
assert configs['ignore-fee-limits'] == False assert configs['ignore-fee-limits'] is False
# Test one at a time. # Test one at a time.
for c in configs.keys(): for c in configs.keys():

11
tests/utils.py

@ -1,6 +1,4 @@
from bitcoin.rpc import RawProxy as BitcoinProxy import binascii
from lightning import LightningRpc
import logging import logging
import os import os
import re import re
@ -8,7 +6,8 @@ import sqlite3
import subprocess import subprocess
import threading import threading
import time import time
import binascii
from bitcoin.rpc import RawProxy as BitcoinProxy
BITCOIND_CONFIG = { BITCOIND_CONFIG = {
@ -28,6 +27,7 @@ LIGHTNINGD_CONFIG = {
DEVELOPER = os.getenv("DEVELOPER", "0") == "1" DEVELOPER = os.getenv("DEVELOPER", "0") == "1"
def write_config(filename, opts): def write_config(filename, opts):
with open(filename, 'w') as f: with open(filename, 'w') as f:
for k, v in opts.items(): for k, v in opts.items():
@ -135,7 +135,6 @@ class TailableProc(object):
exs = [re.compile(r) for r in regexs] exs = [re.compile(r) for r in regexs]
start_time = time.time() start_time = time.time()
pos = self.logsearch_start pos = self.logsearch_start
initial_pos = len(self.logs)
while True: while True:
if timeout is not None and time.time() > start_time + timeout: if timeout is not None and time.time() > start_time + timeout:
print("Time-out: can't find {} in logs".format(exs)) print("Time-out: can't find {} in logs".format(exs))
@ -240,6 +239,7 @@ class BitcoinD(TailableProc):
# lightning-4 => 0382ce59ebf18be7d84677c2e35f23294b9992ceca95491fcf8a56c6cb2d9de199 aka JUNIORFELONY #0382ce # lightning-4 => 0382ce59ebf18be7d84677c2e35f23294b9992ceca95491fcf8a56c6cb2d9de199 aka JUNIORFELONY #0382ce
# lightning-5 => 032cf15d1ad9c4a08d26eab1918f732d8ef8fdc6abb9640bf3db174372c491304e aka SOMBERFIRE #032cf1 # lightning-5 => 032cf15d1ad9c4a08d26eab1918f732d8ef8fdc6abb9640bf3db174372c491304e aka SOMBERFIRE #032cf1
class LightningD(TailableProc): class LightningD(TailableProc):
def __init__(self, lightning_dir, bitcoin_dir, port=9735, random_hsm=False): def __init__(self, lightning_dir, bitcoin_dir, port=9735, random_hsm=False):
TailableProc.__init__(self, lightning_dir) TailableProc.__init__(self, lightning_dir)
@ -281,6 +281,7 @@ class LightningD(TailableProc):
self.proc.wait(timeout) self.proc.wait(timeout)
return self.proc.returncode return self.proc.returncode
class LightningNode(object): class LightningNode(object):
def __init__(self, daemon, rpc, btc, executor, may_fail=False): def __init__(self, daemon, rpc, btc, executor, may_fail=False):
self.rpc = rpc self.rpc = rpc

20
tools/generate-wire.py

@ -3,10 +3,11 @@
import argparse import argparse
import copy import copy
from collections import namedtuple
import fileinput import fileinput
import re import re
from collections import namedtuple
Enumtype = namedtuple('Enumtype', ['name', 'value']) Enumtype = namedtuple('Enumtype', ['name', 'value'])
type2size = { type2size = {
@ -36,6 +37,7 @@ varlen_structs = [
'wirestring', 'wirestring',
] ]
class FieldType(object): class FieldType(object):
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
@ -96,6 +98,7 @@ sizetypemap = {
1: FieldType('u8') 1: FieldType('u8')
} }
# It would be nicer if we had put '*u8' in spec and disallowed bare lenvar. # It would be nicer if we had put '*u8' in spec and disallowed bare lenvar.
# In practice we only recognize lenvar when it's the previous field. # In practice we only recognize lenvar when it's the previous field.
@ -234,6 +237,7 @@ printwire_impl_templ = """void printwire_{name}(const u8 *cursor)
}} }}
""" """
class Message(object): class Message(object):
def __init__(self, name, enum, comments): def __init__(self, name, enum, comments):
self.name = name self.name = name
@ -252,7 +256,7 @@ class Message(object):
if f.is_array() or f.is_variable_size(): if f.is_array() or f.is_variable_size():
raise ValueError('Field {} has non-simple length variable {}' raise ValueError('Field {} has non-simple length variable {}'
.format(field.name, field.lenvar)) .format(field.name, field.lenvar))
f.is_len_var = True; f.is_len_var = True
f.lenvar_for = field f.lenvar_for = field
return return
raise ValueError('Field {} unknown length variable {}' raise ValueError('Field {} unknown length variable {}'
@ -391,7 +395,7 @@ class Message(object):
if f.is_len_var: if f.is_len_var:
field_decls.append('\t{0} {1} = tal_count({2});'.format( field_decls.append('\t{0} {1} = tal_count({2});'.format(
f.fieldtype.name, f.name, f.lenvar_for.name f.fieldtype.name, f.name, f.lenvar_for.name
)); ))
subcalls = [] subcalls = []
for f in self.fields: for f in self.fields:
@ -438,13 +442,13 @@ class Message(object):
subcalls.append('\tprintf("[");') subcalls.append('\tprintf("[");')
subcalls.append('\tfor (size_t i = 0; i < {}; i++) {{' subcalls.append('\tfor (size_t i = 0; i < {}; i++) {{'
.format(num_elems)) .format(num_elems))
subcalls.append('\t\t{} v;'.format(f.fieldtype.name)); subcalls.append('\t\t{} v;'.format(f.fieldtype.name))
if f.fieldtype.is_assignable(): if f.fieldtype.is_assignable():
subcalls.append('\t\tv = fromwire_{}(&cursor, plen);' subcalls.append('\t\tv = fromwire_{}(&cursor, plen);'
.format(name, basetype)) .format(name, basetype))
else: else:
# We don't handle this yet! # We don't handle this yet!
assert not basetype in varlen_structs assert(basetype not in varlen_structs)
subcalls.append('\t\tfromwire_{}(&cursor, &plen, &v);' subcalls.append('\t\tfromwire_{}(&cursor, &plen, &v);'
.format(basetype)) .format(basetype))
@ -489,9 +493,9 @@ class Message(object):
.format(f.fieldtype.name, f.name, basetype)) .format(f.fieldtype.name, f.name, basetype))
else: else:
# Don't handle these yet. # Don't handle these yet.
assert not basetype in varlen_structs assert(basetype not in varlen_structs)
subcalls.append('\t{} {};'. subcalls.append('\t{} {};'.
format(f.fieldtype.name, f.name)); format(f.fieldtype.name, f.name))
subcalls.append('\tfromwire_{}(&cursor, &plen, &{});' subcalls.append('\tfromwire_{}(&cursor, &plen, &{});'
.format(basetype, f.name)) .format(basetype, f.name))
@ -506,6 +510,7 @@ class Message(object):
subcalls='\n'.join(subcalls) subcalls='\n'.join(subcalls)
) )
def find_message(messages, name): def find_message(messages, name):
for m in messages: for m in messages:
if m.name == name: if m.name == name:
@ -513,6 +518,7 @@ def find_message(messages, name):
return None return None
def find_message_with_option(messages, optional_messages, name, option): def find_message_with_option(messages, optional_messages, name, option):
fullname = name + "_" + option.replace('-', '_') fullname = name + "_" + option.replace('-', '_')

Loading…
Cancel
Save