-
Notifications
You must be signed in to change notification settings - Fork 0
/
block2.py
42 lines (34 loc) · 1.25 KB
/
block2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
blockchain by Vincent LIU and Samir SAYAH - MAIN - Polytech Sorbonne
Inspired by: https://anders.com/blockchain/blockchain.html
"""
from hash import sha256
from block1 import Block, difficulty, pattern
class Block_v2(Block):
"""
Block with previous attribute
"""
def __init__(self, block = 0, nonce = 0, data = ""):
Block.__init__(self, block, nonce, data)
self.previous = self.next = None
@property
def hash(self):
s = self.previous.hash if self.previous else "0" * 64
return sha256(str(self.block) + str(self.nonce) + str(self.data) + s)
def text_hash(self, i):
s = self.previous.hash if self.previous else "0" * 64
return sha256(str(self.block) + str(i) + str(self.data) + s)
def show(self):
# Your result
print("\n=================")
print("===== Block =====")
print("=================\n")
print("Block: #{}".format(self.block))
print("Nonce: {}".format(self.nonce))
print("Data: {}".format(self.data))
if self.previous:
print("Previous: {}".format(self.previous.hash))
else:
print("Previous: " + "0" * 64)
print("Hash: {}".format(self.hash))
print("Valid: {}\n".format(self.valid))