mirror of
https://github.com/dashpay/dash.git
synced 2024-12-27 21:12:48 +01:00
17acd6b472
643aad17fa
Enable additional flake8 rules (practicalswift)f020aca297
Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift) Pull request description: Enabled rules: ``` * E242: tab after ',' * E266: too many leading '#' for block comment * E401: multiple imports on one line * E402: module level import not at top of file * E701: multiple statements on one line (colon) * E901: SyntaxError: invalid syntax * E902: TokenError: EOF in multi-line string * F821: undefined name 'Foo' * W293: blank line contains whitespace * W606: 'async' and 'await' are reserved keywords starting with Python 3.7 ``` Note to reviewers: * In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR. * Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes. Before this commit: ``` $ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 . 5 E266 too many leading '#' for block comment 4 E401 multiple imports on one line 6 E402 module level import not at top of file 5 E701 multiple statements on one line (colon) 1 F812 list comprehension redefines 'n' from line 159 4 F821 undefined name 'ConnectionRefusedError' 28 W293 blank line contains whitespace ``` After this commit: ``` $ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 . $ ``` Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a Signed-off-by: pasta <pasta@dashboost.org>
157 lines
5.1 KiB
Python
Executable File
157 lines
5.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
#
|
|
# linearize-hashes.py: List blocks in a linear, no-fork version of the chain.
|
|
#
|
|
# Copyright (c) 2013-2014 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
#
|
|
|
|
from __future__ import print_function
|
|
try: # Python 3
|
|
import http.client as httplib
|
|
except ImportError: # Python 2
|
|
import httplib
|
|
import json
|
|
import re
|
|
import base64
|
|
import sys
|
|
import os
|
|
import os.path
|
|
|
|
settings = {}
|
|
|
|
def hex_switchEndian(s):
|
|
""" Switches the endianness of a hex string (in pairs of hex chars) """
|
|
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]
|
|
return b''.join(pairList[::-1]).decode()
|
|
|
|
class BitcoinRPC:
|
|
def __init__(self, host, port, username, password):
|
|
authpair = "%s:%s" % (username, password)
|
|
authpair = authpair.encode('utf-8')
|
|
self.authhdr = b"Basic " + base64.b64encode(authpair)
|
|
self.conn = httplib.HTTPConnection(host, port=port, timeout=30)
|
|
|
|
def execute(self, obj):
|
|
try:
|
|
self.conn.request('POST', '/', json.dumps(obj),
|
|
{ 'Authorization' : self.authhdr,
|
|
'Content-type' : 'application/json' })
|
|
except ConnectionRefusedError:
|
|
print('RPC connection refused. Check RPC settings and the server status.',
|
|
file=sys.stderr)
|
|
return None
|
|
|
|
resp = self.conn.getresponse()
|
|
if resp is None:
|
|
print("JSON-RPC: no response", file=sys.stderr)
|
|
return None
|
|
|
|
body = resp.read().decode('utf-8')
|
|
resp_obj = json.loads(body)
|
|
return resp_obj
|
|
|
|
@staticmethod
|
|
def build_request(idx, method, params):
|
|
obj = { 'version' : '1.1',
|
|
'method' : method,
|
|
'id' : idx }
|
|
if params is None:
|
|
obj['params'] = []
|
|
else:
|
|
obj['params'] = params
|
|
return obj
|
|
|
|
@staticmethod
|
|
def response_is_error(resp_obj):
|
|
return 'error' in resp_obj and resp_obj['error'] is not None
|
|
|
|
def get_block_hashes(settings, max_blocks_per_call=10000):
|
|
rpc = BitcoinRPC(settings['host'], settings['port'],
|
|
settings['rpcuser'], settings['rpcpassword'])
|
|
|
|
height = settings['min_height']
|
|
while height < settings['max_height']+1:
|
|
num_blocks = min(settings['max_height']+1-height, max_blocks_per_call)
|
|
batch = []
|
|
for x in range(num_blocks):
|
|
batch.append(rpc.build_request(x, 'getblockhash', [height + x]))
|
|
|
|
reply = rpc.execute(batch)
|
|
if reply is None:
|
|
print('Cannot continue. Program will halt.')
|
|
return None
|
|
|
|
for x,resp_obj in enumerate(reply):
|
|
if rpc.response_is_error(resp_obj):
|
|
print('JSON-RPC: error at height', height+x, ': ', resp_obj['error'], file=sys.stderr)
|
|
sys.exit(1)
|
|
assert(resp_obj['id'] == x) # assume replies are in-sequence
|
|
if settings['rev_hash_bytes'] == 'true':
|
|
resp_obj['result'] = hex_switchEndian(resp_obj['result'])
|
|
print(resp_obj['result'])
|
|
|
|
height += num_blocks
|
|
|
|
def get_rpc_cookie():
|
|
# Open the cookie file
|
|
with open(os.path.join(os.path.expanduser(settings['datadir']), '.cookie'), 'r', encoding="ascii") as f:
|
|
combined = f.readline()
|
|
combined_split = combined.split(":")
|
|
settings['rpcuser'] = combined_split[0]
|
|
settings['rpcpassword'] = combined_split[1]
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) != 2:
|
|
print("Usage: linearize-hashes.py CONFIG-FILE")
|
|
sys.exit(1)
|
|
|
|
f = open(sys.argv[1], encoding="utf8")
|
|
for line in f:
|
|
# skip comment lines
|
|
m = re.search('^\s*#', line)
|
|
if m:
|
|
continue
|
|
|
|
# parse key=value lines
|
|
m = re.search('^(\w+)\s*=\s*(\S.*)$', line)
|
|
if m is None:
|
|
continue
|
|
settings[m.group(1)] = m.group(2)
|
|
f.close()
|
|
|
|
if 'host' not in settings:
|
|
settings['host'] = '127.0.0.1'
|
|
if 'port' not in settings:
|
|
settings['port'] = 9998
|
|
if 'min_height' not in settings:
|
|
settings['min_height'] = 0
|
|
if 'max_height' not in settings:
|
|
settings['max_height'] = 313000
|
|
if 'rev_hash_bytes' not in settings:
|
|
settings['rev_hash_bytes'] = 'false'
|
|
|
|
use_userpass = True
|
|
use_datadir = False
|
|
if 'rpcuser' not in settings or 'rpcpassword' not in settings:
|
|
use_userpass = False
|
|
if 'datadir' in settings and not use_userpass:
|
|
use_datadir = True
|
|
if not use_userpass and not use_datadir:
|
|
print("Missing datadir or username and/or password in cfg file", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
settings['port'] = int(settings['port'])
|
|
settings['min_height'] = int(settings['min_height'])
|
|
settings['max_height'] = int(settings['max_height'])
|
|
|
|
# Force hash byte format setting to be lowercase to make comparisons easier.
|
|
settings['rev_hash_bytes'] = settings['rev_hash_bytes'].lower()
|
|
|
|
# Get the rpc user and pass from the cookie if the datadir is set
|
|
if use_datadir:
|
|
get_rpc_cookie()
|
|
|
|
get_block_hashes(settings)
|