Skip to content

gRPC: Implement FetchBlock SyncService method via node kernel access#1232

Open
carbolymer wants to merge 1 commit into
masterfrom
mgalazyn/feature/rpc-fetchblock
Open

gRPC: Implement FetchBlock SyncService method via node kernel access#1232
carbolymer wants to merge 1 commit into
masterfrom
mgalazyn/feature/rpc-fetchblock

Conversation

@carbolymer
Copy link
Copy Markdown
Contributor

@carbolymer carbolymer commented Jun 2, 2026

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_ARGUMENT for malformed block references
  • NOT_FOUND for blocks not present in ChainDB
  • UNAVAILABLE while the node is still starting up

This PR implements part of:

Response completeness

The FetchBlock response populates nativeBytes and cardano.header fields. 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 nativeBytes themselves in the meantime.

Dependencies

How to trust this PR

The E2E test lives in the companion cardano-node PR. To run:

  1. Check out the metarepo with cardano-node branch mgalazyn/feature/fetchblock (PR #6579), which picks up cardano-api from this branch via the local cabal.project.
  2. Run all RPC tests:
    TASTY_PATTERN='/RPC/' cabal test cardano-testnet-test
    

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

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated
  • Self-reviewed the diff
  • Changelog fragment added in .changes/

@carbolymer carbolymer force-pushed the mgalazyn/feature/rpc-fetchblock branch from 4da5f7a to 2bf1c3f Compare June 2, 2026 20:50
Adds in-process block fetching from ChainDB, bypassing N2C IPC.
Response includes raw CBOR bytes and cardano block header (slot, hash, height).
@carbolymer carbolymer force-pushed the mgalazyn/feature/rpc-fetchblock branch from 2bf1c3f to c486747 Compare June 2, 2026 21:12
@carbolymer carbolymer self-assigned this Jun 2, 2026
@carbolymer carbolymer changed the title Implement FetchBlock SyncService method via node kernel access gRPC: Implement FetchBlock SyncService method via node kernel access Jun 2, 2026
@carbolymer carbolymer marked this pull request as ready for review June 2, 2026 21:22
Copilot AI review requested due to automatic review settings June 2, 2026 21:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 NodeKernelAccessF and plumbing in the RPC server environment to access the node kernel once initialised, and add the FetchBlock gRPC 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.Consensus to 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 thread cardano-rpc/src/Cardano/Rpc/Server/NodeKernelAccess/Internal.hs
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).
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)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: fix TOCTOU bug - this should be inside single getBlockComponent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants