-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxBlock.py
210 lines (162 loc) · 6.02 KB
/
txBlock.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
# Downloaded packages
import pickle
import time
import random
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
# Personal packages
from blockchain import CBlock
from signature import generate_keys, sign, verify
from transaction import Transaction as Tx
leading_zeros=2
reward = 25.0
next_char_limit = 20
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 transaction in self.data:
total_in += transaction.total_in
total_out += transaction.total_out
return total_in, total_out
def is_valid(self, show="False"):
if not super(TxBlock, self).is_valid():
return False
for transaction in self.data:
if not transaction.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):
for i in range(1000000):
self.nonce = "".join([
chr(random.randint(0,255)) for i in range(10*leading_zeros)])
if self.good_nonce():
return self.nonce
return None
if __name__ == '__main__':
print("txBlock.py tests start here")
private_key1, public_key1 = generate_keys()
private_key2, public_key2 = generate_keys()
private_key3, public_key3 = generate_keys()
private_key4, public_key4 = generate_keys()
Tx1 = Tx()
Tx1.add_input(public_key1, 1)
Tx1.add_output(public_key2, 1)
Tx1.sign(private_key1)
print('Tx{0} validation status before loading is: {1}'.format(1, Tx1.is_valid()))
savefile = open("save.dat", "wb")
pickle.dump(Tx1, savefile)
savefile.close()
loadfile = open("save.dat", "rb")
loadedTx = pickle.load(loadfile)
loadfile.close()
print('Tx{0} validation status after loading is: {1}'.format(1, loadedTx.is_valid()))
root = TxBlock(None)
root.addTx(Tx1)
Tx2 = Tx()
Tx2.add_input(public_key2, 1.1)
Tx2.add_output(public_key3, 1)
Tx2.sign(private_key2)
root.addTx(Tx2)
B1 = TxBlock(root)
Tx3 = Tx()
Tx3.add_input(public_key3, 1.1)
Tx3.add_output(public_key1, 1)
Tx3.sign(private_key3)
B1.addTx(Tx3)
Tx4 = Tx()
Tx4.add_input(public_key1, 1.1)
Tx4.add_output(public_key2, 1)
Tx4.add_required(public_key3)
Tx4.sign(private_key1)
Tx4.sign(private_key3)
B1.addTx(Tx4)
start = time.time()
mynonce = B1.find_nonce()
#print(mynonce)
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 name, block in [("root", root), ("B1", B1), ("load_B1", load_B1), ("load_B1.previousBlock", load_B1.previousBlock)]:
if block.is_valid():
print('Success! {0} block is valid.'.format(name))
else:
print('Error! {0} block is invalid.'.format(name))
if B1.good_nonce():
print("Success! Nonce is good after save and load!")
else:
print("ERROR! Bad nonce after load")
BadB2 = TxBlock(B1)
Tx5 = Tx()
Tx5.add_input(public_key3, 1)
Tx5.add_output(public_key1, 100)
Tx5.sign(private_key3)
BadB2.addTx(Tx5)
load_B1.previousBlock.addTx(Tx4)
for name, block in [("BadB2", BadB2), ("load_B1", load_B1)]:
if block.is_valid():
print('Error! Bad {0} block verified.'.format(name))
else:
print('Success! Bad {0} block detected.'.format(name))
# Test mining reward and TX fees
Tx6 = Tx()
Tx6.add_output(public_key4, 25)
B3 = TxBlock(B1)
B3.addTx(Tx2)
B3.addTx(Tx3)
B3.addTx(Tx4)
B3.addTx(Tx6)
if B3.is_valid(show='True'):
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(public_key4, 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(public_key4, 26.2)
B5.addTx(Tx8)
if not B5.is_valid():
print("Success! Greedy miner detected.")
else:
print("Error! Greedy miner not detected.")