Changelog
- 22-10-2019: Initial Draft
Context
ICS 26 - Routing Module defines a functionhandlePacketRecv.
In ICS 26, the routing module is defined as a layer above each application module
which verifies and routes messages to the destination modules. It is possible to
implement it as a separate module, however, we already have the functionality to route
messages upon the destination identifiers in the baseapp. This ADR suggests
to utilize existing baseapp.router to route packets to application modules.
Generally, routing module callbacks have two separate steps in them,
verification and execution. This corresponds to the AnteHandler-Handler
model inside the SDK. We can do the verification inside the AnteHandler
in order to increase developer ergonomics by reducing boilerplate
verification code.
For atomic multi-message transaction, we want to keep the IBC related
state modification to be preserved even the application side state change
reverts. One of the example might be IBC token sending message following with
stake delegation which uses the tokens received by the previous packet message.
If the token receiving fails for any reason, we might not want to keep
executing the transaction, but we also don’t want to abort the transaction
or the sequence and commitment will be reverted and the channel will be stuck.
This ADR suggests new CodeType, CodeTxBreak, to fix this problem.
Decision
PortKeeper will have the capability key that is able to access only the
channels bound to the port. Entities that hold a PortKeeper will be
able to call the methods on it which are corresponding with the methods with
the same names on the ChannelKeeper, but only with the
allowed port. ChannelKeeper.Port(string, ChannelChecker) will be defined to
easily construct a capability-safe PortKeeper. This will be addressed in
another ADR and we will use insecure ChannelKeeper for now.
baseapp.runMsgs will break the loop over the messages if one of the handlers
returns !Result.IsOK(). However, the outer logic will write the cached
store if Result.IsOK() || Result.Code.IsBreak(). Result.Code.IsBreak() if
Result.Code == CodeTxBreak.
AnteDecorator for IBC packet receiving. The
AnteDecorator will iterate over the messages included in the transaction, type
switch to check whether the message contains an incoming IBC packet, and if so
verify the Merkle proof.
MsgUpdateClient, MsgPacket, MsgAcknowledgement, MsgTimeoutPacket
are sdk.Msg types correspond to handleUpdateClient, handleRecvPacket,
handleAcknowledgementPacket, handleTimeoutPacket of the routing module,
respectively.
The side effects of RecvPacket, VerifyAcknowledgement,
VerifyTimeout will be extracted out into separated functions,
WriteAcknowledgement, DeleteCommitment, DeleteCommitmentTimeout, respectively,
which will be called by the application handlers after the execution.
WriteAcknowledgement writes the acknowledgement to the state that can be
verified by the counter-party chain and increments the sequence to prevent
double execution. DeleteCommitment will delete the commitment stored,
DeleteCommitmentTimeout will delete the commitment and close channel in case
of ordered channel.
PortKeeper
in order to increase sequence (in case of packet) or remove the commitment
(in case of acknowledgement and timeout).
Calling those functions implies that the application logic has successfully executed.
However, the handlers can return Result with CodeTxBreak after calling those methods
which will persist the state changes that has been already done but prevent any further
messages to be executed in case of semantically invalid packet. This will keep the sequence
increased in the previous IBC packets(thus preventing double execution) without
proceeding to the following messages.
In any case the application modules should never return state reverting result,
which will make the channel unable to proceed.
ChannelKeeper.CheckOpen method will be introduced. This will replace onChanOpen* defined
under the routing module specification. Instead of define each channel handshake callback
functions, application modules can provide ChannelChecker function with the AppModule
which will be injected to ChannelKeeper.Port() at the top level application.
CheckOpen will find the correct ChannelChecker using the
PortID and call it, which will return an error if it is unacceptable by the application.
The ProofVerificationDecorator will be inserted to the top level application.
It is not safe to make each module responsible to call proof verification
logic, whereas application can misbehave(in terms of IBC protocol) by
mistake.
The ProofVerificationDecorator should come right after the default sybil attack
resistant layer from the current auth.NewAnteHandler:
Data field of the Packet of type []byte, which can be deserialised by the receiving module into its own private type. It is up to the application modules to do this according to their own interpretation, not by the IBC keeper. This is crucial for dynamic IBC.
Example application-side usage:
Status
ProposedConsequences
Positive
- Intuitive interface for developers - IBC handlers do not need to care about IBC authentication
- State change commitment logic is embedded into
baseapp.runTxlogic
Negative
- Cannot support dynamic ports, routing is tied to the baseapp router
Neutral
- Introduces new
AnteHandlerdecorator. - Dynamic ports can be supported using hierarchical port identifier, see #5290 for detail
References
- Relevant comment: cosmos/ics#289
- ICS26 - Routing Module