Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module Structure Spec #4618

Merged
merged 8 commits into from
Jun 25, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions docs/modules/SPEC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Module Specification

This document attempts to outline the recommended structure of Cosmos SDK modules.
However, the ideas outlined here are meant to be applied as suggestions. Application
developers are encouraged to improve upon and contribute to module structure and
development design.
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

## Structure

A typical Cosmos SDK module can be structured as follows:

```shell
x/{module}
├── abci.go
├── alias.go
├── client
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about flatten client further? I mean: instead of having cli and rest as subpackages, how about promoting them to live module top level and get rid of client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I don't this that is a wise idea. They're both clients and should be grouped as such.

│   ├── cli
│   │ ├── query.go
│   │   └── tx.go
│   └── rest
│   ├── query.go
│   └── tx.go
├── exported
│   └── exported.go
├── genesis.go
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
├── handler.go
├── internal
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
│   ├── keeper
│   │   ├── invariants.go
│   │   ├── keeper.go
│   │   ├── ...
│   │   └── querier.go
│   └── types
│   ├── codec.go
│   ├── errors.go
│   ├── events.go
│   ├── expected_keepers.go
│   ├── keys.go
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
│   ├── msgs.go
│   ├── params.go
│   ├── ...
│   └── querier.go
├── module.go
├── ...
└── simulation.go
```

- `abci.go`: The module's `BeginBlocker` and `EndBlocker` implementations (if any).
- `alias.go`: The module's exported types, constants, and variables. These are mainly
to improve developer ergonomics by only needing to import a single package. Note,
there is nothing preventing developers from importing other packages from the module
(excluding`internal/`) but it is recommended that `alias.go` have everything
exposed that other modules may need. The majority of the exported values here will
typically come from `internal/` (see below).
- `client/`: The module's CLI and REST client functionality implementation and
testing.
- `exported/`: The module's exported types -- typically type interfaces. If a module
relies on other module keepers, it is expected to receive them as interface
contracts through the `expected_keepers.go` (which are detailed below) design to
avoid having a direct dependency on the implementing module. However, these
contracts can define methods that operate on and/or return types that are specific
to the contract's implementing module and this is where `exported/` comes into play.
Types defined here allow for `expected_keepers.go` in other modules to define
contracts that use single canonical types. This pattern allows for code to remain
DRY and also alleviates import cycle chaos.
- `genesis.go`: The module's genesis related business logic (e.g. `InitGenesis`).
- `handler.go`: The module's message handlers.
- `internal/`: The module's internal types and implementations. The purpose of
this package is mainly two fold. First, it signals that this package is not
intended to be used or imported anywhere outside the defining module. Secondly,
it goes hand-in-hand with `alias.go` in that it allows public types and functions
to be used internally while not being exposed outside to the outside world. This
allows for greater freedom of development while maintaining API stability.
- `internal/types`: The module's type definitions such as messages, `KVStore`
keys, parameter types, and `expected_keepers.go` contracts.
- `internal/keeper`: The module's keeper implementation along with any auxiliary
implementations such as the querier and invariants.
- `module.go`: The module's implementation of the `AppModule` and `AppModuleBasic`
interfaces.
- `simulation.go`: The module's simulation messages and related types (if any).