变更日志
- 2019-10-22:初始草案
背景
ICS 26 - 路由模块 定义了函数handlePacketRecv。
在 ICS 26 中,路由模块被定义为位于各应用模块之上的一层,负责验证消息并将其路由到目标模块。它可以实现为一个独立模块,不过我们已经在 baseapp 中具备基于目标标识符路由消息的能力。因此,本 ADR 建议复用现有的 baseapp.router,将数据包路由到应用模块。
通常,路由模块回调包含两个独立步骤:验证与执行。这与 SDK 内部的 AnteHandler-Handler 模型相对应。我们可以在 AnteHandler 中完成验证,以减少样板式验证代码,从而提升开发者体验。
对于原子性的多消息交易,我们希望即使应用侧状态变更发生回滚,IBC 相关的状态修改仍然能够保留。一个例子是:IBC 代币发送消息后紧接着执行质押委托,后者使用前一个数据包消息接收到的代币。如果代币接收因任何原因失败,我们可能不希望继续执行该交易,但也不希望中止整笔交易,否则序列号和承诺会被回滚,通道也会被卡住。为了解决这个问题,本 ADR 提议引入新的 CodeType:CodeTxBreak。
决策
PortKeeper 将持有能力键,该键只能访问绑定到某个端口的通道。持有 PortKeeper 的实体将能够调用其上的方法,这些方法与 ChannelKeeper 上同名的方法相对应,但仅限于被允许的端口。将定义 ChannelKeeper.Port(string, ChannelChecker),以便更方便地构造具备能力安全性的 PortKeeper。这一点将在另一份 ADR 中进一步说明,当前暂时使用不安全的 ChannelKeeper。
当某个处理器返回 !Result.IsOK() 时,baseapp.runMsgs 会中断对消息的遍历循环。然而,如果 Result.IsOK() || Result.Code.IsBreak() 成立,外层逻辑仍会写入缓存存储。当 Result.Code == CodeTxBreak 时,Result.Code.IsBreak() 为真。
AnteDecorator。该 AnteDecorator 会遍历交易中包含的消息,使用类型 switch 检查消息是否包含传入的 IBC 数据包;如果包含,则验证其 Merkle 证明。
MsgUpdateClient、MsgPacket、MsgAcknowledgement、MsgTimeoutPacket 是 sdk.Msg 类型,分别对应路由模块中的 handleUpdateClient、handleRecvPacket、handleAcknowledgementPacket、handleTimeoutPacket。
RecvPacket、VerifyAcknowledgement、VerifyTimeout 的副作用将被提取到独立函数中,分别是 WriteAcknowledgement、DeleteCommitment、DeleteCommitmentTimeout,这些函数会在应用处理器执行完成后被调用。
WriteAcknowledgement 会将确认写入状态,以便对端链可以验证,并递增序列号以防止重复执行。DeleteCommitment 会删除已存储的承诺,DeleteCommitmentTimeout 则会删除承诺,并在有序通道的情况下关闭通道。
PortKeeper 上调用相应的收尾方法,以便递增序列号(对于数据包)或移除承诺(对于确认与超时)。调用这些函数意味着应用逻辑已经成功执行。
不过,处理器在调用这些方法之后仍然可以返回带有 CodeTxBreak 的 Result,这样既能持久化已经完成的状态变更,又能在遇到语义无效的数据包时阻止后续消息继续执行。这样可以确保前面 IBC 数据包的序列号已被递增(从而防止重复执行),同时不继续处理后续消息。
无论如何,应用模块都不应该返回会导致状态回滚的结果,否则通道将无法继续推进。
将引入 ChannelKeeper.CheckOpen 方法。这将取代路由模块规范中定义的 onChanOpen*。应用模块不再需要分别定义每个通道握手回调函数,而是可以通过 AppModule 提供 ChannelChecker 函数,并在顶层应用中注入到 ChannelKeeper.Port()。CheckOpen 会根据 PortID 找到正确的 ChannelChecker 并调用它;如果应用不接受,则返回错误。
ProofVerificationDecorator 将被插入到顶层应用中。让每个模块各自负责调用证明验证逻辑并不安全,因为应用可能会因疏忽而在 IBC 协议层面表现异常。
ProofVerificationDecorator 应当紧跟当前 auth.NewAnteHandler 中默认的抗女巫攻击层之后:
Packet 中创建一个类型为 []byte 的 Data 字段,接收模块可以将其反序列化为自身的私有类型。具体如何解释和处理该字段由应用模块自行决定,而不是由 IBC keeper 处理。这对于动态 IBC 至关重要。
应用侧用法示例:
状态
提议中影响
正面
- 面向开发者的接口更直观,IBC 处理器无需关心 IBC 认证
- 状态变更承诺逻辑被嵌入到
baseapp.runTx逻辑中
负面
- 无法支持动态端口,路由与
baseapp router绑定
中性
- 引入新的
AnteHandler装饰器。 - 可以通过分层端口标识符支持动态端口,详见 #5290
参考
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