@@ -26,27 +26,24 @@ def block_mining(self):
26
26
self .nonce += 1
27
27
self .hash = self .hash_calculation ()
28
28
29
- # print ("Block mined:" + self.hash + " nonce:" + str(self.nonce))#
29
+ # print("Block mined:" + self.hash + " nonce:" + str(self.nonce))
30
30
31
31
32
32
class BlockChain (object ):
33
33
34
34
def __init__ (self , difficulty ):
35
- self .chain = [self .genesis_block ]
35
+ self .chain = [self .genesis_block () ]
36
36
self .difficulty = difficulty
37
37
38
- @staticmethod
39
38
def genesis_block (self ):
40
39
return Block (2 , 0 , "GENESIS" , "xxx" )
41
40
42
- @staticmethod
43
41
def get_prev_block (self ):
44
- block = self .chain [len (self .chain )- 1 ]
45
- return block .hash
42
+ return self .chain [len (self .chain )- 1 ]
46
43
47
44
def add_block (self , new_block ):
48
45
49
- new_block .previous_hash = self .get_prev_block
46
+ new_block .previous_hash = self .get_prev_block (). hash
50
47
new_block .block_mining ()
51
48
self .chain .append (new_block )
52
49
@@ -56,19 +53,16 @@ def chain_validation(self):
56
53
for k , block in enumerate (self .chain ):
57
54
58
55
if k > 0 :
59
- print (dir (block ))
60
56
prev_block = self .chain [k - 1 ]
61
- if block .hash != block .hash_calculation :
57
+
58
+ if block .hash != block .hash_calculation ():
62
59
return False
63
60
if block .previous_hash != prev_block .hash :
64
61
return False
65
-
66
62
else :
67
-
68
63
pass
69
64
return True
70
65
71
-
72
66
diff = 3
73
67
74
68
data1 = json .dumps ({'username' : 'Pippo' , 'amount' : 1.00 }, sort_keys = True , indent = 4 )
@@ -80,9 +74,10 @@ def chain_validation(self):
80
74
b .add_block (Block (diff , 1 , data1 ))
81
75
b .add_block (Block (diff , 2 , data2 ))
82
76
b .add_block (Block (diff , 3 , data3 ))
77
+ # uncomment line below to invalidate the chain by tampering 1st block with new data (PoW)
83
78
# b.chain[1].hash = "fega" #PoW
84
79
b .add_block (Block (diff , 4 , data4 ))
85
- print ("Chain is valid?: " + str (b .chain_validation ))
80
+ print ("Chain is valid?: " + str (b .chain_validation ) + " \r \n " )
86
81
87
- for elements in b .chain :
88
- print ("data: " + elements .data + " hash: " + elements .hash )
82
+ # for elements in b.chain:
83
+ # print("data: " + elements.data + " hash: " + elements.hash)
0 commit comments