Introducing a new way to review your PRs.

Read the announcement

lfdt-ibc / ibc-go

Inter-Blockchain Communication Protocol (IBC) implementation in Golang.

View on GitHub

20 documents · Updated Nov 13, 2026

docs / core-state-and-keeper

Core state, keeper, and module integration

The IBC core module is the Cosmos SDK runtime boundary for cross-chain clients, connections, channels, packets, proofs, and application callbacks. Its keeper coordinates these state domains and exposes them through SDK messages, queries, genesis, migrations, and application routers.

The core module works with both the established IBC packet model and the IBC v2 client and packet model. For the wider protocol view, see Core IBC protocol. The state used by each lifecycle is described in Client lifecycle, Connection handshake, and Channel and packet lifecycle.

Core keeper

The core keeper is the coordinating object for the IBC submodules. The fully qualified repository path modules/…/keeper.go defines one keeper that owns client, client v2, connection, channel, channel v2, and port responsibilities while sharing one IBC store, codec, authority, and upgrade service.

The keeper builds these relationships in dependency order. Client state supports connection checks. Connections support channel checks. Channels use both client versions and can pass packet work to application routes. The port keeper holds the application routes rather than protocol state.

Keeper responsibilities

The keeper provides the common boundary between protocol state and the Cosmos SDK context. It also carries the authority used for protected administrative messages and the upgrade dependency used for IBC software upgrade data.

ResponsibilitySystem purpose
Client state and consensus stateTrack the remote chain view used to check proofs and delays.
Connection stateLink a local connection to a client and its counterparty.
Channel and packet stateTrack channel progress, packet commitments, receipts, acknowledgements, and sequence values.
Application routingSend channel and packet events to the module bound to a port.
IBC v2 stateTrack client counterparties, aliases, v2 packet state, and asynchronous packets.
SDK integrationServe messages and queries, load genesis, run migrations, and run the client begin-block action.

References: modules/…/keeper.go• L25-69 modules/…/msg_server.go• L23-29 modules/…/module.go• L123-172

Host paths and commitment state

IBC state uses stable host paths so that a local chain and its counterparty can refer to the same logical value when they exchange proofs. The modules/core/24-host package defines the path families for clients, connections, channels, ports, sequences, commitments, acknowledgements, and receipts.

State path model

The path model groups state by the object that owns it. Client state is isolated below a client identifier. Connection state is keyed by connection identifier. Channel state is keyed by port and channel identifiers. Packet records add a sequence to the channel path.

State familyLogical path shapeRole in the protocol
Client stateclients/{clientID}/clientStateStores the remote chain state.
Consensus stateclients/{clientID}/consensusStates/{height}Stores the remote state root and time at a trusted height.
Client connectionsclients/{clientID}/connectionsLinks a client to its local connections.
Connectionconnections/{connectionID}Stores connection handshake state.
ChannelchannelEnds/ports/{portID}/channels/{channelID}Stores channel state and its connection hop.
v1 packet datacommitments/..., acks/..., receipts/...Records packet commitments, acknowledgements, and receive evidence.
v2 packet dataA client or channel identifier plus a typed packet prefix and sequenceProvides the public v2 packet paths.

Identifiers and paths are validated before they become state keys. The host rules reject blank values, path separators inside identifiers, invalid characters, and values outside the allowed length range. A v2 Merkle path can be a prefix while the counterparty path is being built, or a complete path during proof verification; the complete form cannot contain empty key parts.

The v1 channel keeper uses the v2 send-sequence key so v1 and v2 packet flows do not use conflicting send counters. This is a compatibility choice for the shared runtime. The v2 packet key tests also serve as an external key contract for other implementations.

References: modules/…/client_keys.go• L11-54 modules/…/connection_keys.go• L7-20 modules/…/channel_keys.go• L7-22 modules/…/packet_keys.go• L7-71 modules/…/validate.go• L12-119 modules/…/packet_key_test.go• L14-36 modules/…/merkle.go• L14-75 modules/…/keeper.go• L150-183

Commitment and proof state

Commitment state makes local IBC values provable to a counterparty. A commitment root is the hash of stored key-value data. A commitment path identifies the keys from the root to a value. The commitment package checks membership or non-membership proofs against the chain root and rejects empty, incomplete, or mismatched proof inputs.

The proof model supports nested trees. A counterparty supplies a path to the provable IBC store, and the IBC path is joined to it before verification. The verifier checks each proof from the leaf tree toward the root, then compares the resulting root with the trusted client root. The same commitment interfaces expose the v1 root and prefix types with the v2 path type through the protobuf registry.

When a relayer asks for a proof, modules/…/query.go queries the IBC store with proof enabled. It converts the returned proof operations into the IBC Merkle proof format and returns the value, proof, and proof height. Counterparty clients use the matching host path and commitment prefix to verify that result.

References: modules/…/codec.go• L12-38 modules/…/merkle.go• L23-105 modules/…/merkle.go• L107-220 modules/…/commitment.pb.go• L25-58 modules/…/query.go• L19-70

State lifecycle

Core state moves through creation, handshake, packet processing, and export. Each submodule owns the state for its lifecycle, while the top-level core module calls the submodules in an order that preserves their dependencies.

Genesis state

Genesis is the import and export boundary for the full IBC state. The top-level GenesisState combines client v1, client v2, connection, channel v1, and channel v2 state. Validation runs before initialization so identifiers, sequences, parameters, and packet data have a valid starting point.

Initialization follows this order:

  1. Client state, client metadata, consensus state, parameters, and the next client sequence are restored.
  2. Client v2 counterparty information is restored in client stores.
  3. Connections, client-to-connection paths, parameters, and the next connection sequence are restored.
  4. The sentinel localhost connection is created for the local connection identity.
  5. v1 channels and their packet records and sequences are restored.
  6. v2 packet records, asynchronous packets, and send sequences are restored.

Export reverses this view by iterating stored state and assembling the same top-level sections. This makes an exported chain state usable as the input to a later chain start or upgrade.

References: modules/…/genesis.go• L17-52 modules/…/genesis.go• L17-35 modules/…/genesis.go• L16-74 modules/…/genesis.go• L14-42 modules/…/genesis.go• L12-35 modules/…/genesis.go• L12-51 modules/…/genesis.go• L14-74

State migrations

Migrations preserve the meaning of stored IBC state as the module changes. The runtime module registers migrations for its consensus-version changes. The current module reports consensus version 8 and registers transitions for a removed parameter upgrade, stateless localhost clients, and the channel state change from version 7 to 8.

The exported genesis migration for v7 also handles legacy client state. It updates solo machine client state from protobuf v2 to v3, removes solo machine consensus state, and removes localhost client state. The connection genesis step still creates the sentinel localhost connection, so a removed client should not be confused with the local connection identity required by the current runtime.

References: modules/…/module.go• L155-172 modules/…/module.go• L191-197 modules/…/genesis.go• L14-43 modules/…/genesis.go• L14-79 modules/…/genesis.go• L19-26

Ante integration

The IBC ante decorator prevents relayers from paying for packet transactions that no longer have work to perform. It runs the core verification subset during CheckTx and ReCheckTx, then lets the normal SDK transaction path process the transaction.

When a transaction contains only packet messages and client-update messages, the decorator counts packet messages that resolve to no-op results. It rejects the transaction only when every packet message is redundant. A transaction with another message type continues so that useful non-IBC work is not blocked. DeliverTx and simulation skip this redundancy check.

The decorator checks both v1 packet messages and v2 receive, acknowledgement, and timeout messages. It uses a cached context for verification so the mempool check does not leak state changes. Client update checks verify an active client and update its state while omitting misbehaviour checks in this early transaction stage.

flowchart LR
    T[Relayer transaction] --> A[IBC ante decorator]
    A --> C{CheckTx or ReCheckTx?}
    C -->|No| N[Next SDK ante handler]
    C -->|Yes| P[Inspect packet and client-update messages]
    P --> V[Run core verification in cached context]
    V --> R{All packet messages redundant?}
    R -->|Yes| X[Reject as redundant]
    R -->|No| N

References: modules/…/ante.go• L19-41 modules/…/ante.go• L92-140 modules/…/ante.go• L143-211 simapp/ante.go• L37-53

Runtime interfaces

Core exposes two kinds of interfaces: SDK services for external messages and queries, and callback contracts for application modules. These interfaces keep protocol checks in core while allowing application logic to remain in the module that owns a port.

Message and query APIs

The core keeper serves message interfaces for client v1, client v2, connections, and channels. A message handler first performs the protocol action through the relevant sub-keeper. Channel and packet handlers then use the port router to call application logic when the protocol step needs it. The v2 channel service is served by its channel keeper while using the same runtime wiring.

Client creation records its creator for the v2 counterparty registration step. That registration is limited to the creator of the client and can happen only once; it stores counterparty information and starts the v2 send sequence. Protected changes, such as client recovery, software upgrades, and parameter updates, require the configured module authority.

The module registers matching gRPC message and query servers. It also registers gRPC gateway routes for client, connection, channel v1, and channel v2 queries. Command-line transaction and query commands are exposed through the module's basic SDK interface.

References: modules/…/msg_server.go• L23-29 modules/…/msg_server.go• L30-77 modules/…/msg_server.go• L80-102 modules/…/msg_server.go• L625-663 modules/…/module.go• L88-120 modules/…/module.go• L141-153

Application callback APIs

The v1 IBCModule callback contract covers channel open and close events, packet receive, packet acknowledgement, and packet timeout. The ICS4Wrapper contract covers packet sending and acknowledgement writes. Applications must use the wrapper so middleware remains in the packet path.

The v2 api.IBCModule contract uses client identifiers and payloads. It gives applications callbacks for send, receive, timeout, and acknowledgement events. An optional packet-data unmarshaler lets middleware ask the base application to decode payload data.

References: modules/…/module.go• L13-18 modules/…/module.go• L83-117 modules/…/module.go• L11-80

Keeper query APIs

Sub-keepers provide read and write access for protocol state and aggregate views for export and queries. Client APIs return client and consensus state or verify membership and non-membership proofs. Connection APIs return connection ends and client links. Channel APIs return channel state, packet state, sequence values, and the client connected to a channel.

These APIs are state-aware rather than application-aware. The core keeper owns protocol facts. The application callback owns application state changes that result from a valid protocol event.

References: modules/…/module.go• L13-53 modules/…/module.go• L83-152 modules/…/module.go• L11-80 modules/…/keeper.go• L103-160 modules/…/keeper.go• L336-362 modules/…/keeper.go• L64-128 modules/…/keeper.go• L82-115

Module wiring

The Cosmos SDK application wires IBC by giving the core keeper a codec, an IBC KV store service, an upgrade keeper, and an authority. The app then registers application routes and light-client routes before constructing the IBC app module.

Application routers

The v1 router maps a module name to a callback stack. Middleware can sit above an application and intercept both directions of packet flow. The router is sealed after registration so the callback graph cannot change during normal runtime.

The v2 router maps a port identifier or a non-overlapping port prefix to a v2 callback module. Direct routes take priority over prefix routes. This gives an application a stable way to receive v2 events while preventing ambiguous route matches.

flowchart LR
    S[Application send] --> M[IBC middleware stack]
    M --> K[Core channel keeper]
    K --> C[Counterparty proof and packet state]
    C --> K2[Core receive or acknowledgement path]
    K2 --> M2[Route by port or port prefix]
    M2 --> A[Application callback]

References: modules/…/router.go• L13-80 modules/…/router.go• L13-130 modules/…/keeper.go• L77-91 simapp/app.go• L330-332 simapp/app.go• L399-450

Light-client routes

The client keeper routes a client identifier to the light-client module that understands its client type. The core app registers the supported light-client modules after creating the core keeper. Client parameters then control which client types are allowed for use.

This separation lets core store and route generic client state while each light-client module supplies client-specific verification and update behavior. The upgrade keeper remains available through the client keeper for IBC software upgrade state.

References: modules/…/keeper.go• L28-90 modules/…/keeper.go• L364-381 modules/…/params.go• L14-49 simapp/app.go• L452-462

App module lifecycle

The IBC AppModule connects the keeper to the Cosmos SDK module manager. It validates and loads genesis, exports state, registers services and migrations, runs the client begin-block action, and reports the module name and consensus version to the runtime.

The application must provide the keeper to NewAppModule, include the module in its module manager, and register the routers before transactions use application callbacks. A missing route prevents channel or packet messages from reaching their application module. A missing light-client route prevents client identifiers from being updated or used for proof checks.

The module's interface registry also exposes the commitment root, prefix, and path types used by protobuf Any values. This keeps message and client state encoding aligned with the proof types used by the runtime.

References: modules/…/module.go• L53-85 modules/…/module.go• L118-121 modules/…/module.go• L123-197 modules/…/codec.go• L12-38 simapp/app.go• L488-533

Change checklist

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

  • Keep the top-level genesis sections aligned with every sub-keeper included by NewKeeper, including v2 state.
  • Keep host path builders, proof query paths, and counterparty proof paths aligned. A path change can break cross-chain verification without a local type error.
  • Keep v1 send-sequence behavior aligned with the v2 key contract while both packet models share the runtime.
  • When changing a consensus version or stored state shape, update both registered runtime migrations and exported-genesis migrations where the old state can still be imported.
  • When changing message or query services, update the corresponding gateway registration, interface registration, and app-module wiring.
  • When adding an application or middleware route, preserve the callback stack order and seal the router only after all routes are present.
  • When changing packet message handling, keep the ante redundancy result consistent with the message server result for v1 and v2 packets.
  • When changing commitment paths or protobuf commitment types, preserve both prefix-versus-complete path validation and the external v2 packet-key contract.
  • When changing client creation or counterparty registration, preserve creator authorization and the one-time initialization of the v2 send sequence.

Important failure modes

TriggerConsequenceGuard
The core keeper receives an empty upgrade keeper or blank authority.Keeper construction panics and the app cannot start.NewKeeper rejects both values before creating sub-keepers.
A channel or packet message has no route for its port.The protocol action cannot call application logic and returns an invalid-route error.Register every application route before sealing the router.
A route is added after the v1 router is sealed or collides with an existing v2 route.App startup panics or the callback graph becomes ambiguous.Register routes once and use non-overlapping direct and prefix routes.
Genesis omits a required sequence or uses a sequence not greater than an imported identifier.State import fails or a later generated identifier can collide.Validate genesis and restore the next sequence with each submodule.
A relayer submits only packet messages that are already processed.The transaction wastes fees unless the mempool rejects it.The IBC ante decorator returns the redundant-transaction error when all packet messages are no-ops.
A proof query uses an unsupported early height or the wrong IBC store key.The returned proof is unavailable or cannot verify on the counterparty.Query the IBC store with proof enabled at the supported IAVL-aligned height and use the host path contract.
A complete Merkle path contains an empty key or its proof count does not match the path and proof specs.Proof verification fails even when the underlying value exists.Validate the path form and require matching non-empty proof inputs before verification.
A different signer tries to register a client's counterparty or registers it twice.The v2 client setup is rejected and packet sending is not initialized through that path.Match the stored client creator and allow only one counterparty registration.

Key files

FileWhy to read it
modules/…/keeper.goShows the top-level keeper composition, authority, upgrade dependency, and router boundaries.
modules/…/msg_server.goShows how SDK messages become protocol actions, authorization checks, and application callbacks.
modules/…/module.goShows the Cosmos SDK module lifecycle, services, migrations, genesis, begin-block, and interface registration.
modules/…/keeper.goShows client state ownership, light-client routing, proof verification entry points, and client-store isolation.
modules/…/keeper.goShows connection state, client links, connection identifiers, and the sentinel local connection.
modules/…/keeper.goShows channel state, packet commitments, packet evidence, sequences, and the connection-to-client link.
modules/…/keeper.goShows v2 packet state, asynchronous packets, aliases, and v2 routing dependencies.
modules/…/packet_keys.goDefines the stable v1 host paths for packet records and sequences.
modules/…/packet_keys.goDefines the v2 packet key contract shared with external implementations.
modules/…/merkle.goShows how local roots, prefixes, paths, and membership proofs support counterparty verification.
modules/…/genesis.goShows how the core module delegates genesis import and export across its submodules.
modules/…/genesis.goShows the exported-state migration for legacy client data.
modules/…/ante.goShows the mempool guard for redundant relayer transactions.
modules/…/router.goShows the v1 application route and middleware boundary.
modules/…/router.goShows the v2 direct and prefix route model.
simapp/app.goShows representative Cosmos SDK wiring for the keeper, routers, light clients, and app module.