callbacks 中间件与 IBC 应用集成。下文面向基于 Cosmos SDK 构建的开发者,仅适用于 Cosmos SDK 链。
callbacks 中间件是 IBC 中间件接口的一种精简且无状态的实现。它没有 keeper,也不会存储任何状态。它只是将 IBC 中间件消息路由到相应的回调函数,而该函数由次级应用实现。因此,它不需要注册为模块,也不需要添加到模块管理器中。它只需要被加入 IBC 应用栈。
前置阅读
顾名思义,callbacks 中间件扮演的是 IBC 中间件的角色,因此链开发者必须对其进行配置,以便正确路由和处理 IBC 消息。
对于 Cosmos SDK 链,这一设置通过 app/app.go 文件完成;模块会在其中按顺序构建和配置,以引导区块链应用启动。
使用 callbacks 中间件配置应用栈
如 IBC 中间件开发 中所述,应用栈可以由多个中间件或不包含任何中间件组成,这些中间件会嵌套一个基础应用。
这些层共同构成完整的应用逻辑,使开发者能够构建可组合且灵活的 IBC 应用栈。
例如,应用栈可以只是一个像 transfer 这样的基础应用;但如果同一应用栈结合 packet-forward-middleware 与 callbacks 组成,则会通过先由 callbacks 模块封装、再由 packet-forward-middleware 封装的方式,对 transfer 基础应用进行两次嵌套。
callbacks 中间件还要求接收回调的次级应用实现 ContractKeeper。wasmd 的 contract keeper 已在这里实现,并以 WasmKeeper 的名称被引用。
Transfer
下面展示了如何使用transfer、packet-forward-middleware 和 callbacks 创建应用栈的示例。如果你不打算使用 packet-forward-middleware,可以将其省略。
以下 transferStack 在 app/app.go 中配置,并被添加到 IBC Router。
内联注释描述了数据包在应用栈与 IBC 核心之间的执行流程。
Learn how to integrate the callbacks middleware with IBC applications. The following document is intended for developers building on top of the Cosmos SDK and only applies for Cosmos SDK chains. The callbacks middleware is a minimal and stateless implementation of the IBC middleware interface. It does not have a keeper, nor does it store any state. It simply routes IBC middleware messages to the appropriate callback function, which is implemented by the secondary application. Therefore, it doesn’t need to be registered as a module, nor does it need to be added to the module manager. It only needs to be added to the IBC application stack.
Pre-requisite Readings
The callbacks middleware, as the name suggests, plays the role of an IBC middleware and as such must be configured by chain developers to route and handle IBC messages correctly. For Cosmos SDK chains this setup is done via theapp/app.go file, where modules are constructed and configured in order to bootstrap the blockchain application.
Configuring an application stack with the callbacks middleware
As mentioned in IBC middleware development an application stack may be composed of many or no middlewares that nest a base application. These layers form the complete set of application logic that enable developers to build composable and flexible IBC application stacks. For example, an application stack may just be a single base application liketransfer, however, the same application stack composed with packet-forward-middleware and callbacks will nest the transfer base application twice by wrapping it with the callbacks module and then packet forward middleware.
The callbacks middleware also requires a secondary application that will receive the callbacks to implement the ContractKeeper. The wasmd contract keeper has been implemented here and is referenced as the WasmKeeper.
Transfer
See below for an example of how to create an application stack usingtransfer, packet-forward-middleware, and callbacks. Feel free to omit the packet-forward-middleware if you do not want to use it.
The following transferStack is configured in app/app.go and added to the IBC Router.
The in-line comments describe the execution flow of packets between the application stack and IBC core.