Beispiel #1
0
    async def block_header(self, writer, query):  # pylint: disable=W0613,R0911
        """Method: blockchain.block.header
        Return the block header at the given height.
        """
        if "params" not in query or len(query["params"]) < 1:
            return JsonRPCError.invalidparams()
        index = query["params"][0]
        cp_height = query["params"][1] if len(query["params"]) == 2 else 0

        if not is_non_negative_integer(index):
            return JsonRPCError.invalidparams()
        if not is_non_negative_integer(cp_height):
            return JsonRPCError.invalidparams()
        if cp_height != 0 and not index <= cp_height:
            return JsonRPCError.invalidparams()

        if cp_height == 0:
            _ec, header = await self.bx.fetch_block_header(index)
            if _ec and _ec != ZMQError.success:
                self.log.error("bx.fetch_block_header: %s", _ec.name)
                return JsonRPCError.internalerror()
            return {"result": safe_hexlify(header)}

        res = await self._merkle_proof_for_headers(cp_height, index)
        return {"result": res}
Beispiel #2
0
    async def blockchain_block_headers(self, writer, query):  # pylint: disable=W0613,R0911
        """Method: blockchain.block.headers
        Return a concatenated chunk of block headers from the main chain.
        """
        if "params" not in query or len(query["params"]) < 2:
            return JsonRPCError.invalidparams()
        # Electrum doesn't allow max_chunk_size to be less than 2016
        # gopher://bitreich.org/9/memecache/convenience-store.mkv
        max_chunk_size = 2016
        start_height = query["params"][0]
        count = query["params"][1]
        cp_height = query["params"][2] if len(query["params"]) == 3 else 0

        if not is_non_negative_integer(start_height):
            return JsonRPCError.invalidparams()
        if not is_non_negative_integer(count):
            return JsonRPCError.invalidparams()
        if cp_height != 0 and not start_height + (count - 1) <= cp_height:
            return JsonRPCError.invalidparams()

        count = min(count, max_chunk_size)
        headers = bytearray()
        for i in range(count):
            _ec, data = await self.bx.fetch_block_header(start_height + i)
            if _ec and _ec != 0:
                self.log.debug("Got error: %s", repr(_ec))
                return JsonRPCError.internalerror()
            headers.extend(data)

        resp = {
            "hex": safe_hexlify(headers),
            "count": len(headers) // 80,
            "max": max_chunk_size,
        }

        # The assumption is to fetch more headers if necessary.
        # TODO: Review
        if cp_height > 0 and cp_height - start_height > count:
            for i in range(cp_height - start_height):
                _ec, data = await self.bx.fetch_block_header(start_height +
                                                             count + i)
                if _ec and _ec != 0:
                    self.log.debug("Got error: %s", repr(_ec))
                    return JsonRPCError.internalerror()
                headers.extend(data)

        if cp_height > 0:
            # TODO: Review
            # TODO: Is index is 0 or last elem?
            hdr_lst = [headers[i:i + 80] for i in range(0, len(headers), 80)]
            branch, root = merkle_branch_and_root(hdr_lst, 0)
            resp["branch"] = [safe_hexlify(i) for i in branch]
            resp["root"] = safe_hexlify(root)

        return {"result": resp}
Beispiel #3
0
    async def blockchain_transaction_get_merkle(self, writer, query):  # pylint: disable=W0613
        """Method: blockchain.transaction.get_merkle
        Return the merkle branch to a confirmed transaction given its
        hash and height.
        """
        if "params" not in query or len(query["params"]) != 2:
            return JsonRPCError.invalidparams()

        tx_hash = query["params"][0]
        height = query["params"][1]

        if not is_hash256_str(tx_hash):
            return JsonRPCError.invalidparams()
        if not is_non_negative_integer(height):
            return JsonRPCError.invalidparams()

        _ec, hashes = await self.bx.fetch_block_transaction_hashes(height)
        if _ec and _ec != 0:
            self.log.debug("Got error: %s", repr(_ec))
            return JsonRPCError.internalerror()

        # Decouple from tuples
        hashes = [i[0] for i in hashes]
        tx_pos = hashes.index(unhexlify(tx_hash)[::-1])
        branch = merkle_branch(hashes, tx_pos)

        res = {
            "block_height": int(height),
            "pos": int(tx_pos),
            "merkle": branch,
        }
        return {"result": res}
Beispiel #4
0
    async def estimatefee(self, writer, query):  # pylint: disable=W0613,disable=R0911
        """Method: blockchain.estimatefee
        Return the estimated transaction fee per kilobyte for a transaction
        to be confirmed within a certain number of blocks.
        """
        # NOTE: This solution is using the mempool.space API.
        # Let's try to eventually solve it with some internal logic.
        if "params" not in query or len(query["params"]) != 1:
            return JsonRPCError.invalidparams()

        num_blocks = query["params"][0]
        if not is_non_negative_integer(num_blocks):
            return JsonRPCError.invalidparams()

        if self.chain == "testnet":
            return {"result": 0.00001}

        fee_dict = get_mempool_fee_estimates()
        if not fee_dict:
            return {"result": -1}

        # Good enough.
        if num_blocks < 3:
            return {"result": "{:.8f}".format(fee_dict["fastestFee"] / 100000)}

        if num_blocks < 6:
            return {
                "result": "{:.8f}".format(fee_dict["halfHourFee"] / 100000)
            }

        if num_blocks < 10:
            return {"result": "{:.8f}".format(fee_dict["hourFee"] / 100000)}

        return {"result": "{:.8f}".format(fee_dict["minimumFee"] / 100000)}
Beispiel #5
0
    async def block_headers(self, writer, query):  # pylint: disable=W0613,R0911
        """Method: blockchain.block.headers
        Return a concatenated chunk of block headers from the main chain.
        """
        if "params" not in query or len(query["params"]) < 2:
            return JsonRPCError.invalidparams()
        # Electrum doesn't allow max_chunk_size to be less than 2016
        # gopher://bitreich.org/9/memecache/convenience-store.mkv
        max_chunk_size = 2016
        start_height = query["params"][0]
        count = query["params"][1]
        cp_height = query["params"][2] if len(query["params"]) == 3 else 0

        if not is_non_negative_integer(start_height):
            return JsonRPCError.invalidparams()
        if not is_non_negative_integer(count):
            return JsonRPCError.invalidparams()
        # BUG: spec says <= cp_height
        if cp_height != 0 and not start_height + (count - 1) < cp_height:
            return JsonRPCError.invalidparams()

        count = min(count, max_chunk_size)
        headers = bytearray()
        for i in range(count):
            _ec, data = await self.bx.fetch_block_header(start_height + i)
            if _ec and _ec != ZMQError.success:
                self.log.error("bx.fetch_block_header: %s", _ec.name)
                return JsonRPCError.internalerror()
            headers.extend(data)

        resp = {
            "hex": safe_hexlify(headers),
            "count": len(headers) // 80,
            "max": max_chunk_size,
        }

        if cp_height > 0:
            data = await self._merkle_proof_for_headers(
                cp_height, start_height + (len(headers) // 80) - 1)
            resp["branch"] = data["branch"]
            resp["root"] = data["root"]

        return {"result": resp}
Beispiel #6
0
    async def blockchain_block_header(self, writer, query):  # pylint: disable=W0613,R0911
        """Method: blockchain.block.header
        Return the block header at the given height.
        """
        if "params" not in query or len(query["params"]) < 1:
            return JsonRPCError.invalidparams()
        index = query["params"][0]
        cp_height = query["params"][1] if len(query["params"]) == 2 else 0

        if not is_non_negative_integer(index):
            return JsonRPCError.invalidparams()
        if not is_non_negative_integer(cp_height):
            return JsonRPCError.invalidparams()
        if cp_height != 0 and not index <= cp_height:
            return JsonRPCError.invalidparams()

        if cp_height == 0:
            _ec, header = await self.bx.fetch_block_header(index)
            if _ec and _ec != 0:
                self.log.debug("Got error: %s", repr(_ec))
                return JsonRPCError.internalerror()
            return {"result": safe_hexlify(header)}

        cp_headers = []
        # headers up to and including cp_height
        for i in range(index, 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))
                return JsonRPCError.internalerror()
            cp_headers.append(data)

        # TODO: Review
        # TODO: Is index is 0 or last elem?
        branch, root = merkle_branch_and_root(cp_headers, 0)
        return {
            "result": {
                "branch": [safe_hexlify(i) for i in branch],
                "header": safe_hexlify(cp_headers[0]),
                "root": safe_hexlify(root),
            }
        }
Beispiel #7
0
    async def blockchain_transaction_id_from_pos(self, writer, query):  # pylint: disable=R0911,W0613
        """Method: blockchain.transaction.id_from_pos
        Return a transaction hash and optionally a merkle proof, given a
        block height and a position in the block.
        """
        if "params" not in query or len(query["params"]) < 2:
            return JsonRPCError.invalidparams()

        height = query["params"][0]
        tx_pos = query["params"][1]
        merkle = query["params"][2] if len(query["params"]) > 2 else False

        if not is_non_negative_integer(height):
            return JsonRPCError.invalidparams()
        if not is_non_negative_integer(tx_pos):
            return JsonRPCError.invalidparams()
        if not is_boolean(merkle):
            return JsonRPCError.invalidparams()

        _ec, hashes = await self.bx.fetch_block_transaction_hashes(height)
        if _ec and _ec != 0:
            self.log.debug("Got error: %s", repr(_ec))
            return JsonRPCError.internalerror()

        if len(hashes) - 1 < tx_pos:
            return JsonRPCError.internalerror()

        # Decouple from tuples
        hashes = [i[0] for i in hashes]
        txid = hash_to_hex_str(hashes[tx_pos])

        if not merkle:
            return {"result": txid}

        branch = merkle_branch(hashes, tx_pos)
        return {"result": {"tx_hash": txid, "merkle": branch}}