Introducing a new way to review your PRs.

Read the announcement

near / nearcore

Reference client for NEAR Protocol

View on GitHub

20 documents · Updated Nov 23, 2026

docs / node-startup-and-lifecycle

Node startup and lifecycle

The neard executable turns a node home into a running NEAR node. It loads chain and identity data, checks the selected role, opens durable state, assembles node services, and keeps them alive until shutdown. The executable also supports setup, development, offline data work, and a compiler-worker mode. For the wider client role, see Node client services; for external request services, see RPC and indexing services.

Startup boundary

neard is the process boundary. The nearcore library is the assembly boundary that turns validated configuration into the service graph used by the node.

Process entry point

The entry point prepares process-wide facilities before command dispatch. It enables panic diagnostics, creates shared worker capacity, finds HTTPS certificate paths, and raises the file descriptor limit needed for peer connections. It then parses one required subcommand and starts the selected mode (neard/…/main.rs• L47-87).

The executable has one special worker path. The compile-wasm mode enters the compiler sandbox before it creates the shared thread pool. A normal node does not use this path. Node startup can instead use the running neard executable as an isolated compiler daemon. This keeps the executable path valid across a rename-over binary upgrade (neard/…/main.rs• L53-61, nearcore/…/lib.rs• L405-425).

The command layer sets logging and tracing before it runs a command. The run command is the long-lived node path. Other commands initialize a home or operate on stored data without starting the full node service graph (neard/…/cli.rs• L69-177).

References: neard/…/main.rs• L47-87 nearcore/…/lib.rs• L405-425 neard/…/cli.rs• L69-177

Operational modes

The command line selects between node startup and bounded operational tasks. init creates or completes the files needed by a node. localnet creates a multi-node development layout. run starts a configured node. State viewers, validation, database tools, replay tools, and archive tools use the same home and chain data but do not represent a normal running node (neard/…/cli.rs• L108-172, neard/…/cli.rs• L221-284).

The running node role comes from configuration rather than from a separate executable. A validator has a validator signer. An RPC node tracks all shards so it can answer broad state and transaction queries. An archival node keeps historical data. Cloud archival and split storage extend the archival role without changing the process entry point (nearcore/…/config.rs• L714-868, nearcore/…/config_validate.rs• L264-350). Build features also control whether optional request surfaces can start (nearcore/Cargo.toml• L95-115, nearcore/…/lib.rs• L787-861).

Mode or roleSystem aim
ValidatorParticipate in block and chunk production when the node has the required signer and chain configuration.
Non-validator nodeFollow and serve chain data without producing blocks.
RPC nodeTrack the data needed to serve broad public queries.
Archival nodeRetain historical data instead of using normal garbage collection.
LocalnetCreate a self-contained development network with several configured node homes.
Offline toolInspect, validate, replay, or maintain chain data without starting the live service graph.
Compiler workerCompile WASM in the isolated worker process used by a configured node.

References: neard/…/cli.rs• L108-172 neard/…/cli.rs• L221-284 neard/…/cli.rs• L398-433 nearcore/…/config.rs• L714-868 nearcore/…/config_validate.rs• L264-350 neard/Cargo.toml• L61-80 nearcore/Cargo.toml• L95-115 nearcore/…/lib.rs• L405-425 nearcore/…/lib.rs• L787-861

Configuration and identity

The node home is the persistent startup contract. It normally contains config.json, a genesis file, a node key, and an optional validator key. nearcore combines these files into NearConfig, which gives service assembly one view of chain identity, storage, networking, consensus, role, and optional APIs.

Configuration loading

Configuration loading reads the JSON file and records unknown-field and deprecated-field problems before semantic checks. It then validates cross-field rules, loads the node and validator keys, loads and validates genesis, and returns the collected errors together when possible. A cloud archival writer with a validator key is also rejected because the two roles conflict. The --unsafe-fast-startup flag changes genesis validation from the full check to the fast mode. It does not bypass the need for readable configuration, keys, or genesis (neard/…/cli.rs• L101-117, nearcore/…/config.rs• L558-647, nearcore/…/config.rs• L1751-1836).

NearConfig::new maps operator settings into the client and network views used for service construction. It also derives role-sensitive defaults. For example, archival or RPC roles enable outcome data retention by default. Cloud archival writer settings provide state dump settings for the running node (nearcore/…/config.rs• L697-707, nearcore/…/config.rs• L732-892). Running nodes also enable state snapshots for peer state transfer. Offline tools can choose a different snapshot policy (nearcore/…/config.rs• L943-1013).

At run time, command-line values are applied after the home configuration loads. They can change selected client, network, telemetry, archive, RPC, and view-runtime settings for that process. The command also records the executable version, checks the chain and validator architecture, checks Linux network parameters, and rejects sandbox builds on public chains when the build rules require it (neard/…/cli.rs• L484-565).

References: neard/…/cli.rs• L101-117 neard/…/cli.rs• L484-565 nearcore/…/config.rs• L558-647 nearcore/…/config.rs• L697-707 nearcore/…/config.rs• L732-892 nearcore/…/config.rs• L943-1013 nearcore/…/config.rs• L1751-1836

Configuration constraints

Validation protects relationships that are not visible from one field alone. Storage mode, archival mode, tracked shards, garbage collection, state sync, and cloud archival must agree before service construction begins. The validator checks known conditions and returns semantic errors together so an operator can correct the configuration as one unit (nearcore/…/config_validate.rs• L13-87, nearcore/…/config_validate.rs• L374-380).

The main role boundaries are explicit:

  • Cold storage and split storage require archival mode and saved trie changes.
  • Cloud archival must be either a reader or a writer. It cannot be both.
  • A cloud archival writer that archives shard data needs tracked shard data and the related transaction outcome, receipt-to-transaction, and state-change data.
  • A cloud archival writer cannot use a validator key because it must not produce chunks.
  • State dump locations, credentials, compression, and shard restart lists must be usable before a dump service starts.
  • Garbage collection must have positive settings and enough throughput to keep up with block production.
  • The current tracked-shard configuration cannot be mixed with its deprecated alternatives.

References: nearcore/…/config_validate.rs• L32-87 nearcore/…/config_validate.rs• L89-233 nearcore/…/config_validate.rs• L235-372 nearcore/…/config.rs• L1751-1785

Service assembly

nearcore::start_with_config_and_synchronization assembles the live node from a validated NearConfig. It first makes durable state safe to use, then creates chain and runtime foundations, and finally connects actors for client work, shard work, networking, APIs, synchronization, telemetry, and optional archival services.

Startup sequence

The startup sequence establishes lower-level resources before it exposes higher-level services. This order lets later services use the same chain identity, epoch data, runtime, and storage handles.

The diagram shows the main dependency order. It omits individual actors and message types because the important boundary is which shared resources must exist before each service can start.

flowchart TD
    A[Load NearConfig] --> B[Open and migrate storage]
    B --> C[Create epoch manager]
    C --> D[Initialize or verify genesis state]
    D --> E[Create shard tracker and runtime]
    E --> F[Start state snapshots, archival, telemetry, and client services]
    F --> G[Start shard manager and network]
    G --> H[Start configured RPC or Rosetta endpoints]
    H --> I[Return NearNode handles]

Storage opening performs version-aware migrations and checks the database kind and version before any actor starts. Supported older stores can be upgraded as part of opening. The node rejects a store created for a cloud-archive reader because that store is for offline archive access, not for a live node (nearcore/…/lib.rs• L98-202, nearcore/…/lib.rs• L428-435).

Some storage upgrades move historical headers to cold storage and refresh the epoch-sync proof before removing data from the hot store. Chain-dependent work is skipped for state snapshots and stores that have not left the genesis epoch. This keeps the persistent store compatible with the service graph that opens it (nearcore/…/migrations.rs• L393-441, nearcore/…/migrations.rs• L444-476, nearcore/…/migrations.rs• L598-720).

The epoch manager supplies the shard layout for genesis initialization. The runtime applies chain rules to stored state. The shard tracker tells the client which shard data this node must follow. In archival split storage, the view side can use a separate cold-aware store while live processing continues to use the hot store (nearcore/…/lib.rs• L441-503, nearcore/…/config.rs• L943-1013).

References: nearcore/…/lib.rs• L98-202 nearcore/…/lib.rs• L398-503 nearcore/…/config.rs• L943-1013 nearcore/…/migrations.rs• L34-83 nearcore/…/migrations.rs• L352-390 nearcore/…/migrations.rs• L393-441

Service graph

The assembled services share late-bound connections so they can be created in dependency order and linked after their owners exist. The client drives chain progress. The view client serves read work. State request and shard services support synchronization and shard data exchange. The network carries protocol traffic between these services (nearcore/…/lib.rs• L549-747).

The final assembly attaches the RPC handler to the transaction pool and view resources. It then starts the peer manager and, when enabled by configuration and build features, HTTP RPC and Rosetta endpoints. JSON-RPC also receives an entity-debug handler that reads chain and state data from hot storage or an available cold store. NearNode returns handles for primary services and lifecycle control such as cold storage and resharding (nearcore/…/lib.rs• L743-853, nearcore/…/entity_debug.rs• L622-641, nearcore/…/lib.rs• L344-364, nearcore/…/lib.rs• L879-890).

Build features define which optional service surfaces can exist. The default nearcore feature set enables JSON-RPC and Rosetta RPC. The Spice protocol feature changes the service graph by adding its chunk actors and their connections (nearcore/Cargo.toml• L95-115, nearcore/…/lib.rs• L232-345, nearcore/…/lib.rs• L642-733).

References: nearcore/…/lib.rs• L232-345 nearcore/…/lib.rs• L549-747 nearcore/…/lib.rs• L749-897 nearcore/Cargo.toml• L95-115

State synchronization dump service

The state synchronization dump service publishes state needed by other nodes to bootstrap a shard. When configured, it watches finalized chain progress, selects eligible epochs, and uploads a state header plus state parts for shards tracked by this node. Cloud archival writer settings can derive this dump configuration, so the two settings cannot be configured independently. It checks external storage and saved progress before doing work, which lets restarts and other dumpers skip parts that already exist (nearcore/…/config.rs• L697-707, nearcore/…/lib.rs• L775-785, nearcore/…/state_sync.rs• L53-110, nearcore/…/state_sync.rs• L590-721, nearcore/…/state_sync.rs• L790-949).

The service publishes only state for shards that the node tracks. It selects every epoch by default, or epochs that match the configured snapshot cadence, while always including resharding epochs. For each selected epoch, it creates a state header and state parts from a chain snapshot, records progress in the chain store, and reuses headers or parts already present in external storage (nearcore/…/state_sync.rs• L663-721, nearcore/…/state_sync.rs• L790-835, nearcore/…/state_sync.rs• L928-949).

The dumper limits concurrent state-part generation and retries generation and upload failures. It checks external storage again during an active dump because another node or process may have uploaded a part. A newer epoch cancels the older dump so the latest state becomes available first. Actor-system shutdown waits for active dump tasks to stop. The service does not start without dump configuration, and the current Spice build exits without performing dumps (nearcore/…/state_sync.rs• L323-448, nearcore/…/state_sync.rs• L869-925, nearcore/…/state_sync.rs• L952-1045).

References: nearcore/…/config.rs• L697-707 nearcore/…/lib.rs• L775-785 nearcore/…/state_sync.rs• L53-110 nearcore/…/state_sync.rs• L323-448 nearcore/…/state_sync.rs• L663-721 nearcore/…/state_sync.rs• L790-949 nearcore/…/state_sync.rs• L952-1045

Process lifecycle

After assembly, the process waits for an operating-system signal or a shutdown reason sent by the client. It treats reload, ordinary stop, and recovery restart as different lifecycle events so it can preserve data safety and stop background work in the right order.

Runtime operation

The run command creates a Tokio runtime and starts the node inside it. Before assembly, it reads dynamic configuration and creates the updater used by the client. A Unix SIGHUP reloads supported dynamic settings while the node stays up. SIGINT, SIGTERM, and a client shutdown reason leave the wait loop (neard/…/cli.rs• L574-621).

Dynamic reload reads config.json, the optional log_config.json, and the configured validator key. A missing log file resets logging to defaults. The loader exposes only the client settings marked as updatable, plus the log configuration and validator-key state. If any reload input is invalid, it reports the errors together, marks the configuration as incorrect, and does not present a partial update (nearcore/…/dyn_config.rs• L10-76, nearcore/…/dyn_config.rs• L79-136).

The running process owns the actor system rather than a foreground request loop. The returned NearNode handles make selected service and background loops visible to the process controller. This keeps service lifetime tied to the node process even when individual services run on different async or worker runtimes (nearcore/…/lib.rs• L347-367).

References: neard/…/cli.rs• L574-621 nearcore/…/dyn_config.rs• L10-136 nearcore/…/lib.rs• L347-367

Shutdown and restart

Shutdown first stops background archival and resharding work, then shuts down actors and disables tracing. The process waits until all RocksDB instances are dropped before it exits. This gives storage time to close cleanly after service work has stopped (neard/…/cli.rs• L624-648).

Epoch sync can request a data reset when a normal node must re-bootstrap its hot data. The process writes a durable marker before shutdown. On the next startup, a non-archival node removes the hot store and then re-executes the same binary with the same arguments on Unix. Archival nodes ignore the marker to avoid deleting retained data (neard/…/cli.rs• L46-48, neard/…/cli.rs• L624-713).

References: neard/…/cli.rs• L624-713 nearcore/…/lib.rs• L347-364

Change checklist

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

  • When a Config field changes, check its mapping into NearConfig and its effect on role-sensitive defaults.
  • When storage, archive, cold-store, or cloud-archive rules change, check both configuration validation and storage opening.
  • When a database version or migration changes, check hot and cold store compatibility and the data that later startup stages expect.
  • When a service is added or its dependency changes, preserve startup order and bind all late-bound service connections before the node is returned.
  • When state-sync dump behavior changes, check tracked-shard selection, cadence and resharding selection, saved progress, external object reuse, epoch rollover, retry behavior, and shutdown cancellation together.
  • When an optional API feature or its configuration changes, check both compile-time availability and the runtime condition that starts the endpoint.
  • When compiler-worker startup changes, check both the standalone compile-wasm path and the configured daemon path. Preserve the executable-path behavior needed after binary replacement.
  • When dynamic configuration changes, keep the updatable-field list aligned with the client updater and preserve all-or-nothing reload behavior.
  • When a shutdown reason changes, check marker handling, actor stop order, storage drain behavior, and Unix restart behavior together.
  • When a command or build feature changes, check whether it starts the live service graph or only uses node data offline.

Important failure modes

TriggerConsequenceGuard
Missing or invalid config.json, genesis, node key, or validator keyStartup returns a combined configuration or identity error and does not assemble the node.Run configuration validation and keep the files in one node home.
A cloud archival writer has a validator keyStartup rejects the role conflict because the writer must not produce chunks.Remove the validator key or use a non-writer role.
Cold or split storage is configured without archival mode or saved trie changesSemantic validation rejects the node before storage opens.Keep archival, trie-change, and cold-store settings aligned.
Cloud archival sets both reader and writer, or sets neitherThe node cannot choose a valid cloud archival role.Select exactly one cloud archival direction.
A cloud archival writer lacks required tracked shards or retained outcome dataValidation rejects the writer before it can publish complete archive data.Track the required shard data and keep related retention enabled.
A live node is pointed at a cloud-archive reader storeStartup stops before actors start because the store is not writable live-node state.Use that store with the cloud archive tool instead.
A store is outside the supported database-version range, or a migration failsStorage opening stops before service construction.Upgrade from a supported release and resolve migration data errors before starting the node.
Compiler-daemon startup fails or its executable cannot be locatedNode assembly stops before storage and actors start.Keep the configured compiler-daemon binary available and use the same executable path for worker startup.
Entity debug requests data from an absent cold store or an unretained cold columnThe request returns an internal RPC error while the node remains running.Use hot storage or query data retained in the configured cold store.
State-sync dumping is configured in a Spice buildThe dump loop exits without publishing state parts.Use a supported non-Spice build for state-sync dumps.
State-part generation or external upload keeps failingThe affected dump task keeps retrying, so that shard's dump does not complete until the error clears or a newer epoch cancels it.Check the runtime snapshot and external storage path, then allow the retry loop to recover.
A mainnet or testnet validator runs on a non-x86_64 hostThe process panics rather than risk producing incompatible blocks.Run those validators on x86_64.
Epoch sync requests a reset on a non-archival nodeThe next startup deletes the hot store and reboots the process.Treat the marker as an intentional data-reset workflow and protect archival homes.
A dynamic reload has an invalid config, log file, or validator keyThe node keeps its current runtime settings and marks dynamic configuration as incorrect.Fix all reported reload inputs before the next restart or reload.

Key files

FileWhy to read it
neard/…/main.rsShows the process entry point, worker mode, and process-wide setup before command dispatch.
neard/…/cli.rsDefines command modes, startup overrides, signal handling, dynamic reload, shutdown, and restart behavior.
nearcore/Cargo.tomlShows optional API and protocol features that change node capabilities.
nearcore/…/config.rsDefines the node configuration model, compatibility warnings, key and genesis loading, role-sensitive defaults, dynamic settings, and conversion into NearConfig.
nearcore/…/dyn_config.rsDefines the reload contract for logging, client settings, and validator-key changes.
nearcore/…/config_validate.rsShows cross-field rules that protect supported storage, node roles, garbage collection, and state dump settings.
nearcore/…/lib.rsShows storage opening, compiler setup, runtime foundations, service assembly, network attachment, API startup, and lifecycle handles.
nearcore/…/migrations.rsShows database version checks and upgrades during storage opening.
nearcore/…/state_sync.rsShows the optional state publication loop and its epoch selection, retry, reuse, progress, and cancellation behavior.
nearcore/…/entity_debug.rsShows how the debug request surface selects hot or cold storage.