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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.git
.venv
.venv-node
.claude
**/node_modules
**/__pycache__
Expand Down
123 changes: 123 additions & 0 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
name: Node image

# The multi-arch node image deploy/node.yml and deploy/hub.yml run:
# ghcr.io/denimworld12/medchain-node:<tag> linux/amd64 + linux/arm64, IMAGE_VERSION=<tag> baked in
# Published when a release tag is pushed (git tag v1.0.0 && git push origin v1.0.0) or run by hand
# (Actions > Node image > Run workflow, with the tag). Pull requests that touch the image only build
# it, on both architectures, without publishing.
#
# Each architecture builds on its own native runner (ubuntu-24.04-arm for arm64): the image trains the
# risk model and installs torch at build time, which is very slow under QEMU emulation. Each job
# pushes its image by digest, then `merge` ties the two into one tag.

on:
push:
tags: ["v*"]
workflow_dispatch:
inputs:
version:
description: "the tag to publish, e.g. v1.0.0"
required: true
default: v1.0.0
pull_request:
paths:
- node/Dockerfile
- node/requirements.txt
- .github/workflows/image.yml

env:
IMAGE: ghcr.io/denimworld12/medchain-node
VERSION: ${{ github.event.inputs.version || (github.ref_type == 'tag' && github.ref_name) || format('pr-{0}', github.event.pull_request.number) }}
PUBLISH: ${{ github.event_name != 'pull_request' }}

permissions:
contents: read

jobs:
build:
name: build ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-24.04
- platform: linux/arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
timeout-minutes: 60
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: platform slug
run: echo "SLUG=${PLATFORM//\//-}" >> "$GITHUB_ENV"
env:
PLATFORM: ${{ matrix.platform }}
- uses: docker/setup-buildx-action@v3
- if: env.PUBLISH == 'true'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- id: build
uses: docker/build-push-action@v6
with:
context: .
file: node/Dockerfile
platforms: ${{ matrix.platform }}
build-args: IMAGE_VERSION=${{ env.VERSION }}
labels: |
org.opencontainers.image.version=${{ env.VERSION }}
org.opencontainers.image.revision=${{ github.sha }}
outputs: ${{ env.PUBLISH == 'true' && format('type=image,name={0},push-by-digest=true,name-canonical=true,push=true', env.IMAGE) || 'type=cacheonly' }}
cache-from: type=gha,scope=node-${{ env.SLUG }}
cache-to: type=gha,mode=max,scope=node-${{ env.SLUG }}
- if: env.PUBLISH == 'true'
name: export digest
run: |
mkdir -p /tmp/digests
touch "/tmp/digests/${DIGEST#sha256:}"
env:
DIGEST: ${{ steps.build.outputs.digest }}
- if: env.PUBLISH == 'true'
uses: actions/upload-artifact@v4
with:
name: digest-${{ env.SLUG }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

merge:
name: publish ${{ github.event.inputs.version || github.ref_name }}
if: github.event_name != 'pull_request'
needs: build
runs-on: ubuntu-24.04
permissions:
contents: read
packages: write
steps:
- uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digest-*
merge-multiple: true
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: tag both architectures as ${{ env.VERSION }}
working-directory: /tmp/digests
run: |
sources=()
for digest in *; do sources+=("$IMAGE@sha256:$digest"); done
docker buildx imagetools create -t "$IMAGE:$VERSION" "${sources[@]}"
- name: check it
run: |
docker buildx imagetools inspect "$IMAGE:$VERSION"
docker buildx imagetools inspect "$IMAGE:$VERSION" --format '{{json .Manifest.Manifests}}' \
| grep -q '"architecture":"arm64"'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ data/ecg-images/
kits/
# keys and certificates issued by scripts/pki.py (the compose stack writes them on first start)
.pki/
# make node-native builds its own venv
.venv-node/
45 changes: 44 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# MedChain shortcuts. `make up` is exactly the single-machine command from the README.
# `make hub`, `make node` and `make node-native` run the multi-laptop deployment (docs/MULTI_LAPTOP.md).

# what `make lint` compares against: the branch point with main (override: make lint BASE=<ref>)
BASE ?= $(shell git merge-base HEAD origin/main 2>/dev/null || git merge-base HEAD main)
Expand All @@ -8,11 +9,53 @@ CHANGED_PY = $(filter %.py,$(CHANGED))
CHANGED_JS = $(patsubst dashboard/%,%,$(filter dashboard/%.js dashboard/%.jsx,$(CHANGED)))
PYTHON ?= $(if $(wildcard .venv/bin/python),.venv/bin/python,python3)

.PHONY: up lint
# a hospital's kit folder from scripts/provision.py (its .env, pki/, data/, config/)
KIT ?=
KIT_DIR = $(abspath $(KIT))
# the hub's settings: provision.py writes them into the directory kit (template: deploy/hub.env.example)
HUB_ENV ?= kits/directory/hub.env
# make node-native: a Python 3.10+ to build its own venv from node/requirements.txt
NATIVE_PYTHON ?= $(shell command -v python3.12 || command -v python3.13 || command -v python3)
NATIVE_VENV ?= .venv-node

.PHONY: up hub node node-native check-kit lint

up:
docker compose up -d --build --wait

# the organiser's laptop: directory, observer, postgres (loopback only) and the dashboard
hub:
@test -f "$(HUB_ENV)" || { echo "no $(HUB_ENV): run scripts/provision.py first, or copy deploy/hub.env.example there and fill it in"; exit 2; }
docker compose --project-directory . --env-file "$(HUB_ENV)" -f deploy/hub.yml up -d --wait

# one hospital node in Docker, from its kit (the same as `docker compose --project-directory . -f deploy/node.yml up` inside the kit)
node: check-kit
docker compose --project-directory "$(KIT_DIR)" -f deploy/node.yml up

# the same node without Docker (when Docker's networking misbehaves): its own venv, the kit's .env,
# and the kit's folders in place of the container paths deploy/node.yml mounts
node-native: check-kit $(NATIVE_VENV)/.installed models/heart-risk.joblib
set -a && . "$(KIT_DIR)/.env" && set +a && \
export NODE_CONFIG="$(KIT_DIR)/config/node.yaml" DATA_PATH="$(KIT_DIR)/data" \
TLS_CERT="$(KIT_DIR)/pki/$$NODE_ID/tls.crt" TLS_KEY="$(KIT_DIR)/pki/$$NODE_ID/tls.key" TLS_CA="$(KIT_DIR)/pki/ca.crt" \
SIGNING_KEY="$(KIT_DIR)/pki/$$NODE_ID/signing.key" DIRECTORY_CREDENTIAL_FILE="$(KIT_DIR)/pki/$$NODE_ID/credential" \
DIRECTORY_PUBLIC_KEY="file:$(KIT_DIR)/pki/directory/signing.pub" DIRECTORY_TLS_PIN="file:$(KIT_DIR)/pki/directory/tls.crt" \
OBSERVER_TOKEN_FILE="$(KIT_DIR)/pki/observer/token" OBSERVER_TLS_PIN="file:$(KIT_DIR)/pki/observer/tls.crt" && \
exec $(NATIVE_VENV)/bin/python -m node

check-kit:
@test -n "$(KIT)" -a -f "$(KIT_DIR)/.env" || { echo "name a kit folder that has a .env, e.g. make $(MAKECMDGOALS) KIT=kits/hospital-b"; exit 2; }

$(NATIVE_VENV)/.installed: node/requirements.txt
@"$(NATIVE_PYTHON)" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 10) else "make node-native needs Python 3.10 or newer: pass NATIVE_PYTHON=/path/to/python3.12")'
"$(NATIVE_PYTHON)" -m venv $(NATIVE_VENV)
$(NATIVE_VENV)/bin/pip install -r node/requirements.txt
touch $@

# the risk-assessment model the image trains at build time (node/Dockerfile); the ECG model is committed
models/heart-risk.joblib: | $(NATIVE_VENV)/.installed
$(NATIVE_VENV)/bin/python -m node.assess.train_tabular --csv data/cardio/cardio_train.csv --out $@

# ruff and eslint on changed files only (no repo-wide pass, no reformatting)
lint:
@if [ -n "$(strip $(CHANGED_PY))" ]; then echo "ruff: $(words $(CHANGED_PY)) file(s)"; $(PYTHON) -m ruff check $(CHANGED_PY); else echo "ruff: no changed Python files"; fi
Expand Down
100 changes: 60 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,36 @@ behind home and hospital NAT without port forwarding and gives each machine a
stable name such as `hospital-b.tail1234.ts.net`. MedChain encrypts and
authenticates its own traffic (TLS plus signed updates), so the mesh is for
reachability, not for security. Do not expose node ports to the public internet.
A kit's `--bind-ip` publishes the port on one address only, for example the
machine's Tailscale IP.
The hub listens for the hospitals on `HUB_BIND_IP` (its `hub.env`), for example
the hub machine's Tailscale IP.

**Step by step for a friend's laptop:** [`docs/MULTI_LAPTOP.md`](docs/MULTI_LAPTOP.md)
is the guide for someone who has never used Docker: installing Docker Desktop and
Tailscale, the kit, the preflight, the hotspot fallback and a troubleshooting table.

### The deployment: one file per laptop

- `deploy/node.yml` runs one hospital node: a single `node` service from the
published image `ghcr.io/denimworld12/medchain-node:${IMAGE_VERSION}`, configured by
the `.env` next to it. It mounts `./data` at `DATA_PATH` (the slice, checkpoints,
saved membership, `audit.db`), `./pki` read-only and `./config` (the engine settings).
It publishes `NODE_PORT` on every interface. It has no `container_name` and relies on
no Compose DNS: every address comes from `.env` and the directory.
- `deploy/hub.yml` runs the organiser's laptop: the directory, the observer, Postgres
(on `127.0.0.1` only) and the dashboard.
- `deploy/node.env.example` and `deploy/hub.env.example` list their settings, each
with a Tailscale block and a hotspot block that differ only in addresses.
- `make node KIT=kits/hospital-b` and `make hub` run them from a repository checkout.
`make node-native KIT=...` runs the same node without Docker, in its own venv
(`.venv-node`, from `node/requirements.txt`), with the kit's `.env` and folders.
- `.github/workflows/image.yml` builds the image for `linux/amd64` and `linux/arm64`,
each on a native runner, and publishes it to GHCR when a `v*` tag is pushed, with
`IMAGE_VERSION` baked in (`/health` reports it).

### Provision one kit per hospital

`scripts/provision.py` follows NVIDIA FLARE's "startup kit" idea. It writes one
self-contained folder (and optionally a zip) per hospital, plus one for the
directory:
self-contained folder (and optionally a zip) per hospital, plus one for the hub:

```sh
python scripts/provision.py --zip \
Expand All @@ -574,33 +596,36 @@ and `kits/directory/`, plus a `.zip` of each. The network's CA and keys live in
`kits/.pki/` (or `--pki DIR`) and are reused on every later run, so the network
is never re-keyed. Every hospital kit holds:

- `config/hospital-X.yaml`: its own address, the directory's address, the
observer's address, and one freshly generated shared secret (the same for every kit from the same run; pass
it with `--secret` to provision a hospital later). There is no chain in it: the
directory places the hospital. Engine settings come from the repository's
`config/hospital-X.yaml`.
- `.env`: filled in. `NODE_ID`, `NODE_PORT`, its own address (`ADVERTISE_ADDR`), the
directory's and the observer's, and one freshly generated shared secret (the same
for every kit from the same run; pass it with `--secret` to provision a hospital
later). The hotspot block is there to switch to. There is no chain in it: the
directory places the hospital.
- `deploy/node.yml`, a copy of the repository's.
- `config/node.yaml`: the hospital's display name and engine settings, from the
repository's `config/hospital-X.yaml`. Nothing about where it runs.
- `pki/`: only this hospital's own TLS certificate and key (issued for its
address), its update-signing key and its directory credential, plus the CA
certificate, the directory's public key and the directory's certificate (whose
key the node pins), and the observer's certificate (also pinned) and ingest
token (`pki/observer/token`). No other party's private key, and no database
credentials.
- `docker-compose.hospital.yaml`, which runs only that node.
- `data/hospital-X.csv`, that hospital's slice. Pass `--data hospital-X=path.csv`
to use different data.
- `source/`, the node's build context. With `--image <ref>`, the kit uses a
prebuilt image instead.
- `README.md`, with the exact commands, including how to leave the chain.

The directory kit holds the directory's config, its TLS certificate, its
document-signing key and `hospitals.yaml` (who may register: ids and credential
hashes only), plus the observer's TLS certificate, key and token so the hub can
run the observer (its README has the command). Send each kit privately to its operator. Start the directory, then
each hospital, each with one command:
`--image repo[:tag]` names a different image (for example a private registry). By
default the kits use the published `ghcr.io/denimworld12/medchain-node:v1.0.0`.

The directory kit is the hub's. It holds the directory's config, its TLS certificate,
its document-signing key and `hospitals.yaml` (who may register: ids and credential
hashes only), the observer's TLS certificate, key and token, and a filled `hub.env`
for `deploy/hub.yml`. Send each hospital kit privately to its operator. Start the hub,
then each hospital, each with one command:

```sh
docker compose -f docker-compose.directory.yaml up -d --build --wait # on the directory machine
docker compose -f docker-compose.hospital.yaml up -d --build --wait # on each hospital machine
make hub # on the hub, from the repository
docker compose --project-directory . -f deploy/node.yml up -d # on each hospital machine, in its kit
```

Start training from any one node (`POST /round/start`, as above). The start
Expand All @@ -612,27 +637,28 @@ it and start it:
```sh
python scripts/provision.py --hospital hospital-e --secret <the network secret> \
--peers <the same list>,hospital-e=hospital-e.tail1234.ts.net:8005 --directory directory.tail1234.ts.net:8100
# copy kits/.pki/directory/hospitals.yaml to the directory machine's kits/directory/pki/directory/
# copy kits/.pki/directory/hospitals.yaml to the hub's kits/directory/pki/directory/
# (re-read on every registration, no restart); send kits/hospital-e/ to its operator, who runs:
docker compose -f docker-compose.hospital.yaml up -d --build --wait
docker compose --project-directory . -f deploy/node.yml up -d
```

The node registers; if a run is going, the directory appends it at the end of
the chain from a round boundary a few rounds ahead, and it joins the run.

**Removing a hospital for real.** On that hospital's machine:
**Removing a hospital for real.** On that hospital's machine, in its kit:

```sh
docker compose -f docker-compose.hospital.yaml exec hospital-b python -m node.leave
docker compose -f docker-compose.hospital.yaml down
docker compose --project-directory . -f deploy/node.yml exec node python -m node.leave
docker compose --project-directory . -f deploy/node.yml down
```

The directory takes it out at the next boundary and its neighbours bridge. To
stop it from ever registering again, delete its line from `hospitals.yaml`.

**Changing an address without re-provisioning.** `ADVERTISE_ADDR` (alias
`MEDCHAIN_ADVERTISE_URL`) replaces a node's own address and `DIRECTORY_URL`
(alias `MEDCHAIN_DIRECTORY_URL`) the directory's, from the environment. On restart the node re-registers its new address and the
**Changing an address without re-provisioning.** Edit `ADVERTISE_ADDR` (alias
`MEDCHAIN_ADVERTISE_URL`), a node's own address, or `DIRECTORY_URL` (alias
`MEDCHAIN_DIRECTORY_URL`), the directory's, in the kit's `.env` and run `up -d` again.
That is all the hotspot fallback does. On restart the node re-registers its new address and the
directory passes it to its neighbours. Node-to-node links pin the certificate's
key, not its address, so they keep working. Only the dashboard's relay checks the
address. For the relay, re-run `provision.py` for that hospital with the new
Expand All @@ -641,17 +667,9 @@ in its YAML, no `directory:`, as the tests use) still takes
`MEDCHAIN_PEER_<ID>_URL` for each neighbour: `<ID>` is the node id upper-cased,
with `-` and other characters turned into `_`.

**The observer is optional.** The dashboard and its Postgres can run on any one
machine:

```sh
MEDCHAIN_SHARED_SECRET=<kit secret> NODE_EXTRA_CA_CERTS=kits/.pki/ca.crt docker compose up -d postgres dashboard
```

Kit nodes push their telemetry to the observer (`--observer`, by default the
directory's host on port 8200), which the hub runs next to Postgres from the
directory kit; `--observer-db` instead points the nodes straight at a Postgres
URL, as a fallback. It never coordinates training. A node with no observer still
**The observer only observes.** Kit nodes push their telemetry to the observer
(`--observer`, by default the directory's host on port 8200), which `deploy/hub.yml`
runs next to Postgres. It never coordinates training. A node with no observer still
trains and still serves `/status` and `/metrics`.

### Prove it
Expand Down Expand Up @@ -896,13 +914,15 @@ docs/screenshots/ models/ scripts/ Makefile
- `make up` runs the single-machine stack (`docker compose up -d --build --wait`).
`make lint` runs ruff (`ruff.toml`) and eslint (`dashboard/eslint.config.js`)
on the files changed since the branch point with main only. CI does the same
for each change. There is no repo-wide reformatting.
for each change. There is no repo-wide reformatting. `make hub`, `make node` and
`make node-native` run the multi-laptop deployment in `deploy/`
([`docs/MULTI_LAPTOP.md`](docs/MULTI_LAPTOP.md)).
- `observer/` is the telemetry ingest service (`python -m observer`, same image).

- `directory/` is the membership directory (`python -m directory`, same image as
the nodes). `scripts/pki.py` issues the CA, certificates, signing keys and
directory credentials (the compose `pki` service runs it).
- `scripts/provision.py` writes the per-hospital and directory kits,
- `scripts/provision.py` writes the per-hospital and hub kits for `deploy/`,
`scripts/preflight.py` reports each neighbour's path (direct or DERP-relayed), and
`scripts/verify_multihost.py` checks a run across real addresses (see
[Running on separate machines](#running-on-separate-machines)).
Expand Down
25 changes: 25 additions & 0 deletions deploy/hub.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# MedChain hub settings, read by deploy/hub.yml (`make hub`). scripts/provision.py writes a
# filled copy to <kits>/directory/hub.env; docs/MULTI_LAPTOP.md walks through it.

# The directory kit from scripts/provision.py (its config/ and pki/): ./ and relative to the repo
# root, or an absolute path
HUB_KIT=./kits/directory
# The network's shared secret, printed by provision.py (the dashboard's Start / Pause relay sends it)
MEDCHAIN_SHARED_SECRET=change-me-the-provisioned-secret
IMAGE_VERSION=v1.0.0
# The ports in the directory's and the observer's addresses (provision.py --directory / --observer)
DIRECTORY_PORT=8100
OBSERVER_PORT=8200
# The dashboard, on this laptop only: http://localhost:8080
DASHBOARD_BIND_IP=127.0.0.1
DASHBOARD_PORT=8080

# ======== Tailscale (the normal way) ========
# Listen for the hospitals on this laptop's Tailscale IP only (`tailscale ip -4`).
# 0.0.0.0 (every interface) also works.
HUB_BIND_IP=100.101.1.99

# ======== Phone hotspot (fallback) ========
# To switch: put a # in front of the Tailscale line above and remove it below. The hotspot
# hands out addresses that can change, so listen on every interface.
#HUB_BIND_IP=0.0.0.0
Loading
Loading