2 # Copyright(C) 2012 thomasv@gitorious
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.
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.
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>.
20 * server should check and return bitcoind status..
21 * improve txpoint sorting
22 * command to check cache
24 mempool transactions do not need to be added to the database; it slows it down
28 import time, json, socket, operator, thread, ast, sys,re
32 from json import dumps, loads
35 # we need to import electrum
36 sys.path.append('../client/')
37 from wallet import Wallet
38 from interface import Interface
41 config = ConfigParser.ConfigParser()
42 # set some defaults, which will be overwritten by the config file
43 config.add_section('server')
44 config.set('server','banner', 'Welcome to Electrum!')
45 config.set('server', 'host', 'localhost')
46 config.set('server', 'port', '50000')
47 config.set('server', 'password', '')
48 config.set('server', 'irc', 'yes')
49 config.set('server', 'ircname', 'Electrum server')
50 config.add_section('database')
51 config.set('database', 'type', 'psycopg2')
52 config.set('database', 'database', 'abe')
55 f = open('/etc/electrum.conf','r')
59 print "Could not read electrum.conf. I will use the default values."
62 f = open('/etc/electrum.banner','r')
63 config.set('server','banner', f.read())
69 password = config.get('server','password')
75 sessions_sub_numblocks = {} # sessions that have subscribed to the service
77 m_sessions = [{}] # served by http
81 wallets = {} # for ultra-light clients such as bccapi
83 from Queue import Queue
85 output_queue = Queue()
86 address_queue = Queue()
92 class Direct_Interface(Interface):
96 def handler(self, method, params = ''):
97 cmds = {'session.new':new_session,
98 'session.poll':poll_session,
99 'session.update':update_session,
100 'transaction.broadcast':send_tx,
101 'address.get_history':store.get_history
104 return func( params )
109 postdata = dumps({"method": 'importtransaction', 'params': [tx], 'id':'jsonrpc'})
110 respdata = urllib.urlopen(bitcoind_url, postdata).read()
112 if r['error'] != None:
113 out = "error: transaction rejected by memorypool\n"+tx
120 def random_string(N):
121 import random, string
122 return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))
126 def cmd_stop(_,__,pw):
132 return 'wrong password'
134 def cmd_load(_,__,pw):
136 return repr( len(sessions) )
138 return 'wrong password'
144 def modified_addresses(session):
147 addresses = session['addresses']
148 session['last_time'] = time.time()
151 for addr in addresses:
152 status = get_address_status( addr )
153 msg_id, last_status = addresses.get( addr )
154 if last_status != status:
155 addresses[addr] = msg_id, status
158 t2 = time.time() - t1
159 #if t2 > 10: print "high load:", session_id, "%d/%d"%(k,len(addresses)), t2
160 return ret, addresses
163 def poll_session(session_id):
165 session = sessions.get(session_id)
167 print time.asctime(), "session not found", session_id
170 ret, addresses = modified_addresses(session)
171 if ret: sessions[session_id]['addresses'] = addresses
172 return repr( (block_number,ret))
175 def poll_session_json(session_id, message_id):
176 session = m_sessions[0].get(session_id)
178 raise BaseException("session not found %s"%session_id)
181 ret, addresses = modified_addresses(session)
183 m_sessions[0][session_id]['addresses'] = addresses
185 msg_id, status = addresses[addr]
186 out.append( { 'id':msg_id, 'result':status } )
188 msg_id, last_nb = session.get('numblocks')
190 if last_nb != block_number:
191 m_sessions[0][session_id]['numblocks'] = msg_id, block_number
192 out.append( {'id':msg_id, 'result':block_number} )
197 def do_update_address(addr):
198 # an address was involved in a transaction; we check if it was subscribed to in a session
199 # the address can be subscribed in several sessions; the cache should ensure that we don't do redundant requests
201 for session_id in sessions.keys():
202 session = sessions[session_id]
203 if session.get('type') != 'persistent': continue
204 addresses = session['addresses'].keys()
206 if addr in addresses:
207 status = get_address_status( addr )
208 message_id, last_status = session['addresses'][addr]
209 if last_status != status:
210 #print "sending new status for %s:"%addr, status
211 send_status(session_id,message_id,addr,status)
212 sessions[session_id]['addresses'][addr] = (message_id,status)
214 def get_address_status(addr):
215 # get address status, i.e. the last block for that address.
216 tx_points = store.get_history(addr)
220 lastpoint = tx_points[-1]
221 status = lastpoint['blk_hash']
222 # this is a temporary hack; move it up once old clients have disappeared
223 if status == 'mempool': # and session['version'] != "old":
224 status = status + ':%d'% len(tx_points)
228 def send_numblocks(session_id):
229 message_id = sessions_sub_numblocks[session_id]
230 out = json.dumps( {'id':message_id, 'result':block_number} )
231 output_queue.put((session_id, out))
233 def send_status(session_id, message_id, address, status):
234 out = json.dumps( { 'id':message_id, 'result':status } )
235 output_queue.put((session_id, out))
237 def address_get_history_json(_,message_id,address):
238 return store.get_history(address)
240 def subscribe_to_numblocks(session_id, message_id):
241 sessions_sub_numblocks[session_id] = message_id
242 send_numblocks(session_id)
244 def subscribe_to_numblocks_json(session_id, message_id):
246 m_sessions[0][session_id]['numblocks'] = message_id,block_number
249 def subscribe_to_address(session_id, message_id, address):
250 status = get_address_status(address)
251 sessions[session_id]['addresses'][address] = (message_id, status)
252 sessions[session_id]['last_time'] = time.time()
253 send_status(session_id, message_id, address, status)
255 def add_address_to_session_json(session_id, message_id, address):
257 sessions = m_sessions[0]
258 status = get_address_status(address)
259 sessions[session_id]['addresses'][address] = (message_id, status)
260 sessions[session_id]['last_time'] = time.time()
261 m_sessions[0] = sessions
264 def add_address_to_session(session_id, address):
265 status = get_address_status(address)
266 sessions[session_id]['addresses'][addr] = ("", status)
267 sessions[session_id]['last_time'] = time.time()
270 def new_session(version, addresses):
271 session_id = random_string(10)
272 sessions[session_id] = { 'addresses':{}, 'version':version }
274 sessions[session_id]['addresses'][a] = ('','')
275 out = repr( (session_id, config.get('server','banner').replace('\\n','\n') ) )
276 sessions[session_id]['last_time'] = time.time()
280 def client_version_json(session_id, _, version):
282 sessions = m_sessions[0]
283 sessions[session_id]['version'] = version
284 m_sessions[0] = sessions
286 def create_session_json(_, __):
287 sessions = m_sessions[0]
288 session_id = random_string(10)
289 print "creating session", session_id
290 sessions[session_id] = { 'addresses':{}, 'numblocks':('','') }
291 sessions[session_id]['last_time'] = time.time()
292 m_sessions[0] = sessions
297 def get_banner(_,__):
298 return config.get('server','banner').replace('\\n','\n')
300 def update_session(session_id,addresses):
301 """deprecated in 0.42"""
302 sessions[session_id]['addresses'] = {}
304 sessions[session_id]['addresses'][a] = ''
305 sessions[session_id]['last_time'] = time.time()
308 def native_server_thread():
309 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
310 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
311 s.bind((config.get('server','host'), config.getint('server','port')))
314 conn, addr = s.accept()
316 thread.start_new_thread(native_client_thread, (addr, conn,))
318 # can't start new thread if there is no memory..
319 traceback.print_exc(file=sys.stdout)
322 def native_client_thread(ipaddr,conn):
323 #print "client thread", ipaddr
333 msg = msg.split('#', 1)[0]
336 cmd, data = ast.literal_eval(msg)
338 print "syntax error", repr(msg), ipaddr
342 out = do_command(cmd, data, ipaddr)
344 #print ipaddr, cmd, len(out)
348 print "error, could not send"
355 return time.strftime("[%d/%m/%Y-%H:%M:%S]")
357 # used by the native handler
358 def do_command(cmd, data, ipaddr):
361 out = "%d"%block_number
363 elif cmd in ['session','new_session']:
366 addresses = ast.literal_eval(data)
369 version, addresses = ast.literal_eval(data)
370 if version[0]=="0": version = "v" + version
374 print timestr(), "new session", ipaddr, addresses[0] if addresses else addresses, len(addresses), version
375 out = new_session(version, addresses)
377 elif cmd=='address.subscribe':
379 session_id, addr = ast.literal_eval(data)
383 out = add_address_to_session(session_id,addr)
385 elif cmd=='update_session':
387 session_id, addresses = ast.literal_eval(data)
391 print timestr(), "update session", ipaddr, addresses[0] if addresses else addresses, len(addresses)
392 out = update_session(session_id,addresses)
394 elif cmd == 'bccapi_login':
397 v, k = ast.literal_eval(data)
398 master_public_key = k.decode('hex') # todo: sanitize. no need to decode twice...
399 print master_public_key
400 wallet_id = random_string(10)
401 w = Wallet( Direct_Interface() )
402 w.master_public_key = master_public_key.decode('hex')
404 wallets[wallet_id] = w
406 print "wallets", wallets
408 elif cmd == 'bccapi_getAccountInfo':
409 from wallet import int_to_hex
410 v, wallet_id = ast.literal_eval(data)
411 w = wallets.get(wallet_id)
413 num = len(w.addresses)
414 c, u = w.get_balance()
415 out = int_to_hex(num,4) + int_to_hex(c,8) + int_to_hex( c+u, 8 )
416 out = out.decode('hex')
421 elif cmd == 'bccapi_getAccountStatement':
422 from wallet import int_to_hex
423 v, wallet_id = ast.literal_eval(data)
424 w = wallets.get(wallet_id)
426 num = len(w.addresses)
427 c, u = w.get_balance()
428 total_records = num_records = 0
429 out = int_to_hex(num,4) + int_to_hex(c,8) + int_to_hex( c+u, 8 ) + int_to_hex( total_records ) + int_to_hex( num_records )
430 out = out.decode('hex')
435 elif cmd == 'bccapi_getSendCoinForm':
438 elif cmd == 'bccapi_submitTransaction':
442 out = poll_session(data)
447 out = repr( store.get_history( address ) )
454 print timestr(), "sent tx:", ipaddr, out
460 out = repr(peer_list.values())
469 ####################################################################
471 def tcp_server_thread():
472 thread.start_new_thread(process_input_queue, ())
473 thread.start_new_thread(process_output_queue, ())
475 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
476 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
477 s.bind((config.get('server','host'), 50001))
480 conn, addr = s.accept()
482 thread.start_new_thread(tcp_client_thread, (addr, conn,))
484 # can't start new thread if there is no memory..
485 traceback.print_exc(file=sys.stdout)
488 def close_session(session_id):
489 #print "lost connection", session_id
490 sessions.pop(session_id)
491 if session_id in sessions_sub_numblocks:
492 sessions_sub_numblocks.pop(session_id)
495 # one thread per client. put requests in a queue.
496 def tcp_client_thread(ipaddr,conn):
497 """ use a persistent connection. put commands in a queue."""
499 print timestr(), "TCP session", ipaddr
502 session_id = random_string(10)
503 sessions[session_id] = { 'conn':conn, 'addresses':{}, 'version':'unknown', 'type':'persistent' }
514 close_session(session_id)
527 close_session(session_id)
532 print "json error", repr(c)
535 message_id = c.get('id')
536 method = c.get('method')
537 params = c.get('params')
539 print "syntax error", repr(c), ipaddr
543 input_queue.put((session_id, message_id, method, params))
547 # read commands from the input queue. perform requests, etc. this should be called from the main thread.
548 def process_input_queue():
550 session_id, message_id, method, data = input_queue.get()
551 if session_id not in sessions.keys():
554 if method == 'address.subscribe':
556 subscribe_to_address(session_id,message_id,address)
557 elif method == 'numblocks.subscribe':
558 subscribe_to_numblocks(session_id,message_id)
559 elif method == 'client.version':
560 sessions[session_id]['version'] = data[0]
561 elif method == 'server.banner':
562 out = { 'result':config.get('server','banner').replace('\\n','\n') }
563 elif method == 'server.peers':
564 out = { 'result':peer_list.values() }
565 elif method == 'address.get_history':
567 out = { 'result':store.get_history( address ) }
568 elif method == 'transaction.broadcast':
569 postdata = dumps({"method": 'importtransaction', 'params': [data], 'id':'jsonrpc'})
570 txo = urllib.urlopen(bitcoind_url, postdata).read()
571 print "sent tx:", txo
572 out = json.loads(txo)
574 print "unknown command", method
576 out['id'] = message_id
577 out = json.dumps( out )
578 output_queue.put((session_id, out))
580 # this is a separate thread
581 def process_output_queue():
583 session_id, out = output_queue.get()
584 session = sessions.get(session_id)
587 conn = session.get('conn')
590 close_session(session_id)
595 ####################################################################
600 def clean_session_thread():
604 for k,s in sessions.items():
605 if s.get('type') == 'persistent': continue
609 print "lost session", k
614 NICK = 'E_'+random_string(10)
618 s.connect(('irc.freenode.net', 6667))
619 s.send('USER electrum 0 * :'+config.get('server','host')+' '+config.get('server','ircname')+'\n')
620 s.send('NICK '+NICK+'\n')
621 s.send('JOIN #electrum\n')
622 sf = s.makefile('r', 0)
626 line = line.rstrip('\r\n')
629 s.send('PONG '+line[1]+'\n')
630 elif '353' in line: # answer to /names
631 k = line.index('353')
632 for item in line[k+1:]:
633 if item[0:2] == 'E_':
634 s.send('WHO %s\n'%item)
635 elif '352' in line: # answer to /who
636 # warning: this is a horrible hack which apparently works
637 k = line.index('352')
639 ip = socket.gethostbyname(ip)
642 peer_list[name] = (ip,host)
643 if time.time() - t > 5*60:
644 s.send('NAMES #electrum\n')
648 traceback.print_exc(file=sys.stdout)
654 def get_peers_json(_,__):
655 return peer_list.values()
657 def http_server_thread():
658 # see http://code.google.com/p/jsonrpclib/
659 from SocketServer import ThreadingMixIn
660 from StratumJSONRPCServer import StratumJSONRPCServer
661 class StratumThreadedJSONRPCServer(ThreadingMixIn, StratumJSONRPCServer): pass
662 server = StratumThreadedJSONRPCServer(( config.get('server','host'), 8081))
663 server.register_function(get_peers_json, 'server.peers')
664 server.register_function(cmd_stop, 'stop')
665 server.register_function(cmd_load, 'load')
666 server.register_function(get_banner, 'server.banner')
667 server.register_function(lambda a,b,c: send_tx(c), 'transaction.broadcast')
668 server.register_function(address_get_history_json, 'address.get_history')
669 server.register_function(add_address_to_session_json, 'address.subscribe')
670 server.register_function(subscribe_to_numblocks_json, 'numblocks.subscribe')
671 server.register_function(client_version_json, 'client.version')
672 server.register_function(create_session_json, 'session.create') # internal message (not part of protocol)
673 server.register_function(poll_session_json, 'session.poll') # internal message (not part of protocol)
674 server.serve_forever()
680 if __name__ == '__main__':
684 server = jsonrpclib.Server('http://%s:8081'%config.get('server','host'))
687 out = server.load(password)
689 out = server.server.peers()
691 out = server.stop(password)
692 elif cmd == 'clear_cache':
693 out = server.clear_cache(password)
694 elif cmd == 'get_cache':
695 out = server.get_cache(password,sys.argv[2])
697 out = server.address.get_history(sys.argv[2])
699 out = server.transaction.broadcast(sys.argv[2])
701 out = server.numblocks.subscribe()
703 out = "Unknown command: '%s'" % cmd
710 store = db.MyStore(config,address_queue)
713 # supported protocols
714 thread.start_new_thread(native_server_thread, ())
715 thread.start_new_thread(tcp_server_thread, ())
716 thread.start_new_thread(http_server_thread, ())
717 thread.start_new_thread(clean_session_thread, ())
719 if (config.get('server','irc') == 'yes' ):
720 thread.start_new_thread(irc_thread, ())
722 print "starting Electrum server"
726 block_number = store.main_iteration()
728 if block_number != old_block_number:
729 old_block_number = block_number
730 for session_id in sessions_sub_numblocks.keys():
731 send_numblocks(session_id)
735 addr = address_queue.get(False)
738 do_update_address(addr)
741 print "server stopped"