Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ btclib calls the bindings only through ecc.

Roughly bottom-up. The directions that hold without exception are the
substrate, `btclib.ecc` without `btclib.ecc.bms`, the pairs in the next
section and the edge to `btclib_wallet` below, each held by a test;
elsewhere a module reaches up where its subject asks for it, as
`btclib.ecc.bms` does for the address a message signature names.
section and the edges to `bip32_descriptors` and `btclib_wallet` below,
each held by a test; elsewhere a module reaches up where its subject
asks for it, as `btclib.ecc.bms` does for the address a message
signature names.

- **the substrate**: `alias`, `exceptions`, `utils`
- **the curve and what is built on it**: ecc's `curves`,
Expand Down Expand Up @@ -134,7 +135,8 @@ sends nothing itself.

- **BIP32 key derivation and output descriptors** are the
`bip32-descriptors` distribution, imported as `bip32_descriptors`. It
imports btclib.
imports btclib, and nothing in btclib imports it:
`tests/imports_test.py` holds that edge to one direction.
- **The rest of the wallet side** (mnemonics, PSBTs, signers, chain
backends and the Electrum protocol's codec) is the `btclib-wallet`
distribution, imported as `btclib_wallet`. It imports btclib, and
Expand Down
3 changes: 2 additions & 1 deletion ASSURANCE_CASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ describes beside them.
- **Layering.** The curve arithmetic does not import the schemes built
on it, which ecc's `tests/imports_test.py` holds. The codecs do
not import the bitcoin semantics on top of them, and nothing in btclib
imports `btclib_wallet`: btclib's `tests/imports_test.py` holds both.
imports `bip32_descriptors` or `btclib_wallet`: btclib's
`tests/imports_test.py` holds both.

**Constant time where it is claimed.** It is claimed only of
libsecp256k1: CONTRIBUTING.md's `_var` section sets out the tiers of
Expand Down
56 changes: 32 additions & 24 deletions tests/imports_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,10 @@ def test_hashes_stays_stdlib_light() -> None:

# Row 5 of issue #2129, `btclib-wallet`, in that table's order, with
# `mnemonic/`, which went with `bip85`: the units that left this package
# for `btclib_wallet`. That package imports btclib rather than being
# imported by it, so the check on it is not what it reaches but that
# nothing here reaches back -- under its old name or its new one.
# for `btclib_wallet`. `bip32_descriptors` publishes `bip32/` and
# `descriptors/`. Those packages import btclib rather than being
# imported by it, so the check on them is not what they reach but that
# nothing here reaches back -- under the old names or the new ones.
_APPLICATION_SLAB = (
"bip32",
"bip44",
Expand Down Expand Up @@ -431,6 +432,10 @@ def test_hashes_stays_stdlib_light() -> None:
# the import package row 5 is published as
_WALLET_PACKAGE = "btclib_wallet"

# the import package that publishes `bip32/` and `descriptors/`
# (btclib-org/.github#1811)
_DESCRIPTORS_PACKAGE = "bip32_descriptors"


def _is_btclib(name: str) -> bool:
"""Answer whether a dotted name is this package's, the bindings excluded.
Expand All @@ -443,17 +448,17 @@ def _is_btclib(name: str) -> bool:
return name == "btclib" or name.startswith("btclib.")


def _is_btclib_or_wallet(name: str) -> bool:
"""Answer whether a dotted name is this package's or `btclib_wallet`'s."""
def _is_btclib_or_slab(name: str) -> bool:
"""Answer whether a dotted name is this package's or the slab's."""
return (
_is_btclib(name)
or name == _WALLET_PACKAGE
or name.startswith(f"{_WALLET_PACKAGE}.")
or name in (_WALLET_PACKAGE, _DESCRIPTORS_PACKAGE)
or name.startswith((f"{_WALLET_PACKAGE}.", f"{_DESCRIPTORS_PACKAGE}."))
)


def _btclib_names_imported_by(path: Path) -> set[str]:
"""Return every dotted btclib or btclib_wallet name one file's imports name.
"""Return every dotted btclib or slab name one file's imports name.

`from btclib import wallet` and `from btclib.wallet import Wallet` name
the same package two different ways; joining the `from` module with
Expand All @@ -465,11 +470,9 @@ def _btclib_names_imported_by(path: Path) -> set[str]:
for node in ast.walk(tree):
if isinstance(node, ast.Import):
names.update(
alias.name for alias in node.names if _is_btclib_or_wallet(alias.name)
alias.name for alias in node.names if _is_btclib_or_slab(alias.name)
)
elif isinstance(node, ast.ImportFrom) and _is_btclib_or_wallet(
node.module or ""
):
elif isinstance(node, ast.ImportFrom) and _is_btclib_or_slab(node.module or ""):
module = node.module or ""
names.add(module)
names.update(f"{module}.{alias.name}" for alias in node.names)
Expand Down Expand Up @@ -511,24 +514,28 @@ def test_the_application_slab_check_finds_a_planted_edge() -> None:
assert edges == {"outside.py": ["btclib.wallet.wallet"]}


def test_the_application_slab_scan_reads_both_names(tmp_path: Path) -> None:
"""`_btclib_names_imported_by` collects the slab under either name.
def test_the_application_slab_scan_reads_every_name(tmp_path: Path) -> None:
"""`_btclib_names_imported_by` collects the slab under any of its names.

The tree has no import of `btclib_wallet`, so without a planted file
nothing would show that the scan below reads one at all: a collector
keyed on `btclib` alone would answer the same empty set for a module
importing the package above it.
The tree has no import of `btclib_wallet` or `bip32_descriptors`, so
without a planted file nothing would show that the scan below reads
one at all: a collector keyed on `btclib` alone would answer the same
empty set for a module importing a package above it.
"""
planted = tmp_path / "planted.py"
planted.write_text(
"import btclib_wallet.bip32\nfrom btclib_wallet import psbt\n"
"import bip32_descriptors.bip32\nfrom bip32_descriptors import descriptors\n"
"from btclib_secp256k1 import dsa\n",
encoding="utf-8",
)
assert _btclib_names_imported_by(planted) == {
"btclib_wallet.bip32",
"btclib_wallet",
"btclib_wallet.psbt",
"bip32_descriptors.bip32",
"bip32_descriptors",
"bip32_descriptors.descriptors",
}


Expand All @@ -543,21 +550,22 @@ def test_the_application_slab_has_no_inbound_edge() -> None:
installed package the same way `module_names` does, from
`btclib.__path__`, so this checks the tree the suite runs against
rather than a copy on disk that might be stale. Every file is scanned,
for both spellings of the slab: `btclib.<unit>`, which no longer
resolves, and `btclib_wallet`, which depends on this package and so
cannot be depended on by it.
for every spelling of the slab: `btclib.<unit>`, which no longer
resolves, and `btclib_wallet` and `bip32_descriptors`, which depend on
this package and so cannot be depended on by it.

The slab's units are asserted absent from the tree first. A unit that
came back, as a copy or as a module re-exporting `btclib_wallet`'s, is
what rule 3 refuses, and a scan that walked it would be reading the
slab from inside rather than the protocol side.
came back, as a copy or as a module re-exporting `btclib_wallet`'s or
`bip32_descriptors`'s, is what rule 3 refuses, and a scan that walked
it would be reading the slab from inside rather than the protocol side.
"""
root = Path(btclib.__path__[0])
for name in _APPLICATION_SLAB:
assert not (root / f"{name}.py").exists(), name
assert not (root / name).exists(), name
slab_roots = (
_WALLET_PACKAGE,
_DESCRIPTORS_PACKAGE,
*(f"btclib.{name}" for name in _APPLICATION_SLAB),
)
imports_by_file = {
Expand Down
Loading