You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
8.1 KiB

# Copyright (c) 2016, Neil Booth
#
# All rights reserved.
#
# See the file "LICENCE" for information about the copyright
# and warranty status of this software.
'''Classes for local RPC server and remote client TCP/SSL servers.'''
import asyncio
import codecs
import json
import traceback
from functools import partial
from lib.util import LoggedClass
from server.version import VERSION
class Error(Exception):
BAD_REQUEST = 1
INTERNAL_ERROR = 2
class JSONRPC(asyncio.Protocol, LoggedClass):
8 years ago
'''Base class that manages a JSONRPC connection.
When a request comes in for an RPC method M, then a member
function handle_M is called with the request params array, except
that periods in M are replaced with underscores. So a RPC call
for method 'blockchain.estimatefee' will be passed to
handle_blockchain_estimatefee.
'''
def __init__(self, controller):
super().__init__()
self.controller = controller
self.parts = []
def connection_made(self, transport):
8 years ago
'''Handle an incoming client connection.'''
self.transport = transport
8 years ago
self.peername = transport.get_extra_info('peername')
self.logger.info('connection from {}'.format(self.peername))
self.controller.add_session(self)
def connection_lost(self, exc):
8 years ago
'''Handle client disconnection.'''
self.logger.info('disconnected: {}'.format(self.peername))
self.controller.remove_session(self)
def data_received(self, data):
8 years ago
'''Handle incoming data (synchronously).
Requests end in newline characters. Pass complete requests to
decode_message for handling.
'''
while True:
npos = data.find(ord('\n'))
if npos == -1:
8 years ago
self.parts.append(data)
break
tail, data = data[:npos], data[npos + 1:]
8 years ago
parts, self.parts = self.parts, []
parts.append(tail)
self.decode_message(b''.join(parts))
def decode_message(self, message):
8 years ago
'''Decode a binary message and queue it for asynchronous handling.'''
try:
message = json.loads(message.decode())
except Exception as e:
8 years ago
self.logger.info('error decoding JSON message'.format(e))
else:
self.controller.add_job(self.request_handler(message))
async def request_handler(self, request):
'''Called asynchronously.'''
error = result = None
try:
result = await self.json_handler(request)
except Error as e:
error = {'code': e.args[0], 'message': e.args[1]}
except asyncio.CancelledError:
raise
except Exception as e:
# This should be considered a bug and fixed
traceback.print_exc()
error = {'code': Error.INTERNAL_ERROR, 'message': str(e)}
payload = {'id': request.get('id'), 'error': error, 'result': result}
try:
data = json.dumps(payload) + '\n'
except TypeError:
msg = 'cannot JSON encode response to request {}'.format(request)
self.logger.error(msg)
error = {'code': Error.INTERNAL_ERROR, 'message': msg}
payload = {'id': request.get('id'), 'error': error, 'result': None}
data = json.dumps(payload) + '\n'
self.transport.write(data.encode())
async def json_handler(self, request):
method = request.get('method')
handler = None
if isinstance(method, str):
handler_name = 'handle_{}'.format(method.replace('.', '_'))
handler = getattr(self, handler_name, None)
if not handler:
self.logger.info('unknown method: {}'.format(method))
raise Error(Error.BAD_REQUEST, 'unknown method: {}'.format(method))
params = request.get('params', [])
if not isinstance(params, list):
raise Error(Error.BAD_REQUEST, 'params should be an array')
return await handler(params)
class ElectrumX(JSONRPC):
'''A TCP server that handles incoming Electrum connections.'''
def __init__(self, controller, daemon, env):
super().__init__(controller)
self.daemon = daemon
self.env = env
self.addresses = set()
self.subscribe_headers = False
def params_to_hash168(self, params):
if len(params) != 1:
raise Error(Error.BAD_REQUEST,
'params should contain a single address')
address = params[0]
try:
return self.env.coin.address_to_hash168(address)
except:
raise Error(Error.BAD_REQUEST,
'invalid address: {}'.format(address))
async def handle_blockchain_address_get_history(self, params):
hash168 = self.params_to_hash168(params)
return self.controller.get_history(hash168)
async def handle_blockchain_address_subscribe(self, params):
hash168 = self.params_to_hash168(params)
status = self.controller.address_status(hash168)
return status.hex() if status else None
async def handle_blockchain_estimatefee(self, params):
result = await self.daemon.send_single('estimatefee', params)
return result
async def handle_blockchain_headers_subscribe(self, params):
self.subscribe_headers = True
return self.controller.get_current_header()
async def handle_blockchain_relayfee(self, params):
'''The minimum fee a low-priority tx must pay in order to be accepted
to this daemon's memory pool.
'''
net_info = await self.daemon.send_single('getnetworkinfo')
return net_info['relayfee']
async def handle_blockchain_transaction_get(self, params):
if len(params) != 1:
raise Error(Error.BAD_REQUEST,
'params should contain a transaction hash')
tx_hash = params[0]
return await self.daemon.send_single('getrawtransaction', (tx_hash, 0))
async def handle_blockchain_transaction_get_merkle(self, params):
if len(params) != 2:
raise Error(Error.BAD_REQUEST,
'params should contain a transaction hash and height')
tx_hash, height = params
return await self.controller.get_merkle(tx_hash, height)
async def handle_server_banner(self, params):
'''Return the server banner.'''
banner = 'Welcome to Electrum!'
if self.env.banner_file:
try:
with codecs.open(self.env.banner_file, 'r', 'utf-8') as f:
banner = f.read()
except Exception as e:
self.logger.error('reading banner file {}: {}'
.format(self.env.banner_file, e))
return banner
async def handle_server_donation_address(self, params):
'''Return the donation address as a string.
If none is specified return the empty string.
'''
return self.env.donation_address
async def handle_server_peers_subscribe(self, params):
'''Returns the peer (ip, host, ports) tuples.
Despite the name electrum-server does not treat this as a
subscription.
'''
peers = self.controller.get_peers()
return tuple(peers.values())
async def handle_server_version(self, params):
'''Return the server version as a string.'''
return VERSION
class LocalRPC(JSONRPC):
'''A local TCP RPC server for querying status.'''
async def handle_getinfo(self, params):
return {
'blocks': self.controller.height(),
'peers': len(self.controller.get_peers()),
'sessions': len(self.controller.sessions),
'watched': sum(len(s.addresses) for s in self.controller.sessions
if isinstance(s, ElectrumX)),
'cached': 0,
}
async def handle_sessions(self, params):
return []
async def handle_numsessions(self, params):
return len(self.controller.sessions)
async def handle_peers(self, params):
return tuple(self.controller.get_peers().keys())
async def handle_numpeers(self, params):
return len(self.controller.get_peers())