forked from spesmilo/electrum-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·199 lines (164 loc) · 6.54 KB
/
server.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# Copyright(C) 2012 thomasv@gitorious
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/agpl.html>.
import ConfigParser
import logging
import socket
import sys
import time
import threading
import traceback
import json
logging.basicConfig()
if sys.maxsize <= 2**32:
print "Warning: it looks like you are using a 32bit system. You may experience crashes caused by mmap"
def attempt_read_config(config, filename):
try:
with open(filename, 'r') as f:
config.readfp(f)
except IOError:
pass
def create_config():
config = ConfigParser.ConfigParser()
# set some defaults, which will be overwritten by the config file
config.add_section('server')
config.set('server', 'banner', 'Welcome to Electrum!')
config.set('server', 'host', 'localhost')
config.set('server', 'report_host', '')
config.set('server', 'stratum_tcp_port', '50001')
config.set('server', 'stratum_http_port', '8081')
config.set('server', 'stratum_tcp_ssl_port', '')
config.set('server', 'stratum_http_ssl_port', '')
config.set('server', 'report_stratum_tcp_port', '')
config.set('server', 'report_stratum_http_port', '')
config.set('server', 'report_stratum_tcp_ssl_port', '')
config.set('server', 'report_stratum_http_ssl_port', '')
config.set('server', 'ssl_certfile', '')
config.set('server', 'ssl_keyfile', '')
config.set('server', 'password', '')
config.set('server', 'irc', 'yes')
config.set('server', 'irc_nick', '')
config.set('server', 'coin', '')
config.set('server', 'datadir', '')
# use leveldb as default
config.set('server', 'backend', 'leveldb')
config.add_section('leveldb')
config.set('leveldb', 'path', '/dev/shm/electrum_db')
config.set('leveldb', 'pruning_limit', '100')
for path in ('/etc/', ''):
filename = path + 'electrum.conf'
attempt_read_config(config, filename)
try:
with open('/etc/electrum.banner', 'r') as f:
config.set('server', 'banner', f.read())
except IOError:
pass
return config
def run_rpc_command(command, stratum_tcp_port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, int(stratum_tcp_port)))
except:
print "cannot connect to server."
return
method = 'server.' + command
params = [password]
if len(sys.argv) > 2:
params.append(sys.argv[2])
request = json.dumps({'id': 0, 'method': method, 'params': params})
s.send(request + '\n')
msg = ''
while True:
o = s.recv(1024)
if not o: break
msg += o
if msg.find('\n') != -1:
break
s.close()
r = json.loads(msg).get('result')
if command == 'info':
now = time.time()
print 'type address sub version time'
for item in r:
print '%4s %21s %3s %7s %.2f' % (item.get('name'),
item.get('address'),
item.get('subscriptions'),
item.get('version'),
(now - item.get('time')),
)
else:
print r
if __name__ == '__main__':
config = create_config()
password = config.get('server', 'password')
host = config.get('server', 'host')
stratum_tcp_port = config.get('server', 'stratum_tcp_port')
stratum_http_port = config.get('server', 'stratum_http_port')
stratum_tcp_ssl_port = config.get('server', 'stratum_tcp_ssl_port')
stratum_http_ssl_port = config.get('server', 'stratum_http_ssl_port')
ssl_certfile = config.get('server', 'ssl_certfile')
ssl_keyfile = config.get('server', 'ssl_keyfile')
if stratum_tcp_ssl_port or stratum_http_ssl_port:
assert ssl_certfile and ssl_keyfile
if len(sys.argv) > 1:
run_rpc_command(sys.argv[1], stratum_tcp_port)
sys.exit(0)
from processor import Dispatcher, print_log
from backends.irc import ServerProcessor
backend_name = config.get('server', 'backend')
if backend_name == 'libbitcoin':
from backends.libbitcoin import BlockchainProcessor
elif backend_name == 'leveldb':
from backends.bitcoind import BlockchainProcessor
else:
print "Unknown backend '%s' specified\n" % backend_name
sys.exit(1)
for i in xrange(5):
print ""
print_log("Starting Electrum server on", host)
# Create hub
dispatcher = Dispatcher(config)
shared = dispatcher.shared
# Create and register processors
chain_proc = BlockchainProcessor(config, shared)
dispatcher.register('blockchain', chain_proc)
server_proc = ServerProcessor(config)
dispatcher.register('server', server_proc)
transports = []
# Create various transports we need
if stratum_tcp_port:
from transports.stratum_tcp import TcpServer
tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port), False, None, None)
transports.append(tcp_server)
if stratum_tcp_ssl_port:
from transports.stratum_tcp import TcpServer
tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_ssl_port), True, ssl_certfile, ssl_keyfile)
transports.append(tcp_server)
if stratum_http_port:
from transports.stratum_http import HttpServer
http_server = HttpServer(dispatcher, host, int(stratum_http_port), False, None, None)
transports.append(http_server)
if stratum_http_ssl_port:
from transports.stratum_http import HttpServer
http_server = HttpServer(dispatcher, host, int(stratum_http_ssl_port), True, ssl_certfile, ssl_keyfile)
transports.append(http_server)
for server in transports:
server.start()
while not shared.stopped():
try:
time.sleep(1)
except:
shared.stop()
print_log("Electrum Server stopped")