11import time
22
33from backend .util .crypto_hash import crypto_hash
4+ < << << << HEAD
5+ == == == =
6+ from backend .util .hex_to_binary import hex_to_binary
7+ > >> >> >> a77fccd ... Complete proof of work section
48from backend .config import MINE_RATE
59
610GENESIS_DATA = {
@@ -25,9 +29,12 @@ def __init__(self, timestamp, last_hash, hash, data, difficulty, nonce):
2529 self .difficulty = difficulty
2630 self .nonce = nonce
2731
32+ < << << << HEAD
2833 def add_block (self , data ):
2934 self .chain .append (Block (data ))
3035
36+ == == == =
37+ >> >> >> > a77fccd ... Complete proof of work section
3138 def __repr__ (self ):
3239 return (
3340 'Block('
@@ -51,7 +58,7 @@ def mine_block(last_block, data):
5158 nonce = 0
5259 hash = crypto_hash (timestamp , last_hash , data , difficulty , nonce )
5360
54- while hash [0 :difficulty ] != '0' * difficulty :
61+ while hex_to_binary ( hash ) [0 :difficulty ] != '0' * difficulty :
5562 nonce += 1
5663 timestamp = time .time_ns ()
5764 difficulty = Block .adjust_difficulty (last_block , timestamp )
@@ -70,8 +77,8 @@ def genesis():
7077 def adjust_difficulty (last_block , new_timestamp ):
7178 """
7279 Calculate the adjusted difficulty according to the MINE_RATE.
73- Increase the difficulty for slowly mined blocks.
7480 Decrease the difficulty for slowly mined blocks.
81+ Increase the difficulty for quickly mined blocks.
7582 """
7683 if (new_timestamp - last_block .timestamp ) < MINE_RATE :
7784 return last_block .difficulty + 1
@@ -84,7 +91,14 @@ def adjust_difficulty(last_block, new_timestamp):
8491def main ():
8592 genesis_block = Block .genesis ()
8693 block = Block .mine_block (genesis_block , 'foo' )
94+ < << << << HEAD
8795 print (f'block: { block } ' )
8896
8997if __name__ == '__main__' :
9098 main ()
99+ == == == =
100+ print (block )
101+
102+ if __name__ == '__main__' :
103+ main ()
104+ > >> >> >> a77fccd ... Complete proof of work section
0 commit comments