raise limit
[electrum-server.git] / server.py
1 #!/usr/bin/env python
2 # Copyright(C) 2012 thomasv@gitorious
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as
6 # published by the Free Software Foundation, either version 3 of the
7 # License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public
15 # License along with this program.  If not, see
16 # <http://www.gnu.org/licenses/agpl.html>.
17
18 import time, sys, traceback
19 import ConfigParser
20
21 import logging
22 logging.basicConfig()
23
24 def attempt_read_config(config, filename):
25     try:
26         with open(filename, 'r') as f:
27             config.readfp(f)
28     except IOError:
29         pass
30
31 def create_config():
32     config = ConfigParser.ConfigParser()
33     # set some defaults, which will be overwritten by the config file
34     config.add_section('server')
35     config.set('server', 'banner', 'Welcome to Electrum!')
36     config.set('server', 'host', 'localhost')
37     config.set('server', 'stratum_tcp_port', '50001')
38     config.set('server', 'stratum_http_port', '8081')
39     config.set('server', 'password', '')
40     config.set('server', 'irc', 'yes')
41     config.set('server', 'irc_nick', '')
42     config.add_section('database')
43     config.set('database', 'type', 'psycopg2')
44     config.set('database', 'database', 'abe')
45     config.set('database', 'limit', '1000')
46     config.set('server', 'backend', 'abe')
47
48     for path in ('/etc/', ''):
49         filename = path + 'electrum.conf'
50         attempt_read_config(config, filename)
51
52     try:
53         with open('/etc/electrum.banner', 'r') as f:
54             config.set('server','banner', f.read())
55     except IOError:
56         pass
57
58     return config
59
60 def run_rpc_command(command, stratum_tcp_port):
61     import socket, json
62     s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
63     s.connect(( host, int(stratum_tcp_port )))
64     method = 'server.' + command
65     request = json.dumps( { 'id':0, 'method':method, 'params':[password] } )
66     s.send(request + '\n')
67     msg = ''
68     while True:
69         o = s.recv(1024)
70         msg += o
71         if msg.find('\n') != -1: break
72     s.close()
73     r = json.loads(msg).get('result')
74
75     if command == 'info': 
76         now = time.time()
77         print 'type           address   sub  version  time' 
78         for item in r:
79             print '%4s   %15s   %3s  %7s  %.2f'%( item.get('name'), 
80                                                   item.get('address'), 
81                                                   item.get('subscriptions'), 
82                                                   item.get('version'), 
83                                                   (now - item.get('time')) )
84     else:
85         print r
86
87 if __name__ == '__main__':
88     config = create_config()
89     password = config.get('server', 'password')
90     host = config.get('server', 'host')
91     stratum_tcp_port = config.get('server', 'stratum_tcp_port')
92     stratum_http_port = config.get('server', 'stratum_http_port')
93
94     if len(sys.argv) > 1:
95         run_rpc_command(sys.argv[1], stratum_tcp_port)
96         sys.exit(0)
97
98     from processor import Dispatcher
99
100     from backends.irc import ServerProcessor
101     backend_name = config.get('server', 'backend')
102     try:
103         backend = __import__("backends." + backend_name,
104                              fromlist=["BlockchainProcessor"])
105     except ImportError:
106         sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
107         raise
108
109     print "Starting Electrum server on", host
110
111     # Create hub
112     dispatcher = Dispatcher()
113     shared = dispatcher.shared
114
115     # Create and register processors
116     chain_proc = backend.BlockchainProcessor(config)
117     dispatcher.register('blockchain', chain_proc)
118
119     # catch_up first
120     n = chain_proc.store.main_iteration()
121     print "blockchain: %d blocks"%n
122
123     server_proc = ServerProcessor(config)
124     dispatcher.register('server', server_proc)
125
126     transports = []
127     # Create various transports we need
128     if stratum_tcp_port:
129         from transports.stratum_tcp import TcpServer
130         tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
131         transports.append(tcp_server)
132
133     if stratum_http_port:
134         from transports.stratum_http import HttpServer
135         http_server = HttpServer(dispatcher, host, int(stratum_http_port))
136         transports.append(http_server)
137
138     for server in transports:
139         server.start()
140
141     while not shared.stopped():
142         time.sleep(1)
143     print "Server stopped"
144