Changeset View
Changeset View
Standalone View
Standalone View
contrib/linearize/linearize-hashes.py
Show All 10 Lines | |||||
try: # Python 3 | try: # Python 3 | ||||
import http.client as httplib | import http.client as httplib | ||||
except ImportError: # Python 2 | except ImportError: # Python 2 | ||||
import httplib | import httplib | ||||
import json | import json | ||||
import re | import re | ||||
import base64 | import base64 | ||||
import sys | import sys | ||||
import os | |||||
import os.path | |||||
settings = {} | settings = {} | ||||
##### Switch endian-ness ##### | ##### Switch endian-ness ##### | ||||
def hex_switchEndian(s): | def hex_switchEndian(s): | ||||
""" Switches the endianness of a hex string (in pairs of hex chars) """ | """ Switches the endianness of a hex string (in pairs of hex chars) """ | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | while height < settings['max_height'] + 1: | ||||
assert(resp_obj['id'] == x) # assume replies are in-sequence | assert(resp_obj['id'] == x) # assume replies are in-sequence | ||||
if settings['rev_hash_bytes'] == 'true': | if settings['rev_hash_bytes'] == 'true': | ||||
resp_obj['result'] = hex_switchEndian(resp_obj['result']) | resp_obj['result'] = hex_switchEndian(resp_obj['result']) | ||||
print(resp_obj['result']) | print(resp_obj['result']) | ||||
height += num_blocks | height += num_blocks | ||||
def get_rpc_cookie(): | |||||
# Open the cookie file | |||||
with open(os.path.join(os.path.expanduser(settings['datadir']), '.cookie'), 'r') as f: | |||||
combined = f.readline() | |||||
combined_split = combined.split(":") | |||||
settings['rpcuser'] = combined_split[0] | |||||
settings['rpcpassword'] = combined_split[1] | |||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
if len(sys.argv) != 2: | if len(sys.argv) != 2: | ||||
print("Usage: linearize-hashes.py CONFIG-FILE") | print("Usage: linearize-hashes.py CONFIG-FILE") | ||||
sys.exit(1) | sys.exit(1) | ||||
f = open(sys.argv[1]) | f = open(sys.argv[1]) | ||||
for line in f: | for line in f: | ||||
# skip comment lines | # skip comment lines | ||||
Show All 13 Lines | if __name__ == '__main__': | ||||
if 'port' not in settings: | if 'port' not in settings: | ||||
settings['port'] = 8332 | settings['port'] = 8332 | ||||
if 'min_height' not in settings: | if 'min_height' not in settings: | ||||
settings['min_height'] = 0 | settings['min_height'] = 0 | ||||
if 'max_height' not in settings: | if 'max_height' not in settings: | ||||
settings['max_height'] = 313000 | settings['max_height'] = 313000 | ||||
if 'rev_hash_bytes' not in settings: | if 'rev_hash_bytes' not in settings: | ||||
settings['rev_hash_bytes'] = 'false' | settings['rev_hash_bytes'] = 'false' | ||||
use_userpass = True | |||||
use_datadir = False | |||||
if 'rpcuser' not in settings or 'rpcpassword' not in settings: | if 'rpcuser' not in settings or 'rpcpassword' not in settings: | ||||
print("Missing username and/or password in cfg file", file=sys.stderr) | 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) | sys.exit(1) | ||||
settings['port'] = int(settings['port']) | settings['port'] = int(settings['port']) | ||||
settings['min_height'] = int(settings['min_height']) | settings['min_height'] = int(settings['min_height']) | ||||
settings['max_height'] = int(settings['max_height']) | settings['max_height'] = int(settings['max_height']) | ||||
# Force hash byte format setting to be lowercase to make comparisons easier. | # Force hash byte format setting to be lowercase to make comparisons easier. | ||||
settings['rev_hash_bytes'] = settings['rev_hash_bytes'].lower() | 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) | get_block_hashes(settings) |