-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.py
205 lines (113 loc) · 5.54 KB
/
blockchain.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
import hashlib
import networking
import time
from time import sleep
import ecdsa
#Mining difficulty
difficulty = 5
print("Difficulty: " + str(difficulty) + "\n")
#Maximum amount of time (in minutes) that the oldest block will have to wait before being added to a block
max_transaction_time = 1
print("Max transaction wait time: " + str(max_transaction_time) + "\n\n\n")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DO NOT CHANGE THESE VARIABLES!!!
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#Time oldest transaction has been waiting to be added to block
#(starts at 0, and a Unix-time replaces it once a transaction is added)
transaction_countdown = 0
#List of transactions that have been verified and are waiting to be added to block
pending_transactions = []
#Blockchain object/list
blockchain = []
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
genesis = {
'block_data': {
'index': 0,
'previous_hash': "0"*64
},
'transactions':
[
"This is the genesis block of PearCoin! This currency has a fixed value of 1 (one) Australian Dollar; no more, and no less."
],
'block_hash':
{
'nonce': "",
'hash': "0fcc64eda3ca6a55fd06fb0676540e68af26da8f553afeb9e6acc7392ac85de5"
}
}
blockchain.append(genesis)
#Function for easy hashing of any object or string
def hash(*args):
hash = ""
for arg in args:
hash += str(arg)
return str(hashlib.sha256(hash.encode('utf-8')).hexdigest())
'''Check that the fields are filled, sender has sufficient funds,
sender's signature is correct, a suitable fee is attached'''
def verify_transaction(transaction):
new_balances = {}
for x in range(len(blockchain),0 ,-1):
for y in range(len(blockchain['transactions'])):
#if statement to check if the sender's address is in the block as sender
#if statement to check if the sender's address is in the block as receiver
#if the address is not found: break
#if the address is found, use it to check the signature of the transaction
#if the sender does not have adequate funds: break
#if everything is fine:
# new_balances['sender'] = "sender_new_balance"
#find the balance of the sender and calculate their new balance
# for x in range(len(blockchain),0 ,-1):
# for y in range(len(blockchain['transactions'])):
# find the balance of the receiver and calculate their new balance
# if the address was not found, assume the balance is 0, and then add the transaction amount
# new_balances['receiver'] = "receiver_new_balance"
# transaction['new_balances'] = new_balances
pending_transactions.append(transaction)
global transaction_countdown
if transaction_countdown == 0:
transaction_countdown = time.time()
class Block():
def __init__(self):
pass
def create_and_mine(self, data=pending_transactions):
self.block = {}
self.block_data = {}
self.transactions = []
self.block_hash = {}
self.block_data['index'] = int(blockchain[-1]['block_data']['index']) + 1
self.block_data['previous_hash'] = blockchain[-1]['block_hash']['hash']
self.transactions = data
self.block['block_data'] = self.block_data
self.block['transactions'] = self.transactions
self.block_hash['nonce'] = 0
self.block_hash['hash'] = hash(str(self.block['block_data']) + str(self.block['transactions']))
while self.block_hash['hash'][:difficulty] != difficulty * "0":
self.block_hash['hash'] = hash(str(self.block['block_data'])
+ str(self.block['transactions'])
+ str(self.block_hash['nonce']))
# print("nonce: " + str(self.block_hash['nonce']) + (20 * " ") + "hash: " + self.block_hash['hash'], end='\r')
self.block_hash['nonce'] +=1
self.block['block_hash'] = self.block_hash
# return(json.dumps(self.block, indent=4))
global pending_transactions
pending_transactions = []
return(self.block)
'''All the code needed to run (just runs other functions in a loop;
taking in, validating and adding transactions from half_nodes or blocks from full_nodes)'''
def main():
print(str(blockchain[-1]) + "\n\n\n")
while 2+2 == 4:
verify_transaction(networking.listen_half_node())
if (transaction_countdown) + int(max_transaction_time * 60) <= time.time() or len(pending_transactions) >= 5:
#create block object
block = Block()
#Fill block object with data and calculate hash
#add block to blockchain and delete block
blockchain.append(block.create_and_mine())
del block
print(str(blockchain[-1]) + "\n\n\n")
# print(str(blockchain[-1]["transactions"][0]['new_balances']['sender']) + "\n\n\n")
#Apparently this is a good thing to do???
if __name__ == '__main__':
main()