commit 8e6438e245b07df84ca20a2cff16a8957f0c0a92
parent b92d6ab9e16e03747c4ac7b9f093a268b43a3c43
Author: parazyd <parazyd@dyne.org>
Date: Tue, 13 Apr 2021 17:59:01 +0200
Format code.
Diffstat:
4 files changed, 70 insertions(+), 46 deletions(-)
diff --git a/obelisk/protocol.py b/obelisk/protocol.py
@@ -56,6 +56,7 @@ Please consider donating: %s
class ElectrumProtocol(asyncio.Protocol): # pylint: disable=R0904,R0902
"""Class implementing the Electrum protocol, with async support"""
+
def __init__(self, log, chain, endpoints, server_cfg):
self.log = log
self.stopped = False
@@ -78,38 +79,52 @@ class ElectrumProtocol(asyncio.Protocol): # pylint: disable=R0904,R0902
# Here we map available methods to their respective functions
self.methodmap = {
- "blockchain.block.header": self.blockchain_block_header,
- "blockchain.block.headers": self.blockchain_block_headers,
- "blockchain.estimatefee": self.blockchain_estimatefee,
- "blockchain.headers.subscribe": self.blockchain_headers_subscribe,
- "blockchain.relayfee": self.blockchain_relayfee,
+ "blockchain.block.header":
+ self.blockchain_block_header,
+ "blockchain.block.headers":
+ self.blockchain_block_headers,
+ "blockchain.estimatefee":
+ self.blockchain_estimatefee,
+ "blockchain.headers.subscribe":
+ self.blockchain_headers_subscribe,
+ "blockchain.relayfee":
+ self.blockchain_relayfee,
"blockchain.scripthash.get_balance":
- self.blockchain_scripthash_get_balance,
+ self.blockchain_scripthash_get_balance,
"blockchain.scripthash.get_history":
- self.blockchain_scripthash_get_history,
+ self.blockchain_scripthash_get_history,
"blockchain.scripthash.get_mempool":
- self.blockchain_scripthash_get_mempool,
+ self.blockchain_scripthash_get_mempool,
"blockchain.scripthash.listunspent":
- self.blockchain_scripthash_listunspent,
+ self.blockchain_scripthash_listunspent,
"blockchain.scripthash.subscribe":
- self.blockchain_scripthash_subscribe,
+ self.blockchain_scripthash_subscribe,
"blockchain.scripthash.unsubscribe":
- self.blockchain_scripthash_unsubscribe,
+ self.blockchain_scripthash_unsubscribe,
"blockchain.transaction.broadcast":
- self.blockchain_transaction_broadcast,
- "blockchain.transaction.get": self.blockchain_transaction_get,
+ self.blockchain_transaction_broadcast,
+ "blockchain.transaction.get":
+ self.blockchain_transaction_get,
"blockchain.transaction.get_merkle":
- self.blockchain_transaction_get_merkle,
+ self.blockchain_transaction_get_merkle,
"blockchain.transaction.id_from_pos":
- self.blockchain_transaction_from_pos,
- "mempool.get_fee_histogram": self.mempool_get_fee_histogram,
- "server_add_peer": self.server_add_peer,
- "server.banner": self.server_banner,
- "server.donation_address": self.server_donation_address,
- "server.features": self.server_features,
- "server.peers.subscribe": self.server_peers_subscribe,
- "server.ping": self.server_ping,
- "server.version": self.server_version,
+ self.blockchain_transaction_from_pos,
+ "mempool.get_fee_histogram":
+ self.mempool_get_fee_histogram,
+ "server_add_peer":
+ self.server_add_peer,
+ "server.banner":
+ self.server_banner,
+ "server.donation_address":
+ self.server_donation_address,
+ "server.features":
+ self.server_features,
+ "server.peers.subscribe":
+ self.server_peers_subscribe,
+ "server.ping":
+ self.server_ping,
+ "server.version":
+ self.server_version,
}
async def stop(self):
@@ -440,8 +455,7 @@ class ElectrumProtocol(asyncio.Protocol): # pylint: disable=R0904,R0902
if _ec and _ec != 0:
return ERRORS["internalerror"]
- task = asyncio.create_task(self.scripthash_notifier(
- writer, scripthash))
+ task = asyncio.create_task(self.scripthash_notifier(writer, scripthash))
self.sh_subscriptions[scripthash] = {"task": task}
if len(history) < 1:
diff --git a/obelisk/zeromq.py b/obelisk/zeromq.py
@@ -89,6 +89,7 @@ def unpack_table(row_fmt, data):
class ClientSettings:
"""Class implementing ZMQ client settings"""
+
def __init__(self, timeout=10, context=None, loop=None):
self._timeout = timeout
self._context = context
@@ -121,6 +122,7 @@ class Request:
"""Class implementing a _send_ request.
This is either a simple request/response affair or a subscription.
"""
+
def __init__(self, socket, command, data):
self.id_ = create_random_id()
self.socket = socket
@@ -150,6 +152,7 @@ class InvalidServerResponseException(Exception):
class Response:
"""Class implementing a request response"""
+
def __init__(self, frame):
if len(frame) != 3:
raise InvalidServerResponseException(
@@ -175,6 +178,7 @@ class RequestCollection:
"""RequestCollection carries a list of Requests and matches incoming
responses to them.
"""
+
def __init__(self, socket, loop):
self._socket = socket
self._requests = {}
@@ -229,6 +233,7 @@ class RequestCollection:
class Client:
"""This class represents a connection to a libbitcoin server."""
+
def __init__(self, log, endpoints, loop):
self.log = log
self._endpoints = endpoints
@@ -268,8 +273,7 @@ class Client:
return error_code, request.queue
async def _simple_request(self, command, data):
- return await self._wait_for_response(await
- self._request(command, data))
+ return await self._wait_for_response(await self._request(command, data))
async def _request(self, command, data):
"""Make a generic request. Both options are byte objects specified
@@ -318,9 +322,8 @@ class Client:
async def fetch_blockchain_transaction(self, txid):
"""Fetch transaction by txid (not including mempool)"""
command = b"blockchain.fetch_transaction2"
- error_code, data = await self._simple_request(
- command,
- bytes.fromhex(txid)[::-1])
+ error_code, data = await self._simple_request(command,
+ bytes.fromhex(txid)[::-1])
if error_code:
return error_code, None
return error_code, data
@@ -328,9 +331,8 @@ class Client:
async def fetch_mempool_transaction(self, txid):
"""Fetch transaction by txid (including mempool)"""
command = b"transaction_pool.fetch_transaction2"
- error_code, data = await self._simple_request(
- command,
- bytes.fromhex(txid)[::-1])
+ error_code, data = await self._simple_request(command,
+ bytes.fromhex(txid)[::-1])
if error_code:
return error_code, None
return error_code, data
diff --git a/res/format_code.py b/res/format_code.py
@@ -6,4 +6,4 @@
from subprocess import run
run(["black", "-l", "80", "."])
-run(["yapf", "-i", "-r", "."])
+run(["yapf", "--style", "google", "-i", "-r", "."])
diff --git a/tests/test_electrum_protocol.py b/tests/test_electrum_protocol.py
@@ -153,8 +153,9 @@ async def test_blockchain_scripthash_listunspent(protocol, writer):
async def test_blockchain_transaction_get(protocol, writer):
expect = "020000000001011caa5f4ba91ff0ab77712851c1b17943e68f28d46bb0d96cbc13cdbef53c2b87000000001716001412e6e94028ab399b67c1232383d12f1dd3fc03b5feffffff02a40111000000000017a914ff1d7f4c85c562764ca16daa11e97d10eda52ebf87a0860100000000001976a9144a0360eac874a569e82ca6b17274d90bccbcab5e88ac0247304402205392417f5ffba2c0f3a501476fb6872368b2065c53bf18b2a201691fb88cdbe5022016c68ec9e094ba2b06d4bdc6af996ac74b580ab9728c622bb5304aaff04cb6980121031092742ffdf5901ceafcccec090c58170ce1d0ec26963ef7c7a2738a415a317e0b121e00"
params = {
- "params":
- ["a9c3c22cc2589284288b28e802ea81723d649210d59dfa7e03af00475f4cec20"]
+ "params": [
+ "a9c3c22cc2589284288b28e802ea81723d649210d59dfa7e03af00475f4cec20"
+ ]
}
data = await protocol.blockchain_transaction_get(writer, params)
assert data["result"] == expect
@@ -178,6 +179,7 @@ async def test_server_ping(protocol, writer):
class MockWriter(asyncio.StreamWriter):
"""Mock class for StreamWriter"""
+
def __init__(self):
self.mock = None
@@ -196,32 +198,38 @@ async def main():
protocol = ElectrumProtocol(log, "testnet", ENDPOINTS, {})
writer = MockWriter()
functions = {
- "blockchain_block_header": test_blockchain_block_header,
- "blockchain_block_hedaers": test_blockchain_block_headers,
- "blockchain_estimatefee": test_blockchain_estimatefee,
+ "blockchain_block_header":
+ test_blockchain_block_header,
+ "blockchain_block_hedaers":
+ test_blockchain_block_headers,
+ "blockchain_estimatefee":
+ test_blockchain_estimatefee,
# "blockchain_headers_subscribe": test_blockchain_headers_subscribe,
- "blockchain_relayfee": test_blockchain_relayfee,
+ "blockchain_relayfee":
+ test_blockchain_relayfee,
"blockchain_scripthash_get_balance":
- test_blockchain_scripthash_get_balance,
+ test_blockchain_scripthash_get_balance,
"blockchain_scripthash_get_history":
- test_blockchain_scripthash_get_history,
+ test_blockchain_scripthash_get_history,
# "blockchain_scripthash_get_mempool": test_blockchain_scripthash_get_mempool,
"blockchain_scripthash_listunspent":
- test_blockchain_scripthash_listunspent,
+ test_blockchain_scripthash_listunspent,
# "blockchain_scripthash_subscribe": test_blockchain_scripthash_subscribe,
# "blockchain_scripthash_unsubscribe": test_blockchain_scripthash_unsubscribe,
# "blockchain_transaction_broadcast": test_blockchain_transaction_broadcast,
- "blockchain_transaction_get": test_blockchain_transaction_get,
+ "blockchain_transaction_get":
+ test_blockchain_transaction_get,
# "blockchain_transaction_get_merkle": test_blockchain_transaction_get_merkle,
"blockchain_transaction_from_pos":
- test_blockchain_transaction_from_pos,
+ test_blockchain_transaction_from_pos,
# "mempool_get_fee_histogram": test_mempool_get_fee_histogram,
# "server_add_peer": test_server_add_peer,
# "server_banner": test_server_banner,
# "server_donation_address": test_server_donation_address,
# "server_features": test_server_features,
# "server_peers_subscribe": test_server_peers_subscribe,
- "server_ping": test_server_ping,
+ "server_ping":
+ test_server_ping,
# "server_version": test_server_version,
}