Make Python code conform to the Python PEP 8 style guide

This commit is contained in:
practicalswift
2018-02-21 19:46:04 +01:00
committed by Christian Decker
parent 0ac5e93065
commit ae7d857c44
4 changed files with 191 additions and 185 deletions

View File

@@ -1,16 +1,13 @@
from concurrent.futures import ThreadPoolExecutor
from lightning import LightningRpc
from test_lightningd import NodeFactory
import logging
import pytest
import random
import utils
from concurrent import futures
from test_lightningd import NodeFactory
from time import time
from tqdm import tqdm
num_workers = 480
num_payments = 10000
@@ -21,6 +18,7 @@ def executor():
yield ex
ex.shutdown(wait=False)
@pytest.fixture(scope="module")
def bitcoind():
bitcoind = utils.BitcoinD(rpcport=28332)
@@ -42,12 +40,14 @@ def bitcoind():
bitcoind.proc.kill()
bitcoind.proc.wait()
@pytest.fixture
def node_factory(request, bitcoind, executor):
nf = NodeFactory(request.node.name, bitcoind, executor)
yield nf
nf.killall()
def test_single_hop(node_factory, executor):
l1 = node_factory.get_node()
l2 = node_factory.get_node()
@@ -74,6 +74,7 @@ def test_single_hop(node_factory, executor):
diff = time() - start_time
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):
l1 = node_factory.get_node()
l2 = node_factory.get_node()

View File

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

View File

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

View File

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