samedi 18 juin 2016

Unable to understand this python code properly [on hold]


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

  1. ver is a variable and assigns integer type value of 2 to itself via ver = 2

  2. prev_block is a variable and it assigns string type value of "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717" to itself.

  3. krkl_root is a variable and it assigns string type value of "871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a" to itself

  4. time_ is a variable and it assigns integer type value of the hexadecimal of 0x53058b35 (that means this converts from hexadecimal on the fly and stores the decimal value 1392872245 to it's variable?)

  5. bits is a variable and it assigns integer type value of the hexadecimal of 0x19015f53 that is 419520339

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.

  1. mant is a variable and it assigns it self an integer type of value of bits variable and then again it converts the hex value of 0xffffffand stacks and append the existing value in mant variable (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?).

  1. target_str is a variable and it converts whatever type of data (is the data inside it hexademimal?) inside the target_hexstr variable to binary format and assigns that converted binary data to it self (target_str)
  2. nonce is a variable and it assigns the integer type of value 0 to it self.
  3. while nonce < 0x100000000: starts a while loop and it loops until the value of nonce variable reaches the value of 0x100000000 (4294967296 in integer)
  4. header is a variable of string type and it assigns it self (stacks) the following data ?.

    • (struct.pack("<L", ver) is reading what's inside of ver variable which is an integer type of value and converts that value to long type?

    • + sign performs addition operation with what it gets next as data.

    • prev_block.decode('hex')[::-1] is reading what's inside of prev_block variable (string of hexadecimal?) and converts it to binary format? 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 if L is just converting the data to long type what does this <LLL is doing? and will this just stack the data in those variables in integer type such as 1392872245|419520339|0 of time_, bits and nonce variables respectively?

    1. hash is a variable and it converts the data inside the header variable (string type data?) to SHA256 encoding and again it encodes the encoded data to SHA256 encoding (encoding twice) and assigns that data as a string type data it self.

    2. print nonce, hash[::-1].encode('hex') do the converting hexadecimal data which is stored as string type data inside the hash variable and writing those data to the console as string type.

    3. if hash[::-1] < target_str: is performing conditional operations, and it compares if the hexadecimal value with data inside the hash variable? 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