-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTxBlock.py
More file actions
213 lines (187 loc) · 5.65 KB
/
Copy pathTxBlock.py
File metadata and controls
213 lines (187 loc) · 5.65 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
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
212
213
#TxBlock
from BlockChain import CBlock
from Signatures import generate_keys, sign, verify
from Transactions import Tx
import pickle
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
import random
from cryptography.hazmat.primitives import hashes
import time
reward = 25.0
leading_zeros = 2
next_char_limit = 255
class TxBlock (CBlock):
nonce = "AAAAAAA"
def __init__(self, previousBlock):
super(TxBlock, self).__init__([], previousBlock)
def addTx(self, Tx_in):
self.data.append(Tx_in)
def count_totals(self):
total_in = 0
total_out = 0
for tx in self.data:
for addr, amt in tx.inputs:
total_in = total_in + amt
for addr, amt in tx.outputs:
total_out = total_out + amt
return total_in, total_out
def is_valid(self):
if not super(TxBlock, self).is_valid():
return False
for tx in self.data:
if not tx.is_valid():
return False
total_in, total_out = self.count_totals()
if total_out - total_in - reward > 0.000000000001:
return False
return True
def good_nonce(self):
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(bytes(str(self.data),'utf8'))
digest.update(bytes(str(self.previousHash),'utf8'))
digest.update(bytes(str(self.nonce),'utf8'))
this_hash = digest.finalize()
if this_hash[:leading_zeros] != bytes(''.join([ '\x4f' for i in range(leading_zeros)]),'utf8'):
return False
return int(this_hash[leading_zeros]) < next_char_limit
def find_nonce(self,n_tries=1000000):
for i in range(n_tries):
self.nonce = ''.join([
chr(random.randint(0,255)) for i in range(10*leading_zeros)])
if self.good_nonce():
return self.nonce
return None
def findLongestBlockchain(head_blocks):
longest = -1
long_head = None
for b in head_blocks:
current = b
this_len = 0
while current != None:
this_len = this_len + 1
current = current.previousBlock
if this_len > longest:
long_head = b
longest = this_len
return long_head
def saveBlocks(block_list, filename):
fp = open(filename, "wb")
pickle.dump(block_list, fp)
fp.close()
return True
def loadBlocks(filename):
fin = open(filename, "rb")
ret = pickle.load(fin)
fin.close()
return ret
if __name__ == "__main__":
pr1, pu1 = generate_keys()
pr2, pu2 = generate_keys()
pr3, pu3 = generate_keys()
Tx1 = Tx()
Tx1.add_input(pu1, 1)
Tx1.add_output(pu2, 1)
Tx1.sign(pr1)
if Tx1.is_valid():
print("Success! Tx is valid")
savefile = open("tx.dat", "wb")
pickle.dump(Tx1, savefile)
savefile.close()
loadfile = open("tx.dat", "rb")
newTx = pickle.load(loadfile)
if newTx.is_valid():
print("Sucess! Loaded tx is valid")
loadfile.close()
root = TxBlock(None)
root.addTx(Tx1)
Tx2 = Tx()
Tx2.add_input(pu2,1.1)
Tx2.add_output(pu3, 1)
Tx2.sign(pr2)
root.addTx(Tx2)
B1 = TxBlock(root)
Tx3 = Tx()
Tx3.add_input(pu3,1.1)
Tx3.add_output(pu1, 1)
Tx3.sign(pr3)
B1.addTx(Tx3)
Tx4 = Tx()
Tx4.add_input(pu1,1)
Tx4.add_output(pu2, 1)
Tx4.add_reqd(pu3)
Tx4.sign(pr1)
Tx4.sign(pr3)
B1.addTx(Tx4)
start = time.time()
print(B1.find_nonce())
elapsed = time.time() - start
print("elapsed time: " + str(elapsed) + " s.")
if elapsed < 60:
print("ERROR! Mining is too fast")
if B1.good_nonce():
print("Success! Nonce is good!")
else:
print("ERROR! Bad nonce")
savefile = open("block.dat", "wb")
pickle.dump(B1, savefile)
savefile.close()
loadfile = open("block.dat" ,"rb")
load_B1 = pickle.load(loadfile)
for b in [root, B1, load_B1, load_B1.previousBlock]:
if b.is_valid():
print ("Success! Valid block")
else:
print ("ERROR! Bad block")
if B1.good_nonce():
print("Success! Nonce is good after save and load!")
else:
print("ERROR! Bad nonce after load")
B2 = TxBlock(B1)
Tx5 = Tx()
Tx5.add_input(pu3, 1)
Tx5.add_output(pu1, 100)
Tx5.sign(pr3)
B2.addTx(Tx5)
load_B1.previousBlock.addTx(Tx4)
for b in [B2, load_B1]:
if b.is_valid():
print ("ERROR! Bad block verified.")
else:
print ("Success! Bad blocks detected")
# Test mining rewards and tx fees
pr4, pu4 = generate_keys()
B3 = TxBlock(B2)
B3.addTx(Tx2)
B3.addTx(Tx3)
B3.addTx(Tx4)
Tx6 = Tx()
Tx6.add_output(pu4,25)
B3.addTx(Tx6)
if B3.is_valid():
print ("Success! Block reward succeeds")
else:
print("ERROR! Block reward fail")
B4 = TxBlock(B3)
B4.addTx(Tx2)
B4.addTx(Tx3)
B4.addTx(Tx4)
Tx7 = Tx()
Tx7.add_output(pu4,25.2)
B4.addTx(Tx7)
if B4.is_valid():
print ("Success! Tx fees succeeds")
else:
print("ERROR! Tx fees fail")
#Greedy miner
B5 = TxBlock(B4)
B5.addTx(Tx2)
B5.addTx(Tx3)
B5.addTx(Tx4)
Tx8 = Tx()
Tx8.add_output(pu4,26.2)
B5.addTx(Tx8)
if not B5.is_valid():
print ("Success! Greedy miner detected")
else:
print("ERROR! Greedy miner not detected")