Skip to content

Commit

Permalink
add example of sentry node architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
bluele committed Mar 1, 2019
1 parent 63f0791 commit c0098b4
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 0 deletions.
7 changes: 7 additions & 0 deletions k8s/sentry-node/Dockerfile-test.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM bluele/hypermint:<VERSION>

ENV WORKDIR=/go/src/github.com/bluele/hypermint
WORKDIR ${WORKDIR}

RUN apk add bash
RUN /hmd testnet -v=<VALS_NUM> -n=<VALS_NUM> --address=<GENESIS_ADDR> -o=/mytestnet
31 changes: 31 additions & 0 deletions k8s/sentry-node/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
BUILD_DIR?=../../build
CONFIG_DIR?=./config
VERSION?=$(shell cat ../../version)
RESOURCES?=$(wildcard $(CONFIG_DIR)/*.yaml)
RESOURCES_OPT?=$(addprefix -f,$(RESOURCES))
VALS_NUM?=4
# mnemonic "token dash time stand brisk fatal health honey frozen brown flight kitchen"
# m/44'/60'/0'/0
GENESIS_ADDR?=0x1221a0726d56aEdeA9dBe2522DdAE3Dd8ED0f36c

create:
kubectl apply $(RESOURCES_OPT)

destory:
kubectl delete $(RESOURCES_OPT)

gen-config:
@rm -f ./config/*.yaml
@bash ./gen_testnet.sh $(VALS_NUM)

destory-pvc:
@for n in $$(kubectl get pvc -o go-template --template '{{range .items}}{{.metadata.labels.app}}{{"\n"}}{{end}}' | grep 'hm-validator'); do \
kubectl delete pvc -l app=$$n; \
done

build-image:
sed -e "s/<VERSION>/${VERSION}/g" -e "s/<VALS_NUM>/$(VALS_NUM)/g" -e "s/<GENESIS_ADDR>/$(GENESIS_ADDR)/g" Dockerfile-test.tpl | docker build --no-cache=true -t bluele/hypermint-testnet-sentry:${VERSION} -

balance0:
$(eval port := $(shell kubectl get services/hm-validator-0 -o go-template='{{- range .spec.ports }}{{- if (eq "rpc" .name) }}{{.nodePort}}{{- end}}{{- end}}'))
@$(BUILD_DIR)/hmcli balance --address=$(GENESIS_ADDR) --node=tcp://localhost:$(port)
14 changes: 14 additions & 0 deletions k8s/sentry-node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Testnet

## Getting started

```
# Generate k8s yaml files, and build docker image for testnet.
$ make VALS_NUM=4 gen-config
# Create a testnet with generated config.
$ make create
# Destory testnet
$ make destroy
```
6 changes: 6 additions & 0 deletions k8s/sentry-node/config.yaml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: hm-config
data:
validators: "<VALIDATORS>"
1 change: 1 addition & 0 deletions k8s/sentry-node/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.yaml
128 changes: 128 additions & 0 deletions k8s/sentry-node/deployment.yaml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
apiVersion: v1
kind: Service
metadata:
name: hm-validator-<NODENO>
spec:
type: NodePort
selector:
app: hm-validator-<NODENO>
ports:
- port: 26656
name: p2p
- port: 26657
name: rpc

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: hm-validator-<NODENO>
spec:
replicas: 1
selector:
matchLabels:
app: hm-validator-<NODENO>
serviceName: hm-validator-<NODENO>
template:
metadata:
labels:
app: hm-validator-<NODENO>
spec:
containers:
- name: hm-validator
image: bluele/hypermint-testnet-sentry:<VERSION>
command:
- bash
- "-c"
- |
set -ex
sentryid=$(/hmd tendermint show-node-id --home /mytestnet/node<SENTRYNO>/hmd)
peer="${sentryid}@127.0.0.1:26656"
state_file=/mytestnet/node<NODENO>/hmd/data/priv_validator_state.json
if [ ! -e $state_file ]; then
cat << EOF > $state_file
{
"height": "0",
"round": "0",
"step": 0
}
EOF
fi
TM_PARAMS="p2p.allow_duplicate_ip=true,p2p.addr_book_strict=false" /hmd start --home=/mytestnet/node<NODENO>/hmd \
--log_level="*:info" \
--p2p.persistent_peers=$peer \
--p2p.laddr="tcp://0.0.0.0:36656" \
--rpc.laddr="tcp://0.0.0.0:36657" \
--address="tcp://0.0.0.0:36658"
volumeMounts:
- mountPath: /mytestnet/node<NODENO>/hmd/data
name: hmdir-validator-<NODENO>

- name: hm-sentry
image: bluele/hypermint-testnet-sentry:<VERSION>
env:
- name: VALIDATORS
valueFrom:
configMapKeyRef:
name: hm-config
key: validators
command:
- bash
- "-c"
- |
set -ex
IFS=',' read -ra VALIDATORS_NUM <<< "$VALIDATORS"
peers=()

for n in "${VALIDATORS_NUM[@]}"; do
nodeid=$(/hmd tendermint show-node-id --home /mytestnet/node$(expr ${n} + 4)/hmd)
peers+=("${nodeid}@hm-validator-${n}:26656")
done

validatorid=$(/hmd tendermint show-node-id --home /mytestnet/node<NODENO>/hmd)
peers+=("${validatorid}@127.0.0.1:36656")
peers=$(IFS=','; echo "${peers[*]}")

state_file=/mytestnet/node<SENTRYNO>/hmd/data/priv_validator_state.json
if [ ! -e $state_file ]; then
cat << EOF > $state_file
{
"height": "0",
"round": "0",
"step": 0
}
EOF
fi
TM_PARAMS="p2p.allow_duplicate_ip=true,p2p.addr_book_strict=false,p2p.private_peer_ids=${validatorid}" /hmd start --home=/mytestnet/node<SENTRYNO>/hmd \
--log_level="*:info" \
--p2p.persistent_peers="$peers" \
--p2p.laddr="tcp://0.0.0.0:26656" \
--rpc.laddr="tcp://0.0.0.0:26657" \
--address="tcp://0.0.0.0:26658"
ports:
- containerPort: 26656
name: p2p
- containerPort: 26657
name: rpc
volumeMounts:
- mountPath: /mytestnet/node<SENTRYNO>/hmd/data
name: hmdir-sentry-<SENTRYNO>

volumeClaimTemplates:
- metadata:
name: hmdir-validator-<NODENO>
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
- metadata:
name: hmdir-sentry-<SENTRYNO>
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
18 changes: 18 additions & 0 deletions k8s/sentry-node/gen_testnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -eu
VERSION=$(cat ../../version)
VALNUM=$1

vals=()
nvals=()
for i in $(seq 0 $(expr $VALNUM - 1)); do
SENTRYNO=$(expr $i + $VALNUM)
sed -e "s/<VERSION>/${VERSION}/g" -e "s/<NODENO>/$i/g" -e "s/<SENTRYNO>/${SENTRYNO}/g" deployment.yaml.tpl > ./config/deployment${i}.yaml
echo "generate deployment$i.yaml"
vals+=(${i})
nvals+=($SENTRYNO)
done
vals=$(IFS=','; echo "${vals[*]}")
nvals=$(IFS=','; echo "${nvals[*]}")
sed -e "s/<VALIDATORS>/${vals}/g" -e "s/<SENTRYNODES>/${nvals}/g" config.yaml.tpl > ./config/config.yaml
echo "generate config.yaml"

0 comments on commit c0098b4

Please sign in to comment.