-
Notifications
You must be signed in to change notification settings - Fork 8
/
interface.py
90 lines (72 loc) · 2.5 KB
/
interface.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
import cmd
import os
from block import Block
from command import Command
from transaction import Transaction
from utxo import UTXOset
class Interface(cmd.Cmd):
def __init__(self):
super().__init__()
self.prompt = '(blockchain) '
self._default_host = '127.0.0.1'
self._command = Command()
def do_open(self, port):
'''
Open peer listening port Eg: open 5000
'''
self._command.start_peer(self._default_host, int(port))
def do_mine(self, arg):
'''
Mine a new block Eg: mine hello
'''
port = arg.split(' ')[0]
data = arg.split(' ')[1]
self._command.mine(self._default_host, int(port), data)
def do_stop(self, _):
self._command.stop()
def do_newTransaction(self,_):
self._command.newTx()
def do_connect(self, arg):
'''
Connect a peer to another Eg: connect 5000 5001
'''
port = arg.split(' ')[0]
target_port = arg.split(' ')[1]
self._command.connect_peer(
self._default_host, int(port), self._default_host, int(target_port))
def do_show(self, port):
'''
Show blockchain of peer Eg: show 5000
'''
self._command.get_chain(self._default_host, int(port))
def do_exit(self, _):
UTXOset._UTXOset.close()
UTXOset._myUTXOset.close()
Transaction._MemoryPool.close()
Block._raw_block.close()
os._exit(0)
def do_searchTransaction(self, txid):
self._command.get_Tx(txid)
def do_pendingTransaction(self, _):
self._command.getall_Mem()
def do_help(self, _):
print('\n')
print('Commands:')
print('\n')
print('help \t\t\t\t Help for given commands')
print('exit \t\t\t\t Exit application')
print('\n')
print('open <port> \t\t\t Open peer listening port Eg: open 5000')
print(
'connect <port> <target_port> \t '
'Connect a peer to another Eg: connect 5000 5001')
print('\n')
print('mine <port> <data> \t\t Mine a new block Eg: mine hello')
print('stop \t\t Stop mining block Eg: stop')
print('pendingTransaction \t\t Get all transactions in MemoryPool')
print('newTransaction \t\t Generate new Transaction.')
print('searchTransaction \t\t Search Transaction by tx_id')
print('show <port> \t\t\t Show blockchain of peer Eg: show 5000')
print('\n')
def emptyline(self):
pass