Repository navigation
p2p_core%feat!: type the BIP37 and BIP157 messages #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #!/usr/bin/env python3 | ||
| # coding: latin-1 | ||
|
|
||
| # | ||
| # Copyright (c) 2026-present, The Dash Core developers | ||
| # SPDX-License-Identifier: MIT | ||
| # See the accompanying file LICENSE or https://opensource.org/license/MIT | ||
| # | ||
|
|
||
| """Print the literals of the BIP157 programs.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| from pathlib import Path | ||
| from typing import TypeVar | ||
|
|
||
| from util.rpc import Node, RpcError | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
| # The literals the programs take, by mainnet height. A block hash comes from | ||
| # getblockhash, a filter and a filter header from getblockfilter. | ||
| LITERALS = { | ||
| 999: ("HEADER",), | ||
| 1000: ("BLOCK_HASH", "FILTER", "HEADER"), | ||
| 1001: ("BLOCK_HASH", "FILTER"), | ||
| 1002: ("BLOCK_HASH", "FILTER"), | ||
| 2_000_000: ("BLOCK_HASH", "FILTER"), | ||
| } | ||
| def field(obj: object, key: str, kind: type[T]) -> T: | ||
| value = obj.get(key) if isinstance(obj, dict) else None | ||
| if not isinstance(value, kind) or (isinstance(value, bool) and kind is int): | ||
| raise RpcError(f"{key!r} is not a {kind.__name__} in {obj!r:.300}") | ||
| return value | ||
|
|
||
|
|
||
| def render(node: Node) -> str: | ||
| """Return the literals of the programs, read from *node*.""" | ||
| lines = [] | ||
| for height, kinds in LITERALS.items(): | ||
| block_hash = node.call("getblockhash", height) | ||
| if not isinstance(block_hash, str): | ||
| raise RpcError(f"getblockhash {height} did not return a hash") | ||
| result = node.call("getblockfilter", block_hash) | ||
|
Comment on lines
+38
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🟡 Suggestion: Reject non-mainnet nodes before generating mainnet compact-filter literals
source:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving this as is. Tools in 🤖 Posted autonomously by Claude on behalf of pasta. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That rationale is fair. Given the deliberately lightweight checks for Withdrawing this finding. |
||
| for kind in kinds: | ||
| if kind == "BLOCK_HASH": | ||
| source, value = f"from getblockhash {height}", block_hash | ||
| else: | ||
| key = kind.lower() | ||
| source = f"{key} from getblockfilter {block_hash}" | ||
| value = field(result, key, str) | ||
| lines.extend([ | ||
| f" // Mainnet height {height:,}, {source}.", | ||
| f' constexpr char {kind}_{height}[] = "{value}";', | ||
| ]) | ||
| return "\n".join(lines) + "\n" | ||
|
|
||
|
|
||
| def parse_args() -> argparse.Namespace: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "--node", nargs=2, required=True, metavar=("HOST:PORT", "COOKIE") | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main() -> None: | ||
| args = parse_args() | ||
| print(render(Node(args.node[0], Path(args.node[1]).expanduser())), end="") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #!/usr/bin/env python3 | ||
| # coding: latin-1 | ||
|
|
||
| # | ||
| # Copyright (c) 2026-present, The Dash Core developers | ||
| # SPDX-License-Identifier: MIT | ||
| # See the accompanying file LICENSE or https://opensource.org/license/MIT | ||
| # | ||
|
|
||
| """Print the literals of the `merkleblock_payloads` program.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| from pathlib import Path | ||
|
|
||
| from util.rpc import Node, RpcError | ||
|
|
||
| # Mainnet block 2,000,000, and for each proof the transactions it matches. | ||
| BLOCK_HEIGHT = 2_000_000 | ||
| BLOCK_HASH = "0000000000000009bd68b5e00976c3f7482d4cc12b6596614fbba5678ef13a59" | ||
| FIRST_TXID = "8954c8546a4b19860afaaac95f14fc5909e9593b729b6d8228918d5deacfc222" | ||
| LAST_TXID = "ac745767380eac6ba7065a34b460fae44b56d5f2c4f96afba9cdb0ef6d14501e" | ||
| MATCHES = { | ||
| "last": ((LAST_TXID,), "its last transaction"), | ||
| "two": ((FIRST_TXID, LAST_TXID), "transactions at both ends"), | ||
| } | ||
|
|
||
|
|
||
| def render(node: Node) -> str: | ||
| """Return the literals of the program, read from *node*.""" | ||
| block_hash = node.call("getblockhash", BLOCK_HEIGHT) | ||
| if block_hash != BLOCK_HASH: | ||
| raise RpcError(f"unexpected block hash {block_hash!r}") | ||
| lines = [] | ||
| for label, (txids, matched) in MATCHES.items(): | ||
| proof = node.call("gettxoutproof", list(txids), BLOCK_HASH) | ||
| if not isinstance(proof, str): | ||
| raise RpcError(f"gettxoutproof {label} did not return hex") | ||
| lines.extend([ | ||
| f" // Mainnet height {BLOCK_HEIGHT:,}, block hash {BLOCK_HASH}, from" | ||
| f" gettxoutproof for {matched}.", | ||
| f" constexpr char PROOF_{label.upper()}[] =", | ||
| f' "{proof}";', | ||
| ]) | ||
| return "\n".join(lines) + "\n" | ||
|
|
||
|
|
||
| def parse_args() -> argparse.Namespace: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "--node", nargs=2, required=True, metavar=("HOST:PORT", "COOKIE") | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main() -> None: | ||
| args = parse_args() | ||
| print(render(Node(args.node[0], Path(args.node[1]).expanduser())), end="") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # coding: latin-1 | ||
|
|
||
| # | ||
| # Copyright (c) 2026-present, The Dash Core developers | ||
| # SPDX-License-Identifier: MIT | ||
| # See the accompanying file LICENSE or https://opensource.org/license/MIT | ||
| # | ||
|
|
||
| """A JSON-RPC client of one node, for the scripts that read chain data.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| import http.client | ||
| import ipaddress | ||
| import json | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
| RPC_TIMEOUT = 600 | ||
|
|
||
|
|
||
| class RpcError(Exception): | ||
| """A failed call, or a reply without the expected members.""" | ||
|
|
||
|
|
||
| def is_loopback(host: str) -> bool: | ||
| """Whether *host* is `localhost` or a loopback address.""" | ||
| if host == "localhost": | ||
| return True | ||
| if host.startswith("[") and host.endswith("]"): | ||
| host = host[1:-1] | ||
| try: | ||
| return ipaddress.ip_address(host).is_loopback | ||
| except ValueError: | ||
| return False | ||
|
|
||
|
|
||
| class Node: | ||
| """A JSON-RPC client of one node, authenticated with its cookie.""" | ||
|
|
||
| def __init__(self, address: str, cookie: Path) -> None: | ||
| host, _, port = address.rpartition(":") | ||
| if not host or not port.isdigit(): | ||
| raise RpcError(f"{address!r} is not a host:port address") | ||
| if not is_loopback(host): | ||
| raise RpcError(f"{host} is not a loopback address") | ||
| self.host = host | ||
| self.port = int(port) | ||
| token = base64.b64encode(cookie.read_bytes().strip()).decode() | ||
| self.headers = { | ||
| "Authorization": f"Basic {token}", | ||
| "Content-Type": "application/json", | ||
| } | ||
|
|
||
| def call(self, method: str, *params: object) -> object: | ||
| """Return the result of RPC *method* with *params*.""" | ||
| body = json.dumps({"id": 0, "method": method, "params": list(params)}) | ||
| conn = http.client.HTTPConnection(self.host, self.port, timeout=RPC_TIMEOUT) | ||
| try: | ||
| conn.request("POST", "/", body, self.headers) | ||
| response = conn.getresponse() | ||
| data = response.read() | ||
| except http.client.HTTPException as err: | ||
| raise RpcError(f"{method}: {err!r}") from err | ||
| finally: | ||
| conn.close() | ||
| try: | ||
| reply = json.loads(data) | ||
| except ValueError: | ||
| reply = None | ||
| error = reply.get("error") if isinstance(reply, dict) else None | ||
| if error is not None: | ||
| raise RpcError(f"{method}: {error!r:.300}") | ||
| if response.status != http.client.OK or not isinstance(reply, dict): | ||
| raise RpcError(f"{method}: HTTP {response.status}, {data[:300]!r}") | ||
| return reply.get("result") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reject non-mainnet nodes before generating mainnet literals.
If
--nodeselects a testnet node with all configured heights indexed, every string check passes.getblockhashreads that node's active chain, so the script prints testnet values underMainnetcomments. (raw.githubusercontent.com) Check the network or a known mainnet block hash before collecting literals, asmerkleblock.pyalready does.