The script parses Bitcoin database (blkXXXXX.dat) files directly from raw binary to txt human readable view. And I think about how to encrease the speed of processing.
Can anyone suggest how to improve the performance? I know that the best way to encrease performance is to write this on C or C++, but what about Python? Also, is this style of programming is good?
Example cut from code (full code avaible here):
import os
import hashlib
def reverse(input):
L = len(input)
if (L % 2) != 0:
return None
else:
Res = ''
L = L // 2
for i in range(L):
T = input[i*2] + input[i*2+1]
Res = T + Res
T = ''
return (Res);
f = open('blk01234.dat','rb')
tmpHex = ''
fSize = os.path.getsize(t)
while f.tell() != fSize:
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
tmpHex = ''
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('Block size = ' + tmpHex)
tmpHex = ''
tmpPos3 = f.tell()
while f.tell() != tmpPos3 + 80:
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = tmpHex + b
tmpHex = tmpHex.decode('hex')
tmpHex = hashlib.new('sha256', tmpHex).digest()
tmpHex = hashlib.new('sha256', tmpHex).digest()
tmpHex = tmpHex.encode('hex')
tmpHex = tmpHex.upper()
tmpHex = reverse(tmpHex)
resList.append('SHA256 hash of the current block hash = ' + tmpHex)
f.seek(tmpPos3,0)
tmpHex = ''
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('Version number = ' + tmpHex)
tmpHex = ''
#.....next part of code
f.close()
f = open('blk01234.txt','w')
for j in resList:
f.write(j + '\n')
f.close()