async def stream_header_updates(self): """ Stream header updates as they happen. Note: You need to have ``headerStream: true`` in the node config for this to work. >>> async for header in Kadena('host:443').stream_header_updates(): >>> print(header) """ await self._init() async with httpx.AsyncClient(base_url=self.base_url) as client: resp = await client.get(f"{self.prefix}/header/updates", timeout=None, stream=True) resp.raise_for_status() async for message in resp.stream(): print(message) pairs = [ line.split(":", maxsplit=1) for line in message.decode().splitlines() ] event = {pair[0]: pair[1] for pair in pairs if len(pair) == 2} if event["event"] == "BlockHeader": data = json.loads(event["data"]) yield decode_header(data["header"])
async def stream_headers_ascending(self, chain: int, page=None): """ Stream all headers of a ``chain`` starting from ``page``. >>> page = "excluding:sv_e1g4XA35IZic34XIn5DB14V5RvOddY8--q_zRTkw" >>> async for header in Kadena('host:443').stream_headers_ascending(4, page): >>> print(header) """ async for data in self._request_pages(f"/chain/{chain}/header", page=page): for header in data["items"]: yield decode_header(header)
async def get_recent_headers(self, depth=6): """ Get the most recent headers for each chain. """ cut = await self.cut() heights = { chain: block["height"] for chain, block in cut["hashes"].items() } tasks = [ self._request(f"/chain/{chain}/header", params={ "minheight": height - depth, "maxheight": height }) for chain, height in heights.items() ] responses = await asyncio.gather(*tasks) headers = [[ decode_header(header) for header in chain_headers.json()["items"] ] for chain_headers in responses] return dict(zip(heights, headers))
async def get_work(self, mining_request: dict) -> WorkHeader: resp = await self._request("/mining/work", encoding="binary", json=mining_request) return decode_header(resp.content)
def test_decode_block_header_object(): assert types.decode_header(block_header_object) == block_header
def test_decode_block_header_bytes(): assert types.decode_header(block_header_bytes) == block_header_int_version
def test_decode_block_header_base64(): assert types.decode_header(block_header_base64) == block_header_int_version
def test_decode_work_header(): assert types.decode_header(work_header_bytes) == decoded_work_header