-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui_test.py
161 lines (123 loc) · 4.52 KB
/
gui_test.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
import peers
from core.nodes import Miner, Node, Wallet
from core.base import indieChain, Block
import tkinter as tk
from tkinter import *
from tkinter import Canvas, Entry, Label
import logging
from time import sleep
import _thread
from threading import Thread
from core.base import UTXO, Transaction
import random
# loggin.Logger.set
logging.basicConfig(level=logging.DEBUG)
#logging.basicConfig(level=logging.ERROR)
main_port = random.randint(4000, 9999)
nodelist = ['a', 'b', 'c', 'd','e','f','g','h']
man1, man2 = None, None
wal1, wal2 = None, None
def main(port):
global man1, wal1
man = peers.Manager('localhost', port)
man1 = man
ic = indieChain()
miner = Miner(ic)
man.setNode(miner)
miner.setNetwork(man)
#print(miner.getNodePublicKey().exportKey())
try:
man.activity_loop()
except Exception as e:
man.close()
raise(e)
def peer1(id, main_port, recvaddr, amount,sender):
global man2, wal2
sleep(1)
man = peers.Manager('localhost', main_port + id)
man2 = man
ic = indieChain()
node = Node(ic)
man.setNode(node)
node.setNetwork(man)
wallet = Wallet(node, 10000)
wal2 = wallet
try:
loop = man.loop
peer = loop.run_until_complete(man.connect_to_peer('localhost', main_port))
sleep(1)
# loop.run_until_complete(peer.send_raw_data(b'asdfa'))
# man.broadcastTrx(t1)
for i in range(0, len(recvaddr), 2):
wallet.finalizeTransaction([wallet.makePayment(recvaddr[i], int(amount[i]) ), wallet.makePayment(recvaddr[i+1], int(amount[i+1]) )])
# man.broadcastToMiners(b)
if len(recvaddr)%2 == 1:
j = len(recvaddr) -1
wallet.finalizeTransaction([wallet.makePayment(recvaddr[j], int(amount[j]) ), wallet.makePayment(sender,0) ] )
print(peer._key.exportKey())
# blk = loop.run_until_complete(man.getBlock(peer.id, '3248234a983b7894d923c49'))
# print(blk)
man.activity_loop()
except Exception as e:
man.close()
raise(e)
class MainWindow(tk.Frame):
wallet_counter = 0
miner_counter = 0
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
# self.button1 = tk.Button(self, text="Create new node", command=self.create_node)
#self.button2 = tk.Button(self, text="Create new miner", command=self.create_miner)
self.button3 = tk.Button(self, text="Create new wallet", command=self.create_wallet)
# self.button1.pack(side="top")
#self.button2.pack(side="top")
self.button3.pack(side="top")
def create_wallet(self):
self.wallet_counter += 1
self.recvaddr = []
self.amount = []
t = tk.Toplevel(self)
t.wm_title("Wallet #%s" % self.wallet_counter)
lr = Label(t,text='Receiver address: ').pack(side=TOP, padx=10, pady=10)
entryr = Entry(t, width=20)
entryr.pack(side=TOP,padx=10,pady=10)
ln = Label(t,text='Amount: ').pack(side=TOP, padx=10, pady=10)
entryn = Entry(t, width=20)
entryn.pack(side=TOP,padx=10,pady=10)
def onTransact():
recvaddr1 = entryr.get()
self.recvaddr = recvaddr1.split(',')
amount1 = entryn.get()
self.amount = amount1.split(',')
sender = nodelist[self.wallet_counter]
try:
t1 = Thread(target=main, args=(main_port,))
t1.start()
t2 = Thread(target=peer1, args=(1, main_port, self.recvaddr, self.amount, sender) )
t2.start()
t1.join()
print("Thread-1 joined")
except KeyboardInterrupt as kb:
pass
except Exception as e:
print("Error: unable to start thread")
raise(e)
Button(t, text='GO', command=onTransact).pack(side=TOP)
root = tk.Tk()
main_win = MainWindow(root)
main_win.pack(side="top", fill="both", expand=True)
root.mainloop()
# def peer_tester(id, main_port):
# man = peers.Manager('localhost', 5003)
# try:
# loop = man.loop
# peer = loop.run_until_complete(man.connect_to_peer('localhost', main_port))
# loop.run_until_complete(peer.send_raw_data(b'derfadsa'))
# loop.ensure_future(peer.send_raw_data(b'derfadsa'))
# print(loop._ready)
# loop.run_forever()
# print("DAta sent")
# except Exception as e:
# man.close()
# raise(e)
# ex: set tabstop=4 shiftwidth=4 expandtab: