2014-07-24 01:59:13 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
# Copyright (C) 2014 Thomas Voegtlin
|
|
|
|
#
|
|
|
|
# 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 socket
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import threading
|
|
|
|
import traceback
|
|
|
|
import json
|
|
|
|
import Queue
|
2014-07-27 02:33:02 -07:00
|
|
|
|
|
|
|
import util
|
2014-07-24 01:59:13 -07:00
|
|
|
from network import Network
|
2014-07-26 07:24:22 -07:00
|
|
|
from util import print_error, print_stderr, parse_json
|
2014-07-24 01:59:13 -07:00
|
|
|
from simple_config import SimpleConfig
|
2014-07-26 07:24:22 -07:00
|
|
|
from daemon import NetworkServer, DAEMON_PORT
|
2014-07-24 01:59:13 -07:00
|
|
|
|
|
|
|
|
2014-07-27 15:13:40 -07:00
|
|
|
# policies
|
|
|
|
SPAWN_DAEMON=0
|
|
|
|
NEED_DAEMON=1
|
|
|
|
NO_DAEMON=2
|
|
|
|
USE_DAEMON_IF_AVAILABLE=3
|
|
|
|
|
2014-07-24 01:59:13 -07:00
|
|
|
|
|
|
|
class NetworkProxy(threading.Thread):
|
|
|
|
|
|
|
|
def __init__(self, socket, config=None):
|
2014-07-27 02:33:02 -07:00
|
|
|
|
2014-07-24 01:59:13 -07:00
|
|
|
if config is None:
|
|
|
|
config = {} # Do not use mutables as default arguments!
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.config = SimpleConfig(config) if type(config) == type({}) else config
|
|
|
|
self.message_id = 0
|
|
|
|
self.unanswered_requests = {}
|
|
|
|
self.subscriptions = {}
|
|
|
|
self.debug = False
|
|
|
|
self.lock = threading.Lock()
|
|
|
|
self.pending_transactions_for_notifications = []
|
|
|
|
self.callbacks = {}
|
|
|
|
self.running = True
|
|
|
|
self.daemon = True
|
|
|
|
|
2014-07-27 02:33:02 -07:00
|
|
|
if socket:
|
|
|
|
self.pipe = util.SocketPipe(socket)
|
|
|
|
self.network = None
|
|
|
|
else:
|
|
|
|
self.network = Network(config)
|
|
|
|
self.pipe = util.QueuePipe(send_queue=self.network.requests_queue)
|
|
|
|
self.network.start(self.pipe.get_queue)
|
|
|
|
|
2014-07-24 14:14:47 -07:00
|
|
|
# status variables
|
2014-07-25 04:30:27 -07:00
|
|
|
self.status = 'connecting'
|
2014-07-25 00:11:56 -07:00
|
|
|
self.servers = {}
|
2014-07-24 14:14:47 -07:00
|
|
|
self.banner = ''
|
2014-07-25 07:32:19 -07:00
|
|
|
self.blockchain_height = 0
|
|
|
|
self.server_height = 0
|
2014-07-25 00:11:56 -07:00
|
|
|
self.interfaces = []
|
2014-07-24 14:14:47 -07:00
|
|
|
|
2014-07-24 01:59:13 -07:00
|
|
|
def is_running(self):
|
|
|
|
return self.running
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while self.is_running():
|
|
|
|
try:
|
2014-07-27 02:33:02 -07:00
|
|
|
response = self.pipe.get()
|
|
|
|
except util.timeout:
|
2014-07-24 01:59:13 -07:00
|
|
|
continue
|
2014-07-27 02:33:02 -07:00
|
|
|
if response is None:
|
2014-07-24 01:59:13 -07:00
|
|
|
break
|
2014-07-27 02:33:02 -07:00
|
|
|
self.process(response)
|
|
|
|
|
2014-07-24 01:59:13 -07:00
|
|
|
print_error("NetworkProxy thread terminating")
|
2014-07-27 02:33:02 -07:00
|
|
|
self.stop()
|
2014-07-24 01:59:13 -07:00
|
|
|
|
|
|
|
def process(self, response):
|
|
|
|
if self.debug:
|
|
|
|
print_error("<--", response)
|
|
|
|
|
2014-07-24 14:14:47 -07:00
|
|
|
if response.get('method') == 'network.status':
|
|
|
|
key, value = response.get('params')
|
|
|
|
if key == 'status':
|
|
|
|
self.status = value
|
|
|
|
elif key == 'banner':
|
|
|
|
self.banner = value
|
|
|
|
elif key == 'updated':
|
2014-07-25 07:32:19 -07:00
|
|
|
self.blockchain_height, self.server_height = value
|
2014-07-24 14:14:47 -07:00
|
|
|
elif key == 'servers':
|
|
|
|
self.servers = value
|
2014-07-25 00:11:56 -07:00
|
|
|
elif key == 'interfaces':
|
|
|
|
self.interfaces = value
|
2014-07-24 14:14:47 -07:00
|
|
|
self.trigger_callback(key)
|
2014-07-24 01:59:13 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
msg_id = response.get('id')
|
|
|
|
result = response.get('result')
|
2014-07-27 02:33:02 -07:00
|
|
|
if msg_id is not None:
|
|
|
|
with self.lock:
|
|
|
|
method, params, callback = self.unanswered_requests.pop(msg_id)
|
|
|
|
else:
|
|
|
|
method = response.get('method')
|
|
|
|
params = response.get('params')
|
|
|
|
with self.lock:
|
|
|
|
for k,v in self.subscriptions.items():
|
|
|
|
if (method, params) in v:
|
|
|
|
callback = k
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print_error( "received unexpected notification", method, params)
|
|
|
|
return
|
2014-07-24 01:59:13 -07:00
|
|
|
|
2014-07-27 15:13:40 -07:00
|
|
|
|
|
|
|
r = {'method':method, 'params':params, 'result':result, 'id':msg_id}
|
|
|
|
callback(r)
|
2014-07-24 01:59:13 -07:00
|
|
|
|
|
|
|
|
|
|
|
def send(self, messages, callback):
|
|
|
|
"""return the ids of the requests that we sent"""
|
2014-07-27 02:33:02 -07:00
|
|
|
|
|
|
|
# detect subscriptions
|
|
|
|
sub = []
|
|
|
|
for message in messages:
|
|
|
|
m, v = message
|
|
|
|
if m[-10:] == '.subscribe':
|
|
|
|
sub.append(message)
|
|
|
|
if sub:
|
|
|
|
with self.lock:
|
|
|
|
if self.subscriptions.get(callback) is None:
|
|
|
|
self.subscriptions[callback] = []
|
|
|
|
for message in sub:
|
|
|
|
if message not in self.subscriptions[callback]:
|
|
|
|
self.subscriptions[callback].append(message)
|
|
|
|
|
2014-07-24 01:59:13 -07:00
|
|
|
with self.lock:
|
2014-07-27 02:33:02 -07:00
|
|
|
requests = []
|
2014-07-24 01:59:13 -07:00
|
|
|
ids = []
|
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
2014-07-26 04:04:02 -07:00
|
|
|
request = { 'id':self.message_id, 'method':method, 'params':params }
|
2014-07-24 01:59:13 -07:00
|
|
|
self.unanswered_requests[self.message_id] = method, params, callback
|
|
|
|
ids.append(self.message_id)
|
2014-07-27 02:33:02 -07:00
|
|
|
requests.append(request)
|
2014-07-24 01:59:13 -07:00
|
|
|
if self.debug:
|
|
|
|
print_error("-->", request)
|
|
|
|
self.message_id += 1
|
2014-07-27 02:33:02 -07:00
|
|
|
|
|
|
|
self.pipe.send_all(requests)
|
2014-07-24 01:59:13 -07:00
|
|
|
return ids
|
|
|
|
|
|
|
|
|
|
|
|
def synchronous_get(self, requests, timeout=100000000):
|
|
|
|
queue = Queue.Queue()
|
2014-07-27 02:33:02 -07:00
|
|
|
ids = self.send(requests, queue.put)
|
2014-07-24 01:59:13 -07:00
|
|
|
id2 = ids[:]
|
|
|
|
res = {}
|
|
|
|
while ids:
|
|
|
|
r = queue.get(True, timeout)
|
|
|
|
_id = r.get('id')
|
|
|
|
if _id in ids:
|
|
|
|
ids.remove(_id)
|
|
|
|
res[_id] = r.get('result')
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
out = []
|
|
|
|
for _id in id2:
|
|
|
|
out.append(res[_id])
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
def get_servers(self):
|
2014-07-24 14:14:47 -07:00
|
|
|
return self.servers
|
2014-07-24 01:59:13 -07:00
|
|
|
|
2014-07-25 00:11:56 -07:00
|
|
|
def get_interfaces(self):
|
|
|
|
return self.interfaces
|
|
|
|
|
2014-07-24 01:59:13 -07:00
|
|
|
def get_header(self, height):
|
|
|
|
return self.synchronous_get([('network.get_header',[height])])[0]
|
|
|
|
|
|
|
|
def get_local_height(self):
|
2014-07-25 07:32:19 -07:00
|
|
|
return self.blockchain_height
|
|
|
|
|
|
|
|
def get_server_height(self):
|
|
|
|
return self.server_height
|
2014-07-24 01:59:13 -07:00
|
|
|
|
|
|
|
def is_connected(self):
|
2014-07-24 14:14:47 -07:00
|
|
|
return self.status == 'connected'
|
2014-07-24 01:59:13 -07:00
|
|
|
|
2014-07-25 04:30:27 -07:00
|
|
|
def is_connecting(self):
|
|
|
|
return self.status == 'connecting'
|
|
|
|
|
2014-07-24 01:59:13 -07:00
|
|
|
def is_up_to_date(self):
|
2014-07-27 02:33:02 -07:00
|
|
|
return self.unanswered_requests == {}
|
2014-07-24 01:59:13 -07:00
|
|
|
|
2014-07-25 00:11:56 -07:00
|
|
|
def get_parameters(self):
|
|
|
|
return self.synchronous_get([('network.get_parameters',[])])[0]
|
|
|
|
|
|
|
|
def set_parameters(self, *args):
|
|
|
|
return self.synchronous_get([('network.set_parameters',args)])[0]
|
2014-07-24 01:59:13 -07:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.running = False
|
2014-07-27 02:33:02 -07:00
|
|
|
if self.network:
|
|
|
|
self.network.stop()
|
2014-07-24 01:59:13 -07:00
|
|
|
|
2014-07-25 06:42:28 -07:00
|
|
|
def stop_daemon(self):
|
|
|
|
return self.send([('daemon.stop',[])], None)
|
|
|
|
|
2014-07-24 01:59:13 -07:00
|
|
|
def register_callback(self, event, callback):
|
|
|
|
with self.lock:
|
|
|
|
if not self.callbacks.get(event):
|
|
|
|
self.callbacks[event] = []
|
|
|
|
self.callbacks[event].append(callback)
|
|
|
|
|
|
|
|
def trigger_callback(self, event):
|
|
|
|
with self.lock:
|
|
|
|
callbacks = self.callbacks.get(event,[])[:]
|
|
|
|
if callbacks:
|
|
|
|
[callback() for callback in callbacks]
|
|
|
|
|