-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
211 lines (174 loc) · 5.9 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Import in our libraries
import datetime as dt
import hashlib as hasher
import json
import os
import random
# An array that will store our local JSON data, this is for verifying the blockchain
jarr = []
coins_amount = int(input("How many coins? "))
p = input("Set your password to whatever you want! ")
username = input("What is your username? ")
print("*****************************************")
# Set the number of coins
def set_coins(n):
coins_amount = n
return coins_amount
# Set password
def set_password(password):
p = password
return p
# This will set jarr(JSON Array) to the contents of the temporary blockchain. This is needed for other functions.
def read_blockchain():
with open("temp_blockchain.json", 'r', encoding='utf-8') as f:
jsonarr = json.loads(f.read())
return jsonarr
# Create the base block
class Block:
# These will be the defining characteristics of the Block, which will be used later on.
def __init__(self, index: int, timestamp: dt.date, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.hash_block()
# This will be how we obtain the SHA256 Hash based on data.
def hash_block(self):
sha = hasher.sha256()
new_index, new_timestamp, new_data, prev_hash = (str(
self.index).encode(), str(self.timestamp).encode(), str(
self.data).encode(), str(self.previous_hash).encode())
sha.update(new_index + new_timestamp + new_data + prev_hash)
return sha.hexdigest()
# Add attributes of the block to the array, which will then be added to the JSON file.
def update_blockchain(b: Block):
with open("temp_blockchain.json", 'w', encoding='utf-8') as feedjson:
entry = {
'index': b.index,
'timestamp': b.timestamp.strftime("%m/%d/%Y, %H:%M:%S"),
'data': b.data,
'hash': b.hash,
'previous_hash': b.previous_hash,
}
jarr.append(entry)
json.dump(jarr, feedjson, indent=4)
# Simply create a new block, and add it to the blockchain.
def create_genesis_block():
genesis_data = {'to': None, 'from': None, 'amount': 0}
return Block(0, dt.datetime.now(), genesis_data, "0000")
# Create a new block, and add it to the blockchain based on the previous one.
def create_next_block(last_block):
global coins_amount
new_index = last_block.index + 1
new_timestamp = dt.datetime.now()
new_data = {
'to':
input(
f"Block {new_index}\n******************************\nWho would you like to send this to? "
),
'from':
username,
'amount':
int(input("How much would you like to send? ")),
}
# User auth is needed for transfer
password = input("In addition, what is your password? ")
if new_data['amount'] > coins_amount:
print("You don't have enough coins!")
exit()
else:
coins_amount -= new_data['amount']
print(f"You now have {coins_amount} coins")
if password != p:
print("Not authorized user")
print(new_data['from'] + " is not " + "the password.")
exit()
# Setting the new hash
new_hash = last_block.hash
# Adding a way to mine
choice = input("Would you like to mine coins? [y/n]").lower()
if choice == 'y':
mine_add_block()
else:
pass
return Block(new_index, new_timestamp, new_data, new_hash)
# See if the blockchain is valid.
def verify_chain():
with open("temp_blockchain.json", 'r', encoding='utf-8') as feedjson:
j = json.load(feedjson)
for i in range(1, len(j)):
block = j[i]
B = Block(block['index'],
dt.datetime.strptime(block['timestamp'], "%m/%d/%Y, %H:%M:%S"),
block['data'], block['previous_hash'])
prev_block = j[i - 1]
PB = Block(
prev_block['index'],
dt.datetime.strptime(block['timestamp'], "%m/%d/%Y, %H:%M:%S"),
block['data'],
block['hash'],
)
if B.hash != B.hash_block():
print("Invalid block hash")
return False
if block['previous_hash'] != prev_block['hash']:
print("Invalid block hash becasue block prev_hash is not pb hash")
return False
print("The blockchain is valid!")
return True
#Solve a simple math problem and thus add to the number of coins.
def mine_add_block():
global coins_amount
global jarr
jarr = read_blockchain()
NB = None
NB = Block(jarr[-1]['index'] + 1, jarr[-1]['timestamp'], jarr[-1]['data'],
jarr[-1]['hash'])
num1 = random.randint(0, 9)
num2 = random.randint(0, 9)
print(f"What is {num1} plus {num2}?")
ans = int(input(""))
if ans == num1 + num2:
coins_amount += 100
print("*******************************************")
print(f"The amount of coins now is {coins_amount}")
else:
pass
print("You got the answer wrong!")
# Create a demonstration of the blockchain.
def demo():
jsonarr = []
#Read jsonarr
if os.path.getsize("temp_blockchain.json") == 0:
pass
else:
jsonarr = read_blockchain()
# Create a genesis block
if len(jsonarr) > 1:
previous_block = Block(
jsonarr[-1]['index'],
dt.datetime.strptime(jsonarr[-1]['timestamp'], "%m/%d/%Y, %H:%M:%S"),
jsonarr[-1]['data'],
jsonarr[-1]['hash'],
)
else:
previous_block = create_genesis_block()
update_blockchain(previous_block)
#How many transactions?
blocks_to_make = int(input("How many transactions do you want to make? "))
for _ in range(blocks_to_make):
new_block = create_next_block(previous_block)
update_blockchain(new_block)
previous_block = new_block
# Print out new block
print(f"\nBlock #{new_block.index} has been added")
print(f"Timestamp: {new_block.timestamp}")
print(f"Data: {new_block.data}")
print("Hash: " + new_block.hash)
print("Previous Hash " + new_block.previous_hash)
print("\n\n")
print(f"{blocks_to_make} blocks have been added to the blockchain")
verify_chain()
def test_out():
demo()
demo()