Callbacks and rate-limiting middleware
The callbacks and rate-limiting middleware add policy around IBC application modules. Callbacks let packet data connect secondary applications to send, receive, acknowledgement, and timeout events. Rate limiting checks ICS-20 transfer value before it enters or leaves a configured route, then keeps that decision aligned with later acknowledgements and timeouts.
System boundary
These middleware layers sit between IBC core and an application module. They do not replace the application’s packet logic. They inspect or update packet-related state and then either pass the packet to the application or return the result required by the IBC packet lifecycle. The broader application role is described in IBC application modules. Related application flows are described in token transfer, interchain accounts, general message passing, and packet forwarding.
Application hooks
The callbacks middleware lets a packet register a secondary application for IBC lifecycle events. A source callback runs on the sending chain. A destination callback runs on the receiving chain after the application accepts the packet. The contract keeper provides the actor interface, so a secondary application can use the same packet event model without placing callback code in every application module.
| Packet event | Callback location | Effect of callback failure |
|---|---|---|
| Send | Source chain | Rejects the packet send. |
| Receive | Destination chain | Returns a failed acknowledgement for a synchronous receive. |
| Acknowledgement | Source chain | Records the callback result but does not stop acknowledgement processing. |
| Timeout | Source chain | Records the callback result but does not stop timeout processing. |
References: modules/…/README.md• L3-9 modules/…/ibc_middleware.go• L90-134 modules/…/ibc_middleware.go• L137-219 modules/…/ibc_middleware.go• L222-312 modules/…/expected_keepers.go• L14-101
Quota gates
The rate-limiting middleware adds a value gate to ICS-20 transfer packets. It checks outgoing value before the packet is sent through the lower IBC stack and checks incoming value before the application receives the packet. A denied outgoing packet returns an error. A denied incoming packet returns an error acknowledgement, which lets the normal packet lifecycle report the failure to the source chain.
The middleware also handles the later result of an allowed packet. A successful acknowledgement finalizes the recorded send. A failed acknowledgement or timeout gives the outgoing amount back to the current quota. A failed asynchronous receive can similarly give its recorded inflow back when the acknowledgement is written.
References: modules/…/ibc_middleware.go• L66-150 modules/…/packet.go• L183-323 modules/…/ibc_middleware.go• L58-169
Callback middleware
The callbacks middleware is opt-in at the packet level for packet types that expose callback data. It reads callback data through the underlying application’s packet decoder, runs the matching actor hook, and emits a source or destination callback event with the result. Some applications can add their own callback policy. For example, GMP registers a source callback from the sender even when the packet has no memo.
Callback opt-in data
A packet normally opts in by placing callback data under src_callback or dest_callback in its application data. ICS-20 and ICS-27 carry this object in their memo field. The address is required. The gas limit and calldata are optional. A source callback can also receive the packet sender when the application exposes that information.
The absence of a callback object is not an error. An object with an invalid address, gas limit, or calldata is different: it is an invalid opt-in and the matching packet hook rejects it or returns a failed receive result. An application can change this default by creating callback data outside the memo contract. ICA, for example, supports source callbacks but does not support destination callbacks.
| Field | Purpose |
|---|---|
src_callback | Enables source-chain send, acknowledgement, and timeout hooks. |
dest_callback | Enables destination-chain receive hooks. |
address | Identifies the callback actor. |
gas_limit | Sets the requested callback gas as a decimal string. |
calldata | Supplies optional callback bytes as a hex string. |
References: modules/…/callbacks.go• L19-48 modules/…/callbacks.go• L101-157 modules/…/gmp_test.go• L26-47 modules/…/ica_test.go• L24-48
Callback hook sequence
The middleware calls the underlying application at the point required by the packet lifecycle. It then calls the actor only when the packet opted in and the application result permits that callback. Synchronous receive callbacks run only after a successful application receive. Asynchronous receive callbacks wait until the application writes the acknowledgement.
The following flow shows how callback failures affect the four application hooks.
flowchart TD
A[IBC packet event] --> B{Callback data present?}
B -- No --> C[Continue application lifecycle]
B -- Yes --> D[Parse callback data]
D --> E{Valid and eligible?}
E -- No --> F[Reject send or receive]
E -- Yes --> G[Run actor callback in cached state]
G --> H{Hook type}
H -- Send --> I[Reject send on callback failure]
H -- Receive --> J[Return failed acknowledgement on callback failure]
H -- Acknowledgement or timeout --> K[Keep packet lifecycle moving and emit failure]
G -- Success --> L[Commit callback state and emit success]
Callback events identify the hook type, callback address, packet sequence, gas values, application version, and success or failure. This gives relayers and applications a result record without changing the packet data.
References: modules/…/ibc_middleware.go• L90-134 modules/…/ibc_middleware.go• L137-181 modules/…/ibc_middleware.go• L222-312 modules/…/expected_keepers.go• L16-101
Callback gas and state
Each callback runs with a gas limit bounded by the middleware maximum. The actual execution limit is also bounded by gas remaining in the transaction. The middleware keeps a separate commit limit so it can tell whether a relayer supplied enough gas for a retryable out-of-gas failure.
Callback writes use a cached context. Successful execution commits those writes. A callback error or panic discards its writes. A send callback failure is propagated as a send failure. Failures in acknowledgement and timeout callbacks are reported in events while the packet lifecycle continues. A receive callback failure becomes an error acknowledgement. If the callback uses less gas than its commit limit, the system can panic with an out-of-gas result so the callback can be retried with more gas.
References: modules/…/process.go• L16-62 modules/…/callbacks.go• L160-176 modules/…/callbacks.go• L248-251 modules/…/ibc_middleware.go• L253-266
IBC v2 callbacks
The IBC v2 callback layer applies the same source and destination behavior to v2 payloads. It uses the v2 packet result to distinguish successful, failed, and asynchronous receives. The actor interface remains compatible with the existing packet form, so the middleware presents a v1-style packet and acknowledgement to the callback keeper when needed.
For v2 asynchronous acknowledgements, the middleware first finds the stored packet and then invokes the callback. The current callback path supports one payload for this case. Contracts must also recognize the v2 error acknowledgement sentinel when they process acknowledgement bytes.
References: modules/…/ibc_middleware.go• L28-57 modules/…/ibc_middleware.go• L106-231 modules/…/ibc_middleware.go• L372-456 modules/…/ibc_middleware_test.go• L728-854
Rate-limiting middleware
The rate-limiting middleware protects a transfer route with a percentage quota over a time window. A route is identified by a denomination and the local channel or client identifier. The middleware uses the denomination form seen on the local chain, including the IBC hash for traced tokens.
Quota model
A rate limit has a path, a quota, and current flow. The quota sets separate maximum percentages for sends and receives. The flow stores the current inflow and outflow. A fixed channel value, taken from the denomination supply when the limit is created or reset, is the denominator for both percentage checks.
The check uses net movement. A receive adds to inflow after subtracting earlier outflow. A send adds to outflow after subtracting earlier inflow. This allows movement in the opposite direction to offset part of the current window. A packet is denied only when the new net movement is greater than the configured threshold.
| State | Meaning |
|---|---|
| Path | Denomination plus local channel or client identifier. |
| Quota | Send percentage, receive percentage, and window duration. |
| Flow | Inflow, outflow, and fixed channel value for the current window. |
| Whitelist pair | Sender and receiver pair that skips quota flow calculations. |
| Blacklist | Denomination that is denied before normal quota lookup. |
Blacklisted denominations are denied before the normal quota lookup. A whitelisted sender and receiver pair skips flow accounting. A missing rate limit otherwise means that the packet passes without flow accounting.
References: modules/…/msgs.go• L27-76 modules/…/flow.go• L14-56 modules/…/blacklist.go• L13-38 modules/…/whitelist.go• L13-40 modules/…/rate_limit.go• L82-127
Packet interception
The rate limiter inspects transfer data before it changes application state. For sends, it derives the local denomination and source channel, checks the outflow quota, and only then calls the lower ICS4 wrapper. For receives, it derives the local denomination and destination channel, checks the inflow quota, and only then calls the underlying transfer application.
The following flow shows the gate and the later accounting paths.
flowchart TD
A[ICS-20 send or receive] --> B[Parse local denom, channel, and amount]
B --> C{Blacklisted or quota exceeded?}
C -- Yes --> D[Reject send or return error acknowledgement]
C -- No --> E{Whitelisted or no configured limit?}
E -- Yes --> F[Continue without flow update]
E -- No --> G[Update inflow or outflow]
G --> H[Record pending packet marker]
H --> I{Packet final result}
I -- Success --> J[Remove marker]
I -- Failure or timeout --> K[Reverse recorded flow and remove marker]
An unparseable classic receive is logged and passed to the underlying application because the limiter cannot identify its amount. The application remains responsible for handling that invalid packet data. The v2 transfer path instead fails when it cannot convert the payload into the transfer form needed for the check.
References: modules/…/packet.go• L64-180 modules/…/packet.go• L183-249 modules/…/ibc_middleware.go• L66-121 modules/…/ibc_middleware.go• L58-116
Flow state
Pending packet markers connect an initial flow update to its later packet result. A send marker remains until a successful acknowledgement removes it or a failed acknowledgement or timeout reverses the outflow. A receive marker remains for an asynchronous receive until acknowledgement writing completes. A synchronous receive removes its marker after the application returns an acknowledgement.
The marker includes the route denomination as well as the channel or client and sequence. This prevents a later result from changing a different quota window or a different denomination. Reversal is applied only when the marker belongs to the current quota. The stored flow is also clamped at zero during reversal, so a stale or larger correction cannot create a negative total.
References: modules/…/pending_send.go• L15-79 modules/…/pending_send.go• L104-146 modules/…/flow.go• L59-87 modules/…/packet.go• L286-323
Quota lifecycle
Authorized messages add, update, remove, or manually reset a rate limit. Add and update start the flow at zero. An update also takes a new channel value, so its percentage base changes with the new configuration. The message rules require a non-zero duration and keep each send or receive percentage between zero and one hundred.
The chain checks an epoch at block start. When a configured duration expires, it resets the flow, refreshes the channel value, and removes pending markers for that denomination and route. A manual reset follows the same state cleanup. The module does not raise a block-processing error when a scheduled reset fails.
An upgrade from the older pending-marker format clears those markers because they did not include the denomination. It does not compensate flow values for packets that were pending at upgrade time. Those values remain charged until the next quota reset.
References: modules/…/msg_server.go• L20-98 modules/…/rate_limit.go• L82-186 modules/…/epoch.go• L43-74 modules/…/module.go• L109-143 modules/…/migrate.go• L17-35 modules/…/msgs.go• L39-76
Middleware composition
Both layers preserve the application module as the owner of packet business logic. They add checks before application calls and cleanup after packet results. Their relative order in a middleware stack controls which layer observes a packet first, so a change to stack wiring can change whether a callback sees a packet that a quota gate rejects.
Wrapper ordering
The callbacks layer wraps application hooks and uses an ICS4 wrapper for send and asynchronous acknowledgement operations. The rate limiter uses the same wrapper boundary for send and acknowledgement writing. As a result, stack configuration must preserve both the callback opt-in contract and the rate-limit accounting contract across the same packet sequence.
Applications that combine these layers should trace one packet through send, receive, acknowledgement, timeout, and asynchronous acknowledgement paths. The token transfer page explains the transfer application that supplies the packet data. The packet forwarding page is especially relevant because forwarding can create asynchronous acknowledgement paths that must undo receive flow when a forward fails.
References: modules/…/ibc_middleware.go• L58-83 modules/…/ibc_middleware.go• L269-312 modules/…/ibc_middleware.go• L111-150 modules/…/ibc_middleware.go• L132-168
Change checklist
When these files or sections are changed, remember to consider these:
- Keep callback memo keys and callback actor inputs aligned with every application packet decoder that supports callbacks.
- Keep application-specific callback rules aligned with the generic callback contract. GMP source callbacks and ICA destination callbacks are exceptions to the usual memo-driven behavior.
- Keep callback gas and retry behavior aligned between classic packets and v2 payloads.
- Keep rate-limit denomination parsing aligned with token trace changes on both send and receive paths.
- Keep pending send and receive markers aligned with denomination-scoped quota windows and with synchronous, asynchronous, acknowledgement, and timeout finalization.
- Keep quota reset logic aligned with both scheduled epochs and authorized manual reset messages.
- Check middleware stack order when changing send or acknowledgement wrappers. The order changes which policy runs before the underlying application.
- Treat pending-marker schema changes as upgrade changes. Old markers cannot identify the denomination needed for flow reversal.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Callback data is present but its address, gas limit, or calldata is malformed. | The opted-in hook is rejected instead of being silently ignored. | Validate callback data when the packet is sent and keep the same format in application decoders. |
| Send callback fails or panics. | The packet send is rejected. | Run the callback within its declared gas limit and validate the packet sender when the contract needs origin checks. |
| Receive callback fails after a successful application receive. | The destination returns a failed acknowledgement for the packet. | Treat callback failure as a failed receive and ensure the source application handles error acknowledgements. |
| A v2 asynchronous callback has no stored packet or has multiple payloads. | Acknowledgement callback processing fails. | Keep the stored packet available and use the supported single-payload path. |
| A send or receive quota is exceeded. | The send is denied or the receive returns an error acknowledgement. | Configure separate send and receive percentages against the fixed window value. |
| A transfer acknowledgement or timeout is not reconciled with its pending marker. | The current quota keeps an incorrect flow amount. | Preserve denomination-scoped marker cleanup and reversal on all final packet paths. |
| A rate-limited denomination is blacklisted. | The transfer is denied even when no normal quota record exists. | Check blacklist policy before assuming an absent quota means pass-through. |
| A legacy pending marker remains during an upgrade. | The marker cannot identify the denomination for reversal. | Clear legacy markers and expect their flow charge to remain until quota reset. |
Key files
| File | Why to read it |
|---|---|
| modules/…/README.md | States why secondary applications need packet lifecycle callbacks. |
| modules/…/ibc_middleware.go | Shows classic callback hook timing and packet lifecycle effects. |
| modules/…/ibc_middleware.go | Shows the v2 payload and acknowledgement model for callbacks. |
| modules/…/callbacks.go | Defines callback opt-in data and gas-limit decisions. |
| modules/…/process.go | Defines cached callback state, gas handling, and failure precedence. |
| modules/…/expected_keepers.go | Defines the callback actor contract and its packet-event inputs. |
| modules/…/gmp_test.go | Shows application-specific source callback behavior for GMP. |
| modules/…/ica_test.go | Shows the callback support boundary for interchain accounts. |
| modules/…/ibc_middleware.go | Shows classic packet interception and final accounting hooks. |
| modules/…/ibc_middleware.go | Shows rate limiting for v2 transfer payloads and async acknowledgements. |
| modules/…/flow.go | Shows blacklist, whitelist, quota checks, and flow updates. |
| modules/…/packet.go | Shows transfer parsing and send, receive, acknowledgement, and timeout reconciliation. |
| modules/…/pending_send.go | Shows the state that links packet results to the current quota window. |
| modules/…/rate_limit.go | Shows rate-limit creation, update, and reset behavior. |
| modules/…/epoch.go | Shows scheduled quota epoch changes. |
| modules/…/migrate.go | Shows how old pending markers are handled during upgrade. |
| modules/…/module.go | Shows module services, migration registration, and block-start reset handling. |
| modules/…/msgs.go | Defines the public quota management rules. |