Skip to content

Commit

Permalink
Merge pull request #42 from desmos-labs/riccardo/docker-testnet
Browse files Browse the repository at this point in the history
Added automatic testnet command
  • Loading branch information
kwunyeung authored Nov 21, 2019
2 parents 94d641b + 4b25c40 commit 8fdb136
Show file tree
Hide file tree
Showing 9 changed files with 674 additions and 18 deletions.
33 changes: 33 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Simple usage with a mounted data directory:
# > docker build -t desmos .
# > docker run -it -p 46657:46657 -p 46656:46656 -v ~/.desmosd:/root/.desmosd -v ~/.desmoscli:/root/.desmoscli desmos desmosd init
# > docker run -it -p 46657:46657 -p 46656:46656 -v ~/.desmosd:/root/.desmosd -v ~/.desmoscli:/root/.desmoscli desmos desmosd start
FROM golang:alpine AS build-env

# Set up dependencies
ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python

# Set working directory for the build
WORKDIR /go/src/github.com/cosmos/desmos

# Add source files
COPY . .

# Install minimum necessary dependencies, build Cosmos SDK, remove packages
RUN apk add --no-cache $PACKAGES && \
make tools && \
make install

# Final image
FROM alpine:edge

# Install ca-certificates
RUN apk add --update ca-certificates
WORKDIR /root

# Copy over binaries from the build-env
COPY --from=build-env /go/bin/desmosd /usr/bin/desmosd
COPY --from=build-env /go/bin/desmoscli /usr/bin/desmoscli

# Run desmosd by default, omit entrypoint to ease using container with desmoscli
CMD ["desmosd"]
44 changes: 33 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,31 @@ all: lint install
### Install

install: go.sum
go install -mod=readonly $(BUILD_FLAGS) ./cmd/desmosd/main.go
go install -mod=readonly $(BUILD_FLAGS) ./cmd/desmoscli/main.go
go install -mod=readonly $(BUILD_FLAGS) ./cmd/desmoskeyutil/main.go
go install -mod=readonly $(BUILD_FLAGS) ./cmd/desmosd
go install -mod=readonly $(BUILD_FLAGS) ./cmd/desmoscli
go install -mod=readonly $(BUILD_FLAGS) ./cmd/desmoskeyutil

########################################
### Build

build: go.sum
ifeq ($(OS),Windows_NT)
go build -mod=readonly $(BUILD_FLAGS) -o ./build/desmod.exe ./cmd/desmosd/main.go
go build -mod=readonly $(BUILD_FLAGS) -o ./build/desmoscli.exe ./cmd/desmoscli/main.go
go build -mod=readonly $(BUILD_FLAGS) -o ./build/desmod.exe ./cmd/desmosd
go build -mod=readonly $(BUILD_FLAGS) -o ./build/desmoscli.exe ./cmd/desmoscli
else
go build -mod=readonly $(BUILD_FLAGS) -o ./build/desmosd ./cmd/desmosd/main.go
go build -mod=readonly $(BUILD_FLAGS) -o ./build/desmoscli ./cmd/desmoscli/main.go
go build -mod=readonly $(BUILD_FLAGS) -o ./build/desmosd ./cmd/desmosd
go build -mod=readonly $(BUILD_FLAGS) -o ./build/desmoscli ./cmd/desmoscli
endif

build-linux: go.sum
LEDGER_ENABLED=false GOOS=linux GOARCH=amd64 $(MAKE) build

########################################
### Tools & dependencies

go-mod-cache: go.sum
@echo "--> Download go modules to local cache"
@go mod download
.PHONY: go-mod-cache

go.sum: go.mod
@echo "--> Ensure dependencies have not been modified"
Expand All @@ -76,7 +78,27 @@ lint:
########################################
### Testing

test: test_unit
test: test-unit

test-unit:
@VERSION=$(VERSION) go test -mod=readonly $(PACKAGES_NOSIMULATION) -tags='ledger test_ledger_mock'


########################################
### Local validator nodes using docker and docker-compose

build-docker-desmosdnode:
$(MAKE) -C networks/local

# Run a 4-node testnet locally
localnet-start: build-docker-desmosdnode build-linux localnet-stop
@if ! [ -f build/node0/desmosd/config/genesis.json ]; then docker run -e COSMOS_SDK_TEST_KEYRING=y --rm -v $(CURDIR)/build:/desmosd:Z desmos-labs/desmosdnode testnet --v 4 -o . --starting-ip-address 192.168.10.2 ; fi
docker-compose up -d

# Stop testnet
localnet-stop:
docker-compose down

test_unit:
@VERSION=$(VERSION) go test -mod=readonly $(PACKAGES_NOSIMULATION) -tags='ledger test_ledger_mock'
.PHONY: all build-linux install \
go-mod-cache build \
test test-unit
95 changes: 94 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
Desmos social chain

## Installation

``` sh
make install
```

## Start a testnet

### Single node, manual
Run the [desmos-faucet](https://github.com/desmos-labs/desmos-faucet).

``` sh
Expand All @@ -33,3 +33,96 @@ Edit `$HOME/.desmosd/config/genesis.json` and update the `bond_denom` from `stak
``` sh
desmosd start
```

### Multi node, automatic
To start a 4 node testnet run:

```
make localnet-start
```

This command creates a 4-node network using the `desmoslabs/desmosdnode` image.
The ports for each node are found in this table:

| Node ID | P2P Port | RPC Port |
| --------|-------|------|
| `desmosdnode0` | `26656` | `26657` |
| `desmosdnode1` | `26659` | `26660` |
| `desmosdnode2` | `26661` | `26662` |
| `desmosdnode3` | `26663` | `26664` |

To update the binary, just rebuild it and restart the nodes:

```
make build-linux localnet-start
```

#### Configuration

The `make localnet-start` creates files for a 4-node testnet in `./build` by
calling the `desmosd testnet` command. This outputs a handful of files in the
`./build` directory:

```bash
$ tree -L 2 build/
build/
├── desmoscli
├── desmosd
├── gentxs
│ ├── node0.json
│ ├── node1.json
│ ├── node2.json
│ └── node3.json
├── node0
│ ├── desmoscli
│ │ ├── key_seed.json
│ │ └── keys
│ └── desmosd
│ ├── ${LOG:-desmosd.log}
│ ├── config
│ └── data
├── node1
│ ├── desmoscli
│ │ └── key_seed.json
│ └── desmosd
│ ├── ${LOG:-desmosd.log}
│ ├── config
│ └── data
├── node2
│ ├── desmoscli
│ │ └── key_seed.json
│ └── desmosd
│ ├── ${LOG:-desmosd.log}
│ ├── config
│ └── data
└── node3
├── desmoscli
│ └── key_seed.json
└── desmosd
├── ${LOG:-desmosd.log}
├── config
└── data
```

Each `./build/nodeN` directory is mounted to the `/desmosd` directory in each container.

#### Logging
Logs are saved under each `./build/nodeN/desmosd/desmos.log`. You can also watch logs
directly via Docker, for example:

```
docker logs -f desmosdnode0
```

#### Keys & Accounts
To interact with `desmoscli` and start querying state or creating txs, you use the
`desmoscli` directory of any given node as your `home`, for example:

```shell
desmoscli keys list --home ./build/node0/desmoscli
```

Now that accounts exists, you may create new accounts and send those accounts
funds!

**Note**: Each node's seed is located at `./build/nodeN/desmoscli/key_seed.json` and can be restored to the CLI using the `desmoscli keys add --restore` command
18 changes: 12 additions & 6 deletions cmd/desmosd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/client"
"io"

"github.com/cosmos/cosmos-sdk/server"
Expand Down Expand Up @@ -42,14 +43,19 @@ func main() {
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
}
// CLI commands to initialize the chain
rootCmd.AddCommand(genutilcli.InitCmd(ctx, cdc, app.ModuleBasics, app.DefaultNodeHome))
rootCmd.AddCommand(genutilcli.CollectGenTxsCmd(ctx, cdc, genaccounts.AppModuleBasic{}, app.DefaultNodeHome))
rootCmd.AddCommand(genutilcli.MigrateGenesisCmd(ctx, cdc))
rootCmd.AddCommand(
genutilcli.InitCmd(ctx, cdc, app.ModuleBasics, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(ctx, cdc, genaccounts.AppModuleBasic{}, app.DefaultNodeHome),
genutilcli.GenTxCmd(ctx, cdc, app.ModuleBasics, staking.AppModuleBasic{}, genaccounts.AppModuleBasic{}, app.DefaultNodeHome, app.DefaultCLIHome),
genutilcli.ValidateGenesisCmd(ctx, cdc, app.ModuleBasics),
// AddGenesisAccountCmd allows users to add accounts to the genesis file
genaccscli.AddGenesisAccountCmd(ctx, cdc, app.DefaultNodeHome, app.DefaultCLIHome),
genutilcli.GenTxCmd(
ctx, cdc, app.ModuleBasics, staking.AppModuleBasic{},
genaccounts.AppModuleBasic{}, app.DefaultNodeHome, app.DefaultCLIHome,
),
)
rootCmd.AddCommand(genutilcli.ValidateGenesisCmd(ctx, cdc, app.ModuleBasics))
rootCmd.AddCommand(genaccscli.AddGenesisAccountCmd(ctx, cdc, app.DefaultNodeHome, app.DefaultCLIHome))
rootCmd.AddCommand(client.NewCompletionCmd(rootCmd, true))
rootCmd.AddCommand(testnetCmd(ctx, cdc, app.ModuleBasics, genaccounts.AppModuleBasic{}))

server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators)

Expand Down
Loading

0 comments on commit 8fdb136

Please sign in to comment.