Chain synchronization is a stateful protocol that enables clients to efficiently synchronize with the Cardano blockchain. By maintaining a cursor to track synchronization progress, clients can follow the chain, handle reorganizations, and resume syncing after disconnections.
Consumers using AddOgmiosServices() receive blocks through IChainSynchronizationMessageHandlers. Throughput is dominated by handler latency.
RollForwardHandler and RollBackwardHandler run on the chain sync consumer thread. Return quickly — filter in memory, enqueue to a bounded Channel, and process DB/network work on background workers. Slow handlers cause the bounded queue (1000 blocks) to fill and the bot to fall behind.
The client replenishes pipelined nextBlock requests before invoking your handler, so the default inFlight=100 window stays saturated even when handlers do heavy work.
Pass maxBlocksPerSecond to ResumeAsync. Values <= 0 mean no limit. Positive values throttle how fast new nextBlock requests are sent after each processed block — useful to protect downstream systems when syncing from origin.
Use one InteractionContext per concurrent Ogmios protocol. Do not share a socket between chain sync and mempool monitoring.
-
Find Intersection: Establish a common synchronization point between the client's local chain and the blockchain node. The client can provide potential points or start from the origin.
-
Synchronize Blocks:
- Roll Forward: Process new blocks as they are added to the chain.
- Roll Backward: Handle chain reorganizations by reverting to the last known common point.
Locate a starting point on the blockchain for synchronization. If no intersection is found, synchronization begins from the origin. This ensures efficient syncing without reprocessing existing data.
As new blocks are added to the chain:
- The protocol streams these blocks sequentially.
- Clients process block details such as headers, transactions, and metadata.
When a chain fork occurs:
- The protocol reverts the client's synchronization point to the last known common block.
- This guarantees alignment with the canonical chain and ensures data consistency.
Pipelining optimizes synchronization by allowing clients to send multiple requests simultaneously:
- Default
inFlight: 100 concurrentnextBlockrequests primed at session start, replenished after each dequeued response. - Advantages: Reduced latency and improved bandwidth utilization.
- Dynamic Adjustment: Pass a lower
inFlightif memory is constrained; raise it for faster catch-up on high-bandwidth links.
Clients can specify key synchronization points, such as:
- Era Transitions: Points marking the end of an era.
- Significant Milestones: Blocks of interest for specific applications.
This feature is particularly useful for era-specific or milestone-driven data retrieval.
Triggered when the blockchain progresses to new blocks. Clients process the new block data to update their state.
Triggered when the blockchain rolls back to a previous state (e.g., due to a fork). Clients revert their synchronization point accordingly. Process rollbacks on the same ordered pipeline as roll-forwards (or handle synchronously in the handler before returning).
Register IChainSynchronizationMessageHandlers alongside AddOgmiosServices(). Handlers run on a dedicated ordered queue (capacity 2000) so block parsing and pipelining continue while your handler executes.
builder.Services.AddOgmiosServices();
builder.Services.AddSingleton<IChainSynchronizationMessageHandlers, ChainSynchronizationMessageHandlers>();
await chainSynchronizationClientService.ResumeAsync(
[chainContext],
maxBlocksPerSecond: ogmiosConfiguration.MaxBlocksPerSecond,
inFlight: 100,
cancellationToken);public class ChainSynchronizationMessageHandlers : IChainSynchronizationMessageHandlers
{
private readonly Channel<BlockWork> _work = Channel.CreateBounded<BlockWork>(500);
public Task RollBackwardHandler(Generated.Ogmios.PointOrOrigin point, Generated.Ogmios.TipOrOrigin tip)
{
_work.Writer.TryWrite(BlockWork.Rollback(point, tip));
return Task.CompletedTask;
}
public Task RollForwardHandler(Block block, string blockType, Generated.Tip tip)
{
_work.Writer.TryWrite(BlockWork.RollForward(block, blockType, tip));
return Task.CompletedTask;
}
}Chain synchronization offers a robust and efficient mechanism for keeping clients aligned with the Cardano blockchain. By leveraging pipelining, bounded queues, handler offload, and optional maxBlocksPerSecond throttling, clients can achieve reliable and performant synchronization tailored to their needs.
- RollBackwardHandler: Handle reorgs; keep synchronous if rollback state must be visible before the next forward.
- RollForwardHandler: Must return quickly; the library already parses blocks from UTF-8 and maintains the pipelined window.
- Separate sockets: Chain sync and mempool require different
InteractionContextinstances.