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 / packet-forwarding

Packet forwarding middleware

Packet forwarding middleware extends an IBC fungible-token transfer across a chain of intermediate hops. It reads routing instructions from the transfer memo, moves the tokens into a controlled intermediate account, starts the next transfer, and delays the result on the original hop until the full route finishes.

System role

The middleware sits between the IBC channel layer and the token transfer application. It applies only to ICS-20 fungible-token packets that contain forwarding data. Packets without that data continue through the normal transfer path. See IBC application modules for the wider application-module model.

Packet forwarding supports these outcomes:

  • A transfer can cross several chains that do not have a direct connection.
  • A token can travel back through its source path before it moves to its final destination.
  • The source chain receives one final success or error acknowledgement for the complete route.
  • A timed-out hop can retry before the route fails and refunds the earlier hop.

Forwarding metadata

Forwarding metadata is a forward object in the transfer memo. Each object describes one next hop. The next value carries another forwarding object for the following hop.

Hop instructions

The current hop uses the following information:

FieldPurpose
receiverAccount that should receive the tokens on the next chain.
portIBC application port for the next transfer.
channelIBC channel for the next transfer.
timeoutOptional time allowed for this hop. The configured default applies when it is absent or non-positive. A duration string or a numeric nanosecond value is accepted.
retriesOptional retry count for this hop. The middleware setting applies when it is absent. A value of zero disables retries for that hop.
nextOptional nested instructions to place in the next transfer memo.

The middleware validates the receiver, port, and channel before it moves funds. It accepts nested next data as an object or as JSON text. Invalid forwarding data produces an error acknowledgement instead of an ordinary transfer.

References: modules/…/forward.go• L24-48 modules/…/forward.go• L94-175 modules/…/forward.go• L178-209

Intermediate receiver

An intermediate chain does not credit the user-selected receiver directly. It derives a deterministic account from the incoming channel and sender, then uses that account as the sender of the next transfer. This prevents forwarding metadata from directing the tokens to an arbitrary account on an intermediate chain.

The original receiver is used only for the next hop. The intermediate account is therefore a temporary custody point that links receipt of one packet to creation of the next packet.

References: modules/…/ibc_middleware.go• L106-114 modules/…/ibc_middleware.go• L163-222 modules/…/ibc_middleware_test.go• L251-260

Multi-hop transfer flow

Each forwarding chain consumes the instructions for its current hop and passes only the nested instructions onward. The route behaves as a sequence of ordinary IBC transfers, while the middleware links their packet and acknowledgement lifecycles.

This diagram shows how a transfer from chain A reaches chain D through two intermediate forwarding decisions.

sequenceDiagram
    participant A as Chain A
    participant B as Chain B
    participant C as Chain C
    participant D as Chain D
    A->>B: ICS-20 transfer with forward(B→C, next=C→D)
    B->>B: Receive into deterministic intermediate account
    B->>C: ICS-20 transfer with next(C→D)
    C->>C: Receive into deterministic intermediate account
    C->>D: ICS-20 transfer to final receiver
    D-->>C: Acknowledgement
    C-->>B: Acknowledgement
    B-->>A: Final acknowledgement

When a packet arrives, the middleware first checks whether it has forwarding data. If it does, it receives the funds through the underlying transfer application with the receiver replaced by the deterministic intermediate account. It then starts the next transfer with the remaining memo and records the original packet details for later refund or acknowledgement handling. If the packet is not a fungible-token transfer or has no forwarding data, the underlying application handles it normally.

References: modules/…/ibc_middleware.go• L116-208 modules/…/keeper.go• L183-255 modules/…/ibc_middleware_test.go• L109-249

Denomination path

The middleware keeps the token denomination consistent with the direction of travel. When the incoming denomination carries the counterparty channel prefix, the current hop removes that prefix. Otherwise, it adds the current port and channel to the denomination trace. This supports both multi-hop delivery and path unwinding when a token returns toward its source.

References: modules/…/ibc_middleware.go• L88-104 modules/…/ibc_middleware.go• L175-185 modules/…/keeper.go• L114-170

Acknowledgement lifecycle

Forwarding uses an asynchronous acknowledgement: the intermediate chain does not acknowledge the incoming packet when it starts the next hop. It waits for the downstream packet to succeed or fail, then sends the corresponding result back over the packet that brought the tokens to the intermediate chain.

The lifecycle has two paths:

  1. A downstream success acknowledgement is passed back through each stored hop until it reaches the source chain.
  2. A downstream error acknowledgement causes the middleware to restore or destroy the local escrowed funds as needed, then sends an error acknowledgement upstream.

The stored in-flight record keeps the original packet and refund route. The middleware removes that record when it handles a downstream acknowledgement, so a completed hop is not processed again.

References: modules/…/ibc_middleware.go• L234-280 modules/…/keeper.go• L103-173 modules/…/keeper.go• L323-361

Timeout and retry behavior

Each forwarded transfer receives a timeout for its current hop. The timeout can come from that hop's metadata or from the chain-wide forwarding default. A retry count can also be set per hop; otherwise the middleware uses its configured retry count.

When a downstream packet times out, the middleware first handles the local timeout so that the transfer application can refund the current hop. If retries remain, it sends the same forwarding step again and reduces the stored retry count. When no retries remain, it stops forwarding and returns an error acknowledgement through the earlier hops. The source chain then gets a terminal failure result and can complete its normal refund process.

This diagram shows a timeout that retries once and then returns failure to the source chain.

sequenceDiagram
    participant A as Source chain
    participant B as Forwarding chain
    participant C as Next chain
    A->>B: Forwarded transfer
    B->>C: Forwarded transfer
    C--xB: Timeout
    B->>B: Refund timed-out local hop
    B->>C: Retry forwarded transfer
    C--xB: Timeout
    B-->>A: Error acknowledgement
    A->>A: Refund source transfer

An in-flight packet keeps the retry count, timeout, original sender, and refund packet data. This state also means the source packet remains pending while the downstream hop is still active. The timeout path first refunds the timed-out local transfer before it attempts a retry. The timeout tests confirm that a failed final hop returns the funds through the chain of escrows and leaves those escrows empty after the final acknowledgement.

References: modules/…/ibc_middleware.go• L187-208 modules/…/ibc_middleware.go• L283-330 modules/…/keeper.go• L221-240 modules/…/keeper.go• L258-320 modules/…/ibc_middleware_test.go• L210-249

Middleware relationships

The forwarding middleware is part of the transfer application stack. On packet receipt, IBC passes the packet through the outer middleware before it reaches token transfer handling. On packet send, forwarding wraps the channel send path so that each generated transfer remains inside the same IBC stack.

The middleware is designed for token transfer packets rather than as a general packet router. This makes its behavior relevant to token transfer. It is different from general message passing and interchain accounts, which serve broader application messaging goals. Its delayed acknowledgement can affect callbacks and rate limiting. The packet events and timeout proofs still follow the broader channel and packet lifecycle.

References: modules/…/ibc_middleware.go• L332-362 modules/…/keeper.go• L364-377 modules/…/ibc_middleware_test.go• L262-273

Module state and upgrades

Forwarding keeps state only for transfers that are still in progress. The module exports and restores this state with the chain's genesis data so an in-flight route can continue across a chain restart or state export.

In-flight state

An in-flight record identifies the original packet, its refund route, its sender, its timeout values, and the retries that remain. The record is created when a new forwarded packet is sent and is removed after a downstream acknowledgement or terminal timeout result.

Genesis export includes all active records. Genesis initialization restores them without changing the route they represent.

References: modules/…/keeper.go• L221-245 modules/…/genesis.go• L11-39 modules/…/genesis.pb.go• L26-88

State migration

Module upgrades preserve forwarding safety as the stored state changes. The version 2 to 3 migration recalculates transfer escrow totals from escrow account balances. The version 3 to 4 migration removes old parameter state and converts active in-flight records to the current refundable form. It stops when it finds an invalid or nonrefundable record instead of silently changing it.

References: modules/…/module.go• L101-109 modules/…/migrator.go• L23-33 modules/…/migrate.go• L13-56 modules/…/migrate.go• L22-77

Change checklist

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

  • Keep the memo field names and nested next shape aligned with every client that builds forwarding transfers.
  • Keep the in-flight record compatible with acknowledgement, timeout, genesis, and migration paths. A mismatch can strand funds or prevent the source packet from completing.
  • Keep the per-hop timeout and retry defaults aligned with the route assumptions used by relayers and applications. A retry value of zero must remain distinct from an omitted value.
  • Keep the transfer stack order aligned with middleware that observes packet receipt or acknowledgement. A changed order can alter when callbacks run.
  • Keep denomination-trace changes aligned with refund escrow selection. A change can turn a refund into a burn or leave supply accounting incorrect.
  • Treat migration rejection of invalid or nonrefundable records as an upgrade contract. Do not remove the validation without a safe recovery path for active routes.

Important failure modes

TriggerConsequenceGuard
Forwarding data is malformed or lacks a receiver, port, or channel.The packet gets an error acknowledgement at the receiving chain.Parse and validate each hop before receiving and forwarding funds.
The next transfer cannot be created.The current hop returns an error and the earlier hop can refund the transfer.Treat the forwarding error as terminal for that route.
A downstream packet times out with retries remaining.The local hop is refunded and the transfer is sent again.Store the retry count and decrement it for each retry.
A downstream packet times out after the retry limit.No more forward packets are sent. An error acknowledgement returns toward the source.Stop at zero retries and use the stored refund route.
A downstream error acknowledgement arrives for a forwarded packet.Escrowed funds are moved, minted, or burned according to the denomination path before the error is relayed.Use the original packet and denomination trace from the in-flight record.
An intermediate receiver is used as a normal user account.Tokens could be sent to an account that does not represent the forwarding route.Derive the receiver from the incoming channel and sender.
A chain upgrade finds invalid or nonrefundable in-flight state.The state migration stops instead of producing a route with unsafe refund behavior.Repair or explicitly resolve active records before retrying the upgrade.

Key files

FileWhy to read it
modules/…/ibc_middleware.goMain packet receive, acknowledgement, timeout, and middleware-stack behavior.
modules/…/forward.goForwarding memo shape, validation, nested-hop parsing, and duration handling.
modules/…/keeper.goForward transfer creation, in-flight tracking, retry decisions, and refund acknowledgement behavior.
modules/…/genesis.goExport and restore behavior for routes that are still in flight.
modules/…/module.goModule lifecycle and registered state migrations.
modules/…/migrate.goEscrow total recovery during the version 2 to 3 upgrade.
modules/…/migrate.goValidation and conversion of stored in-flight state during the version 3 to 4 upgrade.
modules/…/ibc_middleware_test.goMiddleware behavior for ordinary transfers, forwarding, and acknowledgement flow.
modules/…/migrator_test.goUpgrade behavior for escrow totals and active in-flight records.
modules/…/forward_test.goSupported metadata forms and the validation boundary for forwarding memos.