Help me to understand this python code's functionality (bit coin mining code), I learned python from scratch to understand it but still stuck in various places, I will use your valuable answers to later recreate this code in vb.net code. I tried to recreate this in VB.net, but lack of understanding of what exactly this code is doing step by step, I wasn't successful even after trying all day.
ver = 2
prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
mrkl_root = "871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a"
time_ = 0x53058b35 # 2014-02-20 04:57:25
bits = 0x19015f53
# https://en.bitcoin.it/wiki/Difficulty
exp = bits >> 24
mant = bits & 0xffffff
target_hexstr = '%064x' % (mant * (1<<(8*(exp - 3))))
target_str = target_hexstr.decode('hex')
nonce = 0
while nonce < 0x100000000:
header = ( struct.pack("<L", ver) + prev_block.decode('hex')[::-1] +
mrkl_root.decode('hex')[::-1] + struct.pack("<LLL", time_, bits, nonce))
hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
print nonce, hash[::-1].encode('hex')
if hash[::-1] < target_str:
print 'success'
break
nonce += 1
Following are as I understand, correct me If I'm wrong
veris a variable and assignsintegertype value of2to itself viaver = 2prev_blockis a variable and it assignsstringtype value of"000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"to itself.krkl_rootis a variable and it assignsstringtype value of"871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a"to itselftime_is a variable and it assignsintegertype value of thehexadecimalof0x53058b35(that means this converts from hexadecimal on the fly and stores the decimal value1392872245to it's variable?)bitsis a variable and it assignsintegertype value of thehexadecimalof0x19015f53that is419520339
Then I have no idea of this code --> exp = bits >> 24 I think it should have something to do with bitwise operations (may be shifting), I guess, but don't know exactly what it's doing.
mantis a variable and it assigns it self anintegertype of value ofbitsvariable and then again it converts thehexvalue of0xffffffand stacks and append the existing value inmantvariable (it self)?
Then again I have no idea of what this is doing target_hexstr = '%064x' % (mant * (1<<(8*(exp - 3)))) , all I know is % the modulus and it shows the remainder after two divisions etc. no idea what 1<< is doing (bit shifting?).
target_stris a variable and it converts whatever type of data (is the data inside it hexademimal?) inside thetarget_hexstrvariable to binary format and assigns that converted binary data to it self (target_str)nonceis a variable and it assigns theintegertype of value0to it self.while nonce < 0x100000000:starts a while loop and it loops until the value ofnoncevariable reaches the value of0x100000000(4294967296 in integer)headeris a variable ofstringtype and it assigns it self (stacks) the following data ?.(struct.pack("<L", ver)is reading what's inside ofvervariable which is anintegertype of value and converts that value tolongtype?+sign performs addition operation with what it gets next as data.prev_block.decode('hex')[::-1]is reading what's inside ofprev_blockvariable (string ofhexadecimal?) and converts it tobinaryformat? and I have no idea of what[::-1]this code do (does it prevents overflow exception?)
what comes into my mind is if the ver variable contains integer type data and if prev_block contains binary type of data how can that + operator perform the addition operation for different kind of data?
struct.pack("<LLL", time_, bits, nonce))and again have no idea of what this code is doing. And ifLis just converting the data tolongtype what does this<LLLis doing? and will this just stack the data in those variables inintegertype such as 1392872245|419520339|0 oftime_,bitsandnoncevariables respectively?hashis a variable and it converts the data inside theheadervariable (string type data?) toSHA256encoding and again it encodes the encoded data toSHA256encoding (encoding twice) and assigns that data as astringtype data it self.print nonce, hash[::-1].encode('hex')do the convertinghexadecimaldata which is stored asstringtype data inside thehashvariable and writing those data to the console asstringtype.if hash[::-1] < target_str:is performing conditional operations, and it compares if the hexadecimal value with data inside thehashvariable? how can it do that, as integers can't store that kind of bigger hexadecimal values to compare with another bigger value ? aren't those hexadecimal by now?
that is all I think happens inside it, please be kind enough to go through my question and explain.
Aucun commentaire:
Enregistrer un commentaire