Cosmos SDK 是一个以开发者为先、用于构建自定义区块链的框架。本教程系列将向你展示如何从零开始构建一个模块,将其接入到一条链中,并在本地运行,整个过程只需几分钟。 完成后,你将获得:
  • 一条可在本机运行的 Cosmos SDK 链
  • 一个由你亲手构建并接入链中的自定义模块
  • 对模块、keeper、message 和 query 如何协同工作的清晰认知
本系列从零开始;你无需任何 Cosmos SDK 经验也可以跟着完成。

示例仓库

本系列中的所有教程都基于 cosmos/example。这是一个围绕自定义 x/counter 模块构建的 Cosmos SDK 参考链。 该仓库有两个主要分支:
  • main:包含完整的链,并已接入完整的 x/counter 模块。该分支用于快速开始指南。
  • tutorial/start:与上述链相同,但不包含 counter 模块。x/counter 目录及其在应用中的接线都被移除,因此你可以通过跟随教程从零开始构建该模块。
如果你想边学边自己构建模块,请从 tutorial/start 开始。如果你想先浏览最终完成的实现,请使用 main。

本系列包含什么

  1. 前置条件:安装 Go、Make、Docker 和 Git。克隆仓库,并熟悉项目目录结构。
  2. 快速开始:在几分钟内构建并运行这条链。提交一笔交易,查询结果,并在亲手构建之前先看看 counter 模块的实际运行效果。
  3. 从零开始构建模块:逐步构建一个最小化的 counter 模块:proto 定义、keeper、message server、query server,以及应用接线。如果你想理解一个模块是如何组装起来的,请从这里开始。
  4. 完整模块讲解:逐步讲解 main 分支上的完整 x/counter 实现。涵盖在最小模块之上新增的全部内容:params、受治理控制的 authority、校验、费用、sentinel errors、telemetry、AutoCLI、simulation、block hooks,以及完整的单元测试套件。
  5. 运行与测试:学习完整的开发工作流,包括运行本地区块链、使用 CLI,以及三层测试体系的实践方式:单元测试、端到端测试和 simulation。

The Cosmos SDK is a developer-first framework for building custom blockchains. This tutorial series shows you how to build a module from scratch, wire it into a chain, and run it locally, all in minutes. By the end, you will have:
  • A working Cosmos SDK chain running on your machine
  • A custom module you built yourself, wired into the chain
  • A clear mental model of how modules, keepers, messages, and queries fit together
This series starts from zero; you don’t need any prior Cosmos SDK experience to follow along.

The example repo

All tutorials in this series are based on cosmos/example, a reference Cosmos SDK chain built around a custom x/counter module. The repo has two main branches:
  • main: the complete chain with the full x/counter module wired in. This is used in the Quickstart guide.
  • tutorial/start: the same chain without the counter module. The x/counter directory and its app wiring are stripped out so you can build the module from scratch by following the tutorial.
If you want to follow along and build the module yourself, start from tutorial/start. If you want to browse the finished implementation first, use main.

What’s in this series

  1. Prerequisites: Install Go, Make, Docker, and Git. Clone the repo and get familiar with the layout.
  2. Quickstart: Build and run the chain in minutes. Submit a transaction, query the result, and see the counter module in action before you build it yourself.
  3. Build a Module from Scratch: Build a minimal counter module step by step: proto definitions, keeper, message server, query server, and app wiring. Start here if you want to understand how a module comes together.
  4. Full Module Walkthrough: Walk through the complete x/counter implementation on main. Covers everything added on top of the minimal module: params, governance-gated authority, validation, fees, sentinel errors, telemetry, AutoCLI, simulation, block hooks, and a full unit test suite.
  5. Run and Test: Learn the full development workflow: running a local chain, using the CLI, and working with the three layers of testing: unit tests, end-to-end tests, and simulation.