# Contributing to Sculkworm

Sculkworm is a standalone Rust crate (`crates/sculkworm`) with unit tests (`src/`) and TCP integration tests (`tests/relay.rs`).

## Toolchain and prerequisites

- Rust toolchain pinned in [rust-toolchain.toml](https://github.com/YellowUmbrellaDev/sculk.network/blob/96e9a1b/crates/sculkworm/rust-toolchain.toml) (`1.100.0`, with `rustfmt` and `clippy`)
- `musl-tools` and the `x86_64-unknown-linux-musl` Rust target (only for static release builds outside Docker)
- Docker Engine (only for testing the container build)
- Node.js 22.18 or later (only for regenerating assets or running a live `minecraft-protocol` smoke check)

## Build from source

Run all Cargo commands from `crates/sculkworm`:

```sh
cargo build --locked
```

For an optimized release binary:

```sh
cargo build --release --locked
```

For a static Linux musl binary (matching CI and [Dockerfile](https://github.com/YellowUmbrellaDev/sculk.network/blob/96e9a1b/crates/sculkworm/Dockerfile)):

```sh
rustup target add x86_64-unknown-linux-musl
CC_x86_64_unknown_linux_musl=musl-gcc cargo build --release --locked --target x86_64-unknown-linux-musl
```

## Checks and tests

### Format, lint, and Rust tests

Run the same Rust checks CI executes:

```sh
cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test --locked
```

`cargo test --locked` runs the 21 unit tests in `src/` and the 8 integration tests in `tests/relay.rs`:

```text
test result: ok. 21 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
...
test result: ok. 8 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
```

### Live check with a real `minecraft-protocol` client

To verify the compiled release binary against a real `minecraft-protocol` client (version `1.21.4`) instead of synthetic TCP fixtures:

1. Build the release binary:

   ```sh
   cargo build --release --locked
   ```

2. Run a local status-ping test (for example with the two-backend Node script in [Getting started](https://docs.sculk.network/nightly/sculkworm/getting-started.md#2-start-two-local-minecraft-status-backends), or with `prototypes/sculkworm-relay` if present in your checkout) pointing `SCULKWORM_ROUTES_FILE` at a temporary `routes.json` and `SCULKWORM_LISTEN=127.0.0.1:25592`.
3. Confirm each hostname reaches its matching backend, `unknown` hostnames close with zero bytes, and stderr emits `event=listening`, `event=route`, `event=closed`, and `event=unknown_host`.

### Container smoke test

Build the Docker image and confirm it starts cleanly with a minimal routes file:

```sh
docker build -t sculkworm .
ROUTES_FILE="$(mktemp)"
printf '{"routes":{"smoke.example":"127.0.0.1:1"}}\n' > "$ROUTES_FILE"
chmod 644 "$ROUTES_FILE"
docker run -d --name sculkworm-smoke \
  -v "$ROUTES_FILE:/etc/sculkworm/routes.json:ro" \
  sculkworm
docker logs sculkworm-smoke
docker rm -f sculkworm-smoke
rm -f "$ROUTES_FILE"
```

## Code layout

| File | Responsibility |
| --- | --- |
| `src/lib.rs` | Public module exports and `RunningRelay` lifecycle handle (`start`, `shutdown`). |
| `src/main.rs` | CLI entrypoint, `SCULKWORM_*` startup wiring, `SIGHUP` route reload loop, and `SIGTERM`/`SIGINT` handling. |
| `src/config.rs` | `Config::from_env` / `from_lookup`, environment variable parsing, defaults, and bounds checks. |
| `src/domain.rs` | `Domain` normalization, `HostField` parsing (`\0` FML suffix, 26.4 query string, `_o` origin, `_id`), and percent-decoding. |
| `src/handshake.rs` | Streaming VarInt and Minecraft Handshake (`0x00`) parser, `MAX_HANDSHAKE_PACKET_LEN` (`4096`), legacy ping detection, and `HandshakeError`. |
| `src/routes.rs` | `Target` validation (`host:port` / `[ipv6]:port`), strict `routes.json` deserialization, and `RwLock`-backed `RouteTable`. |
| `src/relay.rs` | `Relay::serve` accept loop, `Semaphore` concurrency cap, handshake timeout, backend DNS/TCP connect, byte replay, bidirectional splice, and `sanitize_log_value`. |
| `tests/relay.rs` | End-to-end TCP integration tests (routing, 1-byte fragmentation, unknown host, timeouts, unreachable backend, live table swap, connection cap, handle drop). |

## Conventions

- **Exact-pinned dependencies**: every crate in [Cargo.toml](https://github.com/YellowUmbrellaDev/sculk.network/blob/96e9a1b/crates/sculkworm/Cargo.toml) uses an exact version constraint (`=X.Y.Z`) and [Cargo.lock](https://github.com/YellowUmbrellaDev/sculk.network/blob/96e9a1b/crates/sculkworm/Cargo.lock) is committed. Pass `--locked` to `cargo build` and `cargo test`.
- **No `.unwrap()` in non-test code**: return typed errors (`ConfigError`, `RoutesError`, `HandshakeError`) or handle poisoned locks explicitly (as in `RouteTable` with `unwrap_or_else(|e| e.into_inner())`). Keep `.unwrap()` inside `#[cfg(test)]` and `tests/` only.
- **Structured stderr logs**: emit single-line `level=<info|warn|error> event=<name> key=value` records to stderr. Pass every untrusted client string through `sanitize_log_value` (`src/relay.rs`) and format OS or parse errors with `{:?}`.
- **Documentation in sync**:
  - Environment variables live in `src/config.rs` and are documented in [docs/configuration.md](https://docs.sculk.network/nightly/sculkworm/configuration.md).
  - Route validation errors live in `src/routes.rs` and are documented in [docs/configuration.md](https://docs.sculk.network/nightly/sculkworm/configuration.md#routes-file-validation-errors).
  - Handshake framing and rejection reasons live in `src/handshake.rs` and `src/domain.rs` and are documented in [docs/protocol.md](https://docs.sculk.network/nightly/sculkworm/protocol.md).
  - Visual assets (`assets/icon-64.png`, `assets/logo-288.png`, `assets/logo.svg`, `assets/banner.png`, `assets/social.png`, `assets/mark.svg`) are generated deterministically by `assets/gen-logo.mjs` and `assets/gen-banner.mjs`.
  - Use relative links only so every page resolves on GitHub and passes `node ../../scripts/check-doc-links.mjs .` from the crate root (or `node scripts/check-doc-links.mjs crates/sculkworm` from the repository root). `pnpm lint` does not check this crate's links, so run one of these yourself. It does parse every Mermaid diagram under `crates/` (`scripts/check-mermaid.mjs`), so a diagram GitHub cannot render fails the build.

## Releases

Sculkworm is released from this monorepo with its own version and tags (`sculkworm-v0.3.0`), by the same `release` workflow as sculk.network. [The release guide](https://docs.sculk.network/nightly/sculk/releases.md) has the model and the steps. In short:

- Every night, if anything under `src/`, `Cargo.toml`, `Cargo.lock`, `rust-toolchain.toml` or `Dockerfile` changed and `ci-ok` passed on `main`, the workflow publishes `sculkworm-vX.Y.0-nightly.<date>.<run>` and the image tags `:X.Y.0-nightly.<date>.<run>` and `:nightly`.
- A maintainer promotes the latest nightly to a stable release with `gh workflow run release --ref main -f channel=stable -f product=sculkworm`. It publishes `:X.Y.0`, `:X.Y` and `:latest`.
- The version lives in the tag and the image's `org.opencontainers.image.version` label. Leave `Cargo.toml` at `0.1.0`, and do not keep a changelog: the GitHub Release notes list the commits that touched Sculkworm.

CI in `.github/workflows/ci.yml` runs `cargo fmt --check`, `cargo clippy --all-targets -- -D warnings`, `cargo test --locked`, a static `x86_64-unknown-linux-musl` build, and a Docker smoke run on every pull request touching `crates/sculkworm/`.
