-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.py
111 lines (90 loc) · 3.04 KB
/
main.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
#!/usr/bin/env python
###############################################################################
# bitcoind-ncurses by esotericnonsense
# thanks to jgarzik for bitcoinrpc
# wumpus and kylemanna for configuration file parsing
# all the users for their suggestions and testing
# and of course the bitcoin dev team for that bitcoin gizmo, pretty neat stuff
###############################################################################
from __future__ import division
from __future__ import print_function
from gevent import monkey
monkey.patch_all()
import gevent
import gevent.queue
import argparse, signal
import os
import rpc2
import block_store
import block_viewer
import interface
import config
def interrupt_signal(signal, frame):
s = {'stop': "Interrupt signal caught"}
response_queue.put(s)
def mainfn(window):
# initialise queues
response_queue = gevent.queue.Queue()
rpc_queue = gevent.queue.Queue()
# parse commandline arguments
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config",
help="path to config file [bitcoin.conf]",
default=os.path.expanduser("~/.bitcoin/bitcoin.conf"))
parser.add_argument("-m", "--mode",
help="initial mode",
default=None)
args = parser.parse_args()
# parse config file
try:
cfg = config.read_file(args.config)
except IOError:
cfg = {}
return "configuration file [{}] does not exist or could not be read".format(args.config)
# initialise interrupt signal handler (^C)
signal.signal(signal.SIGINT, interrupt_signal)
bstore = block_store.BlockStore()
bviewer = block_viewer.BlockViewer(bstore, window)
bstore._on_block = bviewer.on_block
# start RPC thread
rpcc = rpc2.BitcoinRPCClient(
response_queue=response_queue, # TODO: refactor this
block_store=bstore,
rpcuser=cfg["rpcuser"],
rpcpassword=cfg["rpcpassword"],
rpcip=(cfg["rpcip"] if "rpcip" in cfg else "localhost"),
rpcport=(cfg["rpcport"] if "rpcport" in cfg else 18332 if "testnet" in cfg else 8332),
protocol=(cfg["protocol"] if "protocol" in cfg else "http"),
)
bstore._rpcc = rpcc
connected = rpcc.connect()
if not connected:
return "RPCC failed to connect"
rpc2_process = gevent.spawn(rpcc.run)
poller = rpc2.Poller(rpcc)
poller_process = gevent.spawn(poller.run)
if args.mode is not None:
initial_mode = args.mode
elif "mode" in cfg:
initial_mode = cfg["mode"]
else:
initial_mode = None
# main loop
try:
interface.main(bviewer, window, response_queue, rpcc, poller, initial_mode)
finally:
rpcc.stop()
rpc2_process.join()
if __name__ == '__main__':
window = interface.init_curses()
try:
error = mainfn(window)
finally:
import curses
curses.nocbreak()
curses.endwin()
try:
if error:
print(error)
except:
pass