obelisk

Electrum server using libbitcoin as its backend
git clone https://git.parazyd.org/obelisk
Log | Files | Refs | README | LICENSE

commit 6ab9415b33bf5a39fe4461cdbadb4dffbd8be33a
parent 5c35db371a89159452413a55b107cc7a576fe471
Author: parazyd <parazyd@dyne.org>
Date:   Fri, 16 Apr 2021 11:04:11 +0200

Code coverage preparation.

Diffstat:
M.github/workflows/py.yaml | 2+-
M.gitignore | 1+
MMakefile | 3++-
MREADME.md | 2+-
Mobelisk/errors_jsonrpc.py | 2+-
Mobelisk/errors_libbitcoin.py | 2+-
Mobelisk/merkle.py | 8++++----
Mobelisk/protocol.py | 4++--
Mobelisk/util.py | 8++++----
Mobelisk/zeromq.py | 4++--
Mtests/__main__.py | 4++--
Mtests/test_electrum_protocol.py | 14+++++---------
12 files changed, 26 insertions(+), 28 deletions(-)

diff --git a/.github/workflows/py.yaml b/.github/workflows/py.yaml @@ -22,4 +22,4 @@ jobs: pip install -e . - name: Run tests run: | - python ./tests/test_electrum_protocol.py + python tests diff --git a/.gitignore b/.gitignore @@ -1,2 +1,3 @@ *.pyc .coverage +htmlcov diff --git a/Makefile b/Makefile @@ -8,8 +8,9 @@ format: python3 ./res/format_code.py test: - python3 ./tests/test_electrum_protocol.py + python3 tests coverage: coverage run tests coverage report + coverage html diff --git a/README.md b/README.md @@ -71,7 +71,7 @@ It is also recommended to run the test suite and see if anything fails: ``` -python3 ./tests/test_electrum_protocol.py +python3 tests ``` You can chat about Obelisk on Freenode IRC, either `#electrum` or diff --git a/obelisk/errors_jsonrpc.py b/obelisk/errors_jsonrpc.py @@ -17,7 +17,7 @@ """JSON-RPC errors: https://www.jsonrpc.org/specification#error_object""" -class JsonRPCError: +class JsonRPCError: # pragma: no cover """Class implementing functions returning JSON-RPC errors""" def __init__(self): diff --git a/obelisk/errors_libbitcoin.py b/obelisk/errors_libbitcoin.py @@ -22,7 +22,7 @@ def make_error_code(ec): """Return ErrorCode from ec""" if not ec: return None - return ErrorCode(ec) + return ErrorCode(ec) # pragma: no cover class ErrorCode(Enum): diff --git a/obelisk/merkle.py b/obelisk/merkle.py @@ -20,7 +20,7 @@ from math import ceil, log from obelisk.util import double_sha256, hash_to_hex_str -def branch_length(hash_count): +def branch_length(hash_count): # pragma: no cover """Return the length of a merkle branch given the number of hashes""" if not isinstance(hash_count, int): raise TypeError("hash_count must be an integer") @@ -35,14 +35,14 @@ def merkle_branch_and_root(hashes, index, length=None): """ hashes = list(hashes) if not isinstance(index, int): - raise TypeError("index must be an integer") + raise TypeError("index must be an integer") # pragma: no cover # This also asserts hashes is not empty if not 0 <= index < len(hashes): - raise ValueError("index out of range") + raise ValueError("index out of range") # pragma: no cover natural_length = branch_length(len(hashes)) if length is None: length = natural_length - else: + else: # pragma: no cover if not isinstance(length, int): raise TypeError("length must be an integer") if length < natural_length: diff --git a/obelisk/protocol.py b/obelisk/protocol.py @@ -70,7 +70,7 @@ class ElectrumProtocol(asyncio.Protocol): # pylint: disable=R0904,R0902 self.tasks = [] self.sh_subscriptions = {} - if chain == "mainnet": + if chain == "mainnet": # pragma: no cover self.genesis = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" elif chain == "testnet": self.genesis = "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943" @@ -109,7 +109,7 @@ class ElectrumProtocol(asyncio.Protocol): # pylint: disable=R0904,R0902 self.log.debug("ElectrumProtocol.stop()") if self.bx: unsub_pool = [] - for i in self.sh_subscriptions: + for i in self.sh_subscriptions: # pragma: no cover self.log.debug("bx.unsubscribe %s", i) unsub_pool.append(self.bx.unsubscribe_scripthash(i)) await asyncio.gather(*unsub_pool, return_exceptions=True) diff --git a/obelisk/util.py b/obelisk/util.py @@ -30,7 +30,7 @@ def is_non_negative_integer(val): """Check if val is of type int and non-negative""" if is_integer(val): return val >= 0 - return False + return False # pragma: no cover def is_boolean(val): @@ -38,7 +38,7 @@ def is_boolean(val): return isinstance(val, bool) -def is_hex_str(text): +def is_hex_str(text): # pragma: no cover """Check if text is a hex string""" if not isinstance(text, str): return False @@ -52,7 +52,7 @@ def is_hex_str(text): return True -def is_hash256_str(text): +def is_hash256_str(text): # pragma: no cover """Check if text is a sha256 hash""" if not isinstance(text, str): return False @@ -77,7 +77,7 @@ def bh2u(val): return val.hex() -def block_to_header(block): +def block_to_header(block): # pragma: no cover """Return block header from raw block""" if not isinstance(block, (bytes, bytearray)): raise ValueError("block is not of type bytes/bytearray") diff --git a/obelisk/zeromq.py b/obelisk/zeromq.py @@ -107,7 +107,7 @@ class ClientSettings: @context.setter def context(self, context): - self._context = context + self._context = context # pragma: no cover @property def timeout(self): @@ -116,7 +116,7 @@ class ClientSettings: @timeout.setter def timeout(self, timeout): - self._timeout = timeout + self._timeout = timeout # pragma: no cover class Request: diff --git a/tests/__main__.py b/tests/__main__.py @@ -1,3 +1,4 @@ import asyncio from test_electrum_protocol import main -asyncio.run(main())- \ No newline at end of file + +asyncio.run(main()) diff --git a/tests/test_electrum_protocol.py b/tests/test_electrum_protocol.py @@ -59,11 +59,11 @@ def get_expect(method, params): recv_buf = bytearray() while True: data = bs.recv(4096) - if not data or len(data) == 0: + if not data or len(data) == 0: # pragma: no cover raise ValueError("No data received from blockstream") recv_buf.extend(data) lb = recv_buf.find(b"\n") - if lb == -1: + if lb == -1: # pragma: no cover continue while lb != -1: line = recv_buf[:lb].rstrip() @@ -74,7 +74,7 @@ def get_expect(method, params): return resp -def assert_equal(data, expect): +def assert_equal(data, expect): # pragma: no cover try: assert data == expect except AssertionError: @@ -208,7 +208,7 @@ async def test_server_version(protocol, writer, method): assert_equal(data["result"], expect["result"]) -class MockWriter(asyncio.StreamWriter): +class MockWriter(asyncio.StreamWriter): # pragma: no cover """Mock class for StreamWriter""" def __init__(self): @@ -265,7 +265,7 @@ async def main(): await orchestration[func](protocol, writer, func) print(f"PASS: {func}") test_pass.append(func) - except AssertionError: + except AssertionError: # pragma: no cover print(f"FAIL: {func}") traceback.print_exc() test_fail.append(func) @@ -279,7 +279,3 @@ async def main(): ret = 1 if len(test_fail) > 0 else 0 sys.exit(ret) - - -if __name__ == "__main__": - asyncio.run(main())