Skip to content

Commit

Permalink
feat: base-workspace template (#23)
Browse files Browse the repository at this point in the history
* feat: add base workspace template

* fix: workspace members link

* fix: add profile to workspace root in base-workspace template

* fix: failing tests on github action

* fix: script warning

* fix: skip checks on empty workspace with no packages

* chore: update schema example file to point to correct directory
  • Loading branch information
eliasmpw authored Aug 18, 2023
1 parent caaa5ef commit beed066
Show file tree
Hide file tree
Showing 26 changed files with 957 additions and 24 deletions.
36 changes: 20 additions & 16 deletions .github/scripts/test_generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,26 @@ function test-template() {
echo "This is what was generated"
ls -lA

# Check formatting
echo "Checking formatting ..."
cargo fmt -- --check

echo "Running clippy ..."
cargo clippy -- -D warnings -A clippy::derive_partial_eq_without_eq

# Debug builds first to fail fast
echo "Running unit tests ..."
cargo unit-test

echo "Creating schema ..."
cargo schema

echo "Building wasm ..."
cargo wasm
if [ "$TEMPLATE" = "base-workspace" ]; then
echo "Skipping checks because the base-workspace template is an empty workspace without any packages"
else
# Check formatting
echo "Checking formatting ..."
cargo fmt -- --check

echo "Running clippy ..."
cargo clippy -- -D warnings -A clippy::derive_partial_eq_without_eq

# Debug builds first to fail fast
echo "Running unit tests ..."
cargo unit-test

echo "Creating schema ..."
cargo schema

echo "Building wasm ..."
cargo wasm
fi
)
)
}
Expand Down
5 changes: 5 additions & 0 deletions base-workspace/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
wasm-debug = "build --target wasm32-unknown-unknown"
unit-test = "test --lib"
schema = "run --example schema"
61 changes: 61 additions & 0 deletions base-workspace/.circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
version: 2.1

executors:
builder:
docker:
- image: buildpack-deps:trusty

jobs:
docker-image:
executor: builder
steps:
- checkout
- setup_remote_docker
docker_layer_caching: true
- run:
name: Build Docker artifact
command: docker build --pull -t "cosmwasm/cw-gitpod-base:${CIRCLE_SHA1}" .
- run:
name: Push application Docker image to docker hub
command: |
if [ "${CIRCLE_BRANCH}" = "master" ]; then
docker tag "cosmwasm/cw-gitpod-base:${CIRCLE_SHA1}" cosmwasm/cw-gitpod-base:latest
docker login --password-stdin -u "$DOCKER_USER" \<<<"$DOCKER_PASS"
docker push cosmwasm/cw-gitpod-base:latest
docker logout
fi
docker-tagged:
executor: builder
steps:
- checkout
- setup_remote_docker
docker_layer_caching: true
- run:
name: Push application Docker image to docker hub
command: |
docker tag "cosmwasm/cw-gitpod-base:${CIRCLE_SHA1}" "cosmwasm/cw-gitpod-base:${CIRCLE_TAG}"
docker login --password-stdin -u "$DOCKER_USER" \<<<"$DOCKER_PASS"
docker push
docker logout
workflows:
version: 2
test-suite:
jobs:
# this is now a slow process... let's only run on master
- docker-image:
filters:
branches:
only:
- master
- docker-tagged:
filters:
tags:
only:
- /^v.*/
branches:
ignore:
- /.*/
requires:
- docker-image
11 changes: 11 additions & 0 deletions base-workspace/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.rs]
indent_size = 4
1 change: 1 addition & 0 deletions base-workspace/.genignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
meta/
83 changes: 83 additions & 0 deletions base-workspace/.github/workflows/Basic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Based on https://github.com/actions-rs/example/blob/master/.github/workflows/quickstart.yml

on: [push, pull_request]

name: Basic

jobs:

test:
name: Test Suite
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: wasm32-unknown-unknown
override: true
profile: minimal

- name: Run unit tests
uses: actions-rs/cargo@v1
with:
command: unit-test
args: --locked
env:
RUST_BACKTRACE: 1

- name: Compile WASM contract
uses: actions-rs/cargo@v1
with:
command: wasm
args: --locked
env:
RUSTFLAGS: "-C link-arg=-s"

lints:
name: Lints
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
profile: minimal
components: rustfmt, clippy

- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

- name: Run cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings

- name: Generate schema
uses: actions-rs/cargo@v1
with:
command: schema
args: --locked

- name: Verify schema
uses: tj-actions/verify-changed-files@v8
id: verify-schema
with:
files: schema/.*\.json

- name: Display changed schemas
if: steps.verify-schema.outputs.files_changed == 'true'
run: |
echo "The schema files are not in sync with the repository. Please, run 'cargo schema' to generate them again and commit the changes."
exit 1
16 changes: 16 additions & 0 deletions base-workspace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Build results
/target
/artifacts

# Cargo+Git helper file (https://github.com/rust-lang/cargo/blob/0.44.1/src/cargo/sources/git/utils.rs#L320-L327)
.cargo-ok

# Text file backups
**/*.rs.bk

# macOS
.DS_Store

# IDEs
*.iml
.idea
17 changes: 17 additions & 0 deletions base-workspace/.gitpod.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### wasmd ###
FROM cosmwasm/wasmd:v0.18.0 as wasmd

### rust-optimizer ###
FROM cosmwasm/rust-optimizer:0.11.5 as rust-optimizer

FROM gitpod/workspace-full:latest

COPY --from=wasmd /usr/bin/wasmd /usr/local/bin/wasmd
COPY --from=wasmd /opt/* /opt/

RUN sudo apt-get update \
&& sudo apt-get install -y jq \
&& sudo rm -rf /var/lib/apt/lists/*

RUN rustup update stable \
&& rustup target add wasm32-unknown-unknown
10 changes: 10 additions & 0 deletions base-workspace/.gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
image: cosmwasm/cw-gitpod-base:v0.16

vscode:
extensions:
- rust-lang.rust

tasks:
- name: Dependencies & Build
init: |
cargo build
13 changes: 13 additions & 0 deletions base-workspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[workspace]
members = ["contracts/*"]

[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true
96 changes: 96 additions & 0 deletions base-workspace/Developing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Developing

If you have recently created a contract with this template, you probably could use some
help on how to build and test the contract, as well as prepare it for production. This
file attempts to provide a brief overview, assuming you have installed a recent
version of Rust already (eg. 1.51.0+).

## Prerequisites

Before starting, make sure you have [rustup](https://rustup.rs/) along with a
recent `rustc` and `cargo` version installed. Currently, we are testing on 1.51.0+.

And you need to have the `wasm32-unknown-unknown` target installed as well.

You can check that via:

```sh
rustc --version
cargo --version
rustup target list --installed
# if wasm32 is not listed above, run this
rustup target add wasm32-unknown-unknown
```

## Compiling and running tests

Now that you created your custom contract, make sure you can compile and run it before
making any changes. Go into the repository and do:

```sh
# this will produce a wasm build in ./target/wasm32-unknown-unknown/release/YOUR_NAME_HERE.wasm
cargo wasm

# this runs unit tests with helpful backtraces
RUST_BACKTRACE=1 cargo unit-test

# auto-generate json schema
cargo schema
```

### Understanding the tests

The main code is in `src/contract.rs` and the unit tests there run in pure rust,
which makes them very quick to execute and give nice output on failures, especially
if you do `RUST_BACKTRACE=1 cargo unit-test`.

We consider testing critical for anything on a blockchain, and recommend to always keep
the tests up to date.

## Generating JSON Schema

While the Wasm calls (`instantiate`, `execute`, `query`) accept JSON, this is not enough
information to use it. We need to expose the schema for the expected messages to the
clients. You can generate this schema by calling `cargo schema`, which will output
4 files in `./schema`, corresponding to the 3 message types the contract accepts,
as well as the internal `State`.

These files are in standard json-schema format, which should be usable by various
client side tools, either to auto-generate codecs, or just to validate incoming
json wrt. the defined schema.

## Preparing the Wasm bytecode for production

Before we upload it to a chain, we need to ensure the smallest output size possible,
as this will be included in the body of a transaction. We also want to have a
reproducible build process, so third parties can verify that the uploaded Wasm
code did indeed come from the claimed rust code.

To solve both these issues, we have produced `rust-optimizer`, a docker image to
produce an extremely small build output in a consistent manner. The suggest way
to run it is this:

```sh
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/rust-optimizer:0.11.4
```

We must mount the contract code to `/code`. You can use a absolute path instead
of `$(pwd)` if you don't want to `cd` to the directory first. The other two
volumes are nice for speedup. Mounting `/code/target` in particular is useful
to avoid docker overwriting your local dev files with root permissions.
Note the `/code/target` cache is unique for each contract being compiled to limit
interference, while the registry cache is global.

This is rather slow compared to local compilations, especially the first compile
of a given contract. The use of the two volume caches is very useful to speed up
following compiles of the same contract.

This produces an `artifacts` directory with a `PROJECT_NAME.wasm`, as well as
`checksums.txt`, containing the Sha256 hash of the wasm file.
The wasm file is compiled deterministically (anyone else running the same
docker on the same git commit should get the identical file with the same Sha256 hash).
It is also stripped and minimized for upload to a blockchain (we will also
gzip it in the uploading process to make it even smaller).
Loading

0 comments on commit beed066

Please sign in to comment.