-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWallet.py
More file actions
142 lines (121 loc) · 4.26 KB
/
Copy pathWallet.py
File metadata and controls
142 lines (121 loc) · 4.26 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
#Wallet
import pickle
import SocketUtils
import Transactions
import TxBlock
import Signatures
head_blocks = [None]
wallets = [('localhost',5006)]
miners = [('localhost',5005)]
break_now = False
verbose = False
my_private, my_public = Signatures.generate_keys()
def stopAll():
global break_now
break_now = True
def walletServer(my_addr):
global head_blocks
head_blocks = [None]
server = SocketUtils.newServerConnection('localhost',5006)
while not break_now:
newBlock = SocketUtils.recvObj(server)
if isinstance(newBlock,TxBlock.TxBlock):
if verbose:
print("Rec'd block")
for b in head_blocks:
if b == None:
if newBlock.previousHash == None:
newBlock.previousBlock = b
if not newBlock.is_valid():
print("Error! newBlock is not valid")
else:
head_blocks.remove(b)
head_blocks.append(newBlock)
if verbose:
print("Added to head_blocks")
elif newBlock.previousHash == b.computeHash():
newBlock.previousBlock = b
if not newBlock.is_valid():
print("Error! newBlock is not valid")
else:
head_blocks.remove(b)
head_blocks.append(newBlock)
if verbose:
print("Added to head_blocks")
#TODO What if I add to an earlier (non-head) block?
server.close()
return True
def getBalance(pu_key):
long_chain = TxBlock.findLongestBlockchain(head_blocks)
this_block = long_chain
bal = 0.0
while this_block != None:
for tx in this_block.data:
for addr,amt in tx.inputs:
if addr == pu_key:
bal = bal - amt
for addr,amt in tx.outputs:
if addr == pu_key:
bal = bal + amt
this_block = this_block.previousBlock
return bal
def sendCoins(pu_send, amt_send, pr_send, pu_recv, amt_recv, miner_list):
newTx = Transactions.Tx()
newTx.add_input(pu_send, amt_send)
newTx.add_output(pu_recv, amt_recv)
newTx.sign(pr_send)
SocketUtils.sendObj('localhost',newTx)
return True
def loadKeys(pr_file, pu_file):
return Signatures.loadPrivate(pr_file), Signatures.loadPublic(pu_file)
if __name__ == "__main__":
import time
import Miner
import threading
import Signatures
miner_pr, miner_pu = Signatures.generate_keys()
t1 = threading.Thread(target=Miner.minerServer, args=(('localhost',5005),))
t2 = threading.Thread(target=Miner.nonceFinder, args=(wallets, miner_pu))
t3 = threading.Thread(target=walletServer, args=(('localhost',5006),))
t1.start()
t2.start()
t3.start()
pr1,pu1 = loadKeys("private.key", "public.key")
pr2,pu2 = Signatures.generate_keys()
pr3,pu3 = Signatures.generate_keys()
#Query balances
bal1 = getBalance(pu1)
print(bal1)
bal2 = getBalance(pu2)
bal3 = getBalance(pu3)
#Send coins
sendCoins(pu1, 1.0, pr1, pu2, 1.0, miners)
sendCoins(pu1, 1.0, pr1, pu3, 0.3, miners)
time.sleep(30)
#Save/Load all blocks
TxBlock.saveBlocks(head_blocks, "AllBlocks.dat")
head_blocks = TxBlock.loadBlocks("AllBlocks.dat")
#Query balances
new1 = getBalance(pu1)
print(new1)
new2 = getBalance(pu2)
new3 = getBalance(pu3)
#Verify balances
if abs(new1-bal1+2.0) > 0.00000001:
print("Error! Wrong balance for pu1")
else:
print("Success. Good balance for pu1")
if abs(new2-bal2-1.0) > 0.00000001:
print("Error! Wrong balance for pu2")
else:
print("Success. Good balance for pu2")
if abs(new3-bal3-0.3) > 0.00000001:
print("Error! Wrong balance for pu3")
else:
print("Success. Good balance for pu3")
Miner.stopAll()
stopAll()
t1.join()
t2.join()
t3.join()
print ("Exit successful.")