Introducing a new way to review your PRs.

Read the announcement

jamaljsr / polar

One-click Bitcoin Lightning networks for local app development & testing

View on GitHub

19 documents · Updated Oct 19, 2026

docs / network-lifecycle

Network lifecycle management

Polar manages a local Lightning network as one saved workspace. A workspace joins Bitcoin nodes, Lightning nodes, and optional Tap nodes with their data, links, ports, and Docker startup plan. The lifecycle keeps this saved model in step with the running containers and the network designer view.

Lifecycle model

Network workspace

The network workspace is the unit that users create, open, run, stop, share, and delete. It has an identity and description, a stopped or running state, node groups, a data path, mining settings, and an optional payment simulation. Its chart is stored with the workspace so that the saved topology returns with the node data. References: src/…/index.ts• L23-37, src/…/index.ts• L285-297

The workspace has three related node groups:

GroupRole in the local network
BitcoinProvides the local chain and can connect to peer Bitcoin nodes.
LightningProvides the Lightning nodes that use a Bitcoin node as their backend.
TapProvides Tap services that attach to a Lightning backend.

These relationships matter during both configuration and startup. The generated Docker plan uses the selected Bitcoin backend for each Lightning node and the selected Lightning backend for each Tap node. References: src/…/types.ts• L23-28, src/…/types.ts• L68-100, src/…/dockerService.ts• L134-175

The main lifecycle is shown below. Each change passes through the saved network model and then updates the Docker plan when containers are affected.

flowchart LR
    A[Create or import workspace] --> B[Configure nodes and links]
    B --> C[Save network model and Docker plan]
    C --> D[Start containers]
    D --> E[Wait for services and connect peers]
    E --> F[Use local network]
    F --> G[Stop containers]
    G --> H{Keep or share workspace?}
    H -->|Keep| B
    H -->|Export| I[Create archive]
    H -->|Delete| J[Remove data and saved state]
    I --> K[Import on another workspace]
    K --> B

Workspace creation and configuration

Network creation

The new-network flow collects a name, an optional description, and counts for managed or custom node images. It requires at least one Bitcoin node and prevents more Tap nodes than LND nodes because each Tap node needs a Lightning backend. References: src/…/NewNetwork.tsx• L39-61, src/…/NewNetwork.tsx• L75-184

The network model turns those choices into a stopped workspace. It assigns the next available network ID, creates node records, selects image versions and commands, assigns backend links, gives nodes initial ports, and creates the first designer chart. It then saves both the Docker Compose plan and the application network record before opening the new workspace. References: src/…/network.ts• L340-395, src/…/network.ts• L445-497, src/…/network.ts• L499-628

Persistent configuration

Configuration has two parts. Network data records the nodes and their relationships. Application settings provide defaults for node counts, host ports, managed image commands, and custom images. A change to a node command, backend link, node name, or node membership must update the saved network and regenerate the Docker plan so the next start uses the new state. References: src/…/index.ts• L72-84, src/…/network.ts• L495-504, src/…/network.ts• L670-739

Port settings are starting points rather than permanent reservations. Before a stopped node starts, Polar checks the host for conflicts and records replacement ports when needed. Started nodes are excluded from this check because their own ports are already in use. References: src/…/NetworkSetting.tsx• L47-76, src/…/network.ts• L743-769, src/…/network.ts• L784-988

For the detailed node add, rename, version, backend, and removal rules, see Node orchestration.

Startup and shutdown

Network startup

Starting a workspace changes its state to Starting, checks that required non-Polar images exist, repairs conflicting host ports, saves any port changes, and starts the complete Docker Compose project. A successful container start changes the workspace to Started, but individual node status still depends on service readiness. References: src/…/network.ts• L772-819, src/…/dockerService.ts• L183-205

Readiness checks connect the running containers to the rest of the application. Bitcoin services must come online before Polar connects their configured peers, creates a default wallet, and refreshes chain data. Lightning services must come online before Polar connects the Lightning graph and installs event listeners. Tap services receive their own readiness check. Polar also mines one block after the Bitcoin group is ready so dependent Lightning services can finish startup. References: src/…/network.ts• L864-954

The workspace can also start or stop one node. This path checks ports, updates the Compose plan if ports change, starts or stops the selected service, waits for that service when starting, and saves the result. A full network start remains the safer path when several node relationships must become ready together. References: src/…/network.ts• L834-863

Network shutdown

Stopping a workspace turns off automatic mining, removes an active simulation, brings down the Docker Compose project, marks the workspace stopped, removes Lightning event listeners, and clears cached RPC data. If shutdown fails, the workspace enters the error state so the user can see that it is not cleanly stopped. References: src/…/network.ts• L820-840

When the application stops all workspaces, it waits until running and stopping workspaces are gone before it saves state and signals the Docker shutdown path. This prevents application shutdown from racing with container cleanup. References: src/…/network.ts• L841-866

Export and import

Network archive

Export is a stopped-workspace operation. Polar asks for a destination, includes the network model and active chart in export.json, and archives the workspace directory with its Compose plan and node volume data. A running or starting workspace cannot be exported because its data may change during the archive. References: src/…/network.ts• L1089-1127, src/…/network.ts• L1111-1134

Imported workspace

Import extracts the archive to a temporary location, validates the saved network and chart, applies current data migrations, assigns a new local network ID and path, rebuilds node data paths, checks platform support, and copies the result into the local network store. Polar then registers the network and chart, saves the shared network record, and regenerates its Compose plan. References: src/…/ImportNetwork.tsx• L35-66, src/…/network.ts• L1129-1153, src/…/network.ts• L1019-1107

For the archive contents, validation, migration, and platform-specific transfer rules, see Network import and export.

Deletion and persistence

Network deletion

Deleting a workspace first stops it when its workspace, Lightning node, or Bitcoin node state is not stopped. It then removes special Docker volumes, deletes the workspace directory, removes the network and chart from application state, clears node-specific service state, saves the remaining workspaces, and clears cached RPC data. Deletion therefore removes both the running resources and the saved record. References: src/…/network.ts• L1058-1088

Node-level deletion has extra relationship checks. Polar does not remove a Lightning backend that still serves a Tap node or a node used by a simulation. Bitcoin removal also needs to preserve a valid chain and backend assignment. These rules belong to Node orchestration, but they protect the network lifecycle because an invalid workspace cannot start reliably. References: src/…/network.ts• L512-566, src/…/network.ts• L568-668

Application reload

On application initialization, Polar loads settings and image information, reads the saved network file, restores the charts, and starts the saved auto-mining timers for each workspace. The network file is versioned and migrated when its version differs from the application version. A missing or unreadable file produces an empty network list instead of blocking the application from opening. References: src/…/app.ts• L128-138, src/…/network.ts• L279-327, src/…/dockerService.ts• L292-335, src/…/migrations.ts• L190-215

Change checklist

When these files or sections are changed, remember to consider these:

  • A change to node names or backend links must keep the network model, node data paths, chart links, and generated Compose references aligned.
  • A change to base ports or node port fields must preserve the conflict check and the saved port values used by later starts.
  • A change to the Network or NetworksFile shape must include load-time migration and imported archive handling.
  • A change to export contents must have a matching import path update so archives remain portable across local network IDs and data paths.
  • A change to startup readiness must preserve the ordering between Bitcoin readiness, chain preparation, Lightning peer setup, and event listeners.
  • A change to deletion must cover both host data and platform-specific Docker volumes.

Important failure modes

TriggerConsequenceGuard
A required custom Docker image is not presentNetwork startup stops before containers start.Check the image list before Compose startup and show the missing image names.
A host port is already in useA node cannot bind its service endpoint.Find open ports for stopped nodes and save the replacements before starting.
No compatible Bitcoin backend exists for a Lightning or Tap versionNetwork creation or node addition fails.Filter backend choices by version compatibility and reject an empty set.
A service never becomes readyThe affected node enters Error; the full network can also enter Error.Wait for each service through its matching client and record the error message.
Export is requested while a network is running or startingExport is rejected and no archive is created.Allow export only for Stopped or Error workspaces.
An archive lacks a valid network or chartImport stops before the archive is copied into local storage.Validate both objects before migration and installation.
An imported implementation is not supported on the current platformImport is rejected to avoid an unusable workspace.Check each Lightning implementation against the current platform.
A network is deleted while a node or simulation is activeRunning resources or simulation work can remain in an unsafe state.Stop the workspace first and remove the simulation and special volumes during cleanup.

Key files

FileWhy to read it
src/…/network.tsPrimary lifecycle coordinator for creation, persistence, startup, shutdown, export, import, and deletion.
src/…/network.tsDefines the network model, node relationships, port checks, archive conversion, and data-path rules used by the coordinator.
src/…/dockerService.tsShows how saved network state becomes a Compose project and how Docker containers and volumes are controlled.
src/…/composeFile.tsExplains the boundary between node records and the generated container plan.
src/…/index.tsDefines the durable network, chart bundle, settings, and Docker service contracts.
src/…/types.tsDefines the common network and node states and the relationships between Bitcoin, Lightning, and Tap nodes.
src/…/NewNetwork.tsxShows the user choices and validation at the start of a new workspace.
src/…/NetworkView.tsxShows the user-facing controls for start, stop, rename, export, delete, and error display.
src/…/ImportNetwork.tsxShows the user-facing archive selection and import result flow.
src/…/migrations.tsShows how saved network and chart data remains usable after application changes.