From 0be0a89189acb708ad323066d8d21d1086c173ef Mon Sep 17 00:00:00 2001 From: PaddyMc Date: Thu, 8 Oct 2020 12:34:06 +0100 Subject: [PATCH 1/2] feat: adding poa module --- demo-apps/auth-chain/go.mod | 1 - 1 file changed, 1 deletion(-) diff --git a/demo-apps/auth-chain/go.mod b/demo-apps/auth-chain/go.mod index 1ef985f..f85f3a7 100644 --- a/demo-apps/auth-chain/go.mod +++ b/demo-apps/auth-chain/go.mod @@ -3,7 +3,6 @@ module github.com/allinbits/modules/demo-apps/auth-chain go 1.15 require ( - github.com/allinbits/modules/poa v0.0.0-20201006081502-d144cbfb0613 github.com/allinbits/modules/x/poa v0.0.0-20201009193500-337b33a517b1 github.com/cosmos/cosmos-sdk v0.39.1 github.com/golang/mock v1.4.3 // indirect From 9385a7b3edac751cdd8a28b96423983163afe9ef Mon Sep 17 00:00:00 2001 From: PaddyMc Date: Thu, 8 Oct 2020 14:08:11 +0100 Subject: [PATCH 2/2] feat: local test network --- demo-apps/auth-chain/Dockerfile | 17 +++ demo-apps/auth-chain/Makefile | 21 ++++ demo-apps/auth-chain/docker-compose.yml | 82 +++++++++++++ .../add-validators-to-validator-set.sh | 115 ++++++++++++++++++ 4 files changed, 235 insertions(+) create mode 100644 demo-apps/auth-chain/Dockerfile create mode 100644 demo-apps/auth-chain/docker-compose.yml create mode 100755 demo-apps/auth-chain/scripts/add-validators-to-validator-set.sh diff --git a/demo-apps/auth-chain/Dockerfile b/demo-apps/auth-chain/Dockerfile new file mode 100644 index 0000000..186f384 --- /dev/null +++ b/demo-apps/auth-chain/Dockerfile @@ -0,0 +1,17 @@ +FROM golang:alpine + +RUN apk update +RUN apk add --no-cache ca-certificates curl make git libc-dev bash gcc linux-headers eudev-dev python3 + +WORKDIR /auth-chain + +COPY . . + +RUN go get github.com/go-delve/delve/cmd/dlv +RUN make install-debug + +RUN cp ./auth-chaind /usr/bin +RUN cp ./auth-chaincli /usr/bin + + +EXPOSE 26656 26657 1317 9090 40000 diff --git a/demo-apps/auth-chain/Makefile b/demo-apps/auth-chain/Makefile index 2edc16b..1647744 100644 --- a/demo-apps/auth-chain/Makefile +++ b/demo-apps/auth-chain/Makefile @@ -14,7 +14,28 @@ init-validator: go run cmd/auth-chaind/main.go cmd/auth-chaind/genaccounts.go gentx --name auth-chain1 --home ./build/.auth-chaind --moniker authchain --website test.com --identity test --security-contact test@test.com --details atest go run cmd/auth-chaind/main.go cmd/auth-chaind/genaccounts.go collect-gentxs --home ./build/.auth-chaind +install-debug: go.sum + go build -mod=readonly -gcflags="all=-N -l" ./cmd/auth-chaind + go build -mod=readonly -gcflags="all=-N -l" ./cmd/auth-chaincli + build: @mkdir -p build/ @go build -mod=mod -o build/clayd ./cmd/clayd @go build -mod=mod -o build/claycli ./cmd/claycli + +############################################################################### +### Tests & Simulation ### +############################################################################### + +localnet-consensus: + ./scripts/add-validators-to-validator-set.sh + +localnet-start: init-dev export-key + NODE0ADDRESS=$(shell go run cmd/auth-chaind/main.go cmd/auth-chaind/genaccounts.go tendermint show-node-id --home ./build/.auth-chaind)@192.16.10.2:26656 docker-compose up + +export-key: + echo "password1234\npassword1234" | go run cmd/auth-chaincli/main.go keys export auth-chain1 2> ./build/validator + +clean: + sudo rm -r ./build + docker-compose down diff --git a/demo-apps/auth-chain/docker-compose.yml b/demo-apps/auth-chain/docker-compose.yml new file mode 100644 index 0000000..9fac92c --- /dev/null +++ b/demo-apps/auth-chain/docker-compose.yml @@ -0,0 +1,82 @@ +version: "3" + +services: + auth-chaindnode0: + container_name: auth-chaindnode0 + image: "poa" + ports: + - "26656-26657:26656-26657" + - "1317:1317" + - "9090:9090" + - "40000:40000" + environment: + - ID=0 + - LOG=${LOG:-auth-chaind.log} + volumes: + - ./build/.auth-chaind:/root/.auth-chaind + - ./build/validator:/root/validator + command: ["sh", "-c", "dlv --listen=:40000 --headless --accept-multiclient exec --continue ./auth-chaind -- start --home /root/.auth-chaind"] + networks: + localnet: + ipv4_address: 192.16.10.2 + + auth-chaindnode1: + container_name: auth-chaindnode1 + image: "poa" + ports: + - "26666-26667:26656-26657" + - "1318:1317" + - "9091:9090" + environment: + - NODE0ADDRESS=${NODE0ADDRESS} + - ID=1 + - LOG=${LOG:-auth-chaind.log} + command: ["sh", "-c", "auth-chaind init ac1 && cp genesis.json /root/.authchaind/config && sed -i 's/peers = \"\"/peers = \"$NODE0ADDRESS\"/g' /root/.authchaind/config/config.toml && auth-chaind start"] + volumes: + - ./build/.auth-chaind/config/genesis.json:/auth-chain/genesis.json + networks: + localnet: + ipv4_address: 192.16.10.3 + + auth-chaindnode2: + container_name: auth-chaindnode2 + image: "poa" + ports: + - "26676-26677:26656-26657" + - "1319:1317" + - "9092:9090" + environment: + - NODE0ADDRESS=${NODE0ADDRESS} + - ID=1 + - LOG=${LOG:-auth-chaind.log} + command: ["sh", "-c", "auth-chaind init ac2 && cp genesis.json /root/.authchaind/config && sed -i 's/peers = \"\"/peers = \"$NODE0ADDRESS\"/g' /root/.authchaind/config/config.toml && auth-chaind start"] + volumes: + - ./build/.auth-chaind/config/genesis.json:/auth-chain/genesis.json + networks: + localnet: + ipv4_address: 192.16.10.4 + + auth-chaindnode3: + container_name: auth-chaindnode3 + image: "poa" + ports: + - "26686-26687:26656-26657" + - "1320:1317" + - "9093:9090" + environment: + - NODE0ADDRESS=${NODE0ADDRESS} + - ID=1 + - LOG=${LOG:-auth-chaind.log} + command: ["sh", "-c", "auth-chaind init ac3 && cp genesis.json /root/.authchaind/config && sed -i 's/peers = \"\"/peers = \"$NODE0ADDRESS\"/g' /root/.authchaind/config/config.toml && auth-chaind start"] + volumes: + - ./build/.auth-chaind/config/genesis.json:/auth-chain/genesis.json + networks: + localnet: + ipv4_address: 192.16.10.5 + +networks: + localnet: + ipam: + driver: default + config: + - subnet: 192.16.10.0/16 diff --git a/demo-apps/auth-chain/scripts/add-validators-to-validator-set.sh b/demo-apps/auth-chain/scripts/add-validators-to-validator-set.sh new file mode 100755 index 0000000..4f8291d --- /dev/null +++ b/demo-apps/auth-chain/scripts/add-validators-to-validator-set.sh @@ -0,0 +1,115 @@ +#!/bin/sh + +############################################################################### +### FUNCTIONS ### +############################################################################### + +# Creates a validator for a given node +# Take 1 arg the name of the node e.g auth-chaindnode0 +createValidator() { + echo "Creating validator for node $1\n" + + # Create the validator + docker exec -e MONIKER=$1 $1 /bin/sh -c 'auth-chaincli tx poa create-validator $(auth-chaincli keys show validator --bech val -a --keyring-backend test) $(auth-chaind tendermint show-validator) $(echo $MONIKER) identity website security@contact details -y --trust-node --from validator --chain-id auth --keyring-backend test' + + sleep 5 +} + +# Votes for a perspecitve canidate +# Take 2 args the name of the node voting and the candidate node e.g auth-chaindnode0 auth-chaindnode1 +voteForValidator() { + eval CANDIDATE=$(docker exec $2 /bin/sh -c "auth-chaincli keys show validator --bech val -a --keyring-backend test") + echo "Voter $1 is voting for candidate $2" + docker exec -e CANDIDATE=$CANDIDATE $1 /bin/sh -c 'auth-chaincli tx poa vote-validator $(echo $CANDIDATE) -y --trust-node --from validator --chain-id auth --keyring-backend test' + + sleep 5 +} + +# Kicks for a perspecitve canidate +# Take 2 args the name of the node voting and the candidate node e.g auth-chaindnode0 auth-chaindnode1 +kickValidator() { + eval CANDIDATE=$(docker exec $2 /bin/sh -c "auth-chaincli keys show validator --bech val -a --keyring-backend test") + echo "Votee $1 is voting to kick candidate $2" + docker exec -e CANDIDATE=$CANDIDATE $1 /bin/sh -c 'auth-chaincli tx poa kick-validator $(echo $CANDIDATE) -y --trust-node --from validator --chain-id auth --keyring-backend test' + + sleep 5 +} +############################################################################### +### STEP 1 ### +############################################################################### + +# Import the exported key for the first node +docker exec auth-chaindnode0 /bin/sh -c "echo -e 'password1234\n' | auth-chaincli keys import validator /root/validator --keyring-backend test" + +## Create the validator +voteForValidator auth-chaindnode0 auth-chaindnode0 + +############################################################################### +### STEP 2 ### +############################################################################### + +# Create the keys for each node +for var in auth-chaindnode1 auth-chaindnode2 auth-chaindnode3 +do + echo "Creating key for node $var\n" + docker exec $var /bin/sh -c "auth-chaincli keys add validator --keyring-backend test" +done + + +## Send tokens to each validator +for node in auth-chaindnode1 auth-chaindnode2 auth-chaindnode3 +do + eval ADDRESS=$(docker exec $node /bin/sh -c "auth-chaincli keys show validator -a --keyring-backend test") + echo "Sending tokens to $ADDRESS\n" + docker exec -e ADDRESS=$ADDRESS auth-chaindnode0 /bin/sh -c 'auth-chaincli tx send $(auth-chaincli keys show validator -a --keyring-backend test) $(echo $ADDRESS) 100000stake -y --trust-node --from validator --chain-id auth --keyring-backend test' + sleep 5 +done + +############################################################################### +### STEP 3 ### +############################################################################### + +# Create validator for validator set +for var in auth-chaindnode1 auth-chaindnode2 auth-chaindnode3 +do + createValidator $var +done + +############################################################################### +### STEP 4 ### +############################################################################### + +# Adding new validators to the set + +# Vote for validator1 to join the set +voteForValidator auth-chaindnode0 auth-chaindnode1 + +# auth-chaindnode1 votes for auth-chaindnode0 to prove the node is in the consensus +voteForValidator auth-chaindnode1 auth-chaindnode0 + +# auth-chaindnode1 votes for auth-chaindnode1 to stay relevant in the consensus +voteForValidator auth-chaindnode1 auth-chaindnode1 + +# auth-chaindnode1 and poanode0 votes for auth-chaindnode2 to join the consensus +voteForValidator auth-chaindnode0 auth-chaindnode2 +voteForValidator auth-chaindnode1 auth-chaindnode2 + +# auth-chaindnode2 votes for auth-chaindnode2 to stay relevant in the consensus +voteForValidator auth-chaindnode2 auth-chaindnode2 + +# auth-chaindnode2 votes for auth-chaindnode1 to prove the node is in the consensus +voteForValidator auth-chaindnode2 auth-chaindnode1 + +# auth-chaindnode2 votes for auth-chaindnode0 to prove the node is in the consensus +voteForValidator auth-chaindnode2 auth-chaindnode0 + +# kick auth-chaindnode2 out of the consensus +kickValidator auth-chaindnode0 auth-chaindnode2 +kickValidator auth-chaindnode1 auth-chaindnode2 + +echo "POA Consensus started with 2 nodes :thumbs_up:\n" + +sleep 5 + +## Verify valdiators are in the set by checking the validator set +docker exec auth-chaindnode0 /bin/sh -c "curl -X GET 'localhost:26657/validators'"