-
Notifications
You must be signed in to change notification settings - Fork 7
/
pyblock.py
75 lines (61 loc) · 2.27 KB
/
pyblock.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
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
68
69
70
71
72
73
74
75
import datetime
import hashlib
from huepy import *
def banner():
print("\n\n\n")
print(lightpurple ("\t #### # # #### # ### #### # # "))
print(lightred ("\t # # # # # # # # # # # # "))
print(lightcyan ("\t #### ### #### # # # # ## "))
print(lightred ("\t # # # # # # # # # # "))
print(lightpurple ("\t # # #### #### ### #### # # by @howCodeOrg and @debugger "))
print("\n\n\n")
banner()
total = int(input(white("Enter the total number of blocks:")))
diff = int(input(white("Set difficulty from (0,10,20):")))
class Block:
blockNo = 0
data = None
next = None
hash = None
nonce = 0
previous_hash = 0x0
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')
)
return h.hexdigest()
def __str__(self):
return good(lightred("Block Hash: ")) + white(str(self.hash())) + "\n" + good(lightgreen("BlockNo: ")) + white(str(self.blockNo)) + "\n" + good(lightblue("Block Data: ")) + white(str(self.data)) + "\n" + good(lightcyan("Hashes: ")) + white(str(self.nonce)) + "\n--------------"
class Blockchain:
diff = 20
maxNonce = 2**32
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(total):
blockchain.mine(Block("Block " + str(n+1)))
while blockchain.head != None:
print(blockchain.head)
blockchain.head = blockchain.head.next