Skip to content

Commit dc365b7

Browse files
authored
Update blockchain.py
1 parent af3a18b commit dc365b7

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

blockchain.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,24 @@ def block_mining(self):
2626
self.nonce += 1
2727
self.hash = self.hash_calculation()
2828

29-
# print ("Block mined:" + self.hash + " nonce:" + str(self.nonce))#
29+
# print("Block mined:" + self.hash + " nonce:" + str(self.nonce))
3030

3131

3232
class BlockChain(object):
3333

3434
def __init__(self, difficulty):
35-
self.chain = [self.genesis_block]
35+
self.chain = [self.genesis_block()]
3636
self.difficulty = difficulty
3737

38-
@staticmethod
3938
def genesis_block(self):
4039
return Block(2, 0, "GENESIS", "xxx")
4140

42-
@staticmethod
4341
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]
4643

4744
def add_block(self, new_block):
4845

49-
new_block.previous_hash = self.get_prev_block
46+
new_block.previous_hash = self.get_prev_block().hash
5047
new_block.block_mining()
5148
self.chain.append(new_block)
5249

@@ -56,19 +53,16 @@ def chain_validation(self):
5653
for k, block in enumerate(self.chain):
5754

5855
if k > 0:
59-
print(dir(block))
6056
prev_block = self.chain[k-1]
61-
if block.hash != block.hash_calculation:
57+
58+
if block.hash != block.hash_calculation():
6259
return False
6360
if block.previous_hash != prev_block.hash:
6461
return False
65-
6662
else:
67-
6863
pass
6964
return True
7065

71-
7266
diff = 3
7367

7468
data1 = json.dumps({'username': 'Pippo', 'amount': 1.00}, sort_keys=True, indent=4)
@@ -80,9 +74,10 @@ def chain_validation(self):
8074
b.add_block(Block(diff, 1, data1))
8175
b.add_block(Block(diff, 2, data2))
8276
b.add_block(Block(diff, 3, data3))
77+
# uncomment line below to invalidate the chain by tampering 1st block with new data (PoW)
8378
# b.chain[1].hash = "fega" #PoW
8479
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")
8681

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

Comments
 (0)