gRPC: Implement FetchBlock SyncService method via node kernel access#1232
Open
carbolymer wants to merge 1 commit into
Open
gRPC: Implement FetchBlock SyncService method via node kernel access#1232carbolymer wants to merge 1 commit into
carbolymer wants to merge 1 commit into
Conversation
4da5f7a to
2bf1c3f
Compare
Adds in-process block fetching from ChainDB, bypassing N2C IPC. Response includes raw CBOR bytes and cardano block header (slot, hash, height).
2bf1c3f to
c486747
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds initial UTxO RPC SyncService / FetchBlock support by introducing in-process node kernel access and using it to fetch blocks directly from the node’s ChainDB (returning raw CBOR bytes plus a structured Cardano header). It also extends Cardano.Api.Consensus re-exports to expose the consensus types needed for this integration.
Changes:
- Introduce
NodeKernelAccessFand plumbing in the RPC server environment to access the node kernel once initialised, and add theFetchBlockgRPC handler. - Add the UTxO RPC Sync protobuf definitions + generated bindings, and register the Sync service on the server.
- Re-export additional consensus/kernel/ChainDB types from
Cardano.Api.Consensusto support node-kernel-based RPC implementations.
Reviewed changes
Copilot reviewed 17 out of 19 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| cardano-rpc/src/Cardano/Rpc/Server/NodeKernelAccess/Internal.hs | Implements node-kernel-backed nkaFetchBlock and environment lookup for kernel access (new module). |
| cardano-rpc/src/Cardano/Rpc/Server/NodeKernelAccess.hs | Defines NodeKernelAccessF and LedgerSnapshot callback record types (new module). |
| cardano-rpc/src/Cardano/Rpc/Server/Internal/UtxoRpc/Sync.hs | Implements the SyncService FetchBlock RPC method (new module). |
| cardano-rpc/src/Cardano/Rpc/Server/Internal/UtxoRpc/Query.hs | Renames blockNo binding to chainBlockNo for clarity in query methods. |
| cardano-rpc/src/Cardano/Rpc/Server/Internal/Tracing.hs | Adds SyncService tracing types (TraceRpcSync) and injection into top-level trace. |
| cardano-rpc/src/Cardano/Rpc/Server/Internal/Monad.hs | Extends MonadRpc environment requirements to include the node-kernel-access IORef. |
| cardano-rpc/src/Cardano/Rpc/Server/Internal/Env.hs | Extends RpcEnv with rpcNodeKernelAccess reference. |
| cardano-rpc/src/Cardano/Rpc/Server.hs | Registers SyncService handlers; updates runRpcServer to accept node kernel access ref. |
| cardano-rpc/src/Cardano/Rpc/Proto/Api/UtxoRpc/Sync.hs | Adds the SyncService proto API module and metadata instances (new). |
| cardano-rpc/README.md | Updates SyncService method status table for FetchBlock. |
| cardano-rpc/proto/utxorpc/v1beta/sync/sync.proto | Adds UTxO RPC SyncService proto definitions (new). |
| cardano-rpc/gen/Proto/Utxorpc/V1beta/Sync/Sync.hs | Adds generated proto-lens Haskell bindings for SyncService (new, generated). |
| cardano-rpc/gen/Proto/Utxorpc/V1beta/Sync/Sync_Fields.hs | Adds generated field lenses for SyncService messages (new, generated). |
| cardano-rpc/cardano-rpc.cabal | Exposes new Sync modules and adds generated Sync modules to the gen library stanza. |
| cardano-api/src/Cardano/Api/Consensus/Internal/Reexport.hs | Adds re-exports of consensus/kernel/ChainDB-related types to support kernel access. |
| cardano-api/src/Cardano/Api/Consensus.hs | Re-exports additional consensus/kernel/ChainDB-related types and imports ChainDB qualified. |
| .gitignore | Ignores .serena/. |
| .changes/20260602_cardano_rpc_fetchblock.yml | Adds changelog fragment for FetchBlock work (needs kind adjustment per review comments). |
| .changes/20260602_cardano_api_consensus_reexports.yml | Adds changelog fragment for new Cardano.Api.Consensus re-exports. |
| import Cardano.Rpc.Server.Internal.Monad (MonadRpc, grab) | ||
| import Cardano.Rpc.Server.NodeKernelAccess | ||
|
|
||
| import RIO (throwIO) |
Comment on lines
+35
to
+39
| NodeKernelAccessF | ||
| { nkaWithSnapshot = \_ -> error "nkaWithSnapshot: not yet implemented (NKA query migration pending)" | ||
| , nkaSubmitTx = \_ -> error "nkaSubmitTx: not yet implemented (NKA submit migration pending)" | ||
| , nkaFetchBlock = fetchBlock blockType $ Consensus.getChainDB nodeKernel | ||
| } |
Comment on lines
+3
to
+6
| kind: | ||
| - feature | ||
| description: | | ||
| Implement FetchBlock SyncService method via node kernel access. Fetches blocks from ChainDB with raw CBOR bytes and cardano block header (slot, hash, height). |
carbolymer
commented
Jun 3, 2026
| let point = Consensus.RealPoint slot (Consensus.OneEraHash shortHash) | ||
| rawBytes <- liftIO $ Consensus.getBlockComponent chainDB Consensus.GetRawBlock point | ||
| typedBlock <- liftIO $ Consensus.getBlockComponent chainDB Consensus.GetBlock point | ||
| pure $ liftA2 (,) (BSL.toStrict <$> rawBytes) (Consensus.blockNo <$> typedBlock) |
Contributor
Author
There was a problem hiding this comment.
TODO: fix TOCTOU bug - this should be inside single getBlockComponent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First UTxO RPC SyncService method: FetchBlock.
Clients can request blocks by slot and header hash. The response includes raw CBOR bytes and a structured cardano block header (slot, hash, height). Blocks are served directly from the node's ChainDB, avoiding N2C IPC overhead.
Error handling follows gRPC conventions:
INVALID_ARGUMENTfor malformed block referencesNOT_FOUNDfor blocks not present in ChainDBUNAVAILABLEwhile the node is still starting upThis PR implements part of:
Response completeness
The FetchBlock response populates
nativeBytesandcardano.headerfields. The following fields are not yet populated:Block.timestamp- Cardano blocks are slot-indexed, not timestamped. Computing wall-clock time from a slot requires era history from the ledger state, which is not yet accessible (blocked on the ledger snapshot migration).Block.body.tx- Returning structured transaction data requires deserialising raw CBOR and mapping to the UTxO RPC protobuf schema. This is a substantial standalone effort.Clients needing full block data can deserialise
nativeBytesthemselves in the meantime.Dependencies
How to trust this PR
The E2E test lives in the companion cardano-node PR. To run:
mgalazyn/feature/fetchblock(PR #6579), which picks up cardano-api from this branch via the localcabal.project.The FetchBlock test starts a testnet, queries the chain tip via CLI, fetches that block via gRPC, and verifies the response header matches the tip (slot, hash, block number). All 5 RPC tests pass - no regressions in existing methods.
Checklist
.changes/