Block data store and cache
The block data store keeps extended data squares (EDSs) on disk and serves them through read accessors. It adds memory caches for hot heights, exposes share data through getter interfaces, and adapts those reads to the IPFS Bitswap block interface. This page belongs to Storage and retention; see Data pruning, Share retrieval, and Header sync for the systems that remove, request, and identify this data.
System model
The storage path connects a block height to an EDS and then to the data shape requested by a caller. A header supplies the height, the store opens an accessor, and a getter or Bitswap block wrapper reads only the required data when possible.
flowchart LR
H[Extended header and height] --> S[EDS store]
S --> C[Accessor caches]
S --> F[ODS and optional Q4 files]
C --> A[EDS accessor]
F --> A
A --> G[Share getter]
A --> B[Bitswap blockstore]
G --> R[Samples, rows, namespace data, or EDS]
B --> P[Bitswap block]
The same accessor can support local share retrieval and peer data exchange. This keeps the storage layer independent from the protocol that asks for the data.
Storage interfaces
Accessor lookup
home/…/store.go defines AccessorGetter, the small interface used by consumers that need an accessor by network height. The full store also supports writes, existence checks, lookup by data hash, and removal of stored EDS data.
The store accepts two durable layouts:
| Layout | Purpose |
|---|---|
| ODS | Keeps the original data square and its metadata. |
| ODS plus Q4 | Adds the fourth quadrant so parity-side reads can avoid rebuilding data from the original square. |
An empty EDS uses a shared canonical file. Heights link to the data identified by the EDS data hash, so one stored square can serve more than one index without a second full copy.
References: store/store.go• L33-58 store/store.go• L102-118 store/store.go• L306-336
Accessor contract
An accessor presents EDS metadata and data operations such as share reads, axis reads, samples, rows, namespace data, and streaming. Callers receive a closeable accessor and must release it after the read. The store wraps each opened accessor with proof caching, close-once behavior, and validation before it reaches a consumer.
References: store/store.go• L339-400 store/store.go• L552-564 store/…/ods.go• L20-42
Durable file representation
ODS files
The ODS file is the durable base for an EDS. Its header records the file version, share size, square size, and data hash. Axis roots and the original data square follow the header. The stored square contains the first quadrant of the EDS, so the file can read the first half of an axis without loading the whole square.
The file reader selects a small read for an axis half when possible. Requests for the second half are not reconstructed from the ODS-only file and return a not-found result. A full ODS read may remain in the accessor's memory for later operations such as streaming or serving all stored data.
References: store/…/ods.go• L22-42 store/…/ods.go• L44-95 store/…/ods.go• L167-220 store/…/ods.go• L265-320 store/…/square.go• L16-30 store/…/square.go• L33-82
Q4 extension
The Q4 file stores the fourth quadrant of the EDS beside the ODS. The combined accessor opens Q4 only when a request needs a second-half axis. When Q4 is missing or cannot be opened, the accessor keeps ODS-only behavior: it serves stored halves and does not reconstruct missing second-half axes.
This layout favors common reads without requiring every read to load the full EDS. The store checks file sizes when it finds an existing file during a write and recreates a damaged ODS or ODS-plus-Q4 pair.
References: store/…/ods_q4.go• L22-35 store/…/ods_q4.go• L65-110 store/…/ods_q4.go• L125-162 store/…/ods_q4.go• L188-203 store/…/ods_q4_test.go• L47-72 store/store.go• L175-238
Memory-backed representations
EDS accessor cache
The EDS cache keeps closeable accessors by height in an LRU cache. A cache hit returns a reference to the shared accessor. Eviction waits for active readers before it closes the underlying accessor, which lets concurrent reads finish safely.
The default store cache holds recent blocks. A zero size selects a no-op cache, so the store still has the same lookup contract while every read comes from disk.
References: store/…/cache.go• L18-38 store/…/accessor_cache.go• L20-43 store/…/accessor_cache.go• L81-160 store/store_options.go• L7-23
Node memory store
home/…/store_mem.go provides an in-memory implementation of the node's broader Store interface. It keeps keys, arbitrary key-value data, and configuration in memory. It is used for tests and does not replace the disk-backed EDS store used by a running node.
References: nodebuilder/store_mem.go• L12-25 nodebuilder/store_mem.go• L28-55
Cache layers
Recent and blockstore caches
The base store can cache recent heights. A separate cache can be added for a workload such as Bitswap, where repeated reads may target a different set of heights. The extra layer reads the base cache first through a combined view and loads a file into its own cache when needed.
The base store is updated to see both layers. A read through the wrapped store therefore benefits from either cache, while each layer keeps its own write and eviction behavior. Removing a height removes it from every underlying cache before the file indexes are removed.
References: store/store_cache.go• L11-41 store/store_cache.go• L44-65 store/…/doublecache.go• L11-58 store/store.go• L484-537
Cache read behavior
Get reports a cache miss without opening a file. GetOrLoad returns a retained accessor on a hit or calls the supplied loader on a miss. The LRU cache protects each height with a striped lock and tracks references separately from cache membership.
The close path is part of the cache contract. A caller that does not close an accessor can delay eviction; after the close wait limit the cache logs the condition and closes the accessor forcefully. A finalizer also reports accessors that reach garbage collection without an explicit close.
References: store/…/accessor_cache.go• L90-143 store/…/accessor_cache.go• L193-245 store/…/doublecache.go• L29-51
Getter behavior
Local share reads
home/…/getter.go turns a header height into higher-level Shwap results. It uses one accessor for each request and closes that accessor when the request ends.
The getter supports these read shapes:
| Request | Result |
|---|---|
| Samples | Shares and proofs at requested coordinates. |
| EDS | A reconstructed extended data square from stored shares. |
| Row | One complete row. |
| Namespace data | Data shares for one namespace. |
| Range namespace data | Namespace data over a range of original-square positions. |
A missing height becomes the common shwap.ErrNotFound result. Other store or accessor errors keep their cause so callers can distinguish absence from a read failure.
References: store/getter.go• L19-49 store/getter.go• L52-72 store/getter.go• L74-140
Bitswap block reads
The Bitswap blockstore is a read adapter over an AccessorGetter. A Shwap block is an IPFS block that carries a CID and a height. For a Get, the adapter creates the empty block from the CID, finds the EDS accessor by height, populates the requested container, and converts it to a Bitswap block.
Has checks whether the height is present. GetSize returns the registered maximum size for the block type so Bitswap can schedule requests without an extra disk read. The adapter does not implement block writes, deletion, or key listing; durable EDS writes use the EDS store instead.
References: share/…/block.go• L15-33 share/…/block_store.go• L25-56 share/…/block_store.go• L59-101 share/…/block_registry.go• L11-34
Concurrency control
Store operation locks
The EDS store is safe for concurrent use. It uses separate striped read-write locks for heights and data hashes. Reads lock the index they use. Writes and removals lock the related height and hash together so file creation, linking, and deletion cannot race for the same EDS.
The locks are striped rather than allocated for every possible height or hash. This limits memory use while still allowing unrelated operations to proceed in parallel.
References: store/store.go• L44-56 store/store.go• L120-172 store/store.go• L339-367 store/store.go• L453-519 store/striplock.go• L10-43
Node store lock
The node-level file-backed store takes a directory lock when it opens. Only one node process can own a store path at a time. The lock is released when the node store closes, and callers can use the same lock state to discover an opened or stopped node store.
The node store lifecycle requires initialization before opening. The lifecycle checks cover both bridge and light node stores, reject a second open, and use the lock state to find an open store or report that none is open.
References: nodebuilder/store.go• L28-35 nodebuilder/store.go• L59-93 nodebuilder/store.go• L188-253 nodebuilder/store_test.go• L18-57 nodebuilder/store_test.go• L59-148
Metrics
EDS store metrics
Store metrics measure the time and result of puts, gets, existence checks, and removals. Put metrics include whether Q4 was written and the EDS size. Removal metrics also count bytes reclaimed from the canonical disk files and label the removed layout.
Metrics are optional. When enabled, the store also enables metrics on its current cache, including any cache layers added later.
References: store/metrics.go• L16-110 store/metrics.go• L113-194
Cache and Bitswap metrics
Each named accessor cache reports get results, evictions, and current item count. The Bitswap wrapper counts each blockstore operation and marks not-found results separately from other failures. These measures show whether misses come from absent data or from a storage or adapter error.
References: store/…/metrics.go• L16-71 share/…/blockstore_metrics.go• L27-50 share/…/blockstore_metrics.go• L120-203
Nodebuilder construction
File-backed node store
home/…/store.go defines the node-level store. It owns the node path, keystore, configuration, arbitrary key-value datastore, and process lock. The datastore opens lazily and uses a Badger-backed disk store with settings chosen to limit memory growth and reclaim deleted data.
The share module then creates the EDS store under the node store path for bridge nodes. The EDS store receives its cache parameters from share configuration and is stopped with the node lifecycle. Initialization and open steps establish the same lifecycle for both bridge and light node stores before callers use their keystore, datastore, or configuration.
References: nodebuilder/store.go• L37-57 nodebuilder/store.go• L100-159 nodebuilder/store.go• L286-351 nodebuilder/…/module.go• L188-198 nodebuilder/store_test.go• L18-57
Node-type blockstore selection
Bridge nodes use the EDS-backed Bitswap blockstore because they serve data that is already kept as EDS files. The builder can add the blockstore cache layer before it wraps the adapter with operation metrics.
Light nodes use a normal Bitswap blockstore backed by the node datastore. This separates transient peer data from the EDS file store, while both node types expose the same Bitswap blockstore interface to the exchange layer.
The default share configuration enables a recent-block cache and a blockstore cache. Setting the blockstore cache size to zero selects the plain EDS-backed adapter for bridge nodes.
References: nodebuilder/…/bitswap.go• L63-80 nodebuilder/…/module.go• L63-95 nodebuilder/…/config.go• L14-52
Change checklist
When these files or sections are changed, remember to consider these:
- If the file layout or index rules change, update hash lookup, height lookup, hard-link or symlink creation, validation, and both removal paths together.
- If an accessor-producing path changes, preserve the closeable accessor contract and update every wrapper that adds proof caching, validation, or close-once behavior.
- If cache order or loading rules change, check both direct store reads and
CachedStorereads. The combined cache must retain consistent visibility for the base store and its extra layer. - If block types or CID rules change, update block construction, maximum-size scheduling, population, and Bitswap verification as one contract.
- If cache or EDS store metrics change, keep metric registration aligned with caches added after store construction.
- If node type selection changes, check the distinction between the EDS-backed bridge blockstore and the datastore-backed light blockstore.
- If node store initialization or locking changes, preserve the open-before-use, single-owner, and store-discovery behavior for both node types.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No EDS exists for a requested height | Store lookup returns ErrNotFound; getter and Bitswap callers receive their not-found form. | Keep height links and pruning actions in sync with the canonical EDS files. |
| A caller does not close a cached accessor | Eviction waits for the reader and can reach the close timeout before forceful close. | Close every accessor returned by the store or cache. |
| An existing ODS or Q4 file has the wrong size | The next matching write treats the stored pair as damaged and attempts recovery. | Preserve size validation when changing file formats or write paths. |
Bitswap calls Put, DeleteBlock, or AllKeysChan on the EDS adapter | The adapter panics because it is read-only. | Use EDS store writes for durable data and use a datastore blockstore where Bitswap persistence is required. |
| A node store is opened twice or before initialization | Open fails with ErrOpened or ErrNotInited. | Initialize the path first and release the node store on shutdown. |
| A cache layer is created with a non-positive size | Layer construction fails instead of silently creating an unusable cache. | Validate the configured blockstore cache size before adding the layer. |
Key files
| File | Why to read it |
|---|---|
| store/store.go | Defines the durable EDS store, its height and hash indexes, read/write boundary, and accessor wrapping. |
| store/getter.go | Shows how stored accessors become samples, rows, namespace data, and complete EDS values. |
| store/…/cache.go | Defines the cache contract shared by the no-op, LRU, and combined cache forms. |
| store/…/accessor_cache.go | Explains LRU behavior, references, eviction, and accessor lifetime. |
| store/store_cache.go | Shows how a workload-specific cache is attached to the EDS store. |
| store/…/ods.go | Describes the durable original-square read model and lazy full-square loading. |
| store/…/ods_q4.go | Describes the optional parity-side file and its lazy use. |
| store/…/square.go | Shows the in-memory view used for cached ODS reads and axis access. |
| nodebuilder/store.go | Defines the node-level disk store, process lock, datastore, and lifecycle. |
| nodebuilder/store_mem.go | Shows the in-memory node store used by tests. |
| nodebuilder/store_test.go | Verifies initialization, single-owner locking, store discovery, and lifecycle behavior for bridge and light node stores. |
| nodebuilder/…/bitswap.go | Selects the EDS-backed or datastore-backed Bitswap blockstore. |
| nodebuilder/…/module.go | Connects EDS store construction and blockstore construction to node modules. |
| share/…/block_store.go | Defines the read-only Bitswap view over Shwap accessors. |
| store/metrics.go and share/…/blockstore_metrics.go | Define storage, cache-layer, and Bitswap operation measurements. |