Chia Blockchain
repository·main·Indexed 27 days ago
https://github.com/chia-network/chia-blockchainThe core implementation of the Chia Network, a cryptocurrency utilizing Proof of Space and Time. This repository provides the infrastructure for full nodes, farmers, timelords, and wallets. It includes the `chia` CLI for managing blockchain infrastructure, a WebSocket-based RPC interface for daemon communication via the `DaemonProxy` class, and implementations for CHIP-43 custody-based wallet puzzles.
What's inside chia-blockchain
- This directory contains implementations and drivers for CHIP-43, which defines the standard for custody-based wallet puzzles in the Chia network. These puzzles and drivers allow for advanced custody mechanisms within the Chia ecosystem.
Understand the Chia Blockchain Legacy Software and OS Support Policy
mainChia Blockchain ends software support when the original maintainer of a dependency (such as Python, Node.js, or an operating system) declares it End of Life (EOL) or ceases providing support.
To prevent breaking critical operating system components, Chia will also end support for specific operating system versions if those versions rely on a default Python version that has reached EOL. This ensures that users do not encounter issues when attempting to install new Python versions via the Chia installation process.
Handle expensive construction using `@classmethods`
mainKeep
__init__trivial by avoiding expensive operations (like disk I/O, network calls, or complex parsing) during instantiation. Instead, use@classmethodto provide alternative constructors for non-trivial creation logic. This keeps the primary constructor fast and predictable.import json from dataclasses import dataclass from typing import TypeVar, Type _T_Coin = TypeVar("_T_Coin", bound="Coin") @dataclass(frozen=True) class Coin: hash: bytes value: int @classmethod def from_json(cls: Type[_T_Coin], text: str) -> _T_Coin: decoded = json.loads(text) return cls( hash=bytes.fromhex(decoded["hash"]), value=decoded["value"], )Write tests using pytest
mainWhen contributing tests to the repository, follow these patterns:
- Do not import
test_*modules. Instead, locate shared tooling in non-test files within thetests/directory or its subdirectories. - Do not import fixtures. Use
conftest.pyfiles at the appropriate directory layer to make fixtures recursively available. - Do not use test classes. The project is fully dependent on
pytest, so avoidunittestcompatibility to maintain consistency.
- Do not import
Develop CLI tools with Click
mainWhen building Command Line Interfaces (CLI):
- Use the Click library.
- Use subcommands for separate activities.
- Avoid requiring users to write JSON manually.
- Short options should be a single character.
- Long options should use dashes (
--option-name) rather than underscores.
Use `Union` for multiple possible types
mainUse
Union[TypeA, TypeB]when a function or variable can accept or return multiple distinct types that do not share a common inheritance hierarchy.from os import PathLike from pathlib import Path from typing import Union def read_file(path: Union[str, PathLike]) -> str: return Path(path).read_text(encoding="utf-8")Handle Async tasks and cancellation
mainWhen working with asynchronous code:
- Do not catch
asyncio.CancelledErrorunless you are at a high-level boundary (like an RPC framework) and explicitly re-raise it. - Store references to all spawned tasks to ensure they can be cleaned up properly.
- Consider shielding cancellation during shutdown cleanup code.
- Do not catch
Best practices for Python type hinting
mainTo ensure rigorous static analysis with
mypy, follow these type hinting guidelines:- Avoid
Any: Do not useAnyas it defeats type checking. If the type is unknown or irrelevant, useobjectinstead. - Explicit
Nonereturns: If a function does not have areturnstatement or uses a barereturn, explicitly hint the return type as-> None:. - Prefer
objectoverAny: When you don't care about the specific type,objectis safer thanAnybecause it still requires type checks before performing operations. - Use
from __future__ import annotations: This is the recommended way to handle forward references (referencing a class within its own definition) without using string literals like-> "C".
- Avoid
Prerequisites for Chia Blockchain
mainTo run Chia blockchain components, you must have Python 3.10 or higher installed. You can verify your default Python version by running:
python3 --versionNetwork Configuration (NAT/Firewall)
If you are operating behind a NAT, peers outside your subnet may have difficulty reaching you. To resolve this:
- Enable UPnP on your router.
- Or, add a NAT/firewall rule to allow incoming traffic on TCP port 8444 (IPv4 only).
python3Relate types using `TypeVar`
mainUse
TypeVarto indicate a relationship between multiple elements, such as ensuring a function returns the same type it received as an input.from typing import TypeVar T = TypeVar("T") def double(original: T) -> T: return 2 * original an_int = double(original=2) a_list = double(original=["a", "b"])Define data-centric classes using `dataclasses`
mainUse the
dataclassesmodule to define classes. This promotes consistency, provides clear type hints, and ensures that all attributes are present from the moment of instantiation. For classes that should not be modified after creation, use@dataclass(frozen=True)to enforce immutability.from dataclasses import dataclass @dataclass(frozen=True) class Developer: name: str words_per_minute: float primary_language: strImplement Generics with `Generic`
mainUse
Genericto create container classes (like caches or wrappers) where the types of the contents are defined at instantiation time.from dataclasses import dataclass, field from typing import Generic, Optional, TypeVar, Dict KT = TypeVar("KT") VT = TypeVar("VT") @dataclass class Cache(Generic[KT, VT]): _mapping: Dict[KT, VT] = field(default_factory=dict) def get(self, key: KT, default: Optional[VT] = None) -> Optional[VT]: ... def set(self, key: KT, value: VT) -> None: ... c = Cache[int, str]() # error: Argument 1 to "get" of "Cache" has incompatible type "str"; expected "int" c.get("abc")