2012-01-19 08:11:36 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
# Copyright (C) 2011 thomasv@gitorious
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
import random, socket, ast
|
|
|
|
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
import thread, traceback, sys, time, json
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-02-10 04:14:04 -08:00
|
|
|
DEFAULT_TIMEOUT=5
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-01-19 08:11:36 -08:00
|
|
|
class Interface:
|
|
|
|
def __init__(self):
|
2012-02-08 06:59:47 -08:00
|
|
|
self.default_servers = ['ecdsa.org','electrum.novit.ro'] # list of default servers
|
|
|
|
self.host = random.choice( self.default_servers ) # random choice when the wallet is created
|
2012-02-09 06:39:25 -08:00
|
|
|
self.servers = self.default_servers # actual list from IRC
|
2012-01-19 08:11:36 -08:00
|
|
|
self.rtime = 0
|
|
|
|
self.blocks = 0
|
|
|
|
self.message = ''
|
2012-02-08 04:37:14 -08:00
|
|
|
self.was_updated = True # fixme: use a semaphore
|
2012-03-13 08:43:56 -07:00
|
|
|
self.is_up_to_date = False # True after the first poll
|
2012-01-19 08:11:36 -08:00
|
|
|
|
2012-03-14 07:35:39 -07:00
|
|
|
self.is_connected = False
|
|
|
|
self.disconnected_event = threading.Event()
|
|
|
|
self.disconnected_event.clear()
|
|
|
|
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
def send_tx(self, data):
|
|
|
|
out = self.handler('blockchain.transaction.broadcast', data )
|
|
|
|
return out
|
|
|
|
|
|
|
|
def retrieve_history(self, address):
|
|
|
|
out = self.handler('blockchain.address.get_history', address )
|
|
|
|
return out
|
|
|
|
|
|
|
|
def get_servers(self):
|
2012-03-14 02:19:06 -07:00
|
|
|
pass
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
def start_session(self, wallet):
|
|
|
|
pass
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
|
|
|
class NativeInterface(Interface):
|
|
|
|
"""This is the original Electrum protocol. It uses polling, and a non-persistent tcp connection"""
|
2012-01-19 08:11:36 -08:00
|
|
|
|
2012-03-14 08:31:31 -07:00
|
|
|
def __init__(self, host, port):
|
2012-03-12 09:55:33 -07:00
|
|
|
Interface.__init__(self)
|
2012-03-14 08:31:31 -07:00
|
|
|
self.host = host
|
2012-03-12 09:55:33 -07:00
|
|
|
self.port = port
|
2012-01-19 08:11:36 -08:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
def start_session(self, wallet):
|
|
|
|
addresses = wallet.all_addresses()
|
|
|
|
version = wallet.electrum_version
|
2012-03-13 08:43:56 -07:00
|
|
|
self.is_up_to_date = False
|
2012-03-12 09:55:33 -07:00
|
|
|
out = self.handler('session.new', [ version, addresses ] )
|
|
|
|
self.session_id, self.message = ast.literal_eval( out )
|
2012-03-14 07:35:39 -07:00
|
|
|
thread.start_new_thread(self.poll_thread, (wallet,))
|
2012-03-12 09:55:33 -07:00
|
|
|
|
|
|
|
def update_session(self, addresses):
|
|
|
|
out = self.handler('session.update', [ self.session_id, addresses ] )
|
|
|
|
return out
|
|
|
|
|
|
|
|
def handler(self, method, params = ''):
|
2012-01-19 08:11:36 -08:00
|
|
|
import time
|
|
|
|
cmds = {'session.new':'new_session',
|
|
|
|
'peers':'peers',
|
|
|
|
'session.poll':'poll',
|
|
|
|
'session.update':'update_session',
|
|
|
|
'blockchain.transaction.broadcast':'tx',
|
|
|
|
'blockchain.address.get_history':'h'
|
|
|
|
}
|
|
|
|
cmd = cmds[method]
|
|
|
|
if type(params) != type(''): params = repr( params )
|
|
|
|
t1 = time.time()
|
|
|
|
request = repr ( (cmd, params) ) + "#"
|
|
|
|
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
|
2012-02-10 04:14:04 -08:00
|
|
|
s.settimeout(DEFAULT_TIMEOUT)
|
2012-02-08 06:59:47 -08:00
|
|
|
s.connect(( self.host if cmd!='peers' else self.peers_server, self.port) )
|
2012-01-19 08:11:36 -08:00
|
|
|
s.send( request )
|
|
|
|
out = ''
|
|
|
|
while 1:
|
|
|
|
msg = s.recv(1024)
|
|
|
|
if msg: out += msg
|
|
|
|
else: break
|
|
|
|
s.close()
|
|
|
|
self.rtime = time.time() - t1
|
|
|
|
self.is_connected = True
|
|
|
|
if cmd in[ 'peers','h']:
|
|
|
|
out = ast.literal_eval( out )
|
|
|
|
return out
|
|
|
|
|
2012-02-08 04:37:14 -08:00
|
|
|
def poll_interval(self):
|
2012-03-12 09:55:33 -07:00
|
|
|
return 5
|
2012-02-08 04:37:14 -08:00
|
|
|
|
|
|
|
def update_wallet(self, wallet):
|
|
|
|
is_new = False
|
|
|
|
changed_addresses = self.poll()
|
|
|
|
for addr, blk_hash in changed_addresses.items():
|
|
|
|
if wallet.status.get(addr) != blk_hash:
|
|
|
|
print "updating history for", addr
|
|
|
|
wallet.history[addr] = self.retrieve_history(addr)
|
|
|
|
wallet.status[addr] = blk_hash
|
|
|
|
is_new = True
|
|
|
|
|
2012-02-22 06:39:06 -08:00
|
|
|
if is_new or wallet.remote_url:
|
2012-02-22 08:11:21 -08:00
|
|
|
is_new = wallet.synchronize()
|
2012-02-08 04:37:14 -08:00
|
|
|
wallet.update_tx_history()
|
|
|
|
wallet.save()
|
2012-02-22 08:11:21 -08:00
|
|
|
return is_new
|
2012-02-08 04:37:14 -08:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
def poll(self):
|
|
|
|
out = self.handler('session.poll', self.session_id )
|
|
|
|
blocks, changed_addr = ast.literal_eval( out )
|
|
|
|
if blocks == -1: raise BaseException("session not found")
|
|
|
|
self.blocks = int(blocks)
|
|
|
|
if changed_addr: self.was_updated = True
|
2012-03-13 08:43:56 -07:00
|
|
|
self.is_up_to_date = True
|
2012-03-12 09:55:33 -07:00
|
|
|
return changed_addr
|
|
|
|
|
2012-03-14 07:35:39 -07:00
|
|
|
def poll_thread(self, wallet):
|
|
|
|
while self.is_connected:
|
2012-02-08 04:37:14 -08:00
|
|
|
try:
|
2012-03-14 07:35:39 -07:00
|
|
|
if self.update_wallet(wallet):
|
|
|
|
self.update_session( wallet.all_addresses() )
|
2012-02-08 05:13:11 -08:00
|
|
|
time.sleep(self.poll_interval())
|
2012-03-14 07:35:39 -07:00
|
|
|
except socket.gaierror:
|
|
|
|
break
|
|
|
|
except socket.error:
|
|
|
|
break
|
2012-02-08 04:37:14 -08:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-14 07:35:39 -07:00
|
|
|
break
|
|
|
|
|
|
|
|
self.is_connected = False
|
|
|
|
self.disconnected_event.set()
|
2012-02-08 04:37:14 -08:00
|
|
|
|
|
|
|
|
2012-03-14 02:19:06 -07:00
|
|
|
def get_servers(self):
|
|
|
|
thread.start_new_thread(self.update_servers_thread, ())
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
def update_servers_thread(self):
|
|
|
|
# if my server is not reachable, I should get the list from one of the default servers
|
|
|
|
# requesting servers could be an independent process
|
|
|
|
while True:
|
|
|
|
for server in self.default_servers:
|
|
|
|
try:
|
|
|
|
self.peers_server = server
|
|
|
|
out = self.handler('peers')
|
|
|
|
self.servers = map( lambda x:x[1], out )
|
|
|
|
# print "Received server list from %s" % self.peers_server, out
|
|
|
|
break
|
|
|
|
except socket.timeout:
|
|
|
|
continue
|
|
|
|
except socket.error:
|
|
|
|
continue
|
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-02-13 06:44:16 -08:00
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
time.sleep(5*60)
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-14 08:31:31 -07:00
|
|
|
class HttpInterface(NativeInterface):
|
|
|
|
|
|
|
|
def __init__(self, host, port):
|
|
|
|
Interface.__init__(self)
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
|
|
|
|
def handler(self, method, params = []):
|
|
|
|
import urllib2, json, time
|
|
|
|
if type(params) != type([]): params = [ params ]
|
|
|
|
t1 = time.time()
|
|
|
|
data = { 'method':method, 'id':'jsonrpc', 'params':params }
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
host = 'http://%s:%d'%( self.host if method!='peers' else self.peers_server, self.port )
|
|
|
|
req = urllib2.Request(host, data_json, {'content-type': 'application/json'})
|
|
|
|
response_stream = urllib2.urlopen(req)
|
|
|
|
response = json.loads( response_stream.read() )
|
|
|
|
out = response.get('result')
|
|
|
|
if not out:
|
|
|
|
print response
|
|
|
|
self.rtime = time.time() - t1
|
|
|
|
self.is_connected = True
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-13 15:06:06 -07:00
|
|
|
import threading
|
2012-03-12 09:55:33 -07:00
|
|
|
|
|
|
|
class TCPInterface(Interface):
|
2012-03-13 08:43:56 -07:00
|
|
|
"""json-rpc over persistent TCP connection"""
|
2012-03-12 09:55:33 -07:00
|
|
|
|
|
|
|
def __init__(self, host=None, port=50001):
|
|
|
|
Interface.__init__(self)
|
|
|
|
if host: self.host = host
|
|
|
|
self.port = 50001
|
2012-03-13 15:06:06 -07:00
|
|
|
self.tx_event = threading.Event()
|
2012-03-14 06:55:12 -07:00
|
|
|
|
|
|
|
self.addresses_waiting_for_status = []
|
|
|
|
self.addresses_waiting_for_history = []
|
|
|
|
# up to date
|
|
|
|
self.is_up_to_date = False
|
|
|
|
self.up_to_date_event = threading.Event()
|
|
|
|
self.up_to_date_event.clear()
|
2012-03-12 09:55:33 -07:00
|
|
|
|
|
|
|
def send(self, cmd, params = []):
|
|
|
|
request = json.dumps( { 'method':cmd, 'params':params } )
|
|
|
|
self.s.send( request + '\n' )
|
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
def send_tx(self, data):
|
2012-03-13 15:06:06 -07:00
|
|
|
self.tx_event.clear()
|
|
|
|
self.send('transaction.broadcast', data )
|
|
|
|
print "waiting for event.."
|
|
|
|
self.tx_event.wait()
|
|
|
|
out = self.tx_result
|
|
|
|
print "result:", out
|
2012-03-13 08:43:56 -07:00
|
|
|
return out
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
def listen_thread(self, wallet):
|
2012-03-13 08:43:56 -07:00
|
|
|
try:
|
|
|
|
self.is_connected = True
|
|
|
|
out = ''
|
2012-03-14 07:35:39 -07:00
|
|
|
while self.is_connected:
|
|
|
|
try: msg = self.s.recv(1024)
|
|
|
|
except socket.timeout: continue
|
2012-03-13 08:43:56 -07:00
|
|
|
out += msg
|
2012-03-13 09:31:29 -07:00
|
|
|
if msg == '':
|
|
|
|
self.is_connected = False
|
2012-03-14 07:35:39 -07:00
|
|
|
print "disconnected."
|
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
while True:
|
|
|
|
s = out.find('\n')
|
|
|
|
if s==-1: break
|
|
|
|
c = out[0:s]
|
|
|
|
out = out[s+1:]
|
|
|
|
c = json.loads(c)
|
|
|
|
cmd = c.get('method')
|
2012-03-14 02:19:06 -07:00
|
|
|
data = c.get('result')
|
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
if cmd == 'server.banner':
|
2012-03-14 02:19:06 -07:00
|
|
|
self.message = data
|
|
|
|
self.was_updated = True
|
|
|
|
|
|
|
|
elif cmd == 'server.peers':
|
|
|
|
self.servers = map( lambda x:x[1], data )
|
2012-03-13 15:06:06 -07:00
|
|
|
|
|
|
|
elif cmd == 'transaction.broadcast':
|
2012-03-14 02:19:06 -07:00
|
|
|
self.tx_result = data
|
2012-03-13 15:06:06 -07:00
|
|
|
self.tx_event.set()
|
|
|
|
|
2012-03-13 09:31:29 -07:00
|
|
|
elif cmd == 'numblocks.subscribe':
|
2012-03-14 02:19:06 -07:00
|
|
|
self.blocks = data
|
2012-03-13 15:06:06 -07:00
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
elif cmd =='address.subscribe':
|
|
|
|
addr = c.get('address')
|
|
|
|
status = c.get('status')
|
2012-03-14 06:55:12 -07:00
|
|
|
if addr in self.addresses_waiting_for_status:
|
|
|
|
self.addresses_waiting_for_status.remove(addr)
|
2012-03-13 08:43:56 -07:00
|
|
|
if wallet.status.get(addr) != status:
|
|
|
|
wallet.status[addr] = status
|
2012-03-14 06:55:12 -07:00
|
|
|
self.send('address.get_history', addr)
|
|
|
|
self.addresses_waiting_for_history.append(addr)
|
2012-03-13 15:06:06 -07:00
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
elif cmd == 'address.get_history':
|
|
|
|
addr = c.get('address')
|
2012-03-14 06:55:12 -07:00
|
|
|
if addr in self.addresses_waiting_for_history:
|
|
|
|
self.addresses_waiting_for_history.remove(addr)
|
|
|
|
wallet.history[addr] = data
|
2012-03-13 08:43:56 -07:00
|
|
|
wallet.synchronize()
|
|
|
|
wallet.update_tx_history()
|
|
|
|
wallet.save()
|
|
|
|
self.was_updated = True
|
2012-03-14 06:55:12 -07:00
|
|
|
else:
|
|
|
|
print "received message:", c
|
|
|
|
|
|
|
|
if self.addresses_waiting_for_status or self.addresses_waiting_for_history:
|
|
|
|
self.is_up_to_date = False
|
|
|
|
else:
|
|
|
|
self.is_up_to_date = True
|
|
|
|
self.up_to_date_event.set()
|
2012-03-13 08:43:56 -07:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-14 07:35:39 -07:00
|
|
|
|
|
|
|
self.is_connected = False
|
|
|
|
self.disconnected_event.set()
|
2012-03-14 06:55:12 -07:00
|
|
|
|
|
|
|
def update_wallet(self,wallet):
|
|
|
|
self.up_to_date_event.wait()
|
2012-03-13 08:43:56 -07:00
|
|
|
|
|
|
|
def subscribe(self,address):
|
|
|
|
self.send('address.subscribe', address)
|
2012-03-14 06:55:12 -07:00
|
|
|
self.addresses_waiting_for_status.append(address)
|
2012-03-14 02:19:06 -07:00
|
|
|
|
|
|
|
def get_servers(self):
|
|
|
|
self.send('server.peers')
|
2012-03-13 08:43:56 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
def start_session(self, wallet):
|
|
|
|
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
2012-03-14 07:38:03 -07:00
|
|
|
self.s.settimeout(1)
|
2012-03-14 06:55:12 -07:00
|
|
|
self.s.connect(( self.host, self.port))
|
2012-03-12 09:55:33 -07:00
|
|
|
thread.start_new_thread(self.listen_thread, (wallet,))
|
|
|
|
self.send('client.version', wallet.electrum_version)
|
2012-03-13 08:43:56 -07:00
|
|
|
self.send('server.banner')
|
|
|
|
self.send('numblocks.subscribe')
|
2012-03-12 09:55:33 -07:00
|
|
|
for address in wallet.all_addresses():
|
2012-03-13 08:43:56 -07:00
|
|
|
self.subscribe(address)
|
2012-03-12 09:55:33 -07:00
|
|
|
|
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def new_interface(wallet):
|
|
|
|
host = wallet.host
|
|
|
|
port = wallet.port
|
|
|
|
if port == 50000:
|
|
|
|
interface = NativeInterface(host,port)
|
|
|
|
elif port == 50001:
|
|
|
|
interface = TCPInterface(host,port)
|
2012-03-14 08:31:31 -07:00
|
|
|
elif port in [80, 81, 8080, 8081]:
|
2012-03-14 06:55:12 -07:00
|
|
|
interface = HttpInterface(host,port)
|
|
|
|
else:
|
2012-03-14 08:13:12 -07:00
|
|
|
print "unknown port number: %d. using native protocol."%port
|
2012-03-14 08:08:23 -07:00
|
|
|
interface = NativeInterface(host,port)
|
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
return interface
|
|
|
|
|
|
|
|
|
|
|
|
def loop_interfaces_thread(wallet):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
wallet.interface.start_session(wallet)
|
|
|
|
wallet.interface.get_servers()
|
2012-03-14 08:08:23 -07:00
|
|
|
|
|
|
|
wallet.interface.disconnected_event.wait()
|
|
|
|
print "Disconnected"
|
2012-03-14 06:55:12 -07:00
|
|
|
except socket.error:
|
2012-03-14 08:08:23 -07:00
|
|
|
print "socket error"
|
2012-03-14 06:55:12 -07:00
|
|
|
time.sleep(5)
|
2012-03-14 07:35:39 -07:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
|
|
|
continue
|
2012-03-14 08:08:23 -07:00
|
|
|
|
2012-03-14 08:13:12 -07:00
|
|
|
print "Starting new session: %s:%d"%(wallet.host,wallet.port)
|
2012-03-14 06:55:12 -07:00
|
|
|
wallet.interface = new_interface(wallet)
|
|
|
|
|