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

refactor!: extract nft protos #15442

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ When extracting a package to its own go modules, some extra steps are required,
* Add `sonar-projects.properties` (see math [sonar-projects.properties](./math/sonar-projects.properties) for example)
* Add a GitHub Workflow entry for running the scans (see [test.yml](.github/workflows/test.yml))
* Ask the team to add the project to SonarCloud
* (optional) When the migrated package was a SDK module, migrate the protos to the new package.
* Refer to [this PR](https://github.com/cosmos/cosmos-sdk/pull/15442) for an example.
* (optional) Configure a `cosmossdk.io` vanity url by submitting a PR to [cosmos/vanity](https://github.com/cosmos/vanity).

## Protobuf
Expand Down
20 changes: 20 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ The SDK is in the process of removing all `gogoproto` annotations.
The `gogoproto.goproto_stringer = false` annotation has been removed from most proto files. This means that the `String()` method is being generated for types that previously had this annotation. The generated `String()` method uses `proto.CompactTextString` for _stringifying_ structs.
[Verify](https://github.com/cosmos/cosmos-sdk/pull/13850#issuecomment-1328889651) the usage of the modified `String()` methods and double-check that they are not used in state-machine code.

#### Buf

The SDK has been using the buf registry for protobuf versioning.
As the SDK is starting to extract its core modules as standalone go modules, the buf registry (`buf.build/cosmos/cosmos-sdk`) does not contain the protobufs of the extracted modules. These lives in their own buf registry (`buf.build/mods/{module}`).

When using protobufs types from extracted modules, the following must be changed in your application's `buf.yaml`:

```diff
version: v1
name: buf.build/user/my-chain
deps:
- buf.build/cosmos/cosmos-proto
- buf.build/cosmos/gogo-proto
- buf.build/cosmos/cosmos-sdk
+ # Extracted SDK Modules
+ - buf.build/mods/nft
```

Where `nft` is the name of the extracted module.

### SimApp

#### Module Assertions
Expand Down
496 changes: 373 additions & 123 deletions client/docs/swagger-ui/swagger.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contrib/devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $(RUNSIM):
@go install github.com/cosmos/tools/cmd/[email protected]

tools-clean:
rm -f $(STATIK) $(GOLANGCI_LINT) $(RUNSIM)
rm -f $(GOLANGCI_LINT) $(RUNSIM)
rm -f tools-stamp

.PHONY: tools-clean runsim
2 changes: 1 addition & 1 deletion contrib/githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ f_check_cmds
if [[ $STAGED_GO_FILES != "" ]]; then
f_echo_stderr "[pre-commit] fmt'ing staged files..."
for file in $STAGED_GO_FILES; do
if [[ $file =~ vendor/ ]] || [[ $file =~ client/docs/statik/ ]] || [[ $file =~ tests/mocks/ ]] || [[ $file =~ \.pb\.go ]]; then
if [[ $file =~ vendor/ ]] || [[ $file =~ tests/mocks/ ]] || [[ $file =~ \.pb\.go ]]; then
continue
fi

Expand Down
39 changes: 21 additions & 18 deletions docs/docs/building-modules/11-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,6 @@ This document outlines the recommended structure of Cosmos SDK modules. These id

A typical Cosmos SDK module can be structured as follows:

```shell
proto
└── {project_name}
   └── {module_name}
   └── {proto_version}
      ├── {module_name}.proto
      ├── event.proto
      ├── genesis.proto
      ├── query.proto
      └── tx.proto
```

* `{module_name}.proto`: The module's common message type definitions.
* `event.proto`: The module's message type definitions related to events.
* `genesis.proto`: The module's message type definitions related to genesis state.
* `query.proto`: The module's Query service and related message type definitions.
* `tx.proto`: The module's Msg service and related message type definitions.

```shell
x/{module_name}
├── client
Expand All @@ -53,6 +35,19 @@ x/{module_name}
├── module
│   └── module.go
│   └── abci.go
├── proto
│  ├── {project_name}
│   │  └── {module_name}
│   │ ├── {proto_version}
│   │    │ ├── {module_name}.proto
│   │     │ ├── event.proto
│   │     │ ├── genesis.proto
│   │     │ ├── query.proto
│   │    │ └── tx.proto
│   │ └── module
│   │ └── {proto_version}
│   │    └── module.proto
│ └── buf.yaml
├── simulation
│   ├── decoder.go
│   ├── genesis.go
Expand Down Expand Up @@ -80,6 +75,14 @@ x/{module_name}
* `keeper/`: The module's `Keeper` and `MsgServer` implementation.
* `module/`: The module's `AppModule` and `AppModuleBasic` implementation.
* `abci.go`: The module's `BeginBlocker` and `EndBlocker` implementations (this file is only required if `BeginBlocker` and/or `EndBlocker` need to be defined).
* [`proto/`](https://docs.cosmos.network/main/tooling/protobuf): The module's protobufs.
* `{module_name}.proto`: The module's common message type definitions.
* `event.proto`: The module's message type definitions related to events.
* `genesis.proto`: The module's message type definitions related to genesis state.
* `query.proto`: The module's Query service and related message type definitions.
* `tx.proto`: The module's Msg service and related message type definitions.
* `module/`: The module folder in the proto folders defines a `module.proto` which contains the module [app wiring](./15-depinject.md) parameters.
* `buf.yaml` file is used to generate the module's protobuf files using [buf](https://docs.cosmos.network/main/tooling/protobuf).
* `simulation/`: The module's [simulation](./14-simulator.md) package defines functions used by the blockchain simulator application (`simapp`).
* `REAMDE.md`: The module's specification documents outlining important concepts, state storage structure, and message and event type definitions. Learn more how to write module specs in the [spec guidelines](../spec/SPEC-SPEC.md).
* The root directory includes type definitions for messages, events, and genesis state, including the type definitions generated by Protocol Buffers.
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/tooling/00-protobuf.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Next is the `proto/` directory where all of our protobuf files live. In here the

The above diagram all the files and directories within the Cosmos SDK `proto/` directory.

:::warning
Modules protobuf files are being extracted out of the root `proto/` folder to live in their own module directories.
:::

#### `buf.gen.gogo.yaml`

`buf.gen.gogo.yaml` defines how the protobuf files should be generated for use with in the module. This file uses [gogoproto](https://github.com/gogo/protobuf), a separate generator from the google go-proto generator that makes working with various objects more ergonomic, and it has more performant encode and decode steps
Expand Down
2 changes: 1 addition & 1 deletion proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 75b4300737fb4efca0831636be94e517
commit: 463926e7ee924d46ad0a726e1cf4eacd
2 changes: 2 additions & 0 deletions proto/buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ deps:
- buf.build/cosmos/cosmos-proto
- buf.build/cosmos/gogo-proto
- buf.build/googleapis/googleapis
# Required extracted SDK modules
# - buf.build/mods/nft
breaking:
use:
- FILE
Expand Down
26 changes: 17 additions & 9 deletions scripts/protoc-swagger-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@

set -eo pipefail

pwd=$(pwd)

mkdir -p ./tmp-swagger-gen
cd proto
proto_dirs=$(find ./cosmos -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
for dir in $proto_dirs; do
# generate swagger files (filter query files)
query_file=$(find "${dir}" -maxdepth 1 \( -name 'query.proto' -o -name 'service.proto' \))
if [[ ! -z "$query_file" ]]; then
buf generate --template buf.gen.swagger.yaml $query_file
fi
for i in `find . -name buf.gen.swagger.yaml -exec realpath {} \; 2> /dev/null`
do
dir=$(dirname $i)
cd $dir
proto_dirs=$(find $dir -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
for dir in $proto_dirs; do
echo "Generating swagger files for $dir"
# generate swagger files (filter query files)
query_file=$(find "${dir}" -maxdepth 1 \( -name 'query.proto' -o -name 'service.proto' \))
if [[ ! -z "$query_file" ]]; then
buf generate --template buf.gen.swagger.yaml $query_file
fi
done
done

cd ..
cd $pwd

# combine swagger files
# uses nodejs package `swagger-combine`.
# all the individual swagger files need to be configured in `config.json` for merging
Expand Down
12 changes: 7 additions & 5 deletions scripts/protocgen-pulsar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ protoc_install_gopulsar
echo "Cleaning API directory"
(cd api; find ./ -type f \( -iname \*.pulsar.go -o -iname \*.pb.go -o -iname \*.cosmos_orm.go -o -iname \*.pb.gw.go \) -delete; find . -empty -type d -delete; cd ..)

echo "Generating API module"
(cd proto; buf generate --template buf.gen.pulsar.yaml)

echo "Generate Pulsar Test Data"
(cd testutil/testdata; buf generate --template buf.gen.pulsar.yaml)
echo "Generate pulsar files"
for i in `find . -name buf.gen.pulsar.yaml -exec realpath {} \; 2> /dev/null`
do
dir=$(dirname $i)
cd $dir
buf generate --template buf.gen.pulsar.yaml
done
35 changes: 21 additions & 14 deletions scripts/protocgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@

set -e

pwd=$(pwd)

echo "Generating gogo proto code"
cd proto
proto_dirs=$(find ./cosmos ./amino -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
for dir in $proto_dirs; do
for file in $(find "${dir}" -maxdepth 1 -name '*.proto'); do
# this regex checks if a proto file has its go_package set to cosmossdk.io/api/...
# gogo proto files SHOULD ONLY be generated if this is false
# we don't want gogo proto to run for proto files which are natively built for google.golang.org/protobuf
if grep -q "option go_package" "$file" && grep -H -o -c 'option go_package.*cosmossdk.io/api' "$file" | grep -q ':0$'; then
buf generate --template buf.gen.gogo.yaml $file
fi
for i in `find . -name buf.gen.swagger.yaml -exec realpath {} \; 2> /dev/null`; do
dir=$(dirname $i)
cd $dir
proto_dirs=$(find $dir -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
for dir in $proto_dirs; do
for file in $(find "${dir}" -maxdepth 1 -name '*.proto'); do
# this regex checks if a proto file has its go_package set to cosmossdk.io/api/...
# gogo proto files SHOULD ONLY be generated if this is false
# we don't want gogo proto to run for proto files which are natively built for google.golang.org/protobuf
if grep -q "option go_package" "$file" && grep -H -o -c 'option go_package.*cosmossdk.io/api' "$file" | grep -q ':0$'; then
buf generate --template buf.gen.gogo.yaml $file
fi
done
done
done

cd ..
cd $pwd

# generate codec/testdata proto code
(cd testutil/testdata; buf generate)
Expand All @@ -29,9 +34,11 @@ cd ..
(cd baseapp/testutil; buf generate)

# move proto files to the right places
cp -r github.com/cosmos/cosmos-sdk/* ./
cp -r cosmossdk.io/** ./
rm -rf github.com cosmossdk.io
cp -r github.com/cosmos/cosmos-sdk/* ./ && rm -rf github.com
for dir in `find ./ -name cosmossdk.io -not -path '*/\.*' 2> /dev/null`; do
cp -r $dir/** $pwd
rm -r $dir
done

go mod tidy

Expand Down
2 changes: 0 additions & 2 deletions scripts/update-swagger-ui-statik.sh

This file was deleted.

Loading