-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblockchain.py
More file actions
67 lines (53 loc) · 1.76 KB
/
blockchain.py
File metadata and controls
67 lines (53 loc) · 1.76 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import datetime
import hashlib
class Block:
blockNo=0
data=None
next=None
hash=None
nonce=0
previous_hash=0x0
#in hash(var)=0x0 when var is 0
timestamp=datetime.datetime.now()
def __init__(self,data):
self.data=data
def hash(self):
h=hashlib.sha256()
h.update(
str(self.nonce).encode('utf-8')+
str(self.data).encode('utf-8')+
str(self.previous_hash).encode('utf-8')+
str(self.timestamp).encode('utf-8')+
str(self.blockNo).encode('utf-8')
)
# encode(): Converts the string into bytes to be acceptable by hash function
return h.hexdigest()
# digest(): Returns the encoded data in byte format.
# hexdigest(): Returns the encoded data in hexadecimal format.
def __str__(self):
return "Block hash:"+str(self.hash())+"\nBlockNo:"+str(self.blockNo)+"\nBlock Data:"+str(self.data)+"\nHashes:"+str(self.nonce)+"\n---------------"
class Blockchain:
diff=20
maxNonce=2**32 # we can maxNumber stored as 32 bit system
target=2**(256-diff)
block=Block("Genesis")
dummy=head=block
def add(self,block):
block.previous_hash=self.block.hash()
block.blockNo=self.block.blockNo+1
self.block.next=block
self.block=self.block.next
def mine(self,block):
for n in range(self.maxNonce):
if int(block.hash(),16)<=self.target:
self.add(block)
print(block)
break
else:
block.nonce +=1
blockchain=Blockchain()
for n in range(10):
blockchain.mine(Block("Block"+str(n+1)))
while blockchain.head !=None:
print(blockchain.head)
blockchain.head=blockchain.head.next