-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstart_node_repl.py
157 lines (114 loc) · 4.85 KB
/
start_node_repl.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
import asyncio
import signal
import sys
import shlex
import socket
# TODO: Is cmd module made async a better alternative?
# https://pymotw.com/2/cmd/index.html#module-cmd
# https://stackoverflow.com/questions/37866403
from aioconsole import ainput
from node import Node
from start_node import two_phase_protocol, setup_logging
from utils import random_id
from node_repl_utils import get_sock_from_name, generate_help_dict
HELP_DICT = generate_help_dict()
async def node_repl(node):
while True:
line = await ainput(">>> ")
# Since it reads the "\n" when you press enter
line = line.strip()
if not line:
continue
# Handle arguments with spaces etc.
line = list(shlex.shlex(line))
cmd = line[0]
args = line[1:]
if cmd in ['id']:
"Print id of a node"
if len(args) == 1:
peer_name = args[0]
peer_socket = get_sock_from_name(args[0])
try:
peer_id = await node.ping(peer_socket, node.identifier)
print("%s's id is %d" % (peer_name, peer_id))
except socket.timeout:
print("Failed to ping node %s" % args[0])
else:
print("My id is %d" % node.identifier)
# TODO: Print hash table of a particular node
elif cmd in ['ht', 'hash_table']:
"Print my hash table"
print(node.storage_str())
# TODO: Print routing table of a particular node
elif cmd in ['rt', 'routing_table']:
"Print my routing table"
print(node.routing_table)
elif cmd in ['put']:
"Store a (key, value) pair on the network DHT"
if (len(args) != 2):
print("Expected 2 arguments, %d given" % len(args))
else:
num = await node.put(args[0], args[1], hashed=False)
print("Value stored at %d node(s)" % num)
elif cmd in ['get']:
"Access a previously stored value by its key"
if (len(args) != 1):
print("Expected 1 argument, %d given" % len(args))
else:
try:
value = await node.get(args[0], hashed=False)
print(value)
except KeyError:
print("Key not found")
elif cmd in ['sa', 'send_amount']:
"Send bitcoins to a node"
if (len(args) != 4):
print("Expected 4 arguments, %d given" % len(args))
else:
try:
sender_sock = get_sock_from_name(args[0])
receiver_sock = get_sock_from_name(args[1])
witness_sock = get_sock_from_name(args[2])
receiver_id = await node.ping(receiver_sock, node.identifier)
witness_id = await node.ping(witness_sock, node.identifier)
amount = int(args[3])
reply = await node.request(sender_sock, "send_amount", node.identifier, int(receiver_id), int(witness_id), amount)
print(reply)
except Exception as e:
print("Exception Caught : ", e)
elif cmd in ['?', 'help']:
"List commands"
# Find left-justification factor
ljust = max(map(len, HELP_DICT.keys()))
for cmd, doc in HELP_DICT.items():
print(cmd.ljust(ljust) + " : " + doc)
print()
elif cmd in ['bd', 'brd', 'bc', 'broadcast']:
"Broadcast an RPC over the network"
if (len(args) < 1):
print("Expected atleast 1 argument, %d given" % len(args))
else:
await node.broadcast(random_id(), args[0], node.identifier, *args[1:])
elif cmd in ['ld', 'ledger']:
"Pretty print the ledger."
print(node.ledger)
else:
print("Please enter valid input.\nType help to see commands")
def start_node_with_repl(sock_addr):
loop = asyncio.get_event_loop()
# On receiving SIGINT Ctrl+C it will try to stop the loop
loop.add_signal_handler(signal.SIGINT, loop.stop)
f = loop.create_datagram_endpoint(Node, local_addr=sock_addr)
_, node = loop.run_until_complete(f)
setup_logging("cli", to_file=True)
print("MyId :", node.identifier)
node.socket_addr = node.transport.get_extra_info('sockname')
loop.run_until_complete(node.store(node.socket_addr, node.identifier, node.identifier, (node.socket_addr, node.pub_key))) # store my pub_key in my dht
loop.create_task(two_phase_protocol(node))
loop.create_task(node_repl(node))
loop.run_forever()
if __name__ == '__main__':
# TODO: Improved argument parsing via docopt or click
start_node_with_repl(
sock_addr=(sys.argv[1], int(sys.argv[2]))
)