From 3d09eda0264c1d8300f46d95181d99c08c55d8ed Mon Sep 17 00:00:00 2001 From: emidev98 Date: Wed, 20 Mar 2024 19:05:24 +0200 Subject: [PATCH] feat: introduce docker localnet again --- .dockerignore | 2 +- .gitignore | 1 + Makefile | 26 +++++++++- cmd/terrad/root.go | 5 +- docker-compose.yml | 76 +++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 + scripts/containers/Dockerfile | 22 +++++++++ scripts/containers/build-wrapper.sh | 20 ++++++++ 9 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 docker-compose.yml create mode 100644 scripts/containers/Dockerfile create mode 100755 scripts/containers/build-wrapper.sh diff --git a/.dockerignore b/.dockerignore index d1638636..20c1d078 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1 @@ -build/ \ No newline at end of file +integration-tests \ No newline at end of file diff --git a/.gitignore b/.gitignore index 840c3aa8..9349a544 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # OS +.testnets .DS_Store *.swp *.swo diff --git a/Makefile b/Makefile index 8fb34900..b81497fc 100644 --- a/Makefile +++ b/Makefile @@ -299,4 +299,28 @@ format: format-tools find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*statik*" -not -name '*.pb.go' | xargs misspell -w find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*statik*" -not -name '*.pb.go' | xargs goimports -w -local github.com/cosmos/cosmos-sdk -.PHONY: lint lint-fix lint-docker format-tools format \ No newline at end of file +.PHONY: lint lint-fix lint-docker format-tools format + + +############################################################################### +### Local Testnet (docker) ### +############################################################################### + +localnet-rmi: + $(DOCKER) rmi terra-money/localnet-core 2>/dev/null; true + +localnet-build-env: localnet-rmi + $(DOCKER) build --tag terra-money/localnet-core -f scripts/containers/Dockerfile \ + $(shell git rev-parse --show-toplevel) + +localnet-build-nodes: + $(DOCKER) run --rm -v $(CURDIR)/.testnets:/terra terra-money/localnet-core \ + testnet init-files --v 3 -o /terra --starting-ip-address 192.168.15.20 --keyring-backend=test --chain-id=core-testnet-1 + $(DOCKER) compose up -d + +localnet-stop: + $(DOCKER) compose down + +localnet-start: localnet-stop localnet-build-env localnet-build-nodes + +.PHONY: localnet-stop localnet-start localnet-build-env localnet-build-nodes localnet-rmi \ No newline at end of file diff --git a/cmd/terrad/root.go b/cmd/terrad/root.go index 2a4e6f2e..9cf73bdc 100644 --- a/cmd/terrad/root.go +++ b/cmd/terrad/root.go @@ -18,6 +18,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" + sdkcmd "cosmossdk.io/simapp/simd/cmd" "github.com/cosmos/cosmos-sdk/client/config" "github.com/cosmos/cosmos-sdk/client/debug" "github.com/cosmos/cosmos-sdk/client/flags" @@ -34,6 +35,7 @@ import ( genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" wasm "github.com/CosmWasm/wasmd/x/wasm" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" terraapp "github.com/terra-money/core/v2/app" "github.com/terra-money/core/v2/app/params" ) @@ -112,12 +114,13 @@ func initRootCmd(rootCmd *cobra.Command, moduleBasics module.BasicManager, encod a := appCreator{encodingConfig} rootCmd.AddCommand( - InitCmd(terraapp.ModuleBasics, terraapp.DefaultNodeHome), + InitCmd(moduleBasics, terraapp.DefaultNodeHome), config.Cmd(), tmcli.NewCompletionCmd(rootCmd, true), debug.Cmd(), pruning.Cmd(a.newApp, terraapp.DefaultNodeHome), snapshot.Cmd(a.newApp), + sdkcmd.NewTestnetCmd(moduleBasics, banktypes.GenesisBalancesIterator{}), ) server.AddCommands(rootCmd, terraapp.DefaultNodeHome, a.newApp, a.appExport, addModuleInitFlags) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..a0835518 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,76 @@ +version: "3" + +services: + node: + container_name: node + image: "terra-money/localnet-core" + environment: + - DEBUG=1 + - ID=0 + - LOG=${LOG:-core.log} + cap_add: + - SYS_PTRACE + security_opt: + - seccomp:unconfined + ports: + - "26656-26657:26656-26657" + - "1317:1317" + - "9090:9090" + - "2345:2345" + volumes: + - ./.testnets:/terrad/data:Z + networks: + localnet: + ipv4_address: 192.168.15.20 + + node_1: + container_name: node_1 + image: "terra-money/localnet-core" + environment: + - DEBUG=0 + - ID=1 + - LOG=${LOG:-core.log} + cap_add: + - SYS_PTRACE + security_opt: + - seccomp:unconfined + ports: + - "26666-26667:26656-26657" + - "1318:1317" + - "9091:9090" + - "2346:2345" + volumes: + - ./.testnets:/terrad/data:Z + networks: + localnet: + ipv4_address: 192.168.15.21 + + node_2: + container_name: node_2 + image: "terra-money/localnet-core" + environment: + - DEBUG=0 + - ID=2 + - LOG=${LOG:-core.log} + cap_add: + - SYS_PTRACE + security_opt: + - seccomp:unconfined + ports: + - "26676-26677:26656-26657" + - "1319:1317" + - "9092:9090" + - "2347:2345" + volumes: + - ./.testnets:/terrad/data:Z + networks: + localnet: + ipv4_address: 192.168.15.22 + +networks: + localnet: + driver: bridge + ipam: + driver: default + config: + - subnet: 192.168.15.0/25 \ No newline at end of file diff --git a/go.mod b/go.mod index f7d59387..a85915c1 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.20 require ( cosmossdk.io/errors v1.0.0 cosmossdk.io/math v1.2.0 + cosmossdk.io/simapp v0.0.0-20230608160436-666c345ad23d cosmossdk.io/tools/rosetta v0.2.1 github.com/CosmWasm/wasmd v0.45.0 github.com/CosmWasm/wasmvm v1.5.2 diff --git a/go.sum b/go.sum index b4736ad2..6e4bf56c 100644 --- a/go.sum +++ b/go.sum @@ -203,6 +203,8 @@ cosmossdk.io/log v1.2.1 h1:Xc1GgTCicniwmMiKwDxUjO4eLhPxoVdI9vtMW8Ti/uk= cosmossdk.io/log v1.2.1/go.mod h1:GNSCc/6+DhFIj1aLn/j7Id7PaO8DzNylUZoOYBL9+I4= cosmossdk.io/math v1.2.0 h1:8gudhTkkD3NxOP2YyyJIYYmt6dQ55ZfJkDOaxXpy7Ig= cosmossdk.io/math v1.2.0/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0= +cosmossdk.io/simapp v0.0.0-20230608160436-666c345ad23d h1:E/8y0oG3u9hBR8l4F9MtC0LdZIamPCUwUoLlrHrX86I= +cosmossdk.io/simapp v0.0.0-20230608160436-666c345ad23d/go.mod h1:xbjky3L3DJEylaho6gXplkrMvJ5sFgv+qNX+Nn47bzY= cosmossdk.io/tools/rosetta v0.2.1 h1:ddOMatOH+pbxWbrGJKRAawdBkPYLfKXutK9IETnjYxw= cosmossdk.io/tools/rosetta v0.2.1/go.mod h1:Pqdc1FdvkNV3LcNIkYWt2RQY6IP1ge6YWZk8MhhO9Hw= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= diff --git a/scripts/containers/Dockerfile b/scripts/containers/Dockerfile new file mode 100644 index 00000000..a8b1e485 --- /dev/null +++ b/scripts/containers/Dockerfile @@ -0,0 +1,22 @@ +FROM golang:1.20-bullseye AS build + +RUN apt update && apt install build-essential -y + +WORKDIR /terrad +COPY . /terrad + +RUN make build + +FROM golang:1.20-bullseye AS run +COPY ./scripts/containers/build-wrapper.sh /terrad/build-wrapper.sh +RUN chmod +x /terrad/build-wrapper.sh +RUN wget https://github.com/CosmWasm/wasmvm/releases/download/v1.5.2/libwasmvm.x86_64.so -P /usr/lib/ + +VOLUME /terrad +COPY --from=build /terrad/ /terrad/ +WORKDIR /terrad + +EXPOSE 26656 26657 +ENTRYPOINT ["/terrad/build-wrapper.sh"] +CMD ["start", "--log_format", "plain"] +STOPSIGNAL SIGTERM diff --git a/scripts/containers/build-wrapper.sh b/scripts/containers/build-wrapper.sh new file mode 100755 index 00000000..7f8fa2b7 --- /dev/null +++ b/scripts/containers/build-wrapper.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env sh +set -x + +export PATH=$PATH:/terrad/build/terrad +BINARY=/terrad/build/terrad +ID=${ID:-0} +LOG=${LOG:-terrad.log} + +if ! [ -f "${BINARY}" ]; then + echo "The binary $(basename "${BINARY}") cannot be found." + exit 1 +fi + +export TERRAD_HOME="/terrad/data/node${ID}/simd" + +if [ -d "$(dirname "${TERRAD_HOME}"/"${LOG}")" ]; then + "${BINARY}" --home "${TERRAD_HOME}" "$@" | tee "${TERRAD_HOME}/${LOG}" +else + "${BINARY}" --home "${TERRAD_HOME}" "$@" +fi