Show protocol version in sessions RPC call

This commit is contained in:
Neil Booth 2017-09-06 18:14:52 +09:00
parent f2f2b1e7b6
commit 37c15f7018
2 changed files with 15 additions and 8 deletions

View File

@ -542,13 +542,14 @@ class Controller(util.LoggedClass):
'''A generator returning lines for a list of sessions. '''A generator returning lines for a list of sessions.
data is the return value of rpc_sessions().''' data is the return value of rpc_sessions().'''
fmt = ('{:<6} {:<5} {:>17} {:>5} {:>5} ' fmt = ('{:<6} {:<5} {:>17} {:>5} {:>5} {:>5} '
'{:>7} {:>7} {:>7} {:>7} {:>7} {:>9} {:>21}') '{:>7} {:>7} {:>7} {:>7} {:>7} {:>9} {:>21}')
yield fmt.format('ID', 'Flags', 'Client', 'Reqs', 'Txs', 'Subs', yield fmt.format('ID', 'Flags', 'Client', 'Proto',
'Reqs', 'Txs', 'Subs',
'Recv', 'Recv KB', 'Sent', 'Sent KB', 'Time', 'Peer') 'Recv', 'Recv KB', 'Sent', 'Sent KB', 'Time', 'Peer')
for (id_, flags, peer, client, reqs, txs_sent, subs, for (id_, flags, peer, client, proto, reqs, txs_sent, subs,
recv_count, recv_size, send_count, send_size, time) in data: recv_count, recv_size, send_count, send_size, time) in data:
yield fmt.format(id_, flags, client, yield fmt.format(id_, flags, client, proto,
'{:,d}'.format(reqs), '{:,d}'.format(reqs),
'{:,d}'.format(txs_sent), '{:,d}'.format(txs_sent),
'{:,d}'.format(subs), '{:,d}'.format(subs),
@ -566,6 +567,7 @@ class Controller(util.LoggedClass):
session.flags(), session.flags(),
session.peername(for_log=for_log), session.peername(for_log=for_log),
session.client, session.client,
session.protocol_version,
session.count_pending_items(), session.count_pending_items(),
session.txs_sent, session.txs_sent,
session.sub_count(), session.sub_count(),

View File

@ -113,6 +113,7 @@ class ElectrumX(SessionBase):
self.hashX_subs = {} self.hashX_subs = {}
self.mempool_statuses = {} self.mempool_statuses = {}
self.chunk_indices = [] self.chunk_indices = []
self.protocol_version = None
self.set_protocol_handlers((1, 0)) self.set_protocol_handlers((1, 0))
def sub_count(self): def sub_count(self):
@ -322,8 +323,6 @@ class ElectrumX(SessionBase):
except Exception: except Exception:
pass pass
self.log_info('protocol version {} requested'.format(protocol_version))
# Find the highest common protocol version. Disconnect if # Find the highest common protocol version. Disconnect if
# that protocol version in unsupported. # that protocol version in unsupported.
ptuple = util.protocol_version(protocol_version, version.PROTOCOL_MIN, ptuple = util.protocol_version(protocol_version, version.PROTOCOL_MIN,
@ -336,10 +335,11 @@ class ElectrumX(SessionBase):
self.set_protocol_handlers(ptuple) self.set_protocol_handlers(ptuple)
# The return value depends on the protocol version
if ptuple < (1, 1): if ptuple < (1, 1):
return version.VERSION return version.VERSION
else:
return (version.VERSION, '.'.join(str(part) for part in ptuple)) return (version.VERSION, self.protocol_version)
async def transaction_broadcast(self, raw_tx): async def transaction_broadcast(self, raw_tx):
'''Broadcast a raw transaction to the network. '''Broadcast a raw transaction to the network.
@ -382,6 +382,10 @@ class ElectrumX(SessionBase):
return message return message
def set_protocol_handlers(self, ptuple): def set_protocol_handlers(self, ptuple):
protocol_version = '.'.join(str(part) for part in ptuple)
if protocol_version == self.protocol_version:
return
self.protocol_version = protocol_version
controller = self.controller controller = self.controller
handlers = { handlers = {
'blockchain.address.get_balance': controller.address_get_balance, 'blockchain.address.get_balance': controller.address_get_balance,
@ -438,6 +442,7 @@ class LocalRPC(SessionBase):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.client = 'RPC' self.client = 'RPC'
self.max_send = 0 self.max_send = 0
self.protocol_version = 'RPC'
def request_handler(self, method): def request_handler(self, method):
'''Return the async handler for the given request method.''' '''Return the async handler for the given request method.'''