Prefix internal methods with an underscore

This commit is contained in:
Neil Booth 2016-11-12 07:43:24 +09:00
parent cd0edadc13
commit 56274fb9ea

View File

@ -45,7 +45,7 @@ class Daemon(util.LoggedClass):
.format(height)) .format(height))
self._height = height self._height = height
async def post(self, data): async def _post(self, data):
'''Send data to the daemon and handle the response.''' '''Send data to the daemon and handle the response.'''
async with self.workqueue_semaphore: async with self.workqueue_semaphore:
async with aiohttp.post(self.url, data=data) as resp: async with aiohttp.post(self.url, data=data) as resp:
@ -66,7 +66,7 @@ class Daemon(util.LoggedClass):
raise DaemonWarmingUpError raise DaemonWarmingUpError
raise DaemonError(err) raise DaemonError(err)
async def send(self, payload): async def _send(self, payload):
'''Send a payload to be converted to JSON. '''Send a payload to be converted to JSON.
Handles temporary connection issues. Daemon reponse errors Handles temporary connection issues. Daemon reponse errors
@ -77,7 +77,7 @@ class Daemon(util.LoggedClass):
prior_msg = None prior_msg = None
while True: while True:
try: try:
result = await self.post(data) result = await self._post(data)
if prior_msg: if prior_msg:
self.logger.info('connection successfully restored') self.logger.info('connection successfully restored')
return result return result
@ -105,34 +105,29 @@ class Daemon(util.LoggedClass):
count += 1 count += 1
secs = min(16, secs * 2) secs = min(16, secs * 2)
async def send_single(self, method, params=None): async def _send_single(self, method, params=None):
'''Send a single request to the daemon.''' '''Send a single request to the daemon.'''
payload = {'method': method} payload = {'method': method}
if params: if params:
payload['params'] = params payload['params'] = params
return await self.send(payload) return await self._send(payload)
async def send_many(self, mp_iterable): async def _send_vector(self, method, params_iterable):
'''Send several requests at once.'''
payload = [{'method': m, 'params': p} for m, p in mp_iterable]
if payload:
return await self.send(payload)
return []
async def send_vector(self, method, params_iterable):
'''Send several requests of the same method.''' '''Send several requests of the same method.'''
return await self.send_many((method, params) payload = [{'method': method, 'params': p} for p in params_iterable]
for params in params_iterable) if payload:
return await self._send(payload)
return []
async def block_hex_hashes(self, first, count): async def block_hex_hashes(self, first, count):
'''Return the hex hashes of count block starting at height first.''' '''Return the hex hashes of count block starting at height first.'''
params_iterable = ((h, ) for h in range(first, first + count)) params_iterable = ((h, ) for h in range(first, first + count))
return await self.send_vector('getblockhash', params_iterable) return await self._send_vector('getblockhash', params_iterable)
async def raw_blocks(self, hex_hashes): async def raw_blocks(self, hex_hashes):
'''Return the raw binary blocks with the given hex hashes.''' '''Return the raw binary blocks with the given hex hashes.'''
params_iterable = ((h, False) for h in hex_hashes) params_iterable = ((h, False) for h in hex_hashes)
blocks = await self.send_vector('getblock', params_iterable) blocks = await self._send_vector('getblock', params_iterable)
# Convert hex string to bytes # Convert hex string to bytes
return [bytes.fromhex(block) for block in blocks] return [bytes.fromhex(block) for block in blocks]
@ -140,39 +135,39 @@ class Daemon(util.LoggedClass):
'''Return the hashes of the txs in the daemon's mempool.''' '''Return the hashes of the txs in the daemon's mempool.'''
if self.debug_caught_up: if self.debug_caught_up:
return [] return []
return await self.send_single('getrawmempool') return await self._send_single('getrawmempool')
async def estimatefee(self, params): async def estimatefee(self, params):
'''Return the fee estimate for the given parameters.''' '''Return the fee estimate for the given parameters.'''
return await self.send_single('estimatefee', params) return await self._send_single('estimatefee', params)
async def relayfee(self): async def relayfee(self):
'''The minimum fee a low-priority tx must pay in order to be accepted '''The minimum fee a low-priority tx must pay in order to be accepted
to the daemon's memory pool.''' to the daemon's memory pool.'''
net_info = await self.send_single('getnetworkinfo') net_info = await self._send_single('getnetworkinfo')
return net_info['relayfee'] return net_info['relayfee']
async def getrawtransaction(self, hex_hash): async def getrawtransaction(self, hex_hash):
'''Return the serialized raw transaction with the given hash.''' '''Return the serialized raw transaction with the given hash.'''
return await self.send_single('getrawtransaction', (hex_hash, 0)) return await self._send_single('getrawtransaction', (hex_hash, 0))
async def getrawtransactions(self, hex_hashes): async def getrawtransactions(self, hex_hashes):
'''Return the serialized raw transactions with the given hashes. '''Return the serialized raw transactions with the given hashes.
Breaks large requests up. Yields after each sub request.''' Breaks large requests up. Yields after each sub request.'''
params_iterable = ((hex_hash, 0) for hex_hash in hex_hashes) params_iterable = ((hex_hash, 0) for hex_hash in hex_hashes)
txs = await self.send_vector('getrawtransaction', params_iterable) txs = await self._send_vector('getrawtransaction', params_iterable)
# Convert hex strings to bytes # Convert hex strings to bytes
return [bytes.fromhex(tx) for tx in txs] return [bytes.fromhex(tx) for tx in txs]
async def sendrawtransaction(self, params): async def sendrawtransaction(self, params):
'''Broadcast a transaction to the network.''' '''Broadcast a transaction to the network.'''
return await self.send_single('sendrawtransaction', params) return await self._send_single('sendrawtransaction', params)
async def height(self): async def height(self):
'''Query the daemon for its current height.''' '''Query the daemon for its current height.'''
if not self.debug_caught_up: if not self.debug_caught_up:
self._height = await self.send_single('getblockcount') self._height = await self._send_single('getblockcount')
return self._height return self._height
def cached_height(self): def cached_height(self):