tprotocol/blockchain.block.header: Inefficient implementation of cp_height. - obelisk - Electrum server using libbitcoin as its backend
 (HTM) git clone https://git.parazyd.org/obelisk
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit b5e4ed32fabaa3d57057f12114be7aaa22dc0124
 (DIR) parent b4ba13a19e7f5a9f57b537d0139f365a76b78147
 (HTM) Author: parazyd <parazyd@dyne.org>
       Date:   Thu, 15 Apr 2021 23:35:15 +0200
       
       protocol/blockchain.block.header: Inefficient implementation of cp_height.
       
       Diffstat:
         M obelisk/protocol.py                 |      11 ++++++-----
         M tests/test_electrum_protocol.py     |      11 ++++-------
       
       2 files changed, 10 insertions(+), 12 deletions(-)
       ---
 (DIR) diff --git a/obelisk/protocol.py b/obelisk/protocol.py
       t@@ -241,10 +241,11 @@ class ElectrumProtocol(asyncio.Protocol):  # pylint: disable=R0904,R0902
                        return JsonRPCError.internalerror()
                    return {"result": safe_hexlify(header)}
        
       -        # TODO: Help needed
       -        return JsonRPCError.invalidrequest()
       +        # The following works, but is extremely inefficient.
       +        # The best solution would be to figure something out in
       +        # libbitcoin-server
                cp_headers = []
       -        for i in range(index - 1, cp_height):
       +        for i in range(0, cp_height + 1):
                    _ec, data = await self.bx.fetch_block_header(i)
                    if _ec and _ec != 0:
                        self.log.debug("Got error: %s", repr(_ec))
       t@@ -252,11 +253,11 @@ class ElectrumProtocol(asyncio.Protocol):  # pylint: disable=R0904,R0902
                    cp_headers.append(data)
        
                hashed = [double_sha256(i) for i in cp_headers]
       -        branch, root = merkle_branch_and_root(hashed, 1, length=len(cp_headers))
       +        branch, root = merkle_branch_and_root(hashed, index)
                return {
                    "result": {
                        "branch": [hash_to_hex_str(i) for i in branch],
       -                "header": safe_hexlify(cp_headers[1]),
       +                "header": safe_hexlify(cp_headers[index]),
                        "root": hash_to_hex_str(root),
                    }
                }
 (DIR) diff --git a/tests/test_electrum_protocol.py b/tests/test_electrum_protocol.py
       t@@ -76,13 +76,10 @@ async def test_blockchain_block_header(protocol, writer):
            data = await protocol.blockchain_block_header(writer, {"params": params})
            assert data["result"] == expect["result"]
        
       -    # params = [123, 130]
       -    # expect = get_expect(method, params)
       -    # data = await protocol.blockchain_block_header(writer, {"params": params})
       -    # pprint(expect)
       -    # print()
       -    # pprint(data)
       -    # assert data["result"] == expect["result"]
       +    params = [13, 25]
       +    expect = get_expect(method, params)
       +    data = await protocol.blockchain_block_header(writer, {"params": params})
       +    assert data["result"] == expect["result"]
        
        
        async def test_blockchain_block_headers(protocol, writer):