From b0aca7009d5ab0006c78142280af4d74b3fbe816 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sun, 17 Nov 2013 13:30:17 -0800 Subject: [PATCH] flake8 cleanup --- backends/bitcoind/__init__.py | 2 +- backends/bitcoind/blockchain_processor.py | 71 +++++++++-------------- backends/bitcoind/deserialize.py | 47 +++++++-------- backends/irc/__init__.py | 20 ++++--- backends/libbitcoin/__init__.py | 1 - processor.py | 16 ++--- server.py | 6 +- transports/stratum_tcp.py | 8 +-- utils/__init__.py | 15 +---- 9 files changed, 79 insertions(+), 107 deletions(-) diff --git a/backends/bitcoind/__init__.py b/backends/bitcoind/__init__.py index 67df242e..639717cf 100644 --- a/backends/bitcoind/__init__.py +++ b/backends/bitcoind/__init__.py @@ -1 +1 @@ -from blockchain_processor import BlockchainProcessor +from blockchain_processor import BlockchainProcessor # noqa diff --git a/backends/bitcoind/blockchain_processor.py b/backends/bitcoind/blockchain_processor.py index 03de6ee1..d7c21299 100644 --- a/backends/bitcoind/blockchain_processor.py +++ b/backends/bitcoind/blockchain_processor.py @@ -1,19 +1,18 @@ -import ast -import hashlib from json import dumps, loads +from Queue import Queue +import hashlib import leveldb import os -from Queue import Queue import random import sys -import time import threading +import time import traceback import urllib from backends.bitcoind import deserialize from processor import Processor, print_log -from utils import * +from utils import * # todo: import * is generally frowned upon. should import just what is used class BlockchainProcessor(Processor): @@ -38,7 +37,7 @@ def __init__(self, config, shared): self.address_queue = Queue() self.dbpath = config.get('leveldb', 'path') self.pruning_limit = config.getint('leveldb', 'pruning_limit') - self.db_version = 1 # increase this when database needs to be updated + self.db_version = 1 # increase this when database needs to be updated self.dblock = threading.Lock() try: @@ -81,7 +80,7 @@ def __init__(self, config, shared): # check version if self.db_version != db_version: - print_log("Your database '%s' is deprecated. Please create a new database"%self.dbpath) + print_log("Your database '%s' is deprecated. Please create a new database" % self.dbpath) self.shared.stop() return @@ -123,10 +122,10 @@ def serialize(self, h): return s def serialize_item(self, txid, txpos, height, spent=chr(0)): - s = (txid + int_to_hex(txpos, 4) + int_to_hex(height, 3)).decode('hex') + spent + s = (txid + int_to_hex(txpos, 4) + int_to_hex(height, 3)).decode('hex') + spent return s - def deserialize_item(self,s): + def deserialize_item(self, s): txid = s[0:32].encode('hex') txpos = int(rev_hex(s[32:36].encode('hex')), 16) height = int(rev_hex(s[36:39].encode('hex')), 16) @@ -342,7 +341,6 @@ def get_merkle(self, tx_hash, height): return {"block_height": height, "merkle": s, "pos": tx_pos} - def add_to_history(self, addr, tx_hash, tx_pos, tx_height): # keep it sorted s = self.serialize_item(tx_hash, tx_pos, tx_height) + 40*chr(0) @@ -366,55 +364,54 @@ def add_to_history(self, addr, tx_hash, tx_pos, tx_height): txo = (tx_hash + int_to_hex(tx_pos, 4)).decode('hex') self.batch_txio[txo] = addr - - def revert_add_to_history(self, addr, tx_hash, tx_pos, tx_height): serialized_hist = self.batch_list[addr] s = self.serialize_item(tx_hash, tx_pos, tx_height) + 40*chr(0) - if serialized_hist.find(s) == -1: raise + if serialized_hist.find(s) == -1: + raise serialized_hist = serialized_hist.replace(s, '') self.batch_list[addr] = serialized_hist - - def prune_history(self, addr, undo): # remove items that have bit set to one - if undo.get(addr) is None: undo[addr] = [] + if undo.get(addr) is None: + undo[addr] = [] serialized_hist = self.batch_list[addr] l = len(serialized_hist)/80 for i in range(l): - if len(serialized_hist)/80 < self.pruning_limit: break - item = serialized_hist[80*i:80*(i+1)] + if len(serialized_hist)/80 < self.pruning_limit: + break + item = serialized_hist[80*i:80*(i+1)] if item[39:40] == chr(1): assert item[79:80] == chr(2) serialized_hist = serialized_hist[0:80*i] + serialized_hist[80*(i+1):] undo[addr].append(item) # items are ordered self.batch_list[addr] = serialized_hist - def revert_prune_history(self, addr, undo): # restore removed items serialized_hist = self.batch_list[addr] - if undo.get(addr) is not None: + if undo.get(addr) is not None: itemlist = undo.pop(addr) else: - return + return - if not itemlist: return + if not itemlist: + return l = len(serialized_hist)/80 tx_item = '' for i in range(l-1, -1, -1): if tx_item == '': - if not itemlist: + if not itemlist: break else: - tx_item = itemlist.pop(-1) # get the last element + tx_item = itemlist.pop(-1) # get the last element tx_height = int(rev_hex(tx_item[36:39].encode('hex')), 16) - + item = serialized_hist[80*i:80*(i+1)] item_height = int(rev_hex(item[36:39].encode('hex')), 16) @@ -427,7 +424,6 @@ def revert_prune_history(self, addr, undo): self.batch_list[addr] = serialized_hist - def set_spent_bit(self, addr, txi, is_spent, txid=None, index=None, height=None): serialized_hist = self.batch_list[addr] l = len(serialized_hist)/80 @@ -437,7 +433,7 @@ def set_spent_bit(self, addr, txi, is_spent, txid=None, index=None, height=None) if is_spent: new_item = item[0:39] + chr(1) + self.serialize_item(txid, index, height, chr(2)) else: - new_item = item[0:39] + chr(0) + chr(0)*40 + new_item = item[0:39] + chr(0) + chr(0)*40 serialized_hist = serialized_hist[0:80*i] + new_item + serialized_hist[80*(i+1):] break else: @@ -447,12 +443,10 @@ def set_spent_bit(self, addr, txi, is_spent, txid=None, index=None, height=None) self.batch_list[addr] = serialized_hist - def unset_spent_bit(self, addr, txi): self.set_spent_bit(addr, txi, False) self.batch_txio[txi] = addr - def deserialize_block(self, block): txlist = block.get('tx') tx_hashes = [] # ordered txids @@ -501,7 +495,6 @@ def import_block(self, block, block_hash, block_height, sync, revert=False): else: undo_info = {} - if not revert: # read addresses of tx inputs for tx in txdict.values(): @@ -529,17 +522,13 @@ def import_block(self, block, block_hash, block_height, sync, revert=False): for x in tx.get('outputs'): txo = (txid + int_to_hex(x.get('index'), 4)).decode('hex') block_outputs.append(txo) - addr_to_read.append( x.get('address') ) + addr_to_read.append(x.get('address')) undo = undo_info.get(txid) for i, x in enumerate(tx.get('inputs')): addr = undo['prev_addr'][i] addr_to_read.append(addr) - - - - # read histories of addresses for txid, tx in txdict.items(): for x in tx.get('outputs'): @@ -556,20 +545,18 @@ def import_block(self, block, block_hash, block_height, sync, revert=False): self.shared.stop() raise - # process t1 = time.time() if revert: tx_hashes = tx_hashes[::-1] - for txid in tx_hashes: # must be ordered tx = txdict[txid] if not revert: - undo = { 'prev_addr':[] } # contains the list of pruned items for each address in the tx; also, 'prev_addr' is a list of prev addresses - + undo = {'prev_addr': []} # contains the list of pruned items for each address in the tx; also, 'prev_addr' is a list of prev addresses + prev_addr = [] for i, x in enumerate(tx.get('inputs')): txi = (x.get('prevout_hash') + int_to_hex(x.get('prevout_n'), 4)).decode('hex') @@ -583,7 +570,7 @@ def import_block(self, block, block_hash, block_height, sync, revert=False): self.prune_history(addr, undo) prev_addr.append(addr) - undo['prev_addr'] = prev_addr + undo['prev_addr'] = prev_addr # here I add only the outputs to history; maybe I want to add inputs too (that's in the other loop) for x in tx.get('outputs'): @@ -611,10 +598,9 @@ def import_block(self, block, block_hash, block_height, sync, revert=False): assert undo == {} - if revert: + if revert: assert undo_info == {} - # write max_len = 0 max_addr = '' @@ -914,7 +900,6 @@ def memorypool_update(self): for addr in touched_addresses: self.invalidate_cache(addr) - def invalidate_cache(self, address): with self.cache_lock: if address in self.history_cache: diff --git a/backends/bitcoind/deserialize.py b/backends/bitcoind/deserialize.py index 7e010d60..23daf857 100644 --- a/backends/bitcoind/deserialize.py +++ b/backends/bitcoind/deserialize.py @@ -1,13 +1,11 @@ # this code comes from ABE. it can probably be simplified # # - import mmap -import string import struct import types -from utils import * +from utils import * # todo: import * is generally frowned upon. should import just what is used class SerializationError(Exception): @@ -304,20 +302,20 @@ def script_GetOp(bytes): (nSize,) = struct.unpack_from(' len(bytes): - vch = "_INVALID_"+bytes[i:] - i = len(bytes) + vch = "_INVALID_"+bytes[i:] + i = len(bytes) else: - vch = bytes[i:i+nSize] - i += nSize + vch = bytes[i:i+nSize] + i += nSize yield (opcode, vch, i) def script_GetOpName(opcode): - try: - return (opcodes.whatis(opcode)).replace("OP_", "") - except KeyError: - return "InvalidOp_"+str(opcode) + try: + return (opcodes.whatis(opcode)).replace("OP_", "") + except KeyError: + return "InvalidOp_"+str(opcode) def decode_script(bytes): @@ -344,24 +342,23 @@ def match_decoded(decoded, to_match): return True - def get_address_from_input_script(bytes): try: - decoded = [ x for x in script_GetOp(bytes) ] + decoded = [x for x in script_GetOp(bytes)] except: - # coinbase transactions raise an exception + # coinbase transactions raise an exception return [], [], None # non-generated TxIn transactions push a signature # (seventy-something bytes) and then their public key # (33 or 65 bytes) onto the stack: - match = [ opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4 ] + match = [opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4] if match_decoded(decoded, match): return None, None, public_key_to_bc_address(decoded[1][1]) # p2sh transaction, 2 of n - match = [ opcodes.OP_0 ] + match = [opcodes.OP_0] while len(match) < len(decoded): match.append(opcodes.OP_PUSHDATA4) @@ -369,19 +366,19 @@ def get_address_from_input_script(bytes): redeemScript = decoded[-1][1] num = len(match) - 2 - signatures = map(lambda x:x[1].encode('hex'), decoded[1:-1]) - dec2 = [ x for x in script_GetOp(redeemScript) ] + signatures = map(lambda x: x[1].encode('hex'), decoded[1:-1]) + dec2 = [x for x in script_GetOp(redeemScript)] # 2 of 2 - match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG ] + match2 = [opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2, opcodes.OP_CHECKMULTISIG] if match_decoded(dec2, match2): - pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex') ] + pubkeys = [dec2[1][1].encode('hex'), dec2[2][1].encode('hex')] return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5) # 2 of 3 - match2 = [ opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG ] + match2 = [opcodes.OP_2, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_3, opcodes.OP_CHECKMULTISIG] if match_decoded(dec2, match2): - pubkeys = [ dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex') ] + pubkeys = [dec2[1][1].encode('hex'), dec2[2][1].encode('hex'), dec2[3][1].encode('hex')] return pubkeys, signatures, hash_160_to_bc_address(hash_160(redeemScript), 5) return [], [], None @@ -389,7 +386,7 @@ def get_address_from_input_script(bytes): def get_address_from_output_script(bytes): try: - decoded = [ x for x in script_GetOp(bytes) ] + decoded = [x for x in script_GetOp(bytes)] except: return "None" @@ -417,9 +414,9 @@ def get_address_from_output_script(bytes): return hash_160_to_bc_address(decoded[2][1]) # p2sh - match = [ opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL ] + match = [opcodes.OP_HASH160, opcodes.OP_PUSHDATA4, opcodes.OP_EQUAL] if match_decoded(decoded, match): - addr = hash_160_to_bc_address(decoded[1][1],5) + addr = hash_160_to_bc_address(decoded[1][1], 5) return addr return "None" diff --git a/backends/irc/__init__.py b/backends/irc/__init__.py index f0056d09..dd08b116 100644 --- a/backends/irc/__init__.py +++ b/backends/irc/__init__.py @@ -57,17 +57,23 @@ def getname(self): s += 'p' + self.pruning_limit + ' ' def add_port(letter, number): - DEFAULT_PORTS = {'t':'50001', 's':'50002', 'h':'8081', 'g':'8082'} - if not number: return '' + DEFAULT_PORTS = { + 't': '50001', + 's': '50002', + 'h': '8081', + 'g': '8082', + } + if not number: + return '' if DEFAULT_PORTS[letter] == number: return letter + ' ' else: return letter + number + ' ' - s += add_port('t',self.stratum_tcp_port) - s += add_port('h',self.stratum_http_port) - s += add_port('s',self.stratum_tcp_ssl_port) - s += add_port('g',self.stratum_http_ssl_port) + s += add_port('t', self.stratum_tcp_port) + s += add_port('h', self.stratum_http_port) + s += add_port('s', self.stratum_tcp_ssl_port) + s += add_port('g', self.stratum_http_ssl_port) return s def run(self): @@ -93,7 +99,7 @@ def run(self): try: data = s.recv(2048) except: - print_log( "irc: socket error" ) + print_log("irc: socket error") time.sleep(1) break diff --git a/backends/libbitcoin/__init__.py b/backends/libbitcoin/__init__.py index ef2eb1d8..8919e81b 100644 --- a/backends/libbitcoin/__init__.py +++ b/backends/libbitcoin/__init__.py @@ -1,5 +1,4 @@ import threading -import time import bitcoin from bitcoin import bind, _1, _2, _3 diff --git a/processor.py b/processor.py index ec62d482..9f6416e1 100644 --- a/processor.py +++ b/processor.py @@ -1,12 +1,10 @@ -import json -import Queue as queue -import socket +from Queue import Queue +import sys import threading import time import traceback -import sys -from utils import random_string, timestr, print_log +from utils import print_log class Shared: @@ -32,7 +30,7 @@ def __init__(self): threading.Thread.__init__(self) self.daemon = True self.dispatcher = None - self.queue = queue.Queue() + self.queue = Queue() def process(self, request): pass @@ -78,8 +76,8 @@ def __init__(self, shared): self.shared = shared threading.Thread.__init__(self) self.daemon = True - self.request_queue = queue.Queue() - self.response_queue = queue.Queue() + self.request_queue = Queue() + self.response_queue = Queue() self.internal_ids = {} self.internal_id = 1 self.lock = threading.Lock() @@ -160,7 +158,6 @@ def do_dispatch(self, session, request): except: pass - def get_sessions(self): with self.lock: r = self.sessions[:] @@ -188,7 +185,6 @@ def collect_garbage(self): self.sessions = active_sessions[:] - class Session: def __init__(self): diff --git a/server.py b/server.py index 0af6cc61..c453ec85 100755 --- a/server.py +++ b/server.py @@ -14,14 +14,11 @@ # You should have received a copy of the GNU Affero General Public # License along with this program. If not, see # . - import ConfigParser import logging import socket import sys import time -import threading -import traceback import json @@ -95,7 +92,8 @@ def run_rpc_command(command, stratum_tcp_port): msg = '' while True: o = s.recv(1024) - if not o: break + if not o: + break msg += o if msg.find('\n') != -1: break diff --git a/transports/stratum_tcp.py b/transports/stratum_tcp.py index 5fb26a55..76005d40 100644 --- a/transports/stratum_tcp.py +++ b/transports/stratum_tcp.py @@ -1,11 +1,12 @@ import json import Queue as queue import socket +import sys import threading import time -import traceback, sys +import traceback -from processor import Session, Dispatcher +from processor import Session from utils import print_log @@ -77,7 +78,6 @@ def run(self): self.session.stop() - class TcpClientRequestor(threading.Thread): def __init__(self, dispatcher, session): @@ -164,7 +164,7 @@ def __init__(self, dispatcher, host, port, use_ssl, ssl_certfile, ssl_keyfile): self.ssl_certfile = ssl_certfile def run(self): - print_log( ("SSL" if self.use_ssl else "TCP") + " server started on port %d"%self.port) + print_log(("SSL" if self.use_ssl else "TCP") + " server started on port %d" % self.port) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((self.host, self.port)) diff --git a/utils/__init__.py b/utils/__init__.py index f6e64f2b..55caa1f0 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python -# # Electrum - lightweight Bitcoin client # Copyright (C) 2011 thomasv@gitorious # @@ -15,16 +13,13 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import base64 -from functools import partial from itertools import imap +import hashlib import random import string +import sys import threading import time -import hashlib -import re -import sys __b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' __b58base = len(__b58chars) @@ -90,8 +85,6 @@ def header_from_string(s): ############ functions from pywallet ##################### - - def hash_160(public_key): try: md = hashlib.new('ripemd160') @@ -107,7 +100,7 @@ def public_key_to_bc_address(public_key): return hash_160_to_bc_address(hash_160(public_key)) -def hash_160_to_bc_address(h160, addrtype = 0): +def hash_160_to_bc_address(h160, addrtype=0): if h160 == 'None': return 'None' vh160 = chr(addrtype) + h160 @@ -193,8 +186,6 @@ def DecodeBase58Check(psz): return key - - ########### end pywallet functions ####################### def random_string(length):