Skip to content

Commit 5d33a26

Browse files
committed
Code quality
1 parent 011500e commit 5d33a26

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

dev/powinterrupttest.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Code for discovering how C PoW can be interrupted
3+
4+
"""
15
import ctypes
26
import hashlib
37
from multiprocessing import current_process
@@ -9,9 +13,12 @@
913
shutdown = 0
1014

1115

12-
def signal_handler(signal, frame):
16+
# pylint: disable=unused-argument
17+
def signal_handler(signum, frame):
18+
"""Signal handler"""
1319
global shutdown
14-
print("Got signal %i in %s/%s" % (signal, current_process().name, current_thread().name))
20+
print("Got signal %i in %s/%s" % (signum, current_process().name,
21+
current_thread().name))
1522
if current_process().name != "MainProcess":
1623
raise StopIteration("Interrupted")
1724
if current_thread().name != "PyBitmessage":
@@ -31,7 +38,9 @@ def _doCPoW(target, initialHash):
3138
nonce = bmpow(out_h, out_m)
3239
if shutdown:
3340
break
34-
trialValue, = unpack('>Q', hashlib.sha512(hashlib.sha512(pack('>Q', nonce) + initialHash).digest()).digest()[0:8])
41+
trialValue, = unpack('>Q',
42+
hashlib.sha512(hashlib.sha512(
43+
pack('>Q', nonce) + initialHash).digest()).digest()[0:8])
3544
if shutdown != 0:
3645
raise StopIteration("Interrupted")
3746
print("C PoW done")

dev/ssltest.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
Development code for SSL compatibility investigations
3+
"""
14
import os
25
import select
36
import socket
@@ -10,7 +13,10 @@
1013

1114

1215
def sslProtocolVersion():
13-
# sslProtocolVersion
16+
"""
17+
Find a protocol version value with compatibility across
18+
different python versions
19+
"""
1420
if sys.version_info >= (2, 7, 13):
1521
# this means TLSv1 or higher
1622
# in the future change to
@@ -26,18 +32,28 @@ def sslProtocolVersion():
2632

2733

2834
def sslProtocolCiphers():
35+
"""
36+
Find protocol cipher that is compatible for PyBitmessage across
37+
different python and OpenSSL versions
38+
"""
2939
if ssl.OPENSSL_VERSION_NUMBER >= 0x10100000:
3040
return "AECDH-AES256-SHA@SECLEVEL=0"
3141
else:
3242
return "AECDH-AES256-SHA"
3343

3444

3545
def connect():
46+
"""
47+
Connect a socket
48+
"""
3649
sock = socket.create_connection((HOST, PORT))
3750
return sock
3851

3952

4053
def listen():
54+
"""
55+
Listen to a socket
56+
"""
4157
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4258
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
4359
sock.bind((HOST, PORT))
@@ -46,6 +62,9 @@ def listen():
4662

4763

4864
def sslHandshake(sock, server=False):
65+
"""
66+
Perform SSL hadnshake
67+
"""
4968
if sys.version_info >= (2, 7, 9):
5069
context = ssl.SSLContext(sslProtocolVersion())
5170
context.set_ciphers(sslProtocolCiphers())
@@ -54,12 +73,19 @@ def sslHandshake(sock, server=False):
5473
context.verify_mode = ssl.CERT_NONE
5574
context.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3\
5675
| ssl.OP_SINGLE_ECDH_USE | ssl.OP_CIPHER_SERVER_PREFERENCE
57-
sslSock = context.wrap_socket(sock, server_side=server, do_handshake_on_connect=False)
76+
sslSock = context.wrap_socket(sock, server_side=server,
77+
do_handshake_on_connect=False)
5878
else:
59-
sslSock = ssl.wrap_socket(sock, keyfile=os.path.join('src', 'sslkeys', 'key.pem'),
60-
certfile=os.path.join('src', 'sslkeys', 'cert.pem'),
61-
server_side=server, ssl_version=sslProtocolVersion(),
62-
do_handshake_on_connect=False, ciphers='AECDH-AES256-SHA')
79+
sslSock = ssl.wrap_socket(sock, keyfile=os.path.join('src',
80+
'sslkeys',
81+
'key.pem'),
82+
certfile=os.path.join('src',
83+
'sslkeys',
84+
'cert.pem'),
85+
server_side=server,
86+
ssl_version=sslProtocolVersion(),
87+
do_handshake_on_connect=False,
88+
ciphers='AECDH-AES256-SHA')
6389

6490
while True:
6591
try:

0 commit comments

Comments
 (0)