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
ver
is a variable and assignsinteger
type value of2
to itself viaver = 2
prev_block
is a variable and it assignsstring
type value of"000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
to itself.krkl_root
is a variable and it assignsstring
type value of"871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a"
to itselftime_
is a variable and it assignsinteger
type value of thehexadecimal
of0x53058b35
(that means this converts from hexadecimal on the fly and stores the decimal value1392872245
to it's variable?)bits
is a variable and it assignsinteger
type value of thehexadecimal
of0x19015f53
that 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.
mant
is a variable and it assigns it self aninteger
type of value ofbits
variable and then again it converts thehex
value of0xffffff
and stacks and append the existing value inmant
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?).
target_str
is a variable and it converts whatever type of data (is the data inside it hexademimal?) inside thetarget_hexstr
variable to binary format and assigns that converted binary data to it self (target_str
)nonce
is a variable and it assigns theinteger
type of value0
to it self.while nonce < 0x100000000:
starts a while loop and it loops until the value ofnonce
variable reaches the value of0x100000000
(4294967296 in integer)header
is a variable ofstring
type and it assigns it self (stacks) the following data ?.(struct.pack("<L", ver)
is reading what's inside ofver
variable which is aninteger
type of value and converts that value tolong
type?+
sign performs addition operation with what it gets next as data.prev_block.decode('hex')[::-1]
is reading what's inside ofprev_block
variable (string ofhexadecimal
?) and converts it tobinary
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 ifL
is just converting the data tolong
type what does this<LLL
is doing? and will this just stack the data in those variables ininteger
type such as 1392872245|419520339|0 oftime_
,bits
andnonce
variables respectively?hash
is a variable and it converts the data inside theheader
variable (string type data?) toSHA256
encoding and again it encodes the encoded data toSHA256
encoding (encoding twice) and assigns that data as astring
type data it self.print nonce, hash[::-1].encode('hex')
do the convertinghexadecimal
data which is stored asstring
type data inside thehash
variable and writing those data to the console asstring
type.if hash[::-1] < target_str:
is performing conditional operations, and it compares if the hexadecimal value with data inside thehash
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