forked from qwerwon/SGBL_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.py
105 lines (93 loc) · 3.8 KB
/
command.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
import json
import multiprocessing
import socket
import threading
import base64
from mining import Mining
from peer import Peer
from txutils import generate_transaction
class Command(object):
def start_peer(self, host, port):
p = multiprocessing.Process(target=self._start_peer, args=(host, port))
p.start()
print(f'Peer running at {host}:{port}')
def _start_peer(self, host, port):
peer = Peer(host, port)
peer.start()
def connect_peer(self, host, port, target_host, target_port):
print('Connecting...')
message = {'type': 'CONNECT', 'host': target_host, 'port': target_port}
result = self._unicast(host, port, message)
if result == 'OK':
print(f'Peer {host}:{port} connected to {target_host}:{target_port}')
else:
print('Connect failed')
return result
def mine(self, host, port, data):
print('Mining work starts')
'''
message = {'type': 'MINE', 'data': data}
result = self._unicast(host, port, message)
if result == 'OK':
print('A new block was mined')
else:
print('Mine failed')
return result
'''
miningThread = threading.Thread(target=Mining.mineStart)
miningThread.start()
def stop(self):
Mining.flagdown()
print('Stop Mining.\n')
def newTx(self):
receiver = input('Address of receiver : ') #type(receiver) : string
receiver = base64.b64decode(receiver)
amount = float(input('BTC : '))
commission = float(input('Commission : '))
generate_transaction(receiver, amount, commission)
def get_chain(self, host, port):
message = {'type': 'SHOW'}
result = self._unicast(host, port, message)
if result:
chain = json.loads(result)
for block in chain:
index = block['index']
prev_hash = block['previous_hash']
timestamp = block['timestamp']
data = block['data']
nonce = block['nonce']
hash = block['hash']
print('\n')
print(f'# Block {index}')
print('+-----------+-------------------------------------------'
'---------------------+')
print(f'| prev_hash |{prev_hash: >{64}}|')
print('|-----------|-------------------------------------------'
'---------------------|')
print(f'| timestamp |{timestamp: >{64}}|')
print('|-----------|-------------------------------------------'
'---------------------|')
print(f'| data |{data[:64]: >{64}}|')
print('|-----------|-------------------------------------------'
'---------------------|')
print(f'| nonce |{nonce: >{64}}|')
print('|-----------|-------------------------------------------'
'---------------------|')
print(f'| hash |{hash: >{64}}|')
print('+-----------+-------------------------------------------'
'---------------------+')
else:
print('Empty blockchain')
return result
def _unicast(self, host, port, message):
pool = multiprocessing.Pool(1)
result = pool.apply_async( self._send_message, args=(host, port, message))
pool.close()
pool.join()
return result.get()
def _send_message(self, host, port, message):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, port))
s.sendall(json.dumps(message).encode('utf-8'))
response = s.recv(655350)
return response.decode('utf-8')